0% found this document useful (0 votes)
180 views146 pages

CD Unit-3

Uploaded by

Mital
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views146 pages

CD Unit-3

Uploaded by

Mital
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 146

Compiler Design (CD)

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
Token Parse
Source Lexical tree Rest of front IR
Parser
program analyzer end

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
 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
 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: S→S+S | S-S | S*S | S/S | a Output string: a*a-a

S S
Parse tree represents the
→S-S structure of derivation
S - S
→S*S-S
→a*S-S S S
* a
→a*a-S
→a*a-a a a
Leftmost Derivation Parse tree
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: S→S+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 Derivation Parse Tree
Ambiguity
 Ambiguity, is a word, phrase, or statement which contains more than one meaning.

A long thin piece of potato

Chip

A small piece of silicon


Ambiguity
 In formal language grammar, ambiguity would arise if identical string can occur on the RHS of
two or more productions.
 Grammar:
𝑁1 → 𝛼 𝑵𝟏 𝑵𝟐 Replaced by
𝑵𝟏 or 𝑵𝟐 ?
𝑁2 → 𝛼
 𝛼 can be derived from either N1 or N2 𝜶
Ambiguous grammar
 Ambiguous grammar is one that produces more than one leftmost or more then one rightmost
derivation for the same sentence.
 Grammar: S→S+S | S*S | (S) | a Output string: a+a*a

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 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 parsing Bottom up parsing: Bottom up parser starts
parser build parse tree from top to bottom. from leaves and work up to the root.
Grammar: String: abbcde
S
S→aABe S
A→Abc | b
A
B→d a A B e
A B
A b c d
a b b c d e
b
Classification of parsing
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
Classification of parsing
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
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


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: A→A𝜶 | 𝞫
A A

A 𝛼
𝞫
𝞫 𝜶*
A 𝛼
A
A 𝛼
A 𝛼
A 𝛼

𝞫 𝞫
Left recursion elimination

𝞫 𝜶*

𝐴 → 𝐴𝛼
𝛼 |𝛽 𝐴→ 𝐴’

𝐴’→ 𝐴’ | 𝜖
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’ | ε
Problems in Top-down Parsing
Left factoring

A→𝜶𝞫1 |𝜶𝞫2 | 𝜶𝞫3

 Left factoring is a grammar transformation that is useful for producing a grammar suitable for
predictive parsing.
 It is used to remove nondeterminism from the grammar.
Left factoring

𝐴→ 𝛼𝛽 | 𝛼 δ 𝐴→ 𝐴′
𝐴′ → |
Example: Left factoring
S→aAB | aCD
S→aS’
S’→AB | CD
A→ xByA | xByAzA | a

