0% found this document useful (0 votes)
294 views14 pages

SLR Vs CLR and LALR

The document provides an example of constructing the CLR parsing table for a grammar. It defines a grammar with productions S → AaAb | BbBa, A → ε, B → ε. It shows the augmented grammar and outlines the closure and goto operations to construct the CLR items and parsing table with 9 items and actions for a, b, $. It compares this to an SLR parsing table for the same grammar, which would have multiple entries and not be SLR.

Uploaded by

Smart Boy
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)
294 views14 pages

SLR Vs CLR and LALR

The document provides an example of constructing the CLR parsing table for a grammar. It defines a grammar with productions S → AaAb | BbBa, A → ε, B → ε. It shows the augmented grammar and outlines the closure and goto operations to construct the CLR items and parsing table with 9 items and actions for a, b, $. It compares this to an SLR parsing table for the same grammar, which would have multiple entries and not be SLR.

Uploaded by

Smart Boy
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/ 14

SLR Vs.

CLR and
Introduction to LALR
By
Dr. D. Chandrasekhar Rao
Assistant Professor
Department of Information Technology
Example-2 of CLR

 Let us consider the grammar Closure 𝑆 ′ →∙ 𝑆, $ =


𝑆 → 𝐴𝑎𝐴𝑏|𝐵𝑏𝐵𝑎 {𝑆 ′ →∙ 𝑆, $
𝐴→𝜀 𝑆 →∙ 𝐴𝑎𝐴𝑏, $
𝐵→𝜀 𝑆 →∙ 𝐵𝑏𝐵𝑎, $
𝐴 →∙, 𝑎
Augmented grammar is
𝐵 →∙, 𝑏} = 𝐼0
𝑆′ → 𝑆
Goto 𝑰𝟎 , 𝑺
𝑆 → 𝐴𝑎𝐴𝑏|𝐵𝑏𝐵𝑎
=Closure (𝑆 ′ → 𝑆 ∙, $)
𝐴→𝜀
={𝑆 ′ → 𝑆 ∙, $} = 𝐼1
𝐵→𝜀
Goto 𝑰𝟎 , 𝑨
=Closure (𝑆 → 𝐴 ∙ 𝑎𝐴𝑏, $)
={𝑆 → 𝐴 ∙ 𝑎𝐴𝑏, $} = 𝐼2
Example-2 Cont.…

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

 Let us consider the grammar Closure 𝑆 ′ →∙ 𝑆, $ =


𝑆 → 𝐴𝐴 {𝑆 ′ →∙ 𝑆, $
𝐴 → 𝑎𝐴 𝑆 →∙ 𝐴𝐴, $
𝐴→𝑏 𝐴 →∙ 𝑎𝐴, 𝑎|𝑏
𝐴 →∙ b, 𝑎|𝑏} = 𝐼0
Augmented grammar is
Goto 𝑰𝟎 , 𝑺
𝑆′ → 𝑆
=Closure (𝑆 ′ → 𝑆 ∙, $)
𝑆 → 𝐴𝐴
={𝑆 ′ → 𝑆 ∙, $} = 𝐼1
𝐴 → 𝑎𝐴
𝐴→𝑏
Goto 𝑰𝟎 , 𝑨
=Closure (𝑆 → 𝐴 ∙ 𝐴, $)
={𝑆 → 𝐴 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $
𝐴 →∙ 𝑏, $} = 𝐼2
Example-3 Cont.…

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

Various steps involved in the LALR (1) Parsing:


 Add Augment production in the given grammar
 Create Canonical collection of LR (1) items (Same as CLR)
 Combine the underlying LR(0) items with different Look-ahead
 Construct the DFA
 Construct a LALR (1) parsing table (Same as CLR)
Example of LALR

 Let us consider the grammar


𝑆 → 𝐴𝐴
𝐴 → 𝑎𝐴
𝐴→𝑏
 The number of LR(1) items = 10
 If we consider the underlying LR(0) items, then we can observe that the
item ( 𝐼3 , 𝐼6 ), ( 𝐼4 , 𝐼7 ) and ( 𝐼8 , 𝐼9 ) are same.
 Thus, these items will be combined into one by combining its look-ahead to
achieve the advantage of SLR.
 DFA will be constructed with reduced items
 LALR parsing table is constructed as per CLR, which in turn achieves the
power of CLR.
Example Cont.…

Thus the LR(1) item of the {𝐴 → 𝑎 ∙ 𝐴, 𝑎 𝑏 $


grammar will be 𝐴 →∙ 𝑎𝐴, 𝑎 𝑏 $
{𝑆 ′ →∙ 𝑆, $, 𝐴 →∙ 𝑏, 𝑎 𝑏 $} = 𝐼36
𝑆 →∙ 𝐴𝐴, $
𝐴 →∙ 𝑎𝐴, 𝑎|𝑏 {𝐴 → 𝑏 ∙, 𝑎 𝑏 $} = 𝐼47
𝐴 →∙ b, 𝑎|𝑏} = 𝐼0
{𝑆 → 𝐴𝐴 ∙, $} = 𝐼5
{𝑆 ′ → 𝑆 ∙, $} = 𝐼1
{𝐴 → 𝑎𝐴 ∙, 𝑎 𝑏 $} = 𝐼89

{𝑆 → 𝐴 ∙ 𝐴, $
𝐴 →∙ 𝑎𝐴, $
𝐴 →∙ 𝑏, $} = 𝐼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

You might also like