Mod 2 2
Mod 2 2
𝐴→𝐴𝛼∨ β
β
𝜖
𝛼 𝐴’
Examples: Left recursion elimination
EE+T | T
ETE’
E’+TE’ | ε
TT*F | F
TFT’
T’*FT’ | ε
XX%Y | Z
XZX’
X’%YX’ | ε
Exercise: Left recursion
1. AAbd | Aa | a
BBe | b
2. AAB | AC | a | b
3. SA | B
AABC | Acd | a | aa
BBee | b
4. ExpExp+term | Exp-term | term
Left factoring
Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive parsing.
SaAB | aCD
SaS’
S’AB | CD
A xByA | xByAzA | a
A xByAA’ | a
A’ Є | zA
A aAB | aA |a
A’AB | A | 𝜖 A’AA’’ | 𝜖
AaA’ AaA’
A’’B | 𝜖
Exercise
1. SiEtS | iEtSeS | a
2. A ad | a | ab | abc | x
Parsing
• Parsing is a technique that takes input string and produces
output either a parse tree if string is valid sentence of
grammar, or an error message indicating that string is not a
valid.
• Types of parsing are:
1. Top down parsing: In top down parsing parser build parse
tree from top to bottom.
2. Bottom up parsing: Bottom up parser starts from leaves and
work up to the root.
Classification of parsing methods
Parsin
g
descen
t
Backtracking
• In backtracking, expansion of nonterminal symbol we choose
one alternative and if any mismatch occurs then we try another
alternative.
• Grammar: S cAd Input string: cad
A ab | a
S S S
c A d c A d c A d
Make prediction Make prediction
descen
t
LL(1) parser (predictive parser)
• LL(1) is non recursive top down parser.
1. First L indicates input is scanned from left to right.
2. The second L means it uses leftmost derivation for input
string
3. 1 means it uses only input symbol to predict the parsing
process. a + b $ INPU
T
X
Y Predictiv
e parsing OUTPU
Z
Stack program T
$
Parsing table
M
LL(1) parsing (predictive parsing)
Steps to construct LL(1) parser
1. Remove left recursion / Perform left factoring (if any).
2. Compute FIRST and FOLLOW of non terminals.
3. Construct predictive parsing table.
4. Parse the input string using parsing table.
Rules to compute first of non terminal
1. If and is terminal, add to .
2. If , add to .
3. If is nonterminal and is a production, then place in if for
some , a is in , and 𝜖 is in all of that is . If 𝜖 is in for all then
add 𝜖 to .
Everything in is surely in If does not derive 𝜖, then we do
nothing more to , but if , then we add and so on.
Rules to compute first of non terminal
Simplification of Rule 3
If ,
• If does not derives
• If derives
• If & Y2 derives ∈
• If , Y2 & Y3 derives ∈
𝛽 𝑖𝑠𝑎𝑏𝑠𝑒𝑛𝑡 𝛽 𝑖𝑠𝑝𝑟𝑒𝑠𝑒𝑛𝑡
𝑅𝑢𝑙𝑒 2 𝜖 𝜖
Follow(S)
Rule 1: Place $ in FOLLOW(S)
Follow(S)={ $ }
Follow(B)
SaBa BbB
S a B a Rule 2 B b B
Rule 3
A B First( A B Follow(A)-->follow(B)
Follow(B)={ a }
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa
B
Rule: 2
SaBa A
a = first()
a=FIRST(aBa)={ a } M[A,a] = A
M[S,a]=SaBa
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa
B BbB
Rule: 2
BbB A
a = first()
a=FIRST(bB)={ b } M[A,a] = A
M[B,b]=BbB
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa Error Error
B Bϵ BbB Error
Rule: 3
Bϵ A
b = follow(A)
M[B,a]=B𝜖
b=FOLLOW(B)={ a } M[A,b] = A
Example-2: LL(1) parsing
SaB | ϵ
BbC | ϵ
CcS | ϵ
Step 1: Not required NT First
S { a, 𝜖 }
Step 2: Compute FIRST B {b,𝜖}
First(S) C {c,𝜖}
SaB S𝜖
S a B S 𝜖
Rule 1 Rule 2
A A
add to add to
FIRST(S)={ a , 𝜖 }
Example-2: LL(1) parsing
SaB | ϵ
BbC | ϵ
CcS | ϵ
Step 1: Not required NT First
S { a, 𝜖 }
Step 2: Compute FIRST B {b,𝜖}
First(B) C {c,𝜖}
BbC B𝜖
B b C B 𝜖
Rule 1 Rule 2
A A
add to add to
FIRST(B)={ b , 𝜖 }
Example-2: LL(1) parsing
SaB | ϵ
BbC | ϵ
CcS | ϵ
Step 1: Not required NT First
FIRST(B)={ c , 𝜖 }
Example-2: LL(1) parsing
Step 2: Compute FOLLOW
Follow(S) Rule 1: Place $ in FOLLOW(S)
Follow(S)={ $ }
CcS SaB | ϵ
C c S BbC | ϵ
Rule 3
A B Follow(A)follow(B) CcS | ϵ
Follow(S)=Follow(C) ={$}
NT First Follow
S {a,𝜖} {$}
BbC SaB B {b,𝜖} {$}
C {c,𝜖} {$}
B b C S a B
Rule 3 Rule 3
A B Follow(A)follow(B) A B Follow(A)follow(B)
Follow(C)=Follow(B) ={$} Follow(B)=Follow(S)={$}
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a,𝜖} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
S SaB
B
C
SaB Rule: 2
A
a=FIRST(aB)={ a } a = first()
M[A,a] = A
M[S,a]=SaB
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
S SaB S𝜖
B
C
Rule: 3
A
S𝜖 b = follow(A)
M[A,b] = A
b=FOLLOW(S)={ $ }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
S SaB S𝜖
B BbC
C
Rule: 2
A
BbC a = first()
M[A,a] = A
a=FIRST(bC)={ b }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
S SaB S𝜖
B BbC B𝜖
C
Rule: 3
A
B𝜖 b = follow(A)
M[A,b] = A
b=FOLLOW(B)={ $ }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
S SaB S𝜖
B BbC B𝜖
C CcS
Rule: 2
A
CcS a = first()
M[A,a] = A
a=FIRST(cS)={ c }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
First(E’) E’+TE’ | ϵ
E’+TE’ TFT’
E’ + T E’ Rule 1
add to T’*FT’ | ϵ
A NT First
EF(E){ (,id }
{ +, 𝜖 }
| id
E’
T { (,id }
E’𝜖
T’
E’ Rule 2 F { (,id }
A add to
FIRST(E’)={ + , 𝜖 }
Example-3: LL(1) parsing
Step 2: Compute FIRST ETE’
First(T’) E’+TE’ | ϵ
T’*FT’ TFT’
T’ * F T’ Rule 1
add to T’*FT’ | ϵ
A NT First
EF(E){ (,id }
{ +, 𝜖 }
| id
E’
T { (,id }
T’𝜖
T’ { *, 𝜖 }
T’ Rule 2 F { (,id }
A add to
FIRST(T’)={ *, 𝜖 }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(E) E’+TE’ | ϵ
Rule 1: Place $ in FOLLOW(E) TFT’
FOLLOW(E)={ $, ) }
Example-3: LL(1) parsing
ETE’
Step 2: Compute FOLLOW
E’+TE’ | ϵ
FOLLOW(E’) TFT’
ETE’
NT First T’*FT’ |ϵ
Follow
E T E’ F(E)
Rule 3 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id }
T’ { *, 𝜖 }
E’+TE’ F { (,id }
E’ +T E’ Rule 3
A B
FOLLOW(E’)={ $,) }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(T) E’+TE’ | ϵ
TFT’
ETE’
NT First T’*FT’ |ϵ
Follow
E T E’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id }
T’ { *, 𝜖 }
F { (,id }
E T E’ Rule 3
A B
FOLLOW(T)={ +, $, )
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(T) E’+TE’ | ϵ
TFT’
E’+TE’
NT First T’*FT’ |ϵ
Follow
E’ + T E’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id } { +,$,) }
T’ { *, 𝜖 }
F { (,id }
E’ + T E’ Rule 3
A B
FOLLOW(T)={ +, $, ) }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(T’) E’+TE’ | ϵ
TFT’
TFT’
NT First T’*FT’ |ϵ
Follow
T F T’ F(E)
Rule 3 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id } { +,$,) }
T’*FT’ T’ { *, 𝜖 } { +,$,) }
F { (,id }
T’ *F T’ Rule 3
A B
FOLLOW(T’)={+ $,) }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(F) E’+TE’ | ϵ
TFT’
TFT’
NT First T’*FT’ |ϵ
Follow
T F T’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id }
T F T’ Rule 3
A B
FOLLOW(F)={ *, + ,$ , )
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(F) E’+TE’ | ϵ
TFT’
T’*FT’
NT First T’*FT’ |ϵ
Follow
T’ * F T’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
T’ * F T’ Rule 3
A B
FOLLOW(F)={ *,+,$, ) }
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
M[E,(]=ETE’
M[E,id]=ETE’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
M[E’,+]=E’+TE’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
M[E’,$]=E’𝜖
M[E’,)]=E’𝜖
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
M[T,(]=TFT’
M[T,id]=TFT’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
M[T’,*]=T’*FT’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
NT Input Symbol
E’+TE’ | ϵ
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ E’+TE’ E’𝜖 E’𝜖
NT First F(E)
Follow
T TFT’ TFT’ | id
E { (,id } { $,) }
{ +, 𝜖 }
T’ T’𝜖 T’*FT’ T’𝜖 T’𝜖
E’ { $,) }
F
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
T’𝜖 F { (,id } {*,+,$,)}
Rule: 3
b=FOLLOW(T’)={ +,$,) } A
b = follow(A)
M[T’,+]=T’𝜖 M[A,b] = A
M[T’,$]=T’𝜖
M[T’,)]=T’𝜖
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
a=FIRST((E))={ ( }
M[F,(]=F(E)
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
a=FIRST(id)={ id }
M[F,id]=Fid
Example-3: LL(1) parsing
• Step 4: Make each undefined entry of table be Error
NT Input Symbol
id + * ( ) $
E ETE’ Error Error ETE’ Error Error
E’ Error E’+TE’ Error Error E’𝜖 E’𝜖
T TFT’ Error Error TFT’ Error Error
T’ Error T’𝜖 T’*FT’ Error T’𝜖 T’𝜖
F Fid Error Error F(E) Error Error
Example-3: LL(1) parsing
Step 5: Parse the string : id + id * id $ NT Input Symbol
id + * ( ) $
STACK INPUT OUTPUT
E ETE’ Error Error ETE’ Error Error
E$ id+id*id$
E’ Error E’+TE’ Error Error E’𝜖 E’𝜖
TE’$ id+id*id$ ETE’
T TFT’ Error Error TFT’ Error Error
FT’E’$ id+id*id$ TFT’
T’ Error T’𝜖 T’*FT’ Error T’𝜖 T’𝜖
idT’E’$ id+id*id$ Fid
F Fid Error Error F(E) Error Error
T’E’$ +id*id$
E’$ +id*id$ T’𝜖
+TE’$ +id*id$ E’+TE’
TE’$ id*id$ FT’E’$ id$
FT’E’$ id*id$ TFT’ idT’E’$ id$ Fid
idT’E’$ id*id$ Fid T’E’$ $
T’E’$ *id$ E’$ $ T’𝜖
*FT’E’$ *id$ T*FT’ $ $ E’𝜖
Exercise
1. Construct parsing table for the following grammar:
S -> aABb
A -> c | €
B -> d | €
2. Construct parsing table for the following grammar:
S -> AaAb | BbBa
A -> €
B -> €
3. Construct parsing table for the following grammar:
S -> AB |eDa
A -> ab |c
B -> dC
C -> eC | €
D -> fD | €
Example-4:
Step 3: Construct predictive parsing table S → iEtSS’ | a
S’→ eS | ε
NT Input Symbol E→b
a b e i t $
S S → iEtSS’
S’
S → iEtSS’
a=FIRST(iEtSS’)={ i }
M[S,i]= S → iEtSS’
Example-4:
Step 3: Construct predictive parsing table S → iEtSS’ | a
S’→ eS | ε
NT Input Symbol E→b
a b e i t $
S S→a S → iEtSS’
S’
S→a
a=FIRST(a)={ a }
M[S,a]= S →a
Example-4:
Step 3: Construct predictive parsing table S → iEtSS’ | a
S’→ eS | ε
NT Input Symbol E→b
a b e i t $
S S→a S → iEtSS’
S’
E
E→b
E→b
a=FIRST(b)={ b }
M[E,b]= E →b
Example-4:
Step 3: Construct predictive parsing table S → iEtSS’ | a
S’→ eS | ε
NT Input Symbol E→b
a b e i t $
S S→a S → iEtSS’
S’ S’ →eS
E E→b
S’→ eS
a=FIRST(eS)={ e}
M[S’,e]= S’→ eS
Example-4:
Step 3: Construct predictive parsing table S → iEtSS’ | a
S’→ eS | ε
NT Input Symbol E→b
a b e i t $
S S→a S → iEtSS’
S’ S’ →eS
S’ → ε
S’ → ε
E E→b
S’→ ε
a=FIRST(eS)={ε}
b=FOLLOW(S’)={ $, e }
M[S’,e]= S’→ ε , M[S’, $]= S’→ ε
Example-4:
Step 3: Construct predictive parsing table S → iEtSS’ | a
S’→ eS | ε
NT Input Symbol E→b
a b e i t $
S S→a S → iEtSS’
S’ S’ →eS
S’ → ε
S’ → ε
E E→b
• Since there are more than one production, the grammar is not
LL(1) grammar.
Parsing methods
Parsin
g
descen
t
Recursive descent parsing
• A top down parsing that executes a set of recursive
procedure to process the input without backtracking is
called recursive descent parser.
• There is a procedure for each non terminal in the
grammar.
• Consider RHS of any production rule as definition of the
procedure.
• As it reads expected input symbol, it advances input
pointer to next input position.
• The term descent refers to the direction in which the
parse tree is built.
• It builds the parse tree from the root to the leaf.
Cont.,
Example: Recursive
Procedure T descent parsing
Proceduce
Match(token t)
Procedure E {
{ If lookahead=’*’ {
If { If
lookahead=num lookahead=t
{ Match(‘*’);
If lookahead=next_toke
Match(num); lookahead=num n;
T(); { Else
}
Else Match(num); Error();
Error(); Procedure Error
}
If lookahead=$ T(); {
{ } Print(“Error”);
Declare Else }
success;
} Error();
T* numT| 𝜖
Else
Error(); }
Enum T
} Else
3 * 4 $ Succes NULL
} s
Example: Recursive
Procedure T descent
Proceduce parsing
Procedure E { Match(token t)
{ If lookahead=’*’ {
If { If
lookahead=num lookahead=t
{ Match(‘*’);
If lookahead=next_toke
Match(num); lookahead=num n;
T(); { Else
}
Else Match(num); Error();
Error(); P
}rocedure Error
If lookahead=$ T(); {
{ } Print(“Error”);
Declare Else }
success;
} Error();
T* numT| 𝜖
Else
Error(); }
Enum T
} Else
3 * 4 $ Succes NULL
3 4 * $
} s Error
Example 2
Write down the algorithm using Recursive procedures to
implement the following Grammar.
E → TE′
E′ → +TE′
T → FT′
T′ →∗ FT′|ε
F → (E)|id
Solution:
Procedure E() Procedure E’() Procedure T’()
{ T() { if lookahead = + { if lookahead = *
E’() { match(+) { match(*)
if lookahead = T() F()
$ E’() T’()
declare } }
success else else
else NULL NULL
Error() } }
}
Procedure T()
{ F()
T’()
}
Cont…
Procedure F() Proceduce Match(token t)
{ {
If lookahead == id If lookahead=t
{ match(id) } lookahead=next_token;
Else if lookahead == ‘(‘ Else
{ match(‘(‘) Error();
E() }
if lookahead
== ‘)’