0% found this document useful (0 votes)
11 views72 pages

Mod 2 2

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)
11 views72 pages

Mod 2 2

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

Module 2 – Syntax Analysis

Left recursion & Left


factoring
Left recursion
• A grammar is said to be left recursive if it has a non terminal
such that there is a derivation for some string

• Top-down parsing methods cannot handle left-recursive


grammars, so a transformation is needed to eliminate left
recursion.

𝐴→𝐴𝛼∨ β 
β
𝜖
𝛼 𝐴’
Examples: Left recursion elimination
EE+T | T
ETE’
E’+TE’ | ε
TT*F | F
TFT’
T’*FT’ | ε
XX%Y | Z
XZX’
X’%YX’ | ε
Exercise: Left recursion
1. AAbd | Aa | a
BBe | b
2. AAB | AC | a | b
3. SA | B
AABC | Acd | a | aa
BBee | b
4. ExpExp+term | Exp-term | term
Left factoring
Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive parsing.
SaAB | aCD
SaS’
S’AB | CD
A xByA | xByAzA | a

A xByAA’ | a
A’ Є | zA
A aAB | aA |a

A’AB | A | 𝜖 A’AA’’ | 𝜖
AaA’ AaA’

A’’B | 𝜖
Exercise
1. SiEtS | 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

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive
parsing) SLR
LL(1)
CLR
Recursi
ve LALR

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

a b Backtrack a Parsing done


Exercise
1. E 5+T | 3-T
T V | V*V | V+V
V a | b
String: 3-a+b
Parsing Methods
Parsin
g

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive
parsing) SLR
LL(1)
CLR
Recursi
ve LALR

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 ∈

• If , Y2 , Y3 …..YK all derives ∈


(note: if all non terminals derives ∈ then add ∈ to FIRST(A))
Rules to compute FOLLOW of non
terminal
2. If then everything in except for 𝜖 is placed in
1. Place S is start symbol)

3. If there is a production or a production where contains then


everything in
How to apply rules to find FOLLOW of non
terminal?
A→𝛼 𝐵 𝛽

𝛽 𝑖𝑠𝑎𝑏𝑠𝑒𝑛𝑡 𝛽 𝑖𝑠𝑝𝑟𝑒𝑠𝑒𝑛𝑡

𝑅𝑢𝑙𝑒 3 𝛽 isterminal 𝛽𝑖𝑠𝑁𝑜𝑛𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙

𝑅𝑢𝑙𝑒 2 𝜖 𝜖

Rule 2: If then everything in except for 𝜖


