M2 - P4 LR Parser
M2 - P4 LR Parser
Bottom Up Parsing
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 & goto function
Closure(I):
• If ‘I’ is a set of items for a grammar, G then closure of I (closure (I)) is
set of items constructed from I by applying following 3 rules:
1. Construct augmented grammar.
2. Initially, every item in I is added to closure(I).
3. If A → α • B β is in closure (I) and B ⟶ ४ is a production, then add the item B
⟶ •४ to I, if it is not already in existence, we apply this rule until no more new
items can be added to closure(I).
Example:
E E+T | T
T T*F | F
F (E)| id
Computation of closure & goto function
Goto(I,X)
• The function goto can be defined as,
• If there is a production A → α • B β then,
goto (A → α • B β, B) = A → α B • β.
• Simply shifting of “•” one position ahead over grammar symbol.
Example:
E E+T | T
T T*F | F
F id | (E)
Steps to construct Simple LR (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 .
b 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A
𝑰𝟐
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) S A . ,2 𝑎 )
A. aA 3
, 𝐴 𝐼
𝐼( 0 AA. aA 𝑜(
A. b
S’S S’.S
𝑜 𝑡
SAA S. AA 𝑡𝑜 𝐺
𝑜
A. aA 𝐺
A. b A b.
AaA
𝐺𝑜 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) 4
,3 𝐴
Ab A. b
𝑡𝑜 (𝑜 𝐼 A aA .
LR(0) item
(𝐼 𝑡
3 6
Augmented set
Grammar 0 , A a . 𝐺𝑜
𝑎) 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 a 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
Action Go to
S’ S. 𝑰𝟏 A a .
𝑰𝟐 𝐺𝑜𝑡𝑜(𝐼2, 𝐴) A Item a b $ S A
𝑰𝟎 𝐺𝑜𝑡𝑜(𝐼 0,𝑆) ) S A . ,2 𝑎 )
A. aA 3 set
, 𝐴 𝐼 0 S3 S4 1 2
S’. S 𝐼( 0 AA. aA 𝑡𝑜 ( A. b
S. AA 𝑡𝑜 𝐺𝑜 1 Accept
𝑜
A. aA 𝐺
A. b A b. 2 S3 S4 5
𝐺𝑜 𝐺𝑜𝑡𝑜(𝐼2,𝑏) ) 4
,3 𝐴 3 S3 S4 6
A. b
𝑡𝑜 (𝑜 𝐼 A aA . 4 R3 R3 R3
(𝐼 𝑡
3 6
0 , A a . 𝐺𝑜 5 R1
𝑎) A
𝐺𝑜𝑡𝑜(𝐼 0,𝑏) A. aA 𝐺𝑜𝑡𝑜(𝐼 3 ,𝑎) A a . 6 R2 R2 R2
A
A b. 4 A. b 𝐺𝑜 A. aA 3
descen
How to calculate look ahead?
How to calculate look ahead?
SCC
CcC | d
Closure(I)
S’.S,$ S’ . S , $
S.CC, $ A . X ,
C.cC, c|d
C.d, c|d
S . C C , $
A . X ,
Example: CLR(1)- Canonical LR –
LR(0)+LOOKAHEAD
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 b. ,
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
Example 2
Construct CLR parsing table for the following grammar:
SCC
CcC | d
Exercise
Construct CLR parsing table for the following grammar:
S aAd | bBd | aBe | bAe
A c
Bc
Solution
Cont…
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
Construct LALR (1) Parsing table for the grammar.
S → A a|b A c|dc|bda
A→d
Parse input string "bdc" using LALR Parsing Table.
• S → Ab
• A→b|ε
• This grammar can produce two strings, bc and bbc, and it's
unambiguous. However, there's a FIRST/FOLLOW conflict on
production A → b | ε, because b ∈ FIRST(A) and b ∈ FOLLOW(A) as well.
• So the intersection of FIRST and FOLLOW of NT(A) is not null set and
this leads to FIRST/FOLLOW conflict
Points to be noted - SLR
• In case of two reductions, if the
follow of both the reduced
productions have something
common then it will result in
multiple entries in table hence not
SLR.
• In case of one shift and one
reduction, if there is a GOTO
operation from that state on a
terminal which is the follow of the
reduced production than it will
result in multiple entries hence
not SLR.
How do you identify whether a grammar is LL(1), LR(0), or SLR(1)?
X → Yz | a
Y → bZ | ε
Z→ε
FIR(X)={a,z}, FIR(Y)={b, Ԑ}, FIR(Z)={Ԑ}, FOL(X)={$}, FOL(Y}={z}, FOL(Z)={z}
To check if a grammar is LL(1), one option is to construct the LL(1) parsing table and check for
any First / First or First / Follow conflicts.
To check if a
grammar a b z $
is LR(0) or
X a Yz Yz
SLR(1),
we begin by Y bZ ε
building
up all of the Z ε
LR(0)
From this, we can see that the grammar is not LR(0) because there is a
configurating
shift/reduce
sets conflicts in state (1). Specifically, because we have the shift item
Yfor
→ .bZ
the and Y → . (r4), we can't tell whether to shift b or reduce the empty
string
grammar.
Points to be noted - CLR
Augmented grammar –
S’ -> S
S -> AaAb
S -> BbBa
A -> ε
B -> ε
GOTO graph for this grammar will be -
Cont…
• If a state has two reductions and both have same lookahead then
it will in multiple entries in parsing table thus a conflict.
• If a state has one reduction and there is a shift from that state
on a terminal same as the lookahead of the reduction then it will
lead to multiple entries in parsing table thus a conflict.