0% found this document useful (0 votes)
9 views26 pages

Unit 2 2

The document discusses the computation of FIRST and FOLLOW sets, essential for constructing top-down and bottom-up parsers in grammar analysis. It covers various parsing techniques, including non-recursive descent parsing, error recovery in predictive parsing, and shift-reduce parsing, detailing the processes of reductions and handle pruning. Additionally, it provides examples and problems related to the computation of FIRST and FOLLOW sets for different grammars.

Uploaded by

Pramod Shenoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views26 pages

Unit 2 2

The document discusses the computation of FIRST and FOLLOW sets, essential for constructing top-down and bottom-up parsers in grammar analysis. It covers various parsing techniques, including non-recursive descent parsing, error recovery in predictive parsing, and shift-reduce parsing, detailing the processes of reductions and handle pruning. Additionally, it provides examples and problems related to the computation of FIRST and FOLLOW sets for different grammars.

Uploaded by

Pramod Shenoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

UNIT-2 (Contd…)

TOPICS IN THIS SLIDE (UNIT-2


Contd…)
• Computation of FIRST and FOLLOW, Non- Recursive
Descent parsing, Error Recovery in Predictive Parsing

• Bottom-Up Parsing: Reductions, Handle Pruning, Shift-


Reduce Parsing, Shift-Reduce Parsing Conflicts,
Computation of FIRST and FOLLOW sets
• The construction of both top-down and bottom-up parsers is aided by
two functions, FIRST and FOLLOW, associated with a grammar G.
During topdown parsing, FIRST and FOLLOW allow us to choose
which production to apply, based on the next input symbol.
• To compute FIRST(X) for all grammar symbols X, apply the
following rules until no more terminals or ε can be added to any
FIRST set.
1. If X is a terminal, then FIRST(X) = {X}
2. If X is a Non-Terminal, and X  Y1,Y2,…,Yk, then FIRST(X)= Y1 if
Y1 is a terminal; FIRST(X)=FIRST(Y1) if Y1 is a non-terminal.
3. If Xε is a production, then FIRST(X) = {ε}.
FOLLOW set computation
To compute FOLLOW(A) for all non-terminals A,
apply the following rules until nothing can be
added to any FOLLOW set
1. Place $ in FOLLOW(S) where S is the start
symbol, and $ is the input right endmarker.
2. If AαBβ then FOLLOW(B)=FIRST(β)-{ε}
3. If AαB or AαBβ and FIRST(β)={ε}, then
FOLLOW(B)=FOLLOW(A))
Computation of FIRST Set and FOLLOW
set: problems
Compute FIRST and FOLLOW set for the following:
E -> TR
R -> +T R| #
T -> F Y
Y -> *F Y | #
F -> (E) | i
Ans: FIRST(E) = FIRST(T)=FIRST(F)={(, i}
FIRST(R)={+,#}
FIRST(T)=FIRST(F)={(, i}
FIRST(Y)={*, #}
FIRST(F)={(, i}
FOLLOW set computation
FOLLOW(E) = {$, )}
FOLLOW(R) =
E  TR
FOLLOW(E)={$,)}
R  +T R| # FOLLOW(T)=FIRST(R)={+,
T FY #}
Y  *F Y | # FOLLOW(Y)=FOLLOW(T)={
F  (E) | i +,#}
FOLLOW(F)=FIRST(Y)={*,#
}
Problem-2) Compute FIRST and FOLLOW sets
for the following Grammar
FIRST(S)={i,a FOLLOW(S)={$, FIRST(S’)-{ε}, FOLLOW(S’)}
S iEtSS’ | a }
= {$, e}
FIRST(S’)={e,
S’  eS | ε ε} FOLLOW(S’)=FOLLOW(S)={$, e}
FIRST(E)={b} FOLLOW(E) = {t}
Eb
Ans:
FIRST and FOLLOW set problems

3) Compute FIRST and FOLLOW sets for the following


grammars
i) S  ABBA
Aa | λ
Bb|λ

ii) S  aSe | B
B  bBe|C
C  cCe | d

iii) Goal  Expr