is placed in
Rule 3: If there is a production or a 𝑅𝑢𝑙𝑒 2 𝑅𝑢𝑙𝑒2+𝑅𝑢𝑙𝑒3
production where contains then
everything in
Rules to construct predictive parsing
table
1. For each production of the grammar, do steps 2 and 3.
2. For each terminal in , Add to .
3. If is in , Add to for each terminal in . If is in , and is in ,
add to .
4. Make each undefined entry of M be error.
Steps to construct LL(1) parsing (predictive
parsing)
1. Remove left recursion / Perform left factoring (if any).
2. Compute FIRST and FOLLOW of non terminals.
a. FIRST
i. If S-> aSBc, B-> ε then FIRST(S) ={a}, FIRST(B)={ε}
ii. If A->Bxy, B->MN,M->z then FIRST(A)= FIRST(B)= FIRST(M)={z}
b. FOLLOW
i. Place S is start symbol)
ii. If Terminal found on FOLLOW(NT), then add that terminal as FOLLOW(NT)
= Terminal
iii. If No symbol as FOLLOW(NT), then FOLLOW(NT) = FOLLOW(NT on LHS)
iv. If NT found as FOLLOW(NT) then, Find FIRST(FOLLOW(NT) then apply ε
3. Construct find FOLLOW(NT
andpredictive on LHS)
parsing table.
i. For each N.T, if FIRST(NT) is not ε then for each symbols of FIRST(NTRHS) update
its production in the parsing table.
ii. If FIRST(NT) is ε, the for each symbols of FOLLOW(NTLHS) update its production
in the parsing table.
iii. Make each undefined entry of Parsing Table as error.
Example-1: LL(1) parsing
SaBa
NT First
BbB | ϵ S {a}
Step 1: Not required
B {b,𝜖}
Step 2: Compute FIRST
First(S) S  a B a
Rule 1
SaBa A  add to
FIRST(S)={
a}
First(B)
BbB B𝜖
B  b B B  𝜖
A  Rule 1
A 
add to Rule 2
,𝜖}
FIRST(B)={ add to
b
Example-1: LL(1) parsing
SaBa
NT First Follow

BbB | ϵ S {a} {$}

Step 2: Compute FOLLOW B {b,𝜖} {a}

Follow(S)
Rule 1: Place $ in FOLLOW(S)
Follow(S)={ $ }

Follow(B)
SaBa BbB
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
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa
B

Rule: 2
SaBa A
a = first()
a=FIRST(aBa)={ a } M[A,a] = A

M[S,a]=SaBa
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa
B BbB

Rule: 2
BbB A
a = first()
a=FIRST(bB)={ b } M[A,a] = A

M[B,b]=BbB
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa Error Error
B Bϵ BbB 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
SaB | ϵ

BbC | ϵ
CcS | ϵ
Step 1: Not required NT First
S { a, 𝜖 }
Step 2: Compute FIRST B {b,𝜖}
First(S) C {c,𝜖}

SaB S𝜖
S  a B S  𝜖
Rule 1 Rule 2
A  A 
add to add to

FIRST(S)={ a , 𝜖 }
Example-2: LL(1) parsing
SaB | ϵ

BbC | ϵ
CcS | ϵ
Step 1: Not required NT First
S { a, 𝜖 }
Step 2: Compute FIRST B {b,𝜖}
First(B) C {c,𝜖}

BbC B𝜖
B  b C B  𝜖
Rule 1 Rule 2
A  A 
add to add to

FIRST(B)={ b , 𝜖 }
Example-2: LL(1) parsing
SaB | ϵ

BbC | ϵ
CcS | ϵ
Step 1: Not required NT First

Step 2: Compute FIRST S { a, 𝜖 }


B {b,𝜖}
First(C)
C {c,𝜖}
CcS C𝜖
C  c S C  𝜖
Rule 1 Rule 2
A  A 
add to add to

FIRST(B)={ c , 𝜖 }
Example-2: LL(1) parsing
Step 2: Compute FOLLOW
Follow(S) Rule 1: Place $ in FOLLOW(S)
Follow(S)={ $ }

CcS SaB | ϵ

C  c S BbC | ϵ
Rule 3
A  B Follow(A)follow(B) CcS | ϵ
Follow(S)=Follow(C) ={$}
NT First Follow
S {a,𝜖} {$}
BbC SaB 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
SaB | ϵ NT First Follow
S {a,𝜖} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB
B
C

SaB Rule: 2
A
a=FIRST(aB)={ a } a = first()
M[A,a] = A
M[S,a]=SaB
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB S𝜖
B
C

Rule: 3
A
S𝜖 b = follow(A)
M[A,b] = A
b=FOLLOW(S)={ $ }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB S𝜖
B BbC
C

Rule: 2
A
BbC a = first()
M[A,a] = A
a=FIRST(bC)={ b }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB S𝜖
B BbC B𝜖
C

Rule: 3
A
B𝜖 b = follow(A)
M[A,b] = A
b=FOLLOW(B)={ $ }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB S𝜖
B BbC B𝜖
C CcS
Rule: 2
A
CcS a = first()
M[A,a] = A
a=FIRST(cS)={ c }
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $

S SaB Error Error S𝜖


B Error BbC Error B𝜖
C Error Error CcS C𝜖
Rule: 3
A
C𝜖 b = follow(A)
M[A,b] = A
b=FOLLOW(C)={ $ }
Example-3: LL(1) parsing
EE+T | T
TT*F | F
F(E) | id
Step 1: Remove left recursion
ETE’
E’+TE’ | ϵ
TFT’
T’*FT’ | ϵ
F(E) | id
Example-3: LL(1) parsing
Step 2: Compute FIRST ETE’
First(E) E  T E’ Rule 3
E’+TE’ | ϵ
First(A)=First(Y
A Y Y
ETE’  1 2
1)
TFT’
= {(,
FIRST(E)=FIRST(T)
id }
T’*FT’ | ϵ
NT First
EF(E){ (,id
| id}
First(T) T  F T’ Rule 3
First(A)=First(Y
E’
A  Y1 Y2 T { (,id }
TFT’ 1)
= {(,
FIRST(T)=FIRST(F) T’
id }
F { (,id }
First(F)
F  ( E ) F  id
F(E) A  Rule 1 Fid
A  Rule 1
add to add to
FIRST(F)={ ,
( id }
Example-3: LL(1) parsing
Step 2: Compute FIRST ETE’

First(E’) E’+TE’ | ϵ
E’+TE’ TFT’
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 ETE’

First(T’) E’+TE’ | ϵ
T’*FT’ TFT’
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 ETE’

FOLLOW(E) E’+TE’ | ϵ
Rule 1: Place $ in FOLLOW(E) TFT’

F(E) NT First T’*FT’ |ϵ


Follow
F(E)
E { (,id }| id { $,) }
E’ { +, 𝜖 }
F  ( E ) Rule 2 T { (,id }
{ *, 𝜖 }
A  B
T’
F { (,id }

FOLLOW(E)={ $, ) }
Example-3: LL(1) parsing
ETE’
Step 2: Compute FOLLOW
E’+TE’ | ϵ
FOLLOW(E’) TFT’
ETE’
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 ETE’

FOLLOW(T) E’+TE’ | ϵ
TFT’
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 ETE’

FOLLOW(T) E’+TE’ | ϵ
TFT’
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 ETE’

FOLLOW(T’) E’+TE’ | ϵ
TFT’
TFT’
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 ETE’

FOLLOW(F) E’+TE’ | ϵ
TFT’
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 2: Compute FOLLOW ETE’

FOLLOW(F) E’+TE’ | ϵ
TFT’
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 ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
| id
T E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
ETE’ A
a = first()
a=FIRST(TE’)={ (,id } M[A,a] = A

M[E,(]=ETE’
M[E,id]=ETE’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’
| id
T E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
E’+TE’ A
a = first()
a=FIRST(+TE’)={ + } M[A,a] = A

M[E’,+]=E’+TE’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’ E’𝜖 E’𝜖
| id
T E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 3
E’𝜖 A
b = follow(A)
b=FOLLOW(E’)={ $,) } M[A,b] = A

M[E’,$]=E’𝜖
M[E’,)]=E’𝜖
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’ E’𝜖 E’𝜖
| id
T TFT’ TFT’ E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
TFT’ A
a = first()
a=FIRST(FT’)={ (,id } M[A,a] = A

M[T,(]=TFT’
M[T,id]=TFT’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’ E’𝜖 E’𝜖
| id
T TFT’ TFT’ E { (,id } { $,) }
T’ T’*FT’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
T’*FT’ A
a = first()
a=FIRST(*FT’)={ * } M[A,a] = A

M[T’,*]=T’*FT’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
NT Input Symbol
E’+TE’ | ϵ
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ E’+TE’ E’𝜖 E’𝜖
NT First F(E)
Follow
T TFT’ TFT’ | 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 ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’ E’𝜖 E’𝜖
| id
T TFT’ TFT’ E { (,id } { $,) }
T’ T’𝜖 T’*FT’ T’𝜖 T’𝜖 E’ { +, 𝜖 } { $,) }
F F(E) T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
A
a = first()
F(E) M[A,a] = A

a=FIRST((E))={ ( }
M[F,(]=F(E)
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’

NT Input Symbol E’+TE’ | ϵ


TFT’
id + * ( ) $
E ETE’ ETE’ T’*FT’ | ϵ
E’ NT First F(E)
Follow
E’+TE’ E’𝜖 E’𝜖
| id
T TFT’ TFT’ E { (,id } { $,) }
T’ T’𝜖 T’*FT’ T’𝜖 T’𝜖 E’ { +, 𝜖 } { $,) }
F Fid F(E) T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
Rule: 2
A
a = first()
Fid M[A,a] = A

a=FIRST(id)={ id }
M[F,id]=Fid
Example-3: LL(1) parsing
• Step 4: Make each undefined entry of table be Error
NT Input Symbol
id + * ( ) $
E ETE’ Error Error ETE’ Error Error
E’ Error E’+TE’ Error Error E’𝜖 E’𝜖
T TFT’ Error Error TFT’ Error Error
T’ Error T’𝜖 T’*FT’ Error T’𝜖 T’𝜖
F Fid 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 ETE’ Error Error ETE’ Error Error
E$ id+id*id$
E’ Error E’+TE’ Error Error E’𝜖 E’𝜖
TE’$ id+id*id$ ETE’
T TFT’ Error Error TFT’ Error Error
FT’E’$ id+id*id$ TFT’
T’ Error T’𝜖 T’*FT’ Error T’𝜖 T’𝜖
idT’E’$ id+id*id$ Fid
F Fid 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$ TFT’ idT’E’$ id$ Fid
idT’E’$ id*id$ Fid 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

Top down Bottom up parsing (Shift


parsing reduce)
Back tracking Operator
precedence
Parsing without
backtracking LR parsing
(predictive
parsing) SLR
LL(1)
CLR
Recursi
ve LALR

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(); }
Enum 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(); }
Enum 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
== ‘)’

match(‘)’) Procedure Error


else {
Error() Print(“Error”);
} }
Else { Error() }
}

You might also like