A→ xByAA’ | a
A’→ Є | zA
Rules to compute first of non terminal
1. If 𝐴 → 𝛼 and 𝛼 is terminal, add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴).
2. If 𝐴 → ∈, add ∈ to 𝐹𝐼𝑅𝑆𝑇(𝐴).
3. If 𝑋 is nonterminal and 𝑋→𝑌1 𝑌2 … . 𝑌𝑘 is a production, then place 𝑎 in 𝐹𝐼𝑅𝑆𝑇(𝑋) if for some
𝑖, a is in 𝐹𝐼𝑅𝑆𝑇(𝑌𝑖), and 𝜖 is in all of 𝐹𝐼𝑅𝑆𝑇(𝑌1), … … … , 𝐹𝐼𝑅𝑆𝑇(𝑌𝑖−1 ); that is 𝑌1 … 𝑌𝑖−1 ⇒
𝜖. If 𝜖 is in 𝐹𝐼𝑅𝑆𝑇(𝑌𝑗) for all 𝑗 = 1,2, … . . , 𝑘 then add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝑋).
Everything in 𝐹𝐼𝑅𝑆𝑇(𝑌1) is surely in 𝐹𝐼𝑅𝑆𝑇(𝑋) If 𝑌1 does not derive 𝜖, then we do nothing
more to 𝐹𝐼𝑅𝑆𝑇(𝑋), but if 𝑌1 ⇒ 𝜖, then we add 𝐹𝐼𝑅𝑆𝑇(𝑌2) and so on.
Rules to compute first of non terminal
Simplification of Rule 3
If 𝐴 → 𝑌1 𝑌2 … … . . 𝑌𝐾 ,
• If 𝑌1 does not derives ∈ 𝑡ℎ𝑒𝑛, 𝐹𝐼𝑅𝑆𝑇(𝐴) = 𝐹𝐼𝑅𝑆𝑇(𝑌1 )
• If 𝑌1 derives ∈ 𝑡ℎ𝑒𝑛,
𝐹𝐼𝑅𝑆𝑇 𝐴 = 𝐹𝐼𝑅𝑆𝑇 𝑌1 − 𝜖 U 𝐹𝐼𝑅𝑆𝑇(𝑌2 )
• If 𝑌1 & Y2 derives ∈ 𝑡ℎ𝑒𝑛,
𝐹𝐼𝑅𝑆𝑇 𝐴 = 𝐹𝐼𝑅𝑆𝑇 𝑌1 − 𝜖 U 𝐹𝐼𝑅𝑆𝑇(𝑌2 ) − 𝜖 𝑈 𝐹𝐼𝑅𝑆𝑇(𝑌3 )
• If 𝑌1 , Y2 & Y3 derives ∈ 𝑡ℎ𝑒𝑛,
𝐹𝐼𝑅𝑆𝑇 𝐴 = 𝐹𝐼𝑅𝑆𝑇 𝑌1 − 𝜖 𝑈 𝐹𝐼𝑅𝑆𝑇(𝑌2 ) − 𝜖 𝑈 𝐹𝐼𝑅𝑆𝑇(𝑌3 ) − 𝜖 𝑈 𝐹𝐼𝑅𝑆𝑇(𝑌4 )
• If 𝑌1 , Y2 , Y3 …..YK all derives ∈ 𝑡ℎ𝑒𝑛,
𝐹𝐼𝑅𝑆𝑇 𝐴 = 𝐹𝐼𝑅𝑆𝑇 𝑌1 − 𝜖 𝑈 𝐹𝐼𝑅𝑆𝑇(𝑌2 ) − 𝜖 𝑈 𝐹𝐼𝑅𝑆𝑇(𝑌3 ) − 𝜖 𝑈 𝐹𝐼𝑅𝑆𝑇(𝑌4 ) −
𝜖 𝑈 … … … … 𝐹𝐼𝑅𝑆𝑇(𝑌𝑘 ) (note: if all non terminals derives ∈ then add ∈ to FIRST(A))
Rules to compute FOLLOW of non terminal
1. Place $ 𝑖𝑛 𝑓𝑜𝑙𝑙𝑜𝑤 𝑆 . (S is start symbol)
2. If A → 𝛼𝐵𝛽 , then everything in 𝐹𝐼𝑅𝑆𝑇(𝛽) except for 𝜖 is placed in 𝐹𝑂𝐿𝐿𝑂𝑊(𝐵)
3. If there is a production A → 𝛼𝐵 or a production A → 𝛼𝐵𝛽 where 𝐹𝐼𝑅𝑆𝑇(𝛽) contains 𝜖 then
everything in F𝑂𝐿𝐿𝑂𝑊 𝐵 = 𝐹𝑂𝐿𝐿𝑂𝑊 𝐴
Example-1: First & Follow
Compute FIRST E→TE’
First(E) E’→+TE’ | ϵ
E → T E’ Rule 3 T→FT’
E→TE’ A → Y1 Y2 First(A)=First(Y1) T’→*FT’ | ϵ
F→(E) | id
FIRST(E)=FIRST(T) = {(, id }

NT First
First(T) E { (,id }
T → F T’ Rule 3
T→FT’ E’
A → Y1 Y2 First(A)=First(Y1)
T { (,id }
FIRST(T)=FIRST(F)= {(, id } T’
First(F) F { (,id }
F→(E) F→id
F → ( E ) F → id
A → 𝛼 Rule 1 A → 𝛼 Rule 1
add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴)
FIRST(F)={ ( , id }
Example-1: First & Follow
Compute FIRST E→TE’
First(E’) E’→+TE’ | ϵ
T→FT’
E’→+TE’ T’→*FT’ | ϵ
F→(E) | id
E’ → + T E’ Rule 1
add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) NT First
A → 𝛼
E { (,id }

E’→𝜖 E’ { +, 𝜖 }
T { (,id }
T’
E’ → 𝜖 Rule 2
F { (,id }
A → 𝜖 add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝐴)

FIRST(E’)={ + , 𝜖 }
Example-1: First & Follow
Compute FIRST E→TE’
First(T’) E’→+TE’ | ϵ
T→FT’
T’→*FT’ T’→*FT’ | ϵ
F→(E) | id
T’ → * F T’ Rule 1
add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) NT First
A → 𝛼
E { (,id }

T’→𝜖 E’ { +, 𝜖 }
T { (,id }
T’ { *, 𝜖 }
T’ → 𝜖 Rule 2
F { (,id }
A → 𝜖 add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝐴)

FIRST(T’)={ * , 𝜖 }
Example-1: First & Follow
Compute FOLLOW E→TE’
FOLLOW(E) E’→+TE’ | ϵ
T→FT’
Rule 1: Place $ in FOLLOW(E) T’→*FT’ | ϵ
F→(E) | id
F→(E)
NT First Follow
E { (,id } { $,) }
E’ { +, 𝜖 }
F → ( E ) Rule 2 T { (,id }
A → 𝛂 B 𝛃
T’ { *, 𝜖 }
F { (,id }

FOLLOW(E)={ $, ) }
Example-1: First & Follow
E→TE’
Compute FOLLOW E’→+TE’ | ϵ
FOLLOW(E’) T→FT’
T’→*FT’ | ϵ
E→TE’ F→(E) | id
NT First Follow
E → T E’ Rule 3 E { (,id } { $,) }
A → 𝛂 B
E’ { +, 𝜖 } { $,) }
T { (,id }
E’→+TE’ T’ { *, 𝜖 }
F { (,id }
E’ → +T E’ Rule 3
A → 𝛂 B

FOLLOW(E’)={ $,) }
Example-1: First & Follow
Compute FOLLOW E→TE’
FOLLOW(T) E’→+TE’ | ϵ
T→FT’
E→TE’ T’→*FT’ | ϵ
F→(E) | id
NT First Follow
E → T E’ Rule 2 E { (,id } { $,) }
A → 𝛼 B 𝛃
E’ { +, 𝜖 } { $,) }
T { (,id }
T’ { *, 𝜖 }
F { (,id }
E → T E’ Rule 3
A → 𝛼 B 𝛃

FOLLOW(T)={ +, $, )
Example-1: First & Follow
Compute FOLLOW E→TE’
FOLLOW(T) E’→+TE’ | ϵ
T→FT’
E’→+TE’ T’→*FT’ | ϵ
F→(E) | id
NT First Follow
E’ → + T E’ Rule 2 E { (,id } { $,) }
A → 𝛂 B 𝛃
E’ { +, 𝜖 } { $,) }
T { (,id } { +,$,) }
T’ { *, 𝜖 }
F { (,id }
E’ → + T E’ Rule 3
A → 𝛂 B 𝛃

FOLLOW(T)={ +, $, ) }
Example-1: First & Follow
Compute FOLLOW E→TE’
FOLLOW(T’) E’→+TE’ | ϵ
T→FT’
T→FT’ T’→*FT’ | ϵ
F→(E) | id
NT First Follow
T → F T’ Rule 3 E { (,id } { $,) }
A → 𝛂 B
E’ { +, 𝜖 } { $,) }
T’→*FT’ T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id }
T’ → *F T’ Rule 3
A → 𝛂 B

FOLLOW(T’)={+ $,) }
Example-1: First & Follow
Compute FOLLOW E→TE’
FOLLOW(F) E’→+TE’ | ϵ
T→FT’
T→FT’ T’→*FT’ | ϵ
F→(E) | id
NT First Follow
T → F T’ Rule 2 E { (,id } { $,) }
A → 𝛂 B 𝛃
E’ { +, 𝜖 } { $,) }
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id }
T → F T’ Rule 3
A → 𝛂 B 𝛃

FOLLOW(F)={ *, + ,$ , )
Example-1: First & Follow
Compute FOLLOW E→TE’
FOLLOW(F) E’→+TE’ | ϵ
T→FT’
T’→*FT’ T’→*FT’ | ϵ
F→(E) | id
NT First Follow
T’ → * F T’ Rule 2 E { (,id } { $,) }
A → 𝛂 B 𝛃
E’ { +, 𝜖 } { $,) }
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
T’ → * F T’ Rule 3
A → 𝛂 B 𝛃

FOLLOW(F)={ *,+, $, ) }
Example-2: First & Follow
S→ABCDE
A→ a | 𝜖
B→ b | 𝜖
C→ c NT First Follow
D→ d | 𝜖 S {a,b,c} {$}
E→ e | 𝜖 A {a, 𝜖} {b, c}
B {b, 𝜖} {c}
C {c} {d, e, $}
D {d, 𝜖} {e, $}
E {e, 𝜖} {$}
Parsing Methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
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 $ INPUT

X
Predictive
Y
Stack parsing OUTPUT
Z program
$

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.
Example-1: LL(1) parsing
S→aBa
B→bB | ϵ
NT First
Step 1: Not required S {a}

Step 2: Compute FIRST B {b,𝜖}

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 → 𝜖
Rule 1
A → 𝛼 A → 𝜖
add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) Rule 2
add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝐴)
FIRST(B)={ b , 𝜖 }
Example-1: LL(1) parsing
S→aBa
B→bB | ϵ NT First Follow
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
B→bB | ϵ NT First Follow
S {a} {$}
Step 3: Prepare predictive parsing table B {b,𝜖} {a}

