UNIT-2
SYNTAX ANALYSIS
UNIT-2 TOPICS
• Syntax Analysis: Elimination of left recursion
and left factoring
• Syntax Analysis: Top–Down parsing: Recursive
Descent parsing, Computation of FIRST and
FOLLOW, LL(1) Grammar, Non- Recursive Descent
parsing, Error Recovery in Predictive Parsing.
• Bottom-Up Parsing: Reductions, Handle
Pruning, Shift-Reduce Parsing, Shift-Reduce
Parsing Conflicts,
LEFT RECURSION ELIMINATION
• A context-free grammar is said to be left
recursive if it contains a production rule
where the non-terminal on the left-hand side of
the rule also appears as the first symbol on the
right-hand side.
• In other words, the grammar is trying to define a
non-terminal in terms of itself, creating a
recursive loop.
• If AAα1 | Aα2 |…|Aαm | β1 | β2 | … | βn where
no βi begins with ‘A’, we replace A-productions
by
PROBLEMS ON LEFT RECURSION
1) Eliminate immediate left recursion from the following
grammar AAα1 | A β1A’
EE+T|T β1 A’ α1A’ | ε
TT*F|F
F(E) | id
Ans: Left recursion is present in the 1st 2 productions. (E
ETE’
ETE’
and T) E’+TE’ | ε E’+TE’ |
EA EA + T | T ε
α β
1 1
TFT’ (FINAL
GRAMMAR)
T T*F | TFT’ T’*FT’ | ε
T’*FT’ | ε F(E) | id
F
PROBLEMS ON LEFT RECURSION
2) Eliminate indirect left recursion from the
following Grammar A β A’ | β A’ | β A’ | … | β A’
AAα | Aα |…|Aα | β | β
1 2 m 1 2
1 2 3 n
A’ α A’ | α A’ | α A’ | …|
SAa | b |…|β n
α A’ | ε
1 2 3
A Ac | Sd | e
Ans: No immediate left recursion among the S-
productions. So, substitute S production
AbdA’ |in ASd as
eA’
AAc | Aad | bd | e A’ cA’ | adA’ |
A α1 A α2 β1 β2 ε
S Aa | b
Problems on Left Recursion
A β1A’ | β2A’ | β3A’ | … | βnA’
3) A Abd | Aa | AAα
a 1 | Aα2 |…|Aαm | β1 | β2 A’ α1A’ | α2A’ | α3A’ | …|
| … | βn
B Be | b αmA’ | ε
Ans: A=A, α1=bd , α2=a , β1=a
A aA’ FINAL GRAMMAR:
A aA’
A’ bdA’ | aA’ | ε A’ bdA’ | aA’ | ε
A=B, α1=e , β1=b B bB’
B’ eB’ | ε
B bB’
B’ eB’ | ε
Problems on Left Recursion
4) E E + E | E x E | a A β1A’ | β2A’ | β3A’ | … |
AAα1 | Aα2 |…|Aαm | β1 | β2
βnA’
| … | βn
A’ α1A’ | α2A’ | α3A’ | …|
Ans: E aE’ αmA’ | ε
E’ +EE’ | xEE’ | ε
5) S (L) | a
L L, S | S
Ans: S (L) | a
L SL’
L’ ,SL’ | ε
Problems on Left Recursion
6) S → S0S1S / 01 AAα1 | Aα2 |…|Aαm | β1 | β2
A β1A’ | β2A’ | β3A’ | … |
βnA’
| … | βn
Ans: A’ α1A’ | α2A’ | α3A’ | …|
S → 01A’ αmA’ | ε
A’ → 0S1SA’ / ∈
Ans:
S→A
7) S → A
A → aBA’ / acA’
A → Ad / Ae / aB / ac
A’ → dA’ / eA’ / ∈
B → bBc / f
B → bBc / f
LEFT FACTORING
• It is the process of factoring out the common prefixes of
alternates.
• When a grammar has multiple production rules for a non-terminal
with a common prefix, the parser faces ambiguity during the
parsing process.
• It has to choose between these rules without knowing which one
will ultimately lead to a successful parse. In general,
After removing left A αA’ | ϒ
factoring
If A αβ1 | αβ2 | … |αβn | ϒ A’ β1 | β2 | β3 |
… | βn
PROBLEMS ON LEFT FACTORING
1) Remove left factoring from the following
Grammar:
S iCtS | iCtSeS | a
Cb
Ans:
SiCtSS’ | a
S’ ε | eS
In, S iCtS | iCtSeS | a
Cb
A=S, α=iCtS, β1=ε, β2=eS, ϒ = a
PROBLEMS ON LEFT FACTORING
2) Eliminate left factoring from the following
grammar
S T;S | ε
T U.T | U
U x | y | [S]
Ans: Here, left factoring present in T production
T UT’
T U.T | U
T’.T | ε
A α β1 α β2=ε
T UT’
T’.T | ε
Final Grammar after left factoringSisT;S | ε
Ux|y|
[S]
PROBLEMS ON LEFT FACTORING
3) G E
ET+E|T–E|T
TF*T|F/T|F
Factor num | id
Ans:
Left factoring is present in E T + E | T – E | T (Where, A=E, α=T, β1=+E, β2=-E,
β3=ε)
E TE’
E’ +E | -E | ε
Left factoring is present in T F * T | F / T FINAL
| F (Where, A=T,: α=F, β1=*T, β2=/T,
GRAMMAR
β3=ε) GE
T FT’ E TE’
E’+E | -E | ε
T’ *T | /T | ε
T FT’
T’ *T | /T | ε
Eliminate left factoring from A → aAB | aBc | aAc
Ans: (A=A, α=a, β1=AB, β2=Bc, β3=Ac)
Step-1)
A → aA’
A’ → AB | Bc | Ac Again left factoring is present (A=A’, α=A, β1=B, β2=c, ϒ
= Bc)
Step-2) A aA’
A’ AA’’ | Bc
A’’ B|c
Eliminate left factoring from
S → bSSaaS / bSSaSb / bSb / a
Ans: A=S, α=bS, β1=SaaS, β2=SaSb, β3=b, ϒ=a)
Step-1) S bSS’ | a
S’ SaaS | SaSb | b (Left factoring A=S’, α= Sa, β1=aS,
β2=Sb, ϒ=b)
Step-2)
S bSS’ | a
SSaS’’ | b
S’’ aS | Sb
Eliminate left factoring from the following:
1) S → a / ab / abc / abcd
2) S → aAd / aB
A → a / ab
B → ccd / ddc
3) S → aSSbS / aSaSb / abb / b
SYNTAX ANALYSIS: TOP DOWN PARSING
• A parser for Grammar ‘G’ is a program that takes as input a
string ‘w’ and produces an output either a parse tree for
‘w’, if ‘w’ is a sentence of G, or an error message indicating
that ‘w’ is not a sentence of G.
• Top-down parsing is an attempt to find the leftmost
derivation (LMD) for an input string.
• That is, to construct a parse tree for the input starting from
the root and creating nodes of the parse tree in preorder.
• Example: Illustrate the working of a top-down parser for
the following grammar on the input “cad”
ScAd
Aab | a
SYNTAX ANALYSIS: TOP-DOWN PARSING
• Example: Illustrate the working of a top-down parser for the following
grammar on the input “cad”
ScAd
Aab | a
• Fig (a) shows production of S
• Then, leftmost leaf ‘c’ matches
• 1st symbol of ‘w’. So advance
input pointer to ‘a’ the 2nd symbol of ‘w’ and consider the next leaf,
labelled
‘A’. Then expand ‘A’ using 1st alternative for ‘A’ to obtain the tree (Fig
(b)).
• ‘a’ matches but ‘b’ does not match ‘d’ of ‘w’. So try 2 nd alternative of ‘A’
[Aa] shown in Fig (c). Leaf ‘a’ matches 2nd symbol of ‘w’. So halt and
announce successful completion of parsing.
RECURSIVE DESCENT (RD)
PARSER
• A parser that uses a set of recursive procedures to
recognize its input with no backtracking is called a
recursive descent parser.
• Step-1) Eliminate left recursion if present
• Step-2) Eliminate left factoring if present
• Step-3) If both left recursion and left factoring are
present then remove left recursion first and then in the
resulting grammar if left factoring is present then
remove it.
• Step-4) Write recursive procedures for the resulting
grammar.
Problems on Recursive Descent Parser
1) Write a RD parser for the following grammar
ETE’
E’+TE’ | ε
TFT’
T’*FT’ | ε
F(E) | id
Ans: Since neither left recursion or left factoring is present directly write the recursive
Procedure
procedures Procedure
E() Procedure EPRIME() T() Procedure TPRIME()
Begin Begin Begin Begin
T() if (input- F() if (input-symb=‘*’
EPRIME symb=‘+’) then TPRIME then
() Begin () Begin
End ADVANCE(); End ADVANCE();
T(); F();
EPRIME(); TPRIME();
End if End if
End End
Problems on Recursive Descent
Parser
Procedure F()
Begin
if (input-symb=‘id’ then
ADVANCE();
else if input-symb=‘(‘ then
begin
ADVANCE();
E();
if input-symb=‘)’ then
ADVANCE()
else
ERROR()
end if
else
ERROR();
End if
End
Problems on RD parser
2) Write RD parser for A int | int + A | int – A | A –
(A)
Ans: Here, both left recursion and left factoring are
present. So, 1st remove left recursion and in the resulting
grammar if left factoring is present then eliminate it and
then write the RD parser. A β A’ | β A’ | β A’ | … |
AAα | Aα |…|Aα | β | β 1 2 3
Step-1)
|…|β
1
n
Elimination of left
2 m 1
recursion
β A’
2
n
A’ α1A’ | α2A’ | α3A’ | …|
αmA’ | ε A int A’ | int + A A’ | int-
AA’
A’ -(A)A’| ε
A
A A
A –α (A)β | int β| int + A | int - A
1 β
2 3
PROBLEM-2 ON RD PARSER
2) Eliminate left factoring from
A int A’ | int + A A’ | int-AA’
A’ -(A)A’| ε
A int A’’
A int A’ | int + A A’ | int-AA’
A’’ A’ | +AA’ | -AA’
α=int, β1=A’, β2=+AA’, β3=-AA’ A’ -(A)A’| ε
So, final grammar is
Problem-2 on RD parser
Write recursive procedures for the following
Proc A() Proc Adash()
A int A’’ Begin Begin
if (input-symb=‘int’ then) if input_symb=‘-’ then
A’’ A’ | +AA’ | - ADVANCE(); begin
AA’ Adoubledash();
ADVANCE();
end if
A’ -(A)A’| ε End if input_symb=‘(‘
then
Proc Adoubledash() begin
Begin ADVANCE();
Adash(); A();
if (input-symb=‘+’ if input_symb=‘)’
then) then
begin
begin
ADVANCE();
A(); ADVANCE();
Adash(); Adash();
elseif input_symb=-’ end if
then end if
ADVANCE(); end if
A(); end
Adash();
end if
End
Problem-3 on RD Parser
3) Write RD parser for the following
S I | other
I if S | if S else S
Ans: Left factoring is present in I production:
I if S | if S else S I if I’
α=if, β1=S, β2=S else S I’ S| S else S
S I | other
Again, left factoring is
present
Problem-3 on RD Parser
I if I’
I’ SI’’
I’ S| S else S ( α=S, β1=ε, β2=else S)
I’’ ε| else S
S I | other
Procedure S() Procedure I()
So, Final grammar is:Begin
Begin
S I | other if (input- if (input-symb=‘if’) Procedure Idash()
symb=‘other’) then then Begin
I if I’ begin begin S();
ADVANCE() ADVANCE() Idoubledash();
I’ SI’’ else Idash(); end
I’’ ε| else S I() end if
end if end
end
Problem-3 on RD Parser
Procedure
S I | other Idoubledash()
I if I’ Begin
I’ SI’’ if input-symb=‘else’
then
I’’ ε| else S
ADVANCE();
S();
end if
end
Write Recursive Descent Parsers
for the following grammars
1. S → bSSaaS / bSSaSb / bSb / a
2. A → aAB / aBc / aAc
A β1A’ | β2A’ | β3A’ | … | βnA’
AAα1 | Aα2 |…|Aαm | β1 | β2
A’ α1A’ | α2A’ | α3A’ | …|
| … | βn
αmA’ | ε