Expr  Term + Expr | Term – Expr | Term
Term  Factor * Term | Factor / Term | Factor
Factor  num | id
SOLUTIONS ii) S  aSe | B
B  bBe|C
i) S  ABBA C  cCe | d
Aa | λ
Bb|λ FIRST(S)={a} U FIRST(B) = {a.
b, c, d}
FIRST(S) = FIRST(A) = {a, λ } FIRST(B)= {b} U FIRST(C) = {b,
FIRST(B)={b, λ} c, d}
FOLLOW(S)={$} FIRST(C)={c, d}
FOLLOW(A)=FIRST(B) U FOLLOW(S) = FOLLOW(S)={$, e}
{b, λ, $} FOLLOW(B)={e} U
FOLLOW(S)={e, $}
FOLLOW(B)=FIRST(B) U FIRST(A) =
FOLLOW(C) = {e} U FOLLOW(B)
{b, λ, a}
= {e, $}
SOLUTIONS TO FIRST AND FOLLOW SETS
iii) Goal  Expr
Expr  Term + Expr | Term – Expr | Term
Term  Factor * Term | Factor / Term | Factor
Factor  num | id
FIRST(Goal) = FIRST(Expr) = FIRST(Term) =
FIRST(Factor)={num, id}
FOLLOW(Goal)={$}
FOLLOW(Expr)=FOLLOW(Goal) = {$}
FOLLOW(Term)={+, -} U FOLLOW(Expr) = {+, -, $}
FOLLOW(Factor) = {*, /} U FOLLOW(Term) = {*, /, +, -, $}
FIRST and FOLLOW set problems

iv)A  id:=E vi) S  aAF


E  E+E | E*E | -E | AB|C
(E) | id B  D+
C  E+
Dx|y|z
v) S  bCcD |ε
C  Ca | a | ab Ea|b|c
FA|ε
Db
FIRST and FOLLOW set solutions

iv) A  id:=E v) S  bCcD


E  E+E | E*E | -E | (E) C  Ca | a | ab
| id Db
FIRST(A) = {id} FIRST(S)={b}
FIRST(E) = {-, (, id} FIRST(C)={a}
FIRST(D)={b}
FOLLOW(A)={$}
FOLLOW(S)={$}
FOLLOW(E)={+, *, )} U FOLLOW(C)={c, a}
FOLLOW(A) = {+, *, ), $} FOLLOW(D)=FOLLOW(S)={$}
FIRST and FOLLOW set solutions
FIRST(S)={a}
vi) S  aAF FIRST(A)=FIRST(B) U FIRST(C) = {x, y, z, ε, a,
AB|C b, c}
FIRST(B)=FIRST(D)= {x, y, z, ε}
B  D+
FIRST(C)=FIRST(E)= {a, b, c}
C  E+ FIRST(F)=FIRST(A)U{ε} = {x, y, z, ε, a, b, c}
Dx|y|z
|ε FOLLOW(S)={$}
FOLLOW(A)=FIRST(F) U FOLLOW(F) = {$,x, y,
Ea|b|c z, a, b, c}
FA|ε FOLLOW(B)=FOLLOW(A) = {$,x, y, z, a, b, c}
FOLLOW(C) = FOLLOW(A)== {$,x, y, z, a, b,
c}
BOTTOM-UP PARSING
• A bottom-up parse corresponds to the construction of a parse tree for an input
string beginning at the leaves (the bottom) and working up towards the root (the
top).
• It is convenient to describe parsing as the process of building parse trees, although
a front end may in fact carry out a translation directly without building an explicit
tree.
BOTTOM-UP PARSING:
REDUCTIONS
• We can think of bottom-up parsing as the process of "reducing" a string w to the
start symbol of the grammar
• At each reduction step, a specific substring matching the body of a production is
replaced by the nonterminal at the head of that production.
• The key decisions during bottom-up parsing are about when to reduce and about
what production to apply, as the parse proceeds
• Reductions are derivations (LMD or RMD) in reverse order.
• For example, consider the following LMD
E  -E  -(E)  -(E+E)  -(id+E)  -(id+id)
-(id+id) is reduced to –(id+E), -(id+E) is reduced to –(E+E) and so on.
• By definition, a reduction is the reverse of a step in a derivation (recall that in a
derivation, a nonterminal in a sentential form is replaced by the body of one of its
productions). The goal of bottom-up parsing is therefore to construct a derivation
in reverse.
BOTTOM-UP PARSING: Handle
pruning
• Handle is a substring that matches the body of a production and
whose reduction represents one step along the reverse of a
rightmost derivation.
• Rightmost sentential form: Each of the intermediate strings of
terminals and non terminals in a rightmost derivation in reverse
is a right sentential form.
• Example: Consider RMD for string “id+id*id” for following
grammar
E E+E | E*E | (E) | id
E  E+E  E+E*EE+E*id  E+id*id  id+id*id
Here, “E+E”, “E+E*E”, “E+E*id”, “E+id*id”, and “id+id*id”
are the right sentential forms.
Examples Handle, Right Sentential forms

• Example: Consider RMD for string “id+id*id” for


following grammar
E E+E | E*E | (E) | id
E  E+E  E+E*EE+E*id  E+id*id  id+id*id
Right sentential form Handle Reducing production
id+id*id id Eid
E+id*id id Eid
E+E*id id Eid
E+E*E E*E EE*E
E+E E+E EE+E
E - -
Handles
• Handle in a right sentential form ϒ is a production
Aβ and a position of ϒ where the string β may be
found and replaced by A to produce the previous right
sentential form in a rightmost derivation of ϒ.
• That is, αAw αβw the Aβ in the position following α
is a
handlermof αβw. The string ‘w’ to the right of the handle
contains only terminal symbols.
SHIFT REDUCE PARSING
• It is a type of bottom-up parser. Here, we reduce input
string ‘w’ to the start symbol of a grammar.
• Here, each replacement of the RHS of a production by
the left side is called reduction.
• Uses stack and input buffer.
• $ marks bottom of stack and right end of input.