NT Input Symbol
a b $
S S→aBa
B

S→aBa
Rule: 2
a=FIRST(aBa)={ a } A→ 𝛼
a = first(𝛼)
M[S,a]=S→aBa M[A,a] = A→ 𝛼
Example-1: LL(1) parsing
S→aBa
B→bB | ϵ NT First Follow
S {a} {$}
Step 3: Prepare predictive parsing table B {b,𝜖} {a}

NT Input Symbol
a b $
S S→aBa
B B→bB

B→bB
Rule: 2
a=FIRST(bB)={ b } A→ 𝛼
a = first(𝛼)
M[B,b]=B→bB M[A,a] = A→ 𝛼
Example-1: LL(1) parsing
S→aBa
B→bB | ϵ NT First Follow
S {a} {$}
Step 3: Prepare predictive parsing table B {b,𝜖} {a}

NT Input Symbol
a b $
S S→aBa Error Error
B B→ϵ B→bB Error

B→ϵ
Rule: 3
b=FOLLOW(B)={ a } A→ 𝛼
b = follow(A)
M[B,a]=B→𝜖 M[A,b] = A→ 𝛼
Example-2: LL(1) parsing
S→aB | ϵ
B→bC | ϵ
C→cS | ϵ
Step 1: Not required
NT First
Step 2: Compute FIRST S { a, 𝜖 }
First(S) B {b,𝜖}
S→aB S→𝜖 C {c,𝜖}

S → a B S → 𝜖
Rule 1 Rule 2
A → 𝛼 add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) A → 𝜖 add 𝜖 to 𝐹𝐼𝑅𝑆𝑇(𝐴)

FIRST(S)={ a , 𝜖 }
Example-2: LL(1) parsing
S→aB | ϵ
B→bC | ϵ
C→cS | ϵ
Step 1: Not required
NT First
Step 2: Compute FIRST S { a, 𝜖 }
First(B) B {b,𝜖}
B→bC B→𝜖 C {c,𝜖}

B → b C B → 𝜖
Rule 1 Rule 2
A → 𝛼 add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) A → 𝜖 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, 𝜖 }
First(C) B {b,𝜖}
C→cS C→𝜖 C {c,𝜖}

