of 17
7/31/2019 Elimination of Left Recursion
1/17
Elimination of Left Recursion
A A | Can be replace by non-left-recursive productions
A AA A|
7/31/2019 Elimination of Left Recursion
2/17
Elimination of Left RecursionE E + T | TT T*F | FF ( E ) | id
E TEE +TE | T FTT *FT | F (E) | id
7/31/2019 Elimination of Left Recursion
3/17
TechniqueA A1 | A2 | A3 | . A m | 1| 2| 3 | n
Replace A productions by
A 1 A| 2 A|..| nA
A 1 A| 2 A|..| m A|
7/31/2019 Elimination of Left Recursion
4/17
Example
S Aa | bA Ac | Sd |
A Ac | Aad | bd |
S Aa | bA bdA | AA cA | adA |
7/31/2019 Elimination of Left Recursion
5/17
TOP-DOWN Parsing
Can be viewed as an attempt to find aleftmost derivation for an input string
Is an attempt to construct a parse tree for theinput string from the root and creating thenodes of the parse tree in preorder.
7/31/2019 Elimination of Left Recursion
6/17
ExampleS cAdA ab | a
Input string w = cad
S S S
c A d c A d c A d
a b a(a) (b) (c)
7/31/2019 Elimination of Left Recursion
7/17
Left Factoring
Is a grammar transformation Useful for producing a grammar suitable for
predictive parsing.Example:For a production:
A AA 1 | 2
7/31/2019 Elimination of Left Recursion
8/17
The following grammar dangling-
else problem
S iEtS | iEtSeS | aE b
Left factored:S iEtSS | a
S eS | E b
7/31/2019 Elimination of Left Recursion
9/17
Nonrecursive Predictive Parsinga + b $
X
Y
Z
$
Predictive ParsingProgram
ParsingTable M
Output
7/31/2019 Elimination of Left Recursion
10/17
Parser programThree possibilities1. If X = a = $, the parser halts and announces successful
completion of parsing2. If X = a = $, the parser pops X off the stack and advances
the input pointer to the next input symbol.3. If X is a nonterminal, the program consults entry M[X,a] of
the parsing table M. This entry will be either an X-production of the grammar or an error entry. If for example,
M[X,a] = {X UVW}, the parser replaces X on the top of the stack by WVU ( with U on top).
7/31/2019 Elimination of Left Recursion
11/17
Nonrecursive predictive parsing
Input : a string w and a parsing table M forgrammar G
Output: if w is in L(G), a leftmostderivation of w; otherwise, an errorindication.
Setup:Stack is initialized to S$input buffer to w$
7/31/2019 Elimination of Left Recursion
12/17
ProgramSet ip to point ot the first symbol of w$;Repeat
let X be top stack symbol and a the symbol pointed to by ip;if X is a terminal or $ then
if X = a then
pop X from the stack and advance ipelse error()else /* X is a nonterminal */
if M[X,a] = X Y1 Y2Y3. Yk then beginpop X from the stack;
push Y k Yk-1YK-2. Y1 onto the stack, with Y 1 on topoutput the production X Y1 Y2Y3. Yk
endelse error()
Until X = $ /* stack is empty */
7/31/2019 Elimination of Left Recursion
13/17
Non terminal Input Symbol
id + * ( ) $
E E->TE E TE
E E +TE E e E e
T T FT T FT
T T e T *FT T e T e
F F id F (E)
7/31/2019 Elimination of Left Recursion
14/17
STACK INPUT OUTPUT
$E id + id * id$
$ET id + id * id$ E- >ET
$ETF id + id * id$ T->FT
$ETid Id + id * id$ F->id
$ET + id * id$
$E + id * id$ T->e
$ET+ + id * id$ E->+TE
$ET id * id$$ETF id * id$ T->FT
$ETid id * id$ F->id
$ET * id$
$ETF* * id$ T->*FT
$ETF id$$ETid id$ F->id
$ET $
$E $ T->e
$ $ E->e
7/31/2019 Elimination of Left Recursion
15/17
FIRST and FOLLOW:
Rules to compute FIRST(X);
1. If X is terminal, then FIRST(X) is {X}
2. If X e is a production, then add e to FIRST(X).
3. If X is nonterminal and X Y1 Y2 ..Yk is a production then placea in FIRST(X) if for some i, a is in FIRST(Y i), and e is in all of FIRST(Y 1) . FIRST(Y i-1); that is Y 1. Yi-1 * e.
If e is in FIRST(Y j) for all j -> 1 to k then add e to FIRST(X).
Computing FIRST(X) for any string X 1 X2 X nas follows
add to FIRST(X 1 X2 .. Xn) all the non-e symbols of FIRST(X 1).
also add the non-e symbols of FIRST(X 2) if e is in FIRST(X 1). and so on
7/31/2019 Elimination of Left Recursion
16/17
FOLLOW(S)Rules1. Place $ on FOLLOW(S), where S is the start
symbol and $ is the input right endmarker.
2. If there is a production A B, theneverything in FIRST( ) except for e is placed inFOLLOW(B)
3. If there is a production A B, or a
production A B where FIRST( ) containse then everything in FOLLOW(A) is inFOLLOW(B)
7/31/2019 Elimination of Left Recursion
17/17
Example
E TEE +TE | e
T FTT *FT | eF ( E ) | id
FIRST(E) = FIRST(T) = FIRST(F) = {(,id}. R 3
FIRST(E) = {+,e} R 2 for e
FIRST(T) = {*,e}
FOLLOW(E) = FOLLOW(E) = {),$}to compute FOLLOW(E)
$ is due to r 1
) is due to rule 2
to compute FOLLOW(E)
r3 to p E TE $ & ) are addred toFOLLOW(E)
To compute FOLLOW(T) & FOLLOW(T)
Since E e, {),$} are also in FOLLOW(T)plus everything otherthen e in FIRST(E)must be placed in FOLLOW(T).
FOLLOW(F) = {+,*,),$)