Unit 3
Unit 3
GTU # 3170701
Unit – 3
Syntax Analysis (I)
Topics to be covered
Looping
• Role of parser
• Context free grammar
• Derivation & Ambiguity
• Left recursion & Left factoring
• Classification of parsing
• Backtracking
• LL(1) parsing
• Recursive descent paring
• Shift reduce parsing
• Operator precedence parsing
• LR parsing
• Parser generator
Role of Parser
Role of parser
Toke Pars
IR
front end
Source Lexical n etre
Parser Rest of
progra analyzer e
m Get next
token
Symbol table
Parser obtains a string of token from the lexical analyzer and reports
syntax error if any otherwise generates parse tree.
There are two types of parser:
1. Top-down parser
2. Bottom-up parser
Context free grammar
Context free grammar
A context free grammar (CFG) is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set formulas of the form where and
Nonterminal symbol:
The name of syntax category of a language, e.g., noun, verb, etc.
The It is written as a single capital letter, or as a name enclosed
between < … >, e.g., A or <Noun>
<Noun Phrase> →
<Article><Noun>
<Article> → a | an | the
<Noun> → boy | apple
Context free grammar
A context free grammar (CFG) is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set formulas of the form where and
Terminal symbol:
A symbol in the alphabet.
It is denoted by lower case letter and punctuation marks used in
language.
<Noun Phrase> →
<Article><Noun>
<Article> → a | an | the
<Noun> → boy | apple
Context free grammar
A context free grammar (CFG) is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set formulas of the form where and
Start symbol:
First nonterminal symbol of the grammar is called start symbol.
<Noun Phrase> →
<Article><Noun>
<Article> → a | an | the
<Noun> → boy | apple
Context free grammar
A context free grammar (CFG) is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set formulas of the form where and
Production:
A production, also called a rewriting rule, is a rule of grammar. It has
the form of
A nonterminal symbol → String of terminal and nonterminal symbols
<Noun Phrase> →
<Article><Noun>
<Article> → a | an | the
<Noun> → boy | apple
Example: Context Free Grammar
Write non terminals, terminals, start symbol, and productions for following
grammar.
E E O E | (E) | id
O+|-|*|/ |↑
Non terminals: E,
O
Terminals: id +
-*/↑()
Start symbol:
E
Productions: E E O E |
(E) | id
O+|-|*|/ |↑
Derivation
Derivation
A derivation is basically a sequence of production rules, in order to get the
input string.
To decide which non-terminal to be replaced with production rule, we can
have two options:
1. Leftmost derivation
2. Rightmost derivation
Leftmost derivation
A derivation of a string in a grammar is a left most derivation if at every
step the left most non terminal is replaced.
Grammar: SS+S | S-S | S*S | S/S | a Output string: a*a-a
S S Parse tree
represents the
S-S structure of
S - S derivation
S*S-S
a*S-S S S
* a
a*a-S
a*a-a a a
Leftmost
Parse tree
Derivation
Rightmost derivation
A derivation of a string in a grammar is a right most derivation if at every
step the right most non terminal is replaced.
It is all called canonical derivation.
Grammar: SS+S | S-S | S*S | S/S | a Output string: a*a-a
S
S
S*S
S * S
S*S-S
S*S-a a S S
-
S*a-a
a*a-a a a
Rightmost Parse Tree
Derivation
Ambiguous grammar
Ambiguity
Ambiguity, is a word, phrase, or statement which contains more than one
meaning.
Chip
S S S S
S*S S+S
S * S S + S
S+S*S a+S
a+S*S S + S a a+S*S a S * S
a+a*S a+a*S
a+a*a a a a+a*a a a
Here, Two leftmost derivation for string a+a*a is possible hence, above
grammar is ambiguous.
Parsing
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
Top down parsing: In top down Bottom up parsing: Bottom up
parsing parser build parse tree parser starts from leaves and work
from top to bottom. up to the root.
Grammar: String: abbcde S
S
SaABe
AAbc | b A
a A B e
Bd
A B
A b c d
a b b c d e
b
Classification of
Parsing
Classification of parsing
Parsin
g
descen
t
Backtracking
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 Make
c A d c A d
Make
prediction prediction
a Parsing
bBacktrack a done
Left Recursion
Problems in Top-down Parsing
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
Grammar: AA |
A A
A 𝛼 𝞫
𝞫*
A 𝛼
A
A 𝛼
A 𝛼 A 𝛼
𝞫 𝞫
Left recursion elimination
𝞫*
𝐴→ 𝐴𝛼∨¿
𝛼𝛽
𝐴’
𝐴 𝜖’
Examples: Left recursion elimination
EE+T | T
ETE’
E’+TE’ | ε
TT*F | F
TFT’
T’*FT’ | ε
XX%Y | Z
XZX’
X’%YX’ | ε
Left Factoring
Problems in Top-down Parsing
Left factoring
A
𝛼𝛽| 𝛼δ
Example: Left factoring
SaAB | aCD
SaS’
S’AB | CD
A xByA | xByAzA | a
A xByAA’ | a
A’ Є | zA
First & Follow
Rules to compute first of non terminal
1. If and is terminal, add to .
2. If , add to .
• If & Y2 derives ∈
• If , Y2 & Y3 derives ∈
A Rule 1 A Rule 1
add to add to
FIRST(F)={ ,
( id }
Example-1: First & Follow
Compute FIRST ETE’
First(E’)
E’+TE’ | ϵ
E’+TE’
TFT’
E’ + T E’ Rule 1
add to T’*FT’ | ϵ
A NT First
E
F(E){ |(,id
id
E’𝜖 E’ }{ +, 𝜖
T }
{ (,id
T’ }
E’ Rule 2
F { (,id
A add to
}
={ + 𝜖 }
FIRST(E’) ,
Example-1: First & Follow
Compute FIRST ETE’
First(T’)
E’+TE’ | ϵ
T’*FT’
TFT’
T’ * F T’ Rule 1
add to T’*FT’ | ϵ
A NT First
E
F(E){ |(,id
id
T’𝜖 E’ }{ +, 𝜖
T }
{ (,id
}
𝜖
T’ { *,
T’ Rule 2 }
F { (,id
A add to
}
={ * 𝜖 }
FIRST(T’) ,
Example-1: First & Follow
Compute FOLLOW ETE’
FOLLOW(E)
E’+TE’ | ϵ
Rule 1: Place $ in
FOLLOW(E) TFT’
F(E)
NT First Follow
T’*FT’ | ϵ
E { (,id { $,)
E’ { +, F𝜖(E)
} } | id
F ( E ) Rule 2 }
T { (,id
A B }
𝜖
T’ { *,
F }
{ (,id
}
)}
FOLLOW(E)
={ $,
Example-1: First & Follow
ETE’
Compute FOLLOW
FOLLOW(E’) E’+TE’ | ϵ
ETE’ TFT’
NT First Follow
E T E’ T’*FT’ | ϵ
Rule 3 E { (,id { $,)
+, F𝜖(E)
A B } } | id
E’ { { $,)
T }
{ (,id }
E’+TE’ }
𝜖
T’ { *,
F }
{ (,id
E’ +T E’ Rule 3 }
A B
FOLLOW(E’)
}
={ $,)
Example-1: First & Follow
Compute FOLLOW ETE’
FOLLOW(T)
E’+TE’ | ϵ
ETE’
TFT’
NT First Follow
E T E’ T’*FT’ | ϵ
Rule 2 E { (,id { $,)
+, F𝜖(E)
A B } } | id
E’ { { $,)
T }
{ (,id }
}
𝜖
T’ { *,
F }
{ (,id
E T E’ Rule 3 }
A B
FOLLOW(T)=
$, )
{ +,
Example-1: First & Follow
Compute FOLLOW ETE’
FOLLOW(T)
E’+TE’ | ϵ
E’+TE’
TFT’
NT First Follow
E’ + T E’ T’*FT’ | ϵ
Rule 2 E { (,id { $,)
+, F𝜖(E)
A B } } | id
E’ { { $,)
T }
{ (,id } { +,
} $,) }
𝜖
T’ { *,
F }
{ (,id
E’ + T E’ Rule 3 }
A B
FOLLOW(T)=
$, )}
{ +,
Example-1: First & Follow
Compute FOLLOW ETE’
FOLLOW(T’)
E’+TE’ | ϵ
TFT’
TFT’
NT First Follow
T F T’ T’*FT’ | ϵ
Rule 3 E { (,id { $,)
+, F𝜖(E)
A B } } | id
E’ { { $,)
} (,id }
T’*FT’ T { { +,
} $,)
{ +,}
𝜖
T’ { *,
F }
{ (,id $,) }
T’ *F T’ Rule 3 }
A B
FOLLOW(T’)=
}
{+ $,)
Example-1: First & Follow
Compute FOLLOW ETE’
FOLLOW(F)
E’+TE’ | ϵ
TFT’
TFT’
NT First Follow
T F T’ T’*FT’ | ϵ
Rule 2 E { (,id { $,)
+, F𝜖(E)
A B } } | id
E’ { { $,)
T }
{ (,id } { +,
} $,)
{ +,}
𝜖
T’ { *,
F }
{ (,id $,) }
T F T’ Rule 3 }
A B
FOLLOW(F)= + ,
{ *, $,)
Example-1: First & Follow
Compute FOLLOW ETE’
FOLLOW(F)
E’+TE’ | ϵ
T’*FT’
TFT’
NT First Follow
T’ * F T’ T’*FT’ | ϵ
Rule 2 E { (,id { $,)
+, F𝜖(E)
A B } } | id
E’ { { $,)
T }
{ (,id } { +,
} $,)
{ +,}
𝜖
T’ { *,
F }
{ (,id $,) }
{*,+,
T’ * F T’ Rule 3 } $,)}
A B
FOLLOW(F)=
$, )}
{ *,+,
Example-2: First & Follow
SABCDE
A a | 𝜖
B b | 𝜖
C c
D d | 𝜖
NT First Follow
S {a,b,c {$}
E e | 𝜖 {a, 𝜖} {b, c}
A }
B {b, 𝜖} {c}
C {c} {d, e,
D {d, 𝜖} $}
{e, $}
E {e, 𝜖} {$}
Parsing Methods
Parsin
g
descen
t
LL(1) parser (Predictive parser or Non recursive
descent 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
Model of LL(1) Parser
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 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.
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #3170701
#3130006(CD)
(PS) Unit
Unit31––Syntax
Basic Probability
Analysis (I) 52
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #3170701
#3130006(CD)
(PS) Unit
Unit31––Syntax
Basic Probability
Analysis (I) 53
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #3170701
#3130006(CD)
(PS) Unit
Unit31––Syntax
Basic Probability
Analysis (I) 54
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #3170701
#3130006(CD)
(PS) Unit
Unit31––Syntax
Basic Probability
Analysis (I) 55
Example-1: LL(1) parsing
SaBa
NT First
BbB | ϵ
Step 1: Not required S {a}
B {b,𝜖}
Step 2: Compute FIRST
First(S) S a B a Rule 1
SaBa A add to
FIRST(S)={
a}
First(B)
BbB B𝜖
B b B B 𝜖
A Rule 1
add to A
Rule 2
,𝜖}
FIRST(B)={ add to
b
Example-1: LL(1) parsing
SaBa
NT First Follo
w
BbB | ϵ
Step 2: Compute FOLLOW S {a} {$}
𝜖}
Follow(S) B {b, {a}
Rule 1: Place $ in
FOLLOW(S)
Follow(S)=
{$}
Follow(B)
SaBa BbB
S a B a Rule 2 B b B Rule 3
First( Follow(A)=follow(
A B A B B)
Follow(B)={ }
a
Example-1: LL(1) parsing
SaBa
NT First Follo
w
BbB | ϵ
Step 3: Prepare predictive parsing table S {a} {$}
𝜖}
B {b, {a}
NT Input Symbol
a b $
S SaB
a
B
SaBa
Rule: 2
a=FIRST(aBa)={ a } A
a = first()
M[S,a]=SaBa M[A,a] = A
Example-1: LL(1) parsing
SaBa
NT First Follo
w
BbB | ϵ
Step 3: Prepare predictive parsing table S {a} {$}
𝜖}
B {b, {a}
NT Input Symbol
a b $
S SaB
a
B BbB
BbB
Rule: 2
a=FIRST(bB)={ b } A
a = first()
M[B,b]=BbB M[A,a] = A
Example-1: LL(1) parsing
SaBa
NT First Follo
w
BbB | ϵ
Step 3: Prepare predictive parsing table S {a} {$}
𝜖}
B {b, {a}
NT Input Symbol
a b $
S SaB Error Error
a
B Bϵ BbB Error
Bϵ
Rule: 3
b=FOLLOW(B)={ a }
M[B,a]=B𝜖
A
b = follow(A)
M[A,b] = A
Example-2: LL(1) parsing
SaB | ϵ
BbC | ϵ
CcS | ϵ
Step 1: Not required
NT First
Step 2: Compute FIRST
𝜖
S { a,
First(S) B }
{b,𝜖}
SaB S𝜖 C {c,𝜖}
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
Step 2: Compute FIRST
𝜖
S { a,
First(B) B }
{b,𝜖}
BbC B𝜖 C {c,𝜖}
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
Step 2: Compute FIRST
𝜖
S { a,
First(C) B }
{b,𝜖}
CcS C𝜖 C {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)=
{$}
CcS SaB | ϵ
C c S Rule 3 BbC | ϵ
Follow(A)=follow(
A B B)
Follow(S)=Foll CcS | ϵ
={$
ow(C)
}
NT First Follo
w
𝜖}
Rule 3 S a B Rule 3 B {b, {$}
B b C
Follow(A)=follow( Follow(A)=follow(
A B
}
A B B) B) C {c,𝜖 {$}
Follow(C)=Foll Follow(B)=Foll
={$ ={$
ow(B) ow(S)
} }
Example-2: LL(1) parsing
SaB | ϵ NT First Follo
w
BbC | ϵ S {a,𝜖 {$}
CcS3:| ϵ
Step Prepare predictive parsing table
}
𝜖}
B {b, {$}
N Input Symbol
}
T C {c,𝜖 {$}
a b c $
S SaB
B
C
SaB Rule: 2
A
a=FIRST(aB)={ a } a = first()
M[S,a]=SaB M[A,a] = A
Example-2: LL(1) parsing
SaB | ϵ NT First Follo
w
BbC | ϵ S {a} {$}
CcS3:| ϵ
𝜖}
Step Prepare predictive parsing table B {b, {$}
}
N Input Symbol C {c,𝜖 {$}
T a b c $
S SaB S𝜖
B
C
S𝜖 Rule: 3
A
b=FOLLOW(S)={ $ } b = follow(A)
M[S,$]=S𝜖 M[A,b] = A
Example-2: LL(1) parsing
SaB | ϵ NT First Follo
w
BbC | ϵ S {a} {$}
CcS3:| ϵ
𝜖}
Step Prepare predictive parsing table B {b, {$}
}
N Input Symbol C {c,𝜖 {$}
T a b c $
S SaB S𝜖
B BbC
C
BbC Rule: 2
A
a=FIRST(bC)={ b } a = first()
M[B,b]=BbC M[A,a] = A
Example-2: LL(1) parsing
SaB | ϵ NT First Follo
w
BbC | ϵ S {a} {$}
CcS3:| ϵ
𝜖}
Step Prepare predictive parsing table B {b, {$}
}
N Input Symbol C {c,𝜖 {$}
T a b c $
S SaB S𝜖
B BbC B𝜖
C
B𝜖 Rule: 3
A
b=FOLLOW(B)={ $ } b = follow(A)
M[B,$]=B𝜖 M[A,b] = A
Example-2: LL(1) parsing
SaB | ϵ NT First Follo
w
BbC | ϵ S {a} {$}
CcS3:| ϵ
𝜖}
Step Prepare predictive parsing table B {b, {$}
}
N Input Symbol C {c,𝜖 {$}
T a b c $
S SaB S𝜖
B BbC B𝜖
C CcS
CcS Rule: 2
A
a=FIRST(cS)={ c } a = first()
M[C,c]=CcS M[A,a] = A
Example-2: LL(1) parsing
SaB | ϵ NT First Follo
w
BbC | ϵ S {a} {$}
CcS3:| ϵ
𝜖}
Step Prepare predictive parsing table B {b, {$}
}
N Input Symbol C {c,𝜖 {$}
T a b c $
A Rule 1 A Rule 1
add to add to
FIRST(F)={ ,
( id }
Example-3: LL(1) parsing
Step 2: Compute FIRST ETE’
First(E’)
E’+TE’ | ϵ
E’+TE’
TFT’
E’ + T E’ Rule 1
add to T’*FT’ | ϵ
A NT First
E
F(E){ |(,id
id
E’𝜖 E’ }{ +, 𝜖
T }
{ (,id
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
E
F(E){ |(,id
id
T’𝜖 E’ }{ +, 𝜖
T }
{ (,id
}
𝜖
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’
F(E)
NT First Follow
T’*FT’ | ϵ
E { (,id { $,)
E’ { +, F𝜖(E)
} } | id
F ( E ) Rule 2 }
T { (,id
A B }
𝜖
T’ { *,
F }
{ (,id
}
)}
FOLLOW(E)
={ $,
Example-3: LL(1) parsing
ETE’
Step 2: Compute FOLLOW
FOLLOW(E’) E’+TE’ | ϵ
ETE’ TFT’
NT First Follow
E T E’ T’*FT’ | ϵ
Rule 3 E { (,id { $,)
+, F𝜖(E)
A B } } | id
E’ { { $,)
T }
{ (,id }
E’+TE’ }
𝜖
T’ { *,
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’ | ϵ
ETE’
TFT’
NT First Follow
E T E’ T’*FT’ | ϵ
Rule 2 E { (,id { $,)
+, F𝜖(E)
A B } } | id
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’ | ϵ
E’+TE’
TFT’
NT First Follow
E’ + T E’ T’*FT’ | ϵ
Rule 2 E { (,id { $,)
+, F𝜖(E)
A B } } | id
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 Follow
T F T’ T’*FT’ | ϵ
Rule 3 E { (,id { $,)
+, F𝜖(E)
A B } } | id
E’ { { $,)
} (,id }
T’*FT’ T { { +,
} $,)
{ +,}
𝜖
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 Follow
T F T’ T’*FT’ | ϵ
Rule 2 E { (,id { $,)
+, F𝜖(E)
A B } } | id
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’ | ϵ
T’*FT’
TFT’
NT First Follow
T’ * F T’ T’*FT’ | ϵ
Rule 2 E { (,id { $,)
+, F𝜖(E)
A B } } | id
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’
E’+TE’ | ϵ
NT Input Symbol
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ NT First Follow
T E { (,id
F(E) {| $,)
{ +, 𝜖 }
id
T’ E’ } { $,)
F T }
{ (,id }{ +,
} $,)
{ +,}
𝜖
T’ { *,
ETE’ F }
{ (,id $,) }
{*,+,
Rule: 2 } $,)}
a=FIRST(TE’)={ (,id } A
a = first()
M[E,(]=ETE’ M[A,a] = A
M[E,id]=ETE’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
E’+TE’ | ϵ
NT Input Symbol
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ E’+TE NT First Follow
’
E { (,id
F(E) {| $,)
{ +, 𝜖 }
id
T }
E’ { $,)
T’ }
T { (,id }{ +,
F } $,)
{ +,}
𝜖
T’ { *,
E’+TE’ F }
{ (,id $,) }
{*,+,
Rule: 2 } $,)}
a=FIRST(+TE’)={ + } A
a = first()
M[E’,+]=E’+TE’ M[A,a] = A
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
E’+TE’ | ϵ
NT Input Symbol
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ E’+TE E’𝜖 E’𝜖 NT First Follow
’
E { (,id
F(E) {| $,)
{ +, 𝜖 }
id
T }
E’ { $,)
T’ }
T { (,id }{ +,
F } $,)
{ +,}
𝜖
T’ { *,
E’𝜖 F }
{ (,id $,) }
{*,+,
Rule: 3 } $,)}
b=FOLLOW(E’)={ $,) } A
b = follow(A)
M[E’,$]=E’𝜖 M[A,b] = A
M[E’,)]=E’𝜖
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
E’+TE’ | ϵ
NT Input Symbol
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ E’+TE E’𝜖 E’𝜖 NT First Follow
’
E { (,id
F(E) {| $,)
{ +, 𝜖 }
id
T TFT’ TFT’ }
E’ { $,)
T’ }
T { (,id }{ +,
F } $,)
{ +,}
𝜖
T’ { *,
TFT’ F }
{ (,id $,) }
{*,+,
Rule: 2 } $,)}
a=FIRST(FT’)={ (,id } A
a = first()
M[T,(]=TFT’ M[A,a] = A
M[T,id]=TFT’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
E’+TE’ | ϵ
NT Input Symbol
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ E’+TE E’𝜖 E’𝜖 NT First Follow
’
E { (,id
F(E) {| $,)
{ +, 𝜖 }
id
T TFT’ TFT’ }
E’ { $,)
T’ T’*FT’ }
T { (,id }{ +,
F } $,)
{ +,}
𝜖
T’ { *,
T’*FT’ F }
{ (,id $,) }
{*,+,
Rule: 2 } $,)}
a=FIRST(*FT’)={ * } A
a = first()
M[T’,*]=T’*FT’ M[A,a] = A
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
NT Input Symbol E’+TE’ | ϵ
id + * ( ) $
E ETE’ ETE’ TFT’
E’ E’+TE E’𝜖 E’𝜖 T’*FT’ | ϵ
’ NT First Follow
T TFT’ TFT’ E { (,id
F(E) {| $,)
{ +, 𝜖 }
id
T’ T’𝜖 T’*FT’ T’𝜖 T’𝜖 E’ } { $,)
F T }
{ (,id }{ +,
T’𝜖 } $,)
{ +,}
𝜖
T’ { *,
b=FOLLOW(T’)={ +,$,) } F }
{ (,id $,) }
{*,+,
Rule: 3 } $,)}
M[T’,+]=T’𝜖 A
b = follow(A)
M[T’,$]=T’𝜖 M[A,b] = A
M[T’,)]=T’𝜖
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
E’+TE’ | ϵ
NT Input Symbol
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ E’+TE E’𝜖 E’𝜖 NT First Follow
’
E { (,id
F(E) {| $,)
{ +, 𝜖 }
id
T TFT’ TFT’ }
E’ { $,)
T’ T’𝜖 T’*FT’ T’𝜖 T’𝜖 }
T { (,id }{ +,
F F(E) } $,)
{ +,}
𝜖
T’ { *,
F }
{ (,id $,) }
{*,+,
Rule: 2 } $,)}
F(E) A
a = first()
a=FIRST((E))={ ( } M[A,a] = A
M[F,(]=F(E)
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
E’+TE’ | ϵ
NT Input Symbol
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ E’+TE E’𝜖 E’𝜖 NT First Follow
’
E { (,id
F(E) {| $,)
{ +, 𝜖 }
id
T TFT’ TFT’ }
E’ { $,)
T’ T’𝜖 T’*FT’ T’𝜖 T’𝜖 }
T { (,id }{ +,
F Fid F(E) } $,)
{ +,}
𝜖
T’ { *,
F }
{ (,id $,) }
{*,+,
Rule: 2 } $,)}
Fid A
a = first()
a=FIRST(id)={ id } M[A,a] = A
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 4: Parse the string : id + id * id $ N Input Symbol
T id + * ( ) $
STACK INPUT OUTPUT
E$ id+id*id$ E ETE’ Error Error ETE’ Erro Erro
TE’$ id+id*id$ ETE’ r r
E’ Error E’+TE Error Error E’𝜖 E’𝜖
FT’E’$ id+id*id$ TFT’ ’
idT’E’$ id+id*id$ Fid T TFT’ Error Error TFT’ Erro Erro
r r
T’E’$ +id*id$ T’ Error T’𝜖 T’*FT’ Error T’𝜖 T’𝜖
E’$ +id*id$ T’𝜖 F Fid Error Error F(E) Erro Erro
+TE’$ +id*id$ E’+TE’ r r
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’𝜖
LL(1) Parser – Example1
S → aBa a b $ LL(1)
S S → aBa
Parsing
B B→ε B → bB
B → bB | ε Table
Derivation(left-most): S⇒aBa⇒abBa⇒abbBa⇒abba
S
parse tree
a B a
b B
b B
ε
* Unit-2 : Syntax Analysis 93
LL(1) Parser – Example2
E → TE’
E’ → +TE’ | ε
T → FT’
T’ → *FT’ | ε
F → (E) | id
id + * ( ) $
E E→ E → TE’
TE’
E’ E’ → +TE’ E’ → ε E’ → ε
T T→ T → FT’
FT’
T’ T’ → ε T’ → *FT’ T’ → ε T’ → ε
F F → id F → (E)
* Unit-2 : Syntax Analysis 94
LL(1) Parser – Example2
stack input output
$E id+id$ E → TE’
$E’T id+id$ T → FT’
$E’ T’F id+id$ F → id
$ E’ T’id id+id$
$ E ’ T’ +id$ T’ → ε
$ E’ +id$ E’ → +TE’
$ E’ T+ +id$
$ E’ T id$ T → FT’
$ E ’ T’ F id$ F → id
$ E’ T’id id$
$ E ’ T’ $ T’ → ε
$ E’ $ E’ → ε
$ $ accept
FIRST(iCtSE) = {i}
a b e i t $
FIRST(a) = {a}
FIRST(eS) = {e} S S→a S → iCtSE
FIRST(ε) = {ε}
FIRST(b) = {b} E E→eS E→ε
E→ε
two production rules for
M[E,e]
C C→b
Problem 🡺 ambiguity
* Unit-2 : Syntax Analysis 97
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
position.
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #3170701
#3130006(CD)
(PS) Unit
Unit31––Syntax
Basic Probability
Analysis (I) 100
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #3170701
#3130006(CD)
(PS) Unit
Unit31––Syntax
Basic Probability
Analysis (I) 101
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #3170701
#3130006(CD)
(PS) Unit
Unit31––Syntax
Basic Probability
Analysis (I) 102
Example: Recursive descent
Procedure T parsing
Proceduce
Procedure E Match(token t)
{
{ {
If lookahead=’*’
If If
{
lookahead=num lookahead=t
{
Match(‘*’);
If lookahead=next_toke
Match(num); n;
lookahead=num
T(); Else
{
}
Else Error();
Match(num);
Error(); Procedure Error
}
T(); {
If lookahead=$ Print(“Error”);
}
{ }
Else
Declare
success;
Error();
}
T* numT| 𝜖
Else
}
Enum T
Error();
Else
} Succes NULL
3 * 4 $
} s
Example: Recursive
Procedure T
descent parsing
Proceduce
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(); Procedure Error
}
If lookahead=$ T(); {
{ } Print(“Error”);
Declare Else }
success;
} Error();
T* numT| 𝜖
Else
Error(); }
Enum T
} Else
3 * 4 $
Succes NULL
3 4 * $ Error
} s
Handle & Handle
pruning
Handle & Handle pruning
Handle: A “handle” of a string is a substring of the string that matches
the right side of a production, and whose reduction to the non terminal of
the production is one step along the reverse of rightmost derivation.
Handle pruning: The process of discovering a handle and reducing it to
appropriate left hand side non terminal is known as handle pruning.
EE+
E String:
EE*E id1+id2*id3
Eid
Rightmost Right sentential Handle Production
Derivation form
id1+id2*id3 id1 Eid
E E+id2*id3 id2 Eid
E+E E+E*id3 id3 Eid
E+E*E E+E*E E*E EE*E
E+E*id3 E+E E+E EE+E
E+id2*id3 E
id1+id2*id3
Shift reduce parser
The shift reduce parser performs following basic operations:
1. Shift: Moving of the symbols from input buffer onto the stack, this action
is called shift.
2. Reduce: If handle appears on the top of the stack then reduction of it by
appropriate rule is done. This action is called reduce action.
3. Accept: If stack contains start symbol only and input buffer is empty at
the same time then that action is called accept.
4. Error: A situation in which parser cannot either shift or reduce the
symbols, it cannot even perform accept action then it is called error
action.
Example: Shift reduce parser
Grammar: Stack Input Buffer Action
EE+T | T
$ id+id*id$ Shift
TT*F | F
$id +id*id$ Reduce Fid
Fid
$F +id*id$ Reduce TF
String:
id+id*id $T +id*id$ Reduce ET
$E +id*id$ Shift
$E+ id*id$ Shift
$E+id *id$ Reduce Fid
$E+F *id$ Reduce TF
$E+T *id$ Shift
$E+T* id$ Shift
$E+T*id $ Reduce Fid
$E+T*F $ Reduce TT*F
$E+T $ Reduce EE+T
$E $ Accept
Viable Prefix
The set of prefixes of right sentential forms that can appear on the stack
of a shift-reduce parser are called viable prefixes.
Parsing Methods
Parsin
g
descen
Operator precedence
parsing
Operator precedence parsing
Operator Grammar: A Grammar in which there is no Є in RHS of any
production or no adjacent non terminals is called operator grammar.
Example: E EAE | (E) | id
A + | * | -
Above grammar is not operator grammar because right side EAE has
consecutive non terminals.
In operator precedence parsing we define following disjoint relations:
Relation Meaning
a<.b a “yields precedence to” b
a=b a “has the same precedence as”
b
a “takes precedence over” b
a.>b
Precedence & associativity of operators
f+ f* fid f$
g gi g
g*
+ d $
Operator precedence function
Partition the symbols in as many as groups possible, in such a way that f a
.
and gb are in the same group if a = b.
+ * id $
gi + .
> <. <. .
>
fid
d
* .
> .
> <. .
>
id .
> .
> .
>
$ <. <. <.
f* g*
g
f+
+
g
f$
$
Operator precedence function
3. if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb
g
+ * id $
gi + .
> <. <. .
>
fid
d f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g
f+
f+ .> f+ g+
+ fg* +.> f* g+
fgid+.> fid g+
g
f$ fg$ +<. f$ g+
$
g+
Operator precedence function
3. if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb
g
+ * id $
gi + .
> <. <. .
>
fid
d f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g
f+
f+ <. f+ g*
+ fg* *.> f* g*
fgid* .> fid g*
g
f$ fg$ *<. f$ g*
$
g*
Operator precedence function
3. if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb
g
+ * id $
gi + .
> <. <. .
>
fid
d f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g
f+
f+ <. f+ gid
+ fg* id<. f* gid
fg$ id<. f$ gid
g
f$ gid
$
Operator precedence function
3.if a <· b, place an edge from the group of g b to the group of fa
if a ·> b, place an edge from the group of f a to the group of gb
g
+ * id $
gi + .
> <. <. .
>
fid
d f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g
f+
f+ <. f+ g$
+ fg* $<. f* g$
fgid$<. fid g$
g
f$ g$
$
Operator precedence function
+ * id $
f 2
gi
fid g
d
f* g*
4. If the constructed
graph has a cycle
then no precedence
g
f+
functions exist. When
+ there are no cycles
collect the length of
g the longest paths
f$
$
from the groups of fa
and gb respectively.
Operator precedence function
+ * id $
gi f 2
fid
d g 1
f* g*
g
f+
+
g
f$
$
Operator precedence function
+ * id $
gi f 2 4
fid
d g 1
f* g*
g
f+
+
g
f$
$
Operator precedence function
+ * id $
gi f 2 4
fid
d g 1 3
f* g*
g
f+
+
g
f$
$
Operator precedence function
+ * id $
gi f 2 4 4
fid
d g 1 3
f* g*
g
f+
+
g
f$
$
Operator precedence function
+ * id $
gi f 2 4 4
fid
d g 1 3 5
f* g*
g
f+
+
g
f$
$
Operator precedence function
+ * id $
gi f 2 4 4 0
fid
d g 1 3 5 0
f* g*
g
f+
+
g
f$
$
Parsing Methods
Parsin
g
descen
t
Introduction to LR
Parser
LR parser
LR parsing is most efficient method of bottom up parsing which can be
used to parse large class of context free grammar.
The technique is called LR(k) parsing:
1. The “L” is for left to right scanning of input symbol,
2. The “R” for constructing right most derivation in reverse,
3. The “k” for the number of input symbols of look ahead that are used
in making parsing decision. a + b $ INPUT
X
Y LR parsing
program OUTPUT
Z
$
Parsing Table
Action Goto
Closure & goto function
Computation of closure & goto function
SAS | b
S’S.
ASA | a
Closure(I):
S)
SA.S
(I,
) S.AS
A
to
I, S.b
Go to( A.SA
Go A.a
S’.S
S.AS Goto(I,b)
S.b Sb.
A.SA AS.A
Goto(I,S)
A.aG A.SA
ot A.a
o(
I,a S.AS
) S.b
Aa.
SLR Parser
Example: SLR(1)- simple LR
S AA
A aA | S AA . 5
S’ S. 𝑰𝟏 A a .
b 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A
𝑰𝟐
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) S A . ,2 𝑎 )
A. aA 3
, 𝐴 𝐼
𝐼( 0 AA. aA 𝑜(
A. b
S’.S
𝑜 𝑡
S. AA 𝑡𝑜 𝐺
𝑜
A. aA 𝐺
A. b A b.
𝐺𝑜 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) 4
,3 𝐴
A. b
𝑡𝑜 (𝑜 𝐼 A aA .
LR(0) item
Augment (𝐼 𝑡
3 6
set
ed 0 , A a . 𝐺𝑜
grammar 𝑎) A
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A. aA 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎) A a.
A
A b. 4 A. b 𝐺𝑜 A. aA 3
𝑡𝑜 A. b
(𝐼
3,
𝑏)
A b. 4
Rules to construct SLR parsing table
1. Construct , the collection of sets of LR(0) items for
2. Stateis constructed from . The parsing actions for state are determined
as follow :
a) If is in and GOTO , then set to “shift j”. Here a must be terminal.
b) If is in , then set to “reduce A ” for all a in ; here A may not be S’.
c) If is in , then set action to “accept”.
3. The goto transitions for state i are constructed for all non terminals A
using the
4. All entries not defined by rules 2 and 3 are made error.
Example: SLR(1)- simple LR
S AA . 5
S’ S. 𝑰𝟏 A a .
𝑰𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) S A . ,2 𝑎 )
A. aA 3
, 𝐴 𝐼 Action Go
S’. S 𝐼( 0 AA. aA 𝑡𝑜 ( A. b
to
S. AA 𝑡𝑜 𝐺𝑜
𝑜 Ite a b $ S A
A. aA 𝐺
A. b A b.
𝐺𝑜 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) 4
m
,3 𝐴 set
A. b
𝑡𝑜 (𝑜 𝐼 A aA . 0 S3 S4 1 2
(𝐼 𝑡
3 6
0 , A a . 𝐺𝑜 1 Accep
𝑎) A t
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A. aA 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎) A a.
A 2 S3 S4 5
A b. 4 A. b 𝐺𝑜 A. aA 3
3 S3 S4 6
𝑡𝑜 A. b
S AA (𝐼 4 R3 R3 R3
3,
A aA | 𝑏) 5 R1
A b. 4
b 6 R2 R2 R2
CLR Parser
How to calculate look ahead?
How to calculate look ahead?
SCC
S . S , $
C cC | d ’
Closure(I) A . X ,
S’.S,$
S.CC,$
C.cC,c|d S . C C , $
C.d,c|d A . X ,
Example: CLR(1)- canonical LR
5 S AA. , 6
, 𝐴 ) A aA.,$ 9
S’ S.,
𝑰𝟏 $ A a.A,$
𝑡𝑜 ( 𝐼6 6
𝑎)
$
𝑰 𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴)
A. aA, 𝐺𝑜 𝐼 6 , 𝑎 ) A a.A,$
𝑜 𝑡𝑜 (
𝐼2,
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) 𝐴) S A.A, $A. b,
𝐺𝑜 𝑡𝑜 ( 𝐼 𝐺 A. aA,
$A. b,
𝑡𝑜 (
S’.S, 0𝐼 , $A.aA, $ 6 , 𝑏) A $
$ ( $ b. ,S
𝐺𝑜
S.AA,$ 𝑡𝑜 A. b, A b. ,
7 7
A.aA, a| 𝑜 )
𝐺 $ 𝐺𝑜𝑡𝑜(𝐼2,𝑏) $
b
A.b, a| ,3 𝐴 8
Augment 3
b set
ed Aa.A, a| 𝐺𝑜
𝑡𝑜 (
3
grammar b
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A.aA ,a|b𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎)A a.A , a|b
𝐼0,
A. b, a| A.aA , a|
𝐺 𝑜
𝑎)
A b., a|b 4 b b
𝑡𝑜 A.b , a|b
S AA ( 𝐼3
A aA | , 𝑏 A b., a|
)
b 4 b
Example: CLR(1)- canonical LR
5 S AA. , 6
, 𝐴 ) A aA.,$ 9
S’ S.,
𝑰𝟏 $ A a.A,$
𝑡𝑜 ( 𝐼6 6
𝑎)
$
𝑰 𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴)
A. aA, 𝐺𝑜 𝐼 6 , 𝑎 ) A a.A,$
𝑜 𝑡𝑜 (
𝐼2,
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) 𝐴) S A.A, $A. b,
𝐺𝑜 𝑡𝑜 ( 𝐼 𝐺 A. aA,
$A. b,
𝑡𝑜 (
S’.S, 0𝐼 , $A.aA, $ 6 , 𝑏) A b. , $
$ ( $ $
𝐺𝑜
S.AA,$ 𝑡𝑜 A. b, A b. ,
7 7
A.aA, a| 𝑜 )
𝐺 $ 𝐺𝑜𝑡𝑜(𝐼2,𝑏) $ Item Action Go to
b
A.b, a| ,3 𝐴 8 set a b $ S A
b (𝑜 𝐼 A aA.,a|
𝑡
𝐺𝑜
3
b 0 S3 S4 1 2
Aa.A, a| 𝐺𝑜
𝑡𝑜 (
3 1 Accep
b
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A.aA ,a|b𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎)A a.A , a|b
t
𝐼0,
A. b, a| 2 S6 S7 5
A.aA , a|
𝐺 𝑜 3 S3 S4 8
𝑎)
A b., a|b 4 b b
𝑡𝑜 A.b , a|b 4 R3 R3
S AA ( 𝐼3 5 R1
A aA | , 𝑏 A b., a| 6 S6 S7 9
) 7 R3
b 4 b
8 R2 R2
9 R2
LALR Parser
Example: LALR(1)- look ahead LR
, 𝐴)
5 S AA. , 6 A aA.,$ 9
S’ S.,
𝑰𝟏 $ A a.A,$ ( 𝐼 6
𝐺𝑜 𝑡𝑜
6
𝑎)
$
𝑰𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A. aA,
( 𝐼 6 , 𝑎 ) A a.A,$
𝑡 𝑜
𝐼2,
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) $A. b, 𝐺𝑜 A. aA,
,0 𝐴 S A.A, 𝐺𝑜 𝑡𝑜 ( 𝐼 $A. b,
𝑡𝑜 (
S’.S, $
$ 6 , 𝑏) A b. , $
$ ( 𝐼 A.aA, $
𝐺𝑜
S.AA,$ 𝑡𝑜 $A. b, A b. ,
7 7
A.aA, a| 𝑜 )
𝐺 $ 𝐺𝑜𝑡𝑜(𝐼2,𝑏) $
b
A.b, a| ,3 𝐴 8
36 CLR
b (𝑜 𝐼 A aA.,a|
Aa.A, a|b|$
𝑡
𝐺𝑜
3
b
Aa.A, a| 𝐺𝑜 A.aA , a|b|$
𝑡𝑜 (
3
b A. b, a|b|$
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A.aA ,a|b𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎)A a.A , a|b
𝐼0,
A. b, a| A.aA , a|
𝐺 𝑜
𝑎)
47
A b., a|b 4 b b
𝑡𝑜 A.b , a|b A b., a|b|$
S AA (𝐼
3,
A aA | 𝑏) A b., a| 89
A aA.,a|b|
b 4 b $
Example: LALR(1)- look ahead LR
Item Action Go to
set a b $ S A
Item Action Go to
0 S3 S4 1 2 set a b $ S A
1 Accep
t 0 S36 S4 1 2
2 S6 S7 5 7
3 S3 S4 8 1 Accep
4 R3 R3 t
5 R1 2 S36 S4 5
7
6 S6 S7 9
36 S36 S4 89
7 R3 7
8 R2 R2 47 R3 R3 R3
9
CLR Parsing R2
Table 5 LALR Parsing
R1Table
89 R2 R2 R2
Parser Generator
(YACC)
YACC tool or YACC Parser Generator
YACC is a tool which generates the parser.
It takes input from the lexical analyzer (tokens) and produces parse tree
as an output.
Yacc Yacc
y.tab.c
specificatio Compiler
n
(translate.y
)
y.tab.c C Compiler a.out
Structure of
Program <left side><alt 1>|<alt 2>|……..|<alt
It is used to declare variables, constant &
n>
Declaration Header files
%%
Example:
<left side> : <alt 1> {semantic
%{
%% action 1}
int x,y;
Translation rule | <alt 2> {semantic
const int digit=50;
action 2}
%% #include| <ctype.h>
<alt n> {semantic
action n}
%} All the function needed are specified over
%% here.
Supporting C
routines
Example: Yacc Program
Program: Write Yacc program for simple desk calculator