C → c S C → 𝜖
Rule 1 Rule 2
A → 𝛼 add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) A → 𝜖 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 | ϵ
B→bC | ϵ
C → c S Rule 3 C→cS | ϵ
A → 𝛂 B Follow(A)=follow(B)
Follow(S)=Follow(C) ={$}
NT First Follow
S {a,𝜖} {$}
B→bC S→aB B {b,𝜖} {$}
C {c,𝜖} {$}
B → b C Rule 3 S → a B 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
B→bC | ϵ
S {a,𝜖} {$}
C→cS | ϵ
B {b,𝜖} {$}
Step 3: Prepare predictive parsing table C {c,𝜖} {$}

N Input Symbol
T a b c $
S S→aB
B
C

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

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

N Input Symbol
T a b c $
S S→aB S→𝜖
B B→bC
C

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

N Input Symbol
T a b c $
S S→aB S→𝜖
B B→bC B→𝜖
C

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

N Input Symbol
T a b c $
S S→aB S→𝜖
B B→bC B→𝜖
C C→cS

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

N Input Symbol
T a b c $
S S→aB Error Error S→𝜖
B Error B→bB Error B→𝜖
C Error Error C→cS C→𝜖

C→𝜖 Rule: 3
A→ 𝛼
b=FOLLOW(C)={ $ } b = follow(A)
M[C,$]=C→𝜖 M[A,b] = A→ 𝛼
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’→+TE’ | ϵ
E → T E’ Rule 3 T→FT’
E→TE’ A → Y1 Y2 First(A)=First(Y1) T’→*FT’ | ϵ
F→(E) | id
FIRST(E)=FIRST(T) = {(, id }

NT First
First(T) E { (,id }
T → F T’ Rule 3
T→FT’ E’
A → Y1 Y2 First(A)=First(Y1)
T { (,id }
FIRST(T)=FIRST(F)= {(, id } T’
First(F) F { (,id }
F→(E) F→id
F → ( E ) F → id
A → 𝛼 Rule 1 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’ | ϵ
T→FT’
E’→+TE’ T’→*FT’ | ϵ
F→(E) | id
E’ → + T E’ Rule 1
add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) NT First
A → 𝛼
E { (,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 E→TE’
First(T’) E’→+TE’ | ϵ
T→FT’
T’→*FT’ T’→*FT’ | ϵ
F→(E) | id
T’ → * F T’ Rule 1
add 𝛼 to 𝐹𝐼𝑅𝑆𝑇(𝐴) NT First
A → 𝛼
E { (,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 E→TE’
FOLLOW(E) E’→+TE’ | ϵ
T→FT’
Rule 1: Place $ in FOLLOW(E) T’→*FT’ | ϵ
F→(E) | id
F→(E)
NT First Follow
E { (,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’
T’→*FT’ | ϵ
E→TE’ F→(E) | id
NT First Follow
E → T E’ Rule 3 E { (,id } { $,) }
A → 𝛂 B
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 E→TE’
FOLLOW(T) E’→+TE’ | ϵ
T→FT’
E→TE’ T’→*FT’ | ϵ
F→(E) | id
NT First Follow
E → T E’ Rule 2 E { (,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’ T’→*FT’ | ϵ
F→(E) | id
NT First Follow
E’ → + T E’ Rule 2 E { (,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’ T’→*FT’ | ϵ
F→(E) | id
NT First Follow
T → F T’ Rule 3 E { (,id } { $,) }
A → 𝛂 B
E’ { +, 𝜖 } { $,) }
T’→*FT’ T { (,id } { +,$,) }
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’ T’→*FT’ | ϵ
F→(E) | id
NT First Follow
T → F T’ Rule 2 E { (,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’ T’→*FT’ | ϵ
F→(E) | id
NT First Follow
T’ → * F T’ Rule 2 E { (,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’
E’→+TE’ | ϵ
T→FT’
NT Input Symbol
T’→*FT’ | ϵ
id + * ( ) $ F→(E) | id
E E→TE’ E→TE’
E’ NT First Follow
T E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
E→TE’ F { (,id } {*,+,$,)}
Rule: 2
a=FIRST(TE’)={ (,id } A→ 𝛼
a = first(𝛼)
M[E,(]=E→TE’ M[A,a] = A→ 𝛼
M[E,id]=E→TE’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table E→TE’
E’→+TE’ | ϵ
T→FT’
NT Input Symbol
T’→*FT’ | ϵ
id + * ( ) $ F→(E) | id
E E→TE’ E→TE’
E’ E’→+TE’ NT First Follow
T E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
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 E→TE’
E’→+TE’ | ϵ
T→FT’
NT Input Symbol
T’→*FT’ | ϵ
id + * ( ) $ F→(E) | id
E E→TE’ E→TE’
E’ E’→+TE’ E’→𝜖 E’→𝜖 NT First Follow
T E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
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 E→TE’
E’→+TE’ | ϵ
T→FT’
NT Input Symbol
T’→*FT’ | ϵ
id + * ( ) $ F→(E) | id
E E→TE’ E→TE’
E’ E’→+TE’ E’→𝜖 E’→𝜖 NT First Follow
T T→FT’ T→FT’ E { (,id } { $,) }
T’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
T→FT’ F { (,id } {*,+,$,)}
Rule: 2
a=FIRST(FT’)={ (,id } A→ 𝛼
a = first(𝛼)
M[T,(]=T→FT’ M[A,a] = A→ 𝛼
M[T,id]=T→FT’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table E→TE’
E’→+TE’ | ϵ
T→FT’
NT Input Symbol
T’→*FT’ | ϵ
id + * ( ) $ F→(E) | id
E E→TE’ E→TE’
E’ E’→+TE’ E’→𝜖 E’→𝜖 NT First Follow
T T→FT’ T→FT’ E { (,id } { $,) }
T’ T’→*FT’ E’ { +, 𝜖 } { $,) }
F T { (,id } { +,$,) }
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 E→TE’
E’→+TE’ | ϵ
NT Input Symbol T→FT’
id + * ( ) $ T’→*FT’ | ϵ
E E→TE’ E→TE’ F→(E) | id

E’ E’→+TE’ E’→𝜖 E’→𝜖


NT First Follow
T T→FT’ T→FT’
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 E→TE’
E’→+TE’ | ϵ
T→FT’
NT Input Symbol
T’→*FT’ | ϵ
id + * ( ) $ F→(E) | id
E E→TE’ E→TE’
E’ E’→+TE’ E’→𝜖 E’→𝜖 NT First Follow
T T→FT’ T→FT’ E { (,id } { $,) }
T’ T’→𝜖 T’→*FT’ T’→𝜖 T’→𝜖 E’ { +, 𝜖 } { $,) }
F F→(E) T { (,id } { +,$,) }
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 E→TE’
E’→+TE’ | ϵ
T→FT’
NT Input Symbol
T’→*FT’ | ϵ
id + * ( ) $ F→(E) | id
E E→TE’ E→TE’
E’ E’→+TE’ E’→𝜖 E’→𝜖 NT First Follow
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
F→id A→ 𝛼
a = first(𝛼)
a=FIRST(id)={ id } M[A,a] = A→ 𝛼
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 4: 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’→𝜖
Parsing methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
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.
Example: Recursive descent parsing
Procedure E Proceduce Match(token t)
Procedure T
{ {
{
If lookahead=num If lookahead=t
If lookahead=’*’
{ lookahead=next_token;
{
Match(num); Else
Match(‘*’);
T(); Error();
If lookahead=num
} }
{
Else
Match(num);
Error();
T(); Procedure Error
} {
If lookahead=$
Else Print(“Error”);
{
Error(); }
Declare success;
}
}
Else
Else
Error();
}
NULL E→ num T
} T→ * num T | 𝜖
3 * 4 $ Success
Example: Recursive descent parsing
Procedure E Procedure T Proceduce Match(token t)
{ { {
If lookahead=num If lookahead=’*’ If lookahead=t
{ { lookahead=next_token;
Match(num); Match(‘*’); Else
T(); If lookahead=num Error();
} { }
Else Match(num);
Error(); T(); Procedure Error
If lookahead=$ } {
{ Else Print(“Error”);
Declare success; Error(); }
}
Else }
Error(); Else
} NULL E→ num T
} T→ * num T | 𝜖
3 * 4 $ Success 3 4 * $ Error
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.
E→E+E
E→E*E String: id1+id2*id3
E→id
Rightmost Derivation Right sentential form Handle Production
id1+id2*id3 id1 E→id
E
E+E E+id2*id3 id2 E→id
E+E*E E+E*id3 id3 E→id
E+E*id3 E+E*E E*E E→E*E
E+id2*id3 E+E E+E E→E+E
id1+id2*id3 E
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
E→E+T | T $ id+id*id$ Shift
T→T*F | F $id +id*id$ Reduce F→id
F→id $F +id*id$ Reduce T→F
String: id+id*id $T +id*id$ Reduce E→T
$E +id*id$ Shift
$E+ id*id$ Shift
$E+id *id$ Reduce F→id
$E+F *id$ Reduce T→F
$E+T *id$ Shift
$E+T* id$ Shift
$E+T*id $ Reduce F→id
$E+T*F $ Reduce T→T*F
$E+T $ Reduce E→E+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

Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
Parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
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.>b a “takes precedence over” b
Precedence & associativity of operators

Operator Precedence Associative


↑ 1 right
*, / 2 left
+, - 3 left
Steps of operator precedence parsing
1. Find Leading and trailing of non terminal
2. Establish relation
3. Creation of table
4. Parse the string
Leading & Trailing
Leading:- Leading of a non terminal is the first terminal or operator in production of that non
terminal.
Trailing:- Trailing of a non terminal is the last terminal or operator in production of that non
terminal.
Example: E→E+T | T
T→T*F | F
F→id

Non terminal Leading Trailing


E {+,*,id} {+,*,id}
T {*,id} {*,id}
F {id} {id}
Rules to establish a relation
.
1. For a = b, ⇒ 𝑎𝐴𝑏, where 𝐴 is 𝜖 or a single non terminal [e.g : (E)]
2. a <.b ⇒ 𝑂𝑝 . 𝑁𝑇 𝑡ℎ𝑒𝑛 𝑂𝑝 <. 𝐿𝑒𝑎𝑑𝑖𝑛𝑔 𝑁𝑇 [e.g : +T]
3. a .>b ⇒ 𝑁𝑇 . 𝑂𝑝 𝑡ℎ𝑒𝑛 𝑇𝑟𝑎𝑖𝑙𝑖𝑛𝑔(𝑁𝑇 ) . > 𝑂𝑝 [e.g : E+]
4. $ <. Leading (start symbol)
5. Trailing (start symbol) .> $
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E→ E +T| T
Nonterminal Leading Trailing T→ T *F| F
E {+,*,id} {+,*,id} F→ id
T {*,id} {*,id}
F {id} {id}

Step 2: Establish Relation Step3: Creation of Table


+ * id $
1. a <.b + .> <. <. .>

2. 𝑂𝑝 . 𝑁𝑇 𝑂𝑝 <. 𝐿𝑒𝑎𝑑𝑖𝑛𝑔 𝑁𝑇 * .> .> <. .>

id .> .> .>


3. +𝑇 + <. ∗, 𝑖𝑑 $ <. <. <.
4. ∗ 𝐹 ∗ <. {𝑖𝑑}
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E→ E+ T| T
Nonterminal Leading Trailing T→ T* F| F
E {+,*,id} {+,*,id} F→ id
T {*,id} {*,id}
F {id} {id}

Step2: Establish Relation Step3: Creation of Table


1. a .>b + * id $
2. 𝑁𝑇 . 𝑂𝑝 𝑇𝑟𝑎𝑖𝑙𝑖𝑛𝑔(𝑁𝑇 ). > 𝑂𝑝 + .> <. <. .>

* .> .> <. .>


3. 𝐸 + +,∗, 𝑖𝑑 . > +
id .> .> .>
4. 𝑇 ∗ {∗, 𝑖𝑑}. >∗ $ <. <. <.
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E→ E+ T| T
Nonterminal Leading Trailing T→ T* F| F
E {+,*,id} {+,*,id} F→ id
T {*,id} {*,id}
F {id} {id}

Step 2: Establish Relation Step 3: Creation of Table


+ * id $
1. $<. Leading (start symbol) + .> <. <. .>
2. $ <. +,∗, 𝑖𝑑 * .> .> <. .>
3. Trailing (start symbol) .> $ id .> .> .>
4. +,∗, 𝑖𝑑 .> $ $ <. <. <.
Example: Operator precedence parsing
Step 4: Parse the string using precedence table
Assign precedence operator between terminals
String: id+id*id
+ * id $
$ id+id*id $ + .> <. <. .>

$ <. id+id*id$ * .> .> <. .>

id .> .> .>


$ <. id .> +id*id$
$ <. <. <.
$ < id > + < id*id$
. . .

$ <. id .> + <. id .> *id$


$ <. id .> + <. id .> *<. id$
$ <. id .> + <. id .> *<. id .> $
Example: Operator precedence parsing
Step 4: Parse the string using precedence table E→E+T | T
1. Scan the input string until first .> is encountered. T→T*F | F
F→id
2. Scan backward until <. is encountered.
3. The handle is string between <. and .>
$ <. Id .> + <. Id .> * <. Id .> $ Handle id is obtained between <. and .> + * id $
Reduce this by F→id + .> <. <. .>
$ F + <. Id .> * <. Id .> $ Handle id is obtained between <. and .>
* .> .> <. .>
Reduce this by F→id
Handle id is obtained between <. and .> id .> .> .>
$F+F* <. Id .> $
Reduce this by F→id $ <. <. <.
$F+F*F$ Perform appropriate reductions of all nonterminals.
$E+T*F$ Remove all non terminals.
$ + * $ Place relation between operators
$ <. + <. * >$ The * operator is surrounded by <. and .>. This
indicates * becomes handle so reduce by T→T*F.
$ <. + >$ + becomes handle. Hence reduce by E→E+T.

$ $ Parsing Done
Operator precedence function
Algorithm for constructing precedence functions
1. Create functions 𝑓𝑎 and 𝑔𝑎 for each a that is terminal or $.
2. Partition the symbols in as many as groups possible, in such a way that 𝑓𝑎 and 𝑔𝑏 are in the same
group if 𝑎 = 𝑏.
3. Create a directed graph whose nodes are in the groups, next for each symbols 𝑎 𝑎𝑛𝑑 𝑏 do:
a) if 𝑎 <· 𝑏, place an edge from the group of 𝑔𝑏 to the group of 𝑓𝑎
b) if 𝑎 ·> 𝑏, place an edge from the group of 𝑓𝑎 to the group of 𝑔𝑏
4. If the constructed graph has a cycle then no precedence functions exist. When there are no cycles
collect the length of the longest paths from the groups of 𝑓𝑎 and 𝑔𝑏 respectively.
Operator precedence function
1. Create functions fa and ga for each a that is terminal or $. E→ E+T | T
T→ T*F | F
F→ id
𝑎 = {+,∗, 𝑖𝑑} 𝑜𝑟 $

f+ f* fid f$

g+ g* gid g$
Operator precedence function
 Partition the
.
symbols in as many as groups possible, in such a way that fa and gb are in the same
group if a = b.

+ * id $
+ .> <. <. .>
gid fid .> .>
* <. .>

id .> .> .>

$ <. <. <.


f* g*

g+ f+

f$ g$
Operator precedence function
3. if a <· b, place an edge from the group of gb to the group of fa
if a ·> b, place an edge from the group of fa to the group of gb

g
+ * id $
+ .> <. <. .>
gid fid
f * .> .> <. .>

id .> .> .>

f* g* $ <. <. <.

f+ .> g+ f+ → g+
g+ f+
f* .> g+ f* → g+
fid .> g+ fid → g+
f$ g$ f$ <. g+ f$  g+
Operator precedence function
3. if a <· b, place an edge from the group of gb to the group of fa
if a ·> b, place an edge from the group of fa to the group of gb

g
+ * id $
+ .> <. <. .>
gid fid
f * .> .> <. .>

id .> .> .>

f* g* $ <. <. <.

f+ <. g* f+  g*
g+ f+
f* .> g* f* → g*
fid .> g* fid → g*
f$ g$ f$ <. g* f$  g*
Operator precedence function
3. if a <· b, place an edge from the group of gb to the group of fa
if a ·> b, place an edge from the group of fa to the group of gb

g
+ * id $
+ .> <. <. .>
gid fid
f * .> .> <. .>

id .> .> .>

f* g* $ <. <. <.

f+ <. gid f+  gid


g+ f+
f* <. gid f*  gid
f$ <. gid f$  gid
f$ g$
Operator precedence function
3. if a <· b, place an edge from the group of gb to the group of fa
if a ·> b, place an edge from the group of fa to the group of gb

g
+ * id $
+ .> <. <. .>
gid fid
f * .> .> <. .>

id .> .> .>

f* g* $ <. <. <.

f+ <. g$ f+ → g$
g+ f+
f* <. g$ f* → g$
fid <. g$ fid → g$
f$ g$
Operator precedence function

+ * id $
f 2
gid fid g

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 the longest paths
f$ g$ from the groups of fa and gb
respectively.
Operator precedence function

+ * id $

gid fid f 2
g 1

f* g*

g+ f+

f$ g$
Operator precedence function

+ * id $

gid fid f 2 4
g 1

f* g*

g+ f+

f$ g$
Operator precedence function

+ * id $

gid fid f 2 4
g 1 3

f* g*

g+ f+

f$ g$
Operator precedence function

+ * id $

gid fid f 2 4 4
g 1 3

f* g*

g+ f+

f$ g$
Operator precedence function

+ * id $

gid fid f 2 4 4
g 1 3 5

f* g*

g+ f+

f$ g$
Operator precedence function

+ * id $

gid fid f 2 4 4 0
g 1 3 5 0

f* g*

g+ f+

f$ g$
Parsing Methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
Parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
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
LR parsing
Y
program OUTPUT
Z
$
Parsing Table
Action Goto
Computation of closure & goto function
S→AS | b
S’→S.
A→SA | a
Closure(I): S→A.S
S→.AS
S→.b
A→.SA
A→.a
S’→.S
S→.AS Goto(I,b)
S→.b S→b.
A→.SA A→S.A
A→.a A→.SA
A→.a
S→.AS
S→.b
A→a.
Example: SLR(1)- simple LR
S → AA
S→ AA . 𝑰5
A → aA | b S’→ S. 𝑰𝟏
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴) A→ a . A
𝑰𝟐
𝑰𝟎 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆) A→. aA 𝑰3
S→ A . A A→. b
S’→.S
A→. aA
S→. AA
A→. b A→ b. 𝑰4
A→. aA 𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
A→. b
𝑰3 A→ aA . 𝑰6 LR(0) item set
Augmented
grammar A→ a . A
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏) A→. aA 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) A→ a . A
A→. b 𝑰3
A→ b. 𝑰4 A→. aA
A→. b

A→ b. 𝑰4
Rules to construct SLR parsing table
1. Construct 𝐶 = { 𝐼0, 𝐼1, … … . 𝐼𝑛}, the collection of sets of LR(0) items for 𝐺’.
2. State 𝑖 is 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. 𝑰𝟏
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴) A→ a . A 𝐹𝑜𝑙𝑙𝑜𝑤(𝐴) = {𝑎, 𝑏, $}
𝑰𝟐
𝑰𝟎 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆) A→. aA 𝑰3
S→ A . A A→. b Action Go to
S’→. S
A→. aA Item a b $ S A
S→. AA
A→. b A→ b. 𝑰4 set
A→. aA 𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
0 S3 S4 1 2
A→. b
𝑰3 A→ aA . 𝑰6 1 Accept
2 S3 S4 5
A→ a . A
3 S3 S4 6
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏) A→. aA 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) A→ a . A
4 R3 R3 R3
A→. b 𝑰3
A→ b. 𝑰4 A→. aA
5 R1
A→. b
6 R2 R2 R2
S → AA
A → aA | b A→ b. 𝑰4
How to calculate look ahead?
How to calculate look ahead?
S→CC
S’ → . S , $
C→ cC | d
A → 𝜶 . X 𝜷 , 𝒂
Closure(I)
Lookahead = First 𝜷𝒂
S’→.S,$ First $
=$
S→.CC, $
C→.cC, c|d S → . C C , $
C→.d, c|d A → 𝜶 . X 𝜷 , 𝒂
Lookahead = First 𝜷𝒂
First 𝐶$
= 𝒄, 𝒅
Example: CLR(1)- canonical LR
𝑰9
𝑰5
S→ AA. ,$
𝑰6 A→ aA.,$
S’→ S., $ 𝑰𝟏 A→ a.A,$ 𝑰6
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴) A→ a.A,$
𝑰𝟐 A→. aA,$
𝑰𝟎 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆) A→. aA,$
A→. b, $
S→ A.A,$ A→. b, $
S’→.S,$ A→ b. ,S
A→.aA, $ 𝑰7 𝑰7
S→.AA,$
A→. b, $ A→ b. ,$
A→.aA, a|b 𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏)
𝑰8
A→.b, a|b
𝑰3 A→ aA.,a|b LR(1) item set
Augmented
grammar A→a.A, a|b 𝑰3
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏) A→.aA ,a|b 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) A→ a.A , a|b
A→. b, a|b A→.aA , a|b
A→ b., a|b 𝑰4
A→.b , a|b
S → AA
A → aA | b 𝑰4 A→ b., a|b
Example: CLR(1)- canonical LR
𝑰9
𝑰5
S→ AA. ,$
𝑰6 A→ aA.,$
S’→ S., $ 𝑰𝟏 A→ a.A,$ 𝑰6
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴) A→ a.A,$
𝑰𝟐 A→. aA,$
𝑰𝟎 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆) A→. aA,$
A→. b, $
S→ A.A,$ A→. b, $
S’→.S,$ A→ b. ,$
A→.aA, $ 𝑰7 𝑰7
S→.AA,$
A→. b, $ A→ b. ,$
A→.aA, a|b 𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏) Item Action Go to
𝑰8 set a b $ S A
A→.b, a|b
𝑰3 A→ aA.,a|b 0 S3 S4 1 2
1 Accept
A→a.A, a|b 𝑰3
2 S6 S7 5
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏) A→.aA ,a|b 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) A→ a.A , a|b 3 S3 S4 8
A→. b, a|b 4 R3 R3
A→ b., a|b 𝑰4 A→.aA , a|b
5 R1
A→.b , a|b 6 S6 S7 9
S → AA 7 R3
A → aA | b 8 R2 R2
𝑰4 A→ b., a|b
9 R2
Example: LALR(1)- look ahead LR
𝑰9
𝑰5
S→ AA. ,$
𝑰6 A→ aA.,$
S’→ S., $ 𝑰𝟏 A→ a.A,$ 𝑰6
𝐺𝑜 𝑡𝑜 (𝐼2, 𝐴) A→ a.A,$
𝑰𝟐 A→. aA,$
𝑰𝟎 𝐺𝑜 𝑡𝑜 (𝐼0, 𝑆) A→. aA,$
A→. b, $
S→ A.A,$ A→. b, $
S’→.S,$ A→ b. ,$
A→.aA, $ 𝑰7 𝑰7
S→.AA,$
A→. b, $ A→ b. ,$
A→.aA, a|b 𝐺𝑜 𝑡𝑜 (𝐼2, 𝑏) 𝑰36 CLR
𝑰8
A→.b, a|b
𝑰3 A→ aA.,a|b A→a.A, a|b|$
𝑰3 A→.aA , a|b|$
A→a.A, a|b
𝐺𝑜 𝑡𝑜 (𝐼0, 𝑏)
A→. b, a|b|$
A→.aA ,a|b 𝐺𝑜 𝑡𝑜 (𝐼3, 𝑎) A→ a.A , a|b
A→. b, a|b A→.aA , a|b 𝑰47
A→ b., a|b 𝑰4
A→.b , a|b A→ b., a|b|$
S → AA 𝑰89
A → aA | b 𝑰4 A→ b., a|b A→ aA.,a|b|$
Example: LALR(1)- look ahead LR