STAC INPUT
K
$ w$
SHIFT REDUCE PARSING
• Parser operates by shifting 0 or more input symbols
onto stack until a handle β is on top of stack.
• The parser then reduces β to the left side of the
appropriate production.
• Parser repeats this cycle until it has detected an error or
until the stack contains the start symbol and input is
empty.
STAC INPUT STAC INPUT
K K
$ w$ $S $
(In this configuration, parser
halts and announces
successful completion of
parsing)
SHIFT REDUCE PARSING
ACTIONS
• Shift: Shift the next input symbol onto the top of the stack.
• Reduce. The right end of the string to be reduced must be at
the top of the stack. Locate the left end of the string within
the stack and decide with what nonterminal to replace the
string.
• Accept. Announce successful completion of parsing.
• Error. Discover a syntax error and call an error recovery
routine
SHIFT REDUCE PARSER PROBLEMS
1) Construct a shift reduce parser for the input string “id + id*id”
on the grammar: E  E+E| E*E | (E) | id
Ans: Shift (push) symbols on stack until handle found on top of stack. Once
handle is found, apply reduction.
STACK INPUT ACTION
Handles: $ id+id*id$ Shift
• id $id +id*id$ Reduce by Eid
• E+E $E +id*id$ Shift
• E*E $E+ id*id$ Shift
• (E) $E+id *id$ Reduce by Eid
$E+E *id$ Reduce by EE+E
$E *id$ Shift by *
$E* id$ Shift by id
$E*id $ Reduce by Eid
$E*E $ Reduce by EE*E
$E $ Accept
SHIFT REDUCE PARSER PROBLEM-2
2) Design a shift reduce parser for the grammar: S0S1 | 01 for the strings
(i) 000111 (ii) 00S11.
Ans: (i) 000111
Stack Input Reducing production Stack Input Reducing
(ii) 00S11
$ 000111$ Shift production
$ 00S11$ Shift
$0 00111$ Shift
$0 0S11$ Shift
$00 0111$ Shift
$00 S11$ Shift
$000 111$ Shift
$00S 11$ Shift
$0001 11$ Reduce by S01
$00S1 1$ Reduce by S0S1
$00S 11$ Shift
$0S 1$ Shift
$00S1 1$ Reduce by S0S1
$0S1 $ Reduce by S0S1
$0S 1$ Shift
$S $ Accept
$0S1 $ Reduce by S0S1
$S $ Accept

Handles: {0S1, 01}


SHIFT REDUCE PARSER
PROBLEM-3
3) Construct shift
Stack Input Reducing production
$ aaa*a++ Shift a
reduce parser for the $
Grammar: S SS+ | $a aa*a++$ Reduce by Sa
SS* | a on the string $S aa*a++$ Shift a
“aaa*a++” $Sa a*a++$ Reduce by Sa
$SS a*a++$ Shift a
Ans:
$SSa *a++$ Reduce by Sa
$SSS *a++$ Shift *
$SSS* a++$ Reduce by SSS*
$SS a++$ Shift a
$SSa ++$ Reduce by Sa
$SSS ++$ Shift +
$SSS+ +$ Reduce by SSS+
$SS +$ Shift
$SS+ $ Reduce by SSS+
SHIFT REDUCE PARSER PROBLEM-4
4) Design a shift Stack Input Reducing production
reduce parser for the $ SSS+a*+ Shift S
Grammar: SSS+ | SS* $
| a on the string $S SS+a*+$ Shift S
“SSS+a*+” $SS S+a*+$ Shift S
$SSS +a*+$ Shift S
$SSS+ a*+$ Reduce by SSS+
$SS a*+$ Shift a
$SSa *+$ Reduce by Sa
$SSS *+$ Shift *
$SSS* +$ Reduce by SSS*
$SS +$ Shift +
$SS+ +$ Reduce by SSS+
$S $ Accept
SHIFT REDUCE CONFLICTS
• There are context-free grammars for which shift-reduce parsing cannot be used.
• Every shift-reduce parser for such a grammar can reach a configuration in which
the parser, knowing the entire stack contents and the next input symbol, cannot
decide whether to shift or to reduce (a shift/reduce conflict), or cannot decide
which of several reductions to make (a reduce/reduce conflict).
• Types of conflicts are:
• Shift / reduce conflict  Parser is facing conflict whether to shift or reduce
• Reduce / Reduce  Parser is facing conflict which reduction to apply

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy