SLR Vs CLR and LALR
SLR Vs CLR and LALR
CLR and
Introduction to LALR
By
Dr. D. Chandrasekhar Rao
Assistant Professor
Department of Information Technology
Example-2 of CLR
Goto 𝑰𝟎 , 𝑩 Goto 𝑰𝟒 , 𝑨
=Closure (𝑆 → 𝐵 ∙ 𝑏𝐵𝑎, $) =Closure (𝑆 → 𝐴𝑎𝐴 ∙ 𝑏, $)
={𝑆 → 𝐵 ∙ 𝑏𝐵𝑎, $} = 𝐼3 ={𝑆 → 𝐴𝑎𝐴 ∙ 𝑏, $} = 𝐼6
Goto 𝑰𝟐 , 𝒂 Goto 𝑰𝟓 , 𝑩
=Closure (𝑆 → 𝐵𝑏𝐵 ∙ 𝑎, $)
=Closure (𝑆 → 𝐴𝑎 ∙ 𝐴𝑏, $)
={𝑆 → 𝐵𝑏𝐵 ∙ 𝑎, $} = 𝐼7
={𝑆 → 𝐴𝑎 ∙ 𝐴𝑏, $
Goto 𝑰𝟔 , 𝒃
𝐴 →∙, b} = 𝐼4
=Closure (𝑆 → 𝐴𝑎𝐴𝑏 ∙, $)
Goto 𝑰𝟑 , 𝒃
={𝑆 → 𝐴𝑎𝐴𝑏 ∙, $} = 𝐼8
=Closure (𝑆 → 𝐵𝑏 ∙ 𝐵𝑎, $) Goto 𝑰𝟕 , 𝒂
={𝑆 → 𝐵𝑏 ∙ 𝐵𝑎, $ =Closure (𝑆 → 𝐵𝑏𝐵𝑎 ∙, $)
𝐵 →∙, a} = 𝐼5 ={𝑆 → 𝐵𝑏𝐵𝑎 ∙, $} = 𝐼9
Example-2 Cont.…
Item Action Table Goto Table
DFA
a b $ S A B
𝐼0 𝑅3 𝑅4 1 2 3
𝐼1 Acc
𝐼2 𝑆4
𝐼3 𝑆5
𝐼4 𝑅3 6
𝑆 → 𝐴𝑎𝐴𝑏 (1) 𝐼5 𝑅4 7
𝑆 → 𝐵𝑏𝐵𝑎 (2)
𝐴→𝜀 (3) 𝐼6 𝑆8
𝐵→𝜀 (4) 𝐼7 𝑆9
𝐼8 𝑅1
𝐼9 𝑅2
Example-3 of CLR
Goto 𝑰𝟎 , 𝒂 Goto 𝑰𝟐 , 𝒂
=Closure (𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏) = Closure (𝐴 → 𝑎 ∙ 𝐴, $)
={𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏 ={𝐴 → 𝑎 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 𝐴 →∙ 𝑎𝐴, $
𝐴 →∙ 𝑏, 𝑎|𝑏} = 𝐼3 𝐴 →∙ 𝑏, $} = 𝐼6
Goto 𝑰𝟎 , 𝒃 Goto 𝑰𝟐 , 𝒃
=Closure (𝐴 → 𝑏 ∙, 𝑎|𝑏) =Closure (𝐴 → 𝑏 ∙, $)
={𝐴 → 𝑏 ∙, 𝑎|𝑏} = 𝐼4 ={𝐴 → 𝑏 ∙, $} = 𝐼7
Goto 𝑰𝟐 , 𝑨 Goto 𝑰𝟑 , 𝑨
=Closure (𝑆 → 𝐴𝐴 ∙, $) =Closure (𝐴 → 𝑎𝐴 ∙, 𝑎|𝑏)
={𝑆 → 𝐴𝐴 ∙, $} = 𝐼5 ={𝐴 → 𝑎𝐴 ∙, 𝑎|𝑏} = 𝐼8
Example-3 Cont.…
Goto 𝑰𝟑 , 𝒂 Goto 𝑰𝟔 , 𝒂
=Closure (𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏)
= Closure (𝐴 → 𝑎 ∙ 𝐴, $)
={𝐴 → 𝑎 ∙ 𝐴, 𝑎|𝑏
={𝐴 → 𝑎 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏
𝐴 →∙ 𝑏, 𝑎 𝑏 = same 𝑎𝑠 𝐼3 𝐴 →∙ 𝑎𝐴, $
Goto 𝑰𝟑 , 𝒃 𝐴 →∙ 𝑏, $} = same 𝑎𝑠 𝐼6
=Closure (𝐴 → 𝑏 ∙, 𝑎|𝑏) Goto 𝑰𝟔 , 𝒃
= 𝐴 → 𝑏 ∙, 𝑎 𝑏 = same 𝑎𝑠 𝐼4
=Closure (𝐴 → 𝑏 ∙, $)
Goto 𝑰𝟔 , 𝑨
= 𝐴 → 𝑏 ∙, $ = same 𝑎𝑠 𝐼7
=Closure (𝐴 → 𝑎𝐴 ∙, $)
={𝐴 → 𝑎𝐴 ∙, $} = 𝐼9
Example-3 Cont.…
DFA Item Action Table Goto Table
a b $ S A
𝐼0 𝑆3 𝑆4 1 2
𝐼1 Acc
𝐼2 𝑆6 𝑆7 5
𝐼3 𝑆3 𝑆4 8
𝐼4 𝑅3 𝑅3
𝐼5 𝑅1
𝐼6 𝑆6 𝑆7 9
𝐼7 𝑅3
𝑆 → 𝐴𝐴 (1) 𝐼8 𝑅2 𝑅2
𝐴 → 𝑎𝐴 (2)
𝐼9 𝑅2
𝐴→𝑏 (3)
Comparing SLR with CLR
SLR CLR
For Example-1 (𝐸 → 𝐸 + 𝑇 𝑇 , 𝑇 → 𝑇 ∗ 𝐹, For Example-1(𝐸 → 𝐸 + 𝑇 𝑇 , 𝑇 → 𝑇 ∗ 𝐹,
𝐹 → 𝑖𝑑) 𝐹 → 𝑖𝑑)
No. of item = 09 No. of item = 09
Is SLR (i.e., no multiple entries) Is CLR (i.e., no multiple entries)
For Example-2 (𝑆 → 𝐴𝑎𝐴𝑏|𝐵𝑏𝐵𝑎, 𝐴 → 𝜀 , For Example-2 (𝑆 → 𝐴𝑎𝐴𝑏|𝐵𝑏𝐵𝑎, 𝐴 → 𝜀 ,
𝐵 → 𝜀) 𝐵 → 𝜀)
No. of item = 10 No. of item = 10
Is not SLR (i.e., contains multiple entries) Is CLR (i.e., no multiple entries)
For Example-3 (𝑆 → 𝐴𝐴,𝐴 → 𝑎𝐴|𝑏) For Example-3 (𝑆 → 𝐴𝐴,𝐴 → 𝑎𝐴|𝑏)
No. of item = 07 No. of item = 10
Is SLR (i.e., no multiple entries) Is CLR (i.e., no multiple entries)
Conclusion of Comparison
SLR CLR
1. Number of items are less as 1. Number of items are more as
compared to CLR compared to SLR
2. Less powerful (in terms of number of 2. More powerful (in terms of number
grammars it succeeds) of grammars it succeeds)
LALR
LALR Parsing
{𝑆 → 𝐴 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $
𝐴 →∙ 𝑏, $} = 𝐼2
Example Cont.…
Item Action Table Goto Table
a b $ S A
DFA
𝐼0 𝑆36 𝑆47 1 2
𝐼1 Acc
𝐼2 𝑆36 𝑆47 5
𝐼36 𝑆36 𝑆47 89
𝐼47 𝑅3 𝑅3 𝑅3
𝐼5 𝑅1
𝐼89 𝑅2 𝑅2 𝑅2 9