Item Action Go to
set a b $ S A
0 S3 S4 1 2 Item Action Go to
1 Accept set a b $ S A
2 S6 S7 5 0 S36 S47 1 2
3 S3 S4 8 1 Accept
4 R3 R3 2 S36 S47 5
5 R1 36 S36 S47 89
6 S6 S7 9 47 R3 R3 R3
5 R1
7 R3
89 R2 R2 R2
8 R2 R2
9 R2

CLR Parsing Table LALR Parsing Table


(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 Compiler y.tab.c
specification
(translate.y)

y.tab.c C Compiler a.out

Input a.out output


Structure of Yacc Program
 Any Yacc program contains mainly three sections
1. Declaration
2. Translation rules
3. Supporting C-routines

Structure of Program
<left side>→<alt 1>|<alt 2>|……..|<alt n>
Declaration It is used to declare variables, constant & Header files
%%
Example:
<left side> : <alt 1> {semantic action 1}
%{
%% | <alt 2> {semantic action 2}
int x,y;
Translation rule | <alt n> {semantic action n}
const int digit=50;
%% %%
#include <ctype.h>
%}
Supporting C routines All the function needed are specified over here.
Example: Yacc Program
 Program: Write Yacc program for simple desk calculator
/* Declaration */ /* Translation rule */ /* Supporting C routines*/
%{ %% yylex()
#include <ctype.h> line : expr ‘\n’ {print(“%d\n”,$1);} {
%} expr : expr ‘+’ term {$$=$1 + $3;} int c;
% token DIGIT | term; c=getchar();
term : term ‘*’ factor {$$=$1 * $3;} if(isdigit(c))
| factor; {
factor : ‘(‘ expr ‘)’ {$$=$2;} yylval= c-’0’
| DIGIT; return DIGIT
%% }
E→E+T | T return c;
T→T*F | F }
F→(E) | id
References
Books:
1. Compilers Principles, Techniques and Tools, PEARSON Education (Second Edition)
Authors: Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman
2. Compiler Design, PEARSON (for Gujarat Technological University)
Authors: Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman

You might also like