CD Parser
CD Parser
S
X = a + b * c;
id = E ;
id = id + id * id ; Parser
E + T
S à id = E ; T T * F
EàE+T|T
TàT*F|F F F
F à id
id id
Generation of Parse Tree S à aABe, A à Abc | a, B à d
S S S à aABe
Decision: à aAde
a A B e Which à aAbcde
production to use. A B à aabcde
A b c d A (Right Most Derivation)
In reverse
S à aABe
a a a b c d e
à aAbcBe
à aabcBe Decision:
à aabcde When to reduce
(Left Most Derivation)
Classification of Parsers
Parsers
With Without
Backtracking Backtracking Operator
LR Parsers
Precedence Parsers
Brute Forcing
Predictive
Recursive
Parsers
Descent LR(0) SLR(1) LALR(1) CLR(1)
LL(1),
Parsers
LL(K)
Top-down Parser
§ Top-down approach
§ In order to construct top-down parser the
context free grammars should not have,
1. Left Recursion S
2. Non-determinism Decision:
a A B e Which
AàAα|β A è βα* production to us.
A b c d
A à βA’
A’ à α A’ | ε S à aABe
a
à aAbcBe
A à α β1 | α β2 | α β3 à aabcBe
à aabcde
A à αA’
(Left Most Derivation)
A’ à β1 | β2 | β3
Recursive Descent Parser
§ A recursive descent parser is a top-down parser built from a set of mutually
recursive procedures (or a non-recursive equivalent) where each such procedure
implements one of the non-terminals of the grammar. Thus, the structure of the
resulting program closely mirrors that of the grammar it recognizes.
i E’
+ i E’
Input: i + i $
E à iE’
Recursive Descent Parser E’ à + iE’ | ε
Input: i + i $
E à iE’
Recursive Descent Parser E’ à + iE’ | ε
match(i)
E()
main()
Input: i + i $
E à iE’
Recursive Descent Parser E’ à + iE’ | ε
E’(i)
E()
main()
Input: i + i $
Predictive Parser (LL(1))
Input Buffer:
$
LL(1) Parser
$
Stack
LL(1)
Parsing Table
LL(1) Parser
“1” Look-ahead symbol.
Use Left most Derivation.
Scan from Left to Right.
LL(1) Parser
1. FIRST( ):
Given any non-terminal of a CFG, if we derive all the possible strings from it, the
first terminal(s) is the FIRST() of the non-terminal.
S A
E.g (1): S à aABC FIRST(S): FIRST(A):
Aàb
Bàc a A B C
b
Càd
FIRST(B): FIRST(C):
B C
c d
LL(1) Parser
1. FIRST( ):
Given any non-terminal of a CFG, if we derive all the possible strings from it, the
first terminal(s) is the FIRST() of the non-terminal.
S
E.g (2): S à ABC FIRST(S): a
Aàa|ε
Bàb A B C
Càc
a
LL(1) Parser
1. FIRST( ):
Given any non-terminal of a CFG, if we derive all the possible strings from it, the
first terminal(s) is the FIRST() of the non-terminal.
S
E.g (2): S à ABC FIRST(S): {a, b}
Aàa|ε
Bàb A B C
Càc
ε b
LL(1) Parser
2. FOLLOW( ):
During the process of derivation, the terminal(s) that could follow the non-
terminal are to be considered as FOLLOW( ) of the non-terminal.
b
LL(1) Parser
2. FOLLOW( ):
During the process of derivation, the terminal(s) that could follow the non-
terminal are to be considered as FOLLOW( ) of the non-terminal.
ε c FOLLOW(C): {$}
Derivation of FIRST:
FIRST (F): { id, ( }
E à TE’
E’ à +TE’ | ε FIRST (T’): { *, ε }
T à FT’ FIRST (T): FIRST(F): { id, ( }
T’ à *FT’ | ε
F à id | (E) FIRST (E’): { +, ε }
S à ABCDE
FIRST FOLLOW
Aàa|ε
Bàb|ε S à ABCDE {a, b, c} {$}
Càc
Dàd|ε Aàa|ε {a, ε} {b, c}
Eàe|ε Bàb|ε {b, ε} {c}
Càc {c} {d, e, $}
Dàd|ε {d, ε} {e, $}
Eàe|ε {e, ε} {$}
Solved Examples:
Q3. Find the FIRST and FOLLOW of all the non-terminals:
S à Bb | Cd
B à aB | ε FIRST FOLLOW
C à cC | ε
S à Bb | Cd {a, b, c, d} {$}
B à aB | ε {a, ε} {b}
C à cC | ε {c, ε} {d}
Solved Examples:
Q4. Find the FIRST and FOLLOW of all the non-terminals:
S à aBDh
FIRST FOLLOW
B à cC
C à bC | ε S à aBDh {a} {$}
D à EF
Eàg|ε B à cC {c} {g, f, h}
Fàf|ε C à bC | ε {b, ε} {g, f, h}
D à EF {g, f, ε} {h}
Eàg|ε {g, ε} {f, h}
Fàf|ε {f, ε} {h}
Solved Examples:
Q5. Find the FIRST and FOLLOW of all the non-terminals:
S à ACB | CbB | Ba
A à da | BC
Bàg|ε
Càh|ε FIRST FOLLOW
S à ACB | CbB | Ba {d, g, h, b, a, ε} {$}
A à da | BC {d, g, h, ε} {h, g, $}
Bàg|ε {g, ε} {$, a, h, g}
Càh|ε {h, ε} {g, $, b, h}
LL(1) Parser
“1” Look-ahead symbol.
Use Left most Derivation.
Scan from Left to Right.
Construction of LL(1) Parsing Table
E à TE’ id ( ) + * $
E’ à +TE’ | ε
T à FT’ E E à TE’ E à TE’
T’ à *FT’ | ε E’ E’ à ε E’ à +TE’ E’ à ε
F à id | (E)
T T à FT’ T à FT’
T’ T’ à ε T’ à ε T’ à *FT’ T’ à ε
F F à id F à (E)
Rules:
1. All the ε-productions are placed under FOLLOW sets.
2. Remaining productions are placed under the FIRSTs.
LL(1) Parsing FIRST FOLLOW
S
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $
S a
A
a A B d
B
b
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $
S
A
a A B d
B
ε b
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $
S
a A B d
B
ε b
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $
S
a A B d
d
ε d b
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $
S
a A B d
ε d b
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
FIRST FOLLOW
LL(1) Parsing
S à aABb {a} {$}
S à aABb Input Buffer:
Aàc|ε Aàc|ε {c, ε} {d, b}
Bàd|ε Bàd|ε {d, ε} {b} a d b $
S
a A B d
ε d
LL(1) Parser
$
Stack
a b c d $ LL(1)
S S à aABb Parsing Table
A Aàε Aàc Aàε
B Bàε Bàd
Problem Solving
1. Find out whether the following grammar is LL(1)?
S à aSbS | bSaS | ε
a b $
S à aSbS S à bSaS
S
Sàε Sàε Sàε
Problem Solving
2. Find out whether the following grammar is LL(1)?
S à (S) | ε
( ) $
S S à (S) Sàε Sàε
Problem Solving
3. Find out whether the following grammar is LL(1)?
S à AaAb | BbBa
Aàε
Bàε
a b $
S S à AaAb S à BbBa
A Aàε Aàε
B Bàε Bàε
Problem Solving
4. Find out whether the following grammar is LL(1)?
SàA|a
Aàa
5. Find out whether the following grammar is LL(1)?
S à aB | ε
B à bC | ε
C à cS | ε
6. Find out whether the following grammar is LL(1)?
S à AB
Aàa|ε
Bàb|ε
7. Find out whether the following grammar is LL(1)?
S à aSA | ε
Aàc|ε
Problem Solving
8. Find out whether the following grammar is LL(1)?
SàA
A à Bb | Cd
B à aB | ε
C à cC | ε
9. Find out whether the following grammar is LL(1)?
S à aAa | ε
A à abS | ε