3 Role of Parser
3 Role of Parser
Role of Parser- Parse Tree - Elimination of Ambiguity – Top Down Parsing - Recursive Descent Parsing -
Symbol table
• Parser obtains a string of token from the lexical analyzer and reports syntax
error if any otherwise generates syntax tree.
• There are two types of parser:
1. Top-down parser
2. Bottom-up parser
Syntax Error Handling
The parser considers the program in hand as a whole and tries to figure out what the
program is intended to do and tries to find out a closest match for it, which is error-free.
When an erroneous input (statement) X is fed, it creates a parse tree for some closest error-
free statement Y.
This may allow the parser to make minimal changes in the source code, but due to the
complexity (time and space) of this strategy, it has not been implemented in practice yet.
Types of Grammar
• Regular Grammar (Type 3)
• Context-free Grammar (Type 2)
• Context-sensitivity Grammar (Type 1)
• Phrase-Structured Grammar (Type 0)
Context free grammar
Context free grammar
• A context free grammar (CFG) is a 4-tuple <VN, VT, S, S> where,
VN is finite set of non terminals,
VT is disjoint finite set of terminals,
S is an element of and it’s a start symbol,
P 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 <VN, VT, S, S> where,
VN is finite set of non terminals,
VT is disjoint finite set of terminals,
S is an element of and it’s a start symbol,
P 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.
Start symbol: E
Productions: E E O E| (E) | -E | id
O+|-|*|/ |↑
Derivation & Ambiguity
Derivation
• Derivation is used to find whether the string belongs to a given grammar or not.
• Types of derivations are:
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
S-S Parse tree represents the
S*S-S
structure of derivation 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: 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 Derivation Parse Tree
Exercise: Derivation
1. Perform leftmost derivation and draw parse tree.
A0A | 𝜖
SA1B
B0B | 1B | 𝜖
Output string: 1001
2. Perform leftmost derivation and draw parse tree.
S0S1 | 01 Output string: 000111
3. Perform rightmost derivation and draw parse tree.
EE+E | E*E | id | (E) | -E
Output string: id + id * id
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
S + S a a S * S
a+S*S a+S*S
a+a*S a a a+a*S a a
a+a*a a+a*a
• Here, Two leftmost derivation for string a+a*a is possible hence, above grammar
is ambiguous.
Exercise: Ambiguous Grammar
𝜖
Examples: Left recursion elimination
EE+T | T
ETE’
E’+TE’ | ε
TT*F | F
TFT’
T’*FT’ | ε
XX%Y | Z
XZX’
X’%YX’ | ε
Exercise: Left recursion
1. AAbd | Aa | a
BBe | b
2. AAB | AC | a | b
3. SA | B
AABC | Acd | a | aa
BBee | b
4. ExpExp+term | Exp-term | term
Left factoring
Left factoring is a grammar transformation that is useful for producing a grammar
suitable for predictive parsing.
Algorithm to left factor a grammar
Input: Grammar G
Output: An equivalent left factored grammar.
Method:
For each non terminal A find the longest prefix common to two or more of its
alternatives. If , i.e., there is a non trivial common prefix, replace all the productions
where represents all alternatives that do not begin with by
Here A' is new non terminal. Repeatedly apply this transformation until no two
alternatives for a non-terminal have a common prefix.
Left factoring elimination
| δ
Example: Left factoring elimination
SaAB | aCD
SaS’
S’AB | CD
A xByA | xByAzA | a
A xByAA’ | a
A’ Є | zA
A aAB | aA |a
A’AB | A | 𝜖
AaA’
A’AA’’ | 𝜖
A’’B | 𝜖
Exercise
1. SiEtS | iEtSeS | a
2. A ad | a | ab | abc | x
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 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
Parsing
S S S
c A d c A d c A d
Make prediction Make prediction
a + b $ INPUT
X
Y Predictiv
e parsing OUTPUT
Z
Stack program
$
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 .
𝜖 𝜖
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
SaBa
NT First
BbB | ϵ S {a}
Step 1: Not required
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
A
add to Rule 2
,𝜖}
FIRST(B)={ add to
b
Example-1: LL(1) parsing
SaBa
NT First Follow
Follow(S)
Rule 1: Place $ in FOLLOW(S)
Follow(S)={ $ }
Follow(B)
SaBaS a B a
BbB
Rule 2 B b B
Rule 3
A B First( A B Follow(A)=follow(B)
Follow(B)={ a }
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa
B
Rule: 2
SaBa A
a = first()
a=FIRST(aBa)={ a } M[A,a] = A
M[S,a]=SaBa
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa
B BbB
Rule: 2
BbB A
a = first()
a=FIRST(bB)={ b } M[A,a] = A
M[B,b]=BbB
Example-1: LL(1) parsing
SaBa
NT First Follow
S {a} {$}
BbB | ϵ
B {b,𝜖} {a}
Step 3: Prepare predictive parsing table
NT Input Symbol
a b $
S SaBa Error Error
B Bϵ BbB Error
Rule: 3
Bϵ A
b = follow(A)
M[B,a]=B𝜖
b=FOLLOW(B)={ a } M[A,b] = A
Example-2: LL(1) parsing
SaB | ϵ
BbC | ϵ
CcS | ϵ
Step 1: Not required NT First
S { a, 𝜖 }
Step 2: Compute FIRST B {b,𝜖}
First(S) C {c,𝜖}
SaB S𝜖
S a B S 𝜖
Rule 1 Rule 2
A A
add to add to
FIRST(S)={ a , 𝜖 }
Example-2: LL(1) parsing
SaB | ϵ
BbC | ϵ
CcS | ϵ
Step 1: Not required NT First
S { a, 𝜖 }
Step 2: Compute FIRST B {b,𝜖}
First(B) C {c,𝜖}
BbC B𝜖
B b C B 𝜖
Rule 1 Rule 2
A A
add to add to
FIRST(B)={ b , 𝜖 }
Example-2: LL(1) parsing
SaB | ϵ
BbC | ϵ
CcS | ϵ
Step 1: Not required NT First
S { a, 𝜖 }
Step 2: Compute FIRST B {b,𝜖}
First(C) C {c,𝜖}
CcS 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 BbC | ϵ
Rule 3
A B Follow(A)=follow(B) CcS | ϵ
Follow(S)=Follow(C) ={$}
NT First Follow
S {a,𝜖} {$}
B {b,𝜖} {$}
C {c,𝜖} {$}
BbC B b C
Rule 3 SaB
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
SaB | ϵ NT First Follow
S {a,𝜖} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
S SaB
B
C
Rule: 2
SaB A
a = first()
a=FIRST(aB)={ a } M[A,a] = A
M[S,a]=SaB
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
S SaB S𝜖
B
C
Rule: 3
S𝜖 A
b = follow(A)
b=FOLLOW(S)={ $ } M[A,b] = A
M[S,$]=S𝜖
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
S SaB S𝜖
B BbC
C
Rule: 2
BbC A
a = first()
a=FIRST(bC)={ b } M[A,a] = A
M[B,b]=BbC
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
S SaB S𝜖
B BbC B𝜖
C
Rule: 3
B𝜖 A
b = follow(A)
b=FOLLOW(B)={ $ } M[A,b] = A
M[B,$]=B𝜖
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
S SaB S𝜖
B BbC B𝜖
C CcS
Rule: 2
CcS A
a = first()
a=FIRST(cS)={ c } M[A,a] = A
M[C,c]=CcS
Example-2: LL(1) parsing
SaB | ϵ NT First Follow
S {a} {$}
BbC | ϵ B {b,𝜖} {$}
CcS | ϵ C {c,𝜖} {$}
Step 3: Prepare predictive parsing table
N Input Symbol
T
a b c $
First(E’) E’+TE’ | ϵ
E’+TE’ TFT’
E’ + T E’ Rule 1
add to T’*FT’ | ϵ
A NT First
EF(E){ (,id }
{ +, 𝜖 }
| id
E’
T { (,id }
E’𝜖
T’
E’ Rule 2 F { (,id }
A add to
FIRST(E’)={ + , 𝜖 }
Example-3: LL(1) parsing
Step 2: Compute FIRST ETE’
First(T’) E’+TE’ | ϵ
T’*FT’ TFT’
T’ * F T’ Rule 1
add to T’*FT’ | ϵ
A NT First
EF(E){ (,id }
{ +, 𝜖 }
| id
E’
T { (,id }
T’𝜖
T’ { *, 𝜖 }
T’ Rule 2 F { (,id }
A add to
FIRST(T’)={ *, 𝜖 }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(E) E’+TE’ | ϵ
Rule 1: Place $ in FOLLOW(E) TFT’
FOLLOW(E)={ $, ) }
Example-3: LL(1) parsing
ETE’
Step 2: Compute FOLLOW
E’+TE’ | ϵ
FOLLOW(E’) TFT’
ETE’
NT First T’*FT’ |ϵ
Follow
E T E’ F(E)
Rule 3 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id }
T’ { *, 𝜖 }
E’+TE’ F { (,id }
E’ +T E’ Rule 3
A B
FOLLOW(E’)={ $,) }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(T) E’+TE’ | ϵ
TFT’
ETE’
NT First T’*FT’ |ϵ
Follow
E T E’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id }
T’ { *, 𝜖 }
F { (,id }
E T E’ Rule 3
A B
FOLLOW(T)={ +, $, )
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(T) E’+TE’ | ϵ
TFT’
E’+TE’
NT First T’*FT’ |ϵ
Follow
E’ + T E’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id } { +,$,) }
T’ { *, 𝜖 }
F { (,id }
E’ + T E’ Rule 3
A B
FOLLOW(T)={ +, $, ) }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(T’) E’+TE’ | ϵ
TFT’
TFT’
NT First T’*FT’ |ϵ
Follow
T F T’ F(E)
Rule 3 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id } { +,$,) }
T’*FT’ T’ { *, 𝜖 } { +,$,) }
F { (,id }
T’ *F T’ Rule 3
A B
FOLLOW(T’)={+ $,) }
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(F) E’+TE’ | ϵ
TFT’
TFT’
NT First T’*FT’ |ϵ
Follow
T F T’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id }
T F T’ Rule 3
A B
FOLLOW(F)={ *, + ,$ , )
Example-3: LL(1) parsing
Step 2: Compute FOLLOW ETE’
FOLLOW(F) E’+TE’ | ϵ
TFT’
T’*FT’
NT First T’*FT’ |ϵ
Follow
T’ * F T’ F(E)
Rule 2 E { (,id }| id { $,) }
{ +, 𝜖 } { $,) }
A B
E’
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
F { (,id } {*,+,$,)}
T’ * F T’ Rule 3
A B
FOLLOW(F)={ *,+,$, ) }
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
M[E’,+]=E’+TE’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
M[T’,*]=T’*FT’
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
NT Input Symbol
E’+TE’ | ϵ
id + * ( ) $ TFT’
E ETE’ ETE’
T’*FT’ | ϵ
E’ E’+TE’ E’𝜖 E’𝜖
NT First F(E)
Follow
T TFT’ TFT’ | id
E { (,id } { $,) }
{ +, 𝜖 }
T’ T’𝜖 T’*FT’ T’𝜖 T’𝜖
E’ { $,) }
F
T { (,id } { +,$,) }
T’ { *, 𝜖 } { +,$,) }
T’𝜖
F { (,id } {*,+,$,)}
b=FOLLOW(T’)={ +,$,) } Rule: 3
A
M[T’,+]=T’𝜖 b = follow(A)
M[A,b] = A
M[T’,$]=T’𝜖
M[T’,)]=T’𝜖
Example-3: LL(1) parsing
Step 3: Construct predictive parsing table ETE’
T* numT| 𝜖
Else
Error(); }
Enum T
} Else
3 * 4 $ Succes NULL
} 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 * $
} s Error
Parsing Methods
Parsing
$ $ Parsing Done
Operator precedence function
Algorithm for constructing precedence functions
1. Create functions and for each 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 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 $
+ .
> <. <. .
>
gid fid
f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g+ f+
f+ .> g+ f+ g+
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 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 $
+ .
> <. <. .
>
gid fid
f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g+ f+
f+ <. g* f+ g*
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 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 $
+ .
> <. <. .
>
gid fid
f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g+ f+
f+ <. gid f+ gid
f* <. gid f* gid
f$ <. gid f$ gid
f$ 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 $
+ .
> <. <. .
>
gid fid
f * .
> .
> <. .
>
id .
> .
> .
>
f* g* $ <. <. <.
g+ f+
f+ <. g$ f+ g$
f* <. g$ f* g$
fid <. g$ fid g$
f$ g$
Operator precedence function
+ * id $
f 2
gid fid g
f* g*
g+ f+
f$ g$
Operator precedence function
+ * id $
f 2 4
gid fid
g 1
f* g*
g+ f+
f$ g$
Operator precedence function
+ * id $
f 2 4
gid fid
g 1 3
f* g*
g+ f+
f$ g$
Operator precedence function
+ * id $
f 2 4 4
gid fid
g 1 3
f* g*
g+ f+
f$ g$
Operator precedence function
+ * id $
f 2 4 4
gid fid
g 1 3 5
f* g*
g+ f+
f$ g$
Operator precedence function
+ * id $
f 2 4 4 0
gid fid
g 1 3 5 0
f* g*
g+ f+
f$ g$
Parsing Methods
Parsin
g
descen
t
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
Parsing Methods
Parsin
g
descen
t
Computation of closure & go to function
X Xb
Closure(I):
.
X X b
Goto(I,X)
.
X X b
Steps to construct SLR parser
1. Construct Canonical set of LR(0) items
2. Construct SLR parsing table
3. Parse the input string
Example: SLR(1)- simple LR
S AA
A aA | S AA . 5
S’ S. 𝑰𝟏 A a . A
b 𝐺𝑜𝑡𝑜(𝐼2, 𝐴)
𝑰𝟐
𝐺𝑜𝑡𝑜(𝐼 0,𝑆) A. aA 3
S A . A A. b
S’.S
A. aA
S. AA
A. b A b. 4
A. aA
A. b
3 A aA . 6 LR(0) item set
Augmented
grammar A a . A
A. aA A a . A
A. b A. aA 3
A b. 4
A. b
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 to
S’. S 𝐼( 0 AA. aA 𝑡𝑜 ( A. b
S. AA 𝑡𝑜 𝐺𝑜 Item
set
a b $ S A
𝑜
A. aA 𝐺
A. b A b.
𝐺𝑜 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) 4
0 S3 S4 1 2
,3 𝐴
(𝑜 𝐼
A. b 1 Accept
𝑡𝑜 A aA .
(𝐼 𝑡
3 6
2 S3 S4 5
0 , A a . 𝐺𝑜
𝑎) A 3 S3 S4 6
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A. aA 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎) A a. 4 R3 R3 R3
A
A b. 4 A. b 𝐺𝑜 A. aA 3
5 R1
𝑡𝑜 A. b 6 R2 R2 R2
S AA (𝐼
3,
A aA | 𝑏)
A b. 4
b
Parsing Methods
Parsin
g
descen
How to calculate look ahead?
How to calculate look ahead?
SCC S’ . S , $
C cC | d A . X ,
Closure(I)
S’.S,$
$
S.CC, c|d
S . C C , $
C.cC,c|d A . X ,
C.d,
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
𝐺𝑜
S.AA,$ 𝑡𝑜 A. b, A
7 7
3
b 0 S3 S4 1 2
Aa.A, a| 𝐺𝑜 1 Accept
𝑡𝑜 (
3
b 2 S6 S7 5
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A.aA ,a|b𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎)A a.A , a|b
𝐼0,
3 S3 S4 8
A. b, a| A.aA , a|
𝐺 𝑜 4 R3 R3
𝑎)
A b., a|b 4 b b
𝑡𝑜 A.b , a|b 5 R1
S AA ( 𝐼3 6 S6 S7 9
A aA | , 𝑏 A b., a| 7 R3
) 8 R2 R2
b 4 b 9 R2
Parsing Methods
Parsin
g
descen
t
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 Accept
2 S6 S7 5 0 S36 S47 1 2
3 S3 S4 8 1 Accept
4 R3 R3 2 S36 S47 5
36 S36 S47 89
5 R1 47 R3 R3 R3
6 S6 S7 9 5 R1
7 R3 89 R2 R2 R2
8 R2 R2
9 R2
CLR Parsing Table LALR Parsing Table