Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Examples on LR(0) Parser

(SLR Parser)
VII Semester Language Processors Unit 2-Lecture notes
M.B.Chandak
Course coordinator
Example-1: Generate LR(0) Parser: Canonical collections of
LR(0) items
S  AA Goto (I0, S) = I1 Goto (I2, b) = I4
A  aA | b S’  S. //** Goto (I3, A) = I6
Solution: Goto(I0, A) = I2 A  aA.
Step: 1 – Grammar Augmentation S  A.A Goto (I3, a) = I3
A  .aA Goto (I3, b) = I4
S’  .S … Rule 0 A  .b
S  .AA … Rule 1 Goto(I0, a) = I3
A  .aA …. Rule 2 A  a.A
A  .b ….Rule 3 A .aA
Step: 2 – Closure operation = I0 A .b
S’  .S Goto(I0,b) = I4
S  .AA A  b. //**
A  .aA Goto(I2, A) = I5
S  AA.
A.b Goto (I2, a) = I3
Rules for construction of parsing table from Canonical collections of LR(0) items

• Action part: For Terminal • Go To Part: For Non Terminal


Symbols Symbols
• If A  α.aβ is state Ix in Items • If goto(Ix, A) = Iy, then goto(Ix,A)
and goto(Ix,a)=Iy then set action in table = Y
[Ix,a]=Sy (represented as shift to • It is numeric value of state Y.
state Iy]
• If Aα. is in Ix, then set
action[Ix,f] to reduce Aα for
all symbols “f” where “f” is in • All other entries are considered
Follow(A) (Use rule number) as error.
• If S’S. is in Ix then set • Initial state is S’.S
action[Ix,$]=accept.
DFA and Parsing Table
a b $ S A
ACTION GOTO
I1
I0 S3 S4 1 2
S
A
I1 Accept
A I2 I5
I2 S3 S4 5
I0
a
a I3 S3 S4 6
A
a
I3 I6 I4 r3 r3 r3
b
b b I5 r1
I4
I6 r2 r2 r2

Follow(S) = $
Follow(A) = {a,b,$}
Example:2
SAa | bAc| Bc | bBa Goto (i0, S) = i1 Goto(i2,a)=i6
Ad
Bd
S’  S. SAa.
Solution: Goto (i0,A) = i2 Goto(i3,A)=i7
Step 1: Grammar Augmentation S  A.a SbA.c
S’  .S … rule 0
S  .Aa … rule 1
Goto(i0,b) = i3 Goto(i3,B)=i8
S  .bAc … rule 2 S  b.Ac SbB.a
S  .Bc …. rule 3 S  b.Ba Goto(i3,d)=i5 //loop
S  .bBa ….rule 4
A  .d …. rule 5
A  .d Goto(i4,c)=i9
B  .d … rule 6 B  .d SBc.
Step 2: Closure operation Goto(i0,B)=i4 Goto(i7,c)=i10
S’  .S S  B.c SbAc.
S  .Aa
S  .bAc Goto(i0,d)=i5 Goto(i8,a)=i11
S  .Bc A d. SbBa.
S  .bBa B d.
A  .d
B  .d
DFA and Parsing Table SAa | bAc| Bc | bBa
Ad
Bd
i1 Follow(S) = {$}
Follow(A) = {a,c}
Follow(B) = {a,c}
S
i2 a i6
A

A i7 c i10
i0 b i3
B
B d
i8 i11
i4 c
i9 a
d

i5
Parsing table: Not LR(0): Reduce-Reduce conflict
a b c d $ S A B
I0 S3 S5 1 2 4
I1 Accept
I2 S6
I3 S5 7 8
I4 S9
I5 R5,R6 R5,R6
I6 R1
I7 S10
I8 S11
I9 R3
I10 R2
I11 R4
Example:3
SL = R | R Goto (i0, S) = i1 R  .L
L  * R | id S’  S. L  .*R
RL
Solution:
Goto (i0,L) = i2 L  .id
Step 1: Grammar Augmentation SL.=R Goto(i4,R)=i7
S’  .S … rule 0 R  L. //** L  *R.
S  .L = R … rule 1 Goto(i0,R) = i3 Goto(i4,L)=i8
S  . R … rule 2 S  R. //** R  L. //****
L  .* R …. rule 3 Goto(i0,*)=i4 Goto(i4,*)=i4
L  .id ….rule 4
R  .L…. rule 5
L  *.R Goto(i4,id)=i5
Step 2: Closure operation (S’.S) R.L Goto(i6,R)=i9
S’  .S L.*R S  L=R.
S  .L = R L.id Goto(i6,L)=i8
S  .R Goto(i0,id)=i5 Goto(i6,*)=i4
L  .*R L  id. Goto(i6,id)=i5
L  .id
R  .L Goto(i2,=)=i6
S  L = .R
DFA and Parsing Table
SL = R | R
L  * R | id
RL
i1 Follow(S) = {$}
Follow(L) = {=,$}
S R Follow(R) = {=,$}
i2 = i6 i9
L
*
i0 R i3

*
*
i4 R L
i7 id
id
id L

i8
i5
Parsing table: Not LR(0): Shift-Reduce conflict
= * id $ S L R
I0 S4 S5 1 2 3
I1 Accept
I2 S6, R5 R5
I3 R2
I4 S4 S5 8 7
I5 R4 R4
I6 S4 S5 8 9
I7 R3 R3
I8 R5 R5
I9 R1
Example 4: LR(0) or SLR Parsing
EE+T | T Step 2: GOTO(i0, F) =i3
T  T*F | F Closure of E’.E = i0 T  F. //Rule completed
E’  .E GOTO{i0,( } = i4
F  (E) | id
E .E+T F  (.E) // dot is prefixed to E
Step 1: LR(0) Parser permits Left E .T
recursion and left factoring as it E  .E+T
operates in Bottom up order. T.T*F E  .T //dot is prefixed to T
Grammar Augmentation: T  .F T  .T*F
E’  .E F.(E) T  .F
F .id F  .(E)
E  .E+T
Step 3:GOTO(i0,E) = i1 F  .id
E  .T E’E. GOTO(i0, id) = i5
T .T*F EE.+T //as dot crosses E F  id. //rule completed
T.F GOTO(i0,T) = i2
F.(E) E  T. //Processing of step i0 is
completed.
F.id T T.*F
Example 4:
Continue with i1 Goto(i4, T)=i2 Goto(i7,F)=i10
GOTO(i1, +) = i6 Goto(i4,F)=i3 TT*F.
E  E+.T Goto(i4, ( )=i4
T  .T*F Goto(i4,id) =i5 Goto(i7,( )=i4
T  .F Goto(i7,id) =i5
F .(E) Goto(i6,T) = i9
F.id EE+T. Goto(i8, ) )=i11
GOTO(i2, *) = i7 TT.*F F(E).
T  T*.F
F.(E) Goto(i6,F)=i3 Goto(i8,+)=i6
F.id Goto(i6,( )=i4 Goto(i9,*)=i7
Goto(i4, E) = i8 Goto(i6, id) = i5
F(E.)
EE.+T
DFA for the states
E + T *
i0 i1 i6 i9 To state i7

With F to state i3
* With ( to state i4
T F
i2 i7 i10 With id to state i5

F
i3 With ( to state i4
With id to state i5
( E )
i4
i8 i11

( With + to i6
id

id

i5 With T to i2
With F to i3
Parsing Table: No multiple entries: LR(0) Grammar
id + * ( ) $ E T F
I0 s5 1 2 3
I1 s6 Accept
I2 r2 s7 r2 r2
I3 r4 r4 r4 r4
I4 s5 s4 8 2 3
I5 r6 r6 r6 r6
I6 s5 S4 9 3
I7 s5 S4 10
I8 s6 S11
I9 r1 s7 r1 r1
I10 r3 r3 r3 r3
I11 r5 r5 r5 r5
Example:5Production with only € transition
Step 2: Closure of S’  .S = i0 Goto(i3,b) = i5
S  AaAb | BbBa
{S’  .S S  Bb.Ba
A€ S  .AaAb B.
B€ S  .BbBa Goto(i4,A) = i6
A. S  AaA.b
Solution:
B. } Goto(i5,B) = i7
Step 1: Grammar Step 3: Goto operations S  BbB.a
Augmentation Goto(i0,S)=i1 Goto(i6,b) = i8
S’ .S .. Rule 0 S’  S. S  AaAb.
Goto(i0, A)=i2 Goto(i7, b) = i9
S  .AaAb .. Rule 1 S  A.aAb S  BbBa.
S .BbBa … Rule 2 Goto(i0,B) = i3
A  . (rule 3) S  B.bBa
Goto(i2, a) = i4 FOLLOW(S) = {$}
B  . (rule 4) S  Aa.Ab FOLLOW(A) = {a,b}
A. FOLLOW(B) = {a,b}
Parsing Table: Reduce – Reduce Conflict
a b $ S A B
I0 R3, R4 R3, R4 1
I1 Accept
I2 S4
I3 S5
I4 R3 R3 6
I5 R4 R4 7
I6 S8
I7 S9
I8 R1
I9 R2
Example: 6: Total 9 states
S  xAy | xBy | xAz
A  aS | q
Bq
Example 7:
S  aSbS | bSaS | €
Example 8
S  aAB | bB
A  Aa |
B  Bb |
Example-1:String Parsing using LR(0) parsing table

S  AA Goto (I0, S) = I1 Goto (I2, b) = I4


A  aA | b S’  S. //** Goto (I3, A) = I6
Solution: Goto(I0, A) = I2 A  aA.
Step: 1 – Grammar Augmentation S  A.A Goto (I3, a) = I3
A  .aA Goto (I3, b) = I4
S’  .S … Rule 0 A  .b
S  .AA … Rule 1 Goto(I0, a) = I3
A  .aA …. Rule 2 A  a.A
A  .b ….Rule 3 A .aA
Step: 2 – Closure operation = I0 A .b
S’  .S Goto(I0,b) = I4
S  .AA A  b. //**
A  .aA Goto(I2, A) = I5
S  AA.
A.b Goto (I2, a) = I3
DFA and Parsing Table
a b $ S A
ACTION GOTO
I1
I0 S3 S4 1 2
S
A
I1 Accept
A I2 I5
I2 S3 S4 5
I0
a
a I3 S3 S4 6
A
a
I3 I6 I4 r3 r3 r3
b
b b I5 r1
I4
I6 r2 r2 r2

Follow(S) = $
Follow(A) = {a,b,$}
String: aabb
Stack Input Action
a b $ S A
0 aabb$ I0  a, S3
ACTION GOTO 0a3 aabb$ I3a, S3
I0 S3 S4 1 2 0a3a3 aabb$ I3  b, S4
I1 Accept 0a3a3b4 aabb$ I4b, r3
I2 S3 S4 5 0a3a3A6 aabb$ I6b, r2

I3 S3 S4 6 0a3A6 aabb$ I6b, r2


0A2 aabb$ I2b, s4
I4 r3 r3 r3
0A2b4 aabb$ I4$, r3
I5 r1
0A2A5 aabb$ I5$,r1
I6 r2 r2 r2 0s1 aabb$ accept
Shift “a” and goto state 3
Reduction means: reduce the previous symbol set to RHS and not reducing the actual symbol at the pointer.
Pop number symbols = Length of RHS * 2
Below “A” is 3, so 3A = i6

You might also like