Date post: | 12-Apr-2018 |
Category: |
Documents |
Upload: | nguyenkhanh |
View: | 309 times |
Download: | 2 times |
Examples on LR(0) Parser(SLR Parser)
VII Semester Language Processors Unit 2-Lecture notesM.B.Chandak
Course coordinator
Example-1: Generate LR(0) Parser: Canonical collections ofLR(0) items
S AAA aA | bSolution:Step: 1 – Grammar AugmentationS’ .S … Rule 0S .AA … Rule 1A .aA …. Rule 2A .b ….Rule 3Step: 2 – Closure operation = I0S’ .SS .AAA .aAA.b
Goto (I0, S) = I1S’ S. //**Goto(I0, A) = I2S A.AA .aAA .bGoto(I0, a) = I3A a.AA .aAA .bGoto(I0,b) = I4A b. //**Goto(I2, A) = I5S AA.Goto (I2, a) = I3
Goto (I2, b) = I4Goto (I3, A) = I6A aA.Goto (I3, a) = I3Goto (I3, b) = I4
Rules for construction of parsing table from Canonical collections of LR(0) items
• Action part: For TerminalSymbols
• If A α.aβ is state Ix in Itemsand goto(Ix,a)=Iy then set action[Ix,a]=Sy (represented as shift tostate Iy]
• If Aα. is in Ix, then setaction[Ix,f] to reduce Aα forall symbols “f” where “f” is inFollow(A) (Use rule number)
• If S’S. is in Ix then setaction[Ix,$]=accept.
• Go To Part: For Non TerminalSymbols
• If goto(Ix, A) = Iy, then goto(Ix,A)in table = Y
• It is numeric value of state Y.
• All other entries are consideredas error.
• Initial state is S’.S
DFA and Parsing Tablea b $ S A
ACTION GOTOI0 S3 S4 1 2I1 Accept
I2 S3 S4 5I3 S3 S4 6I4 r3 r3 r3I5 r1I6 r2 r2 r2
I0
I1
I2
I3
I4
I6
I5A
a
S
b
A
A
a
bba
Follow(S) = $Follow(A) = {a,b,$}
Example:2
SAa | bAc| Bc | bBaA dB dSolution:Step 1: Grammar AugmentationS’ .S … rule 0S .Aa … rule 1S .bAc … rule 2S .Bc …. rule 3S .bBa ….rule 4A .d …. rule 5B .d … rule 6Step 2: Closure operationS’ .SS .AaS .bAcS .BcS .bBaA .dB .d
Goto (i0, S) = i1S’ S.Goto (i0,A) = i2S A.aGoto(i0,b) = i3S b.AcS b.BaA .dB .dGoto(i0,B)=i4S B.cGoto(i0,d)=i5A d.Bd.
Goto(i2,a)=i6SAa.Goto(i3,A)=i7SbA.cGoto(i3,B)=i8SbB.aGoto(i3,d)=i5 //loopGoto(i4,c)=i9SBc.Goto(i7,c)=i10SbAc.Goto(i8,a)=i11SbBa.
DFA and Parsing Table SAa | bAc| Bc | bBaA dB dFollow(S) = {$}Follow(A) = {a,c}Follow(B) = {a,c}
i0
i1
i3
i4
i5
i2 i6
i7
i8i9
i10
i11a
c
c
B
A
a
d
B
b
A
S
d
Parsing table: Not LR(0): Reduce-Reduce conflict
a b c d $ S A BI0 S3 S5 1 2 4I1 AcceptI2 S6I3 S5 7 8I4 S9I5 R5,R6 R5,R6I6 R1I7 S10I8 S11I9 R3I10 R2I11 R4
Example:3
SL = R | RL * R | idR LSolution:Step 1: Grammar AugmentationS’ .S … rule 0S .L = R … rule 1S . R … rule 2L .* R …. rule 3L .id ….rule 4R .L…. rule 5Step 2: Closure operation (S’.S)S’ .SS .L = RS .RL .*RL .idR .L
Goto (i0, S) = i1S’ S.Goto (i0,L) = i2S L . = RR L. //**Goto(i0,R) = i3S R. //**Goto(i0,*)=i4L *.RR.LL.*RL.idGoto(i0,id)=i5L id.Goto(i2,=)=i6S L = .R
R .LL .*RL .idGoto(i4,R)=i7L *R.Goto(i4,L)=i8R L. //****Goto(i4,*)=i4Goto(i4,id)=i5Goto(i6,R)=i9S L=R.Goto(i6,L)=i8Goto(i6,*)=i4Goto(i6,id)=i5
DFA and Parsing TableSL = R | RL * R | idR LFollow(S) = {$}Follow(L) = {=,$}Follow(R) = {=,$}
i0
i1
i3
i4
i5
i2 i6 i9
i8
i7
L
R
R=
id
*
R
L
S
id
*
L
*
id
Parsing table: Not LR(0): Shift-Reduce conflict
= * id $ S L RI0 S4 S5 1 2 3I1 AcceptI2 S6, R5 R5I3 R2I4 S4 S5 8 7I5 R4 R4I6 S4 S5 8 9I7 R3 R3I8 R5 R5I9 R1
Example 4: LR(0) or SLR ParsingEE+T | TT T*F | FF (E) | idStep 1: LR(0) Parser permits Leftrecursion and left factoring as itoperates in Bottom up order.Grammar Augmentation:E’ .EE .E+TE .TT .T*FT.FF.(E)F.id
Step 2:Closure of E’.E = i0E’ .EE .E+TE .TT.T*FT .FF.(E)F .idStep 3:GOTO(i0,E) = i1E’E.EE.+T //as dot crosses EGOTO(i0,T) = i2E T.T T.*F
GOTO(i0, F) =i3T F. //Rule completedGOTO{i0,( } = i4F (.E) // dot is prefixed to EE .E+TE .T //dot is prefixed to TT .T*FT .FF .(E)F .idGOTO(i0, id) = i5F id. //rule completed
//Processing of step i0 iscompleted.
Example 4:Continue with i1GOTO(i1, +) = i6E E+.TT .T*FT .FF.(E)F.idGOTO(i2, *) = i7T T*.FF.(E)F.idGoto(i4, E) = i8F(E.)EE.+T
Goto(i4, T)=i2Goto(i4,F)=i3Goto(i4, ( )=i4Goto(i4,id) =i5
Goto(i6,T) = i9EE+T.TT.*F
Goto(i6,F)=i3Goto(i6,( )=i4Goto(i6, id) = i5
Goto(i7,F)=i10TT*F.
Goto(i7,( )=i4Goto(i7,id) =i5
Goto(i8, ) )=i11F(E).
Goto(i8,+)=i6Goto(i9,*)=i7
DFA for the states
i0 i1
i2
i4
i5
i6
i7
i8
i3
i9
i10
i11
E
T
F
(
id
+ T
*
E
F
)
(id
*To state i7
With F to state i3With ( to state i4With id to state i5
With ( to state i4With id to state i5
With + to i6
With T to i2With F to i3
Parsing Table: No multiple entries: LR(0) Grammarid + * ( ) $ E T F
I0 s5 1 2 3I1 s6 AcceptI2 r2 s7 r2 r2I3 r4 r4 r4 r4I4 s5 s4 8 2 3I5 r6 r6 r6 r6I6 s5 S4 9 3I7 s5 S4 10I8 s6 S11I9 r1 s7 r1 r1
I10 r3 r3 r3 r3I11 r5 r5 r5 r5
Example:5Production with only € transitionS AaAb | BbBaA €B €Solution:Step 1: GrammarAugmentationS’ .S .. Rule 0S .AaAb .. Rule 1S .BbBa … Rule 2A . (rule 3)B . (rule 4)
Step 2: Closure of S’ .S = i0{S’ .SS .AaAbS .BbBaA .B . }Step 3: Goto operationsGoto(i0,S)=i1S’ S.Goto(i0, A)=i2S A.aAbGoto(i0,B) = i3S B.bBaGoto(i2, a) = i4S Aa.AbA .
Goto(i3,b) = i5S Bb.BaB .Goto(i4,A) = i6S AaA.bGoto(i5,B) = i7S BbB.aGoto(i6,b) = i8S AaAb.Goto(i7, b) = i9S BbBa.
FOLLOW(S) = {$}FOLLOW(A) = {a,b}FOLLOW(B) = {a,b}
Parsing Table: Reduce – Reduce Conflicta b $ S A B
I0 R3, R4 R3, R4 1I1 AcceptI2 S4I3 S5I4 R3 R3 6I5 R4 R4 7I6 S8I7 S9I8 R1I9 R2
Example-1:String Parsing using LR(0) parsing table
S AAA aA | bSolution:Step: 1 – Grammar AugmentationS’ .S … Rule 0S .AA … Rule 1A .aA …. Rule 2A .b ….Rule 3Step: 2 – Closure operation = I0S’ .SS .AAA .aAA.b
Goto (I0, S) = I1S’ S. //**Goto(I0, A) = I2S A.AA .aAA .bGoto(I0, a) = I3A a.AA .aAA .bGoto(I0,b) = I4A b. //**Goto(I2, A) = I5S AA.Goto (I2, a) = I3
Goto (I2, b) = I4Goto (I3, A) = I6A aA.Goto (I3, a) = I3Goto (I3, b) = I4
DFA and Parsing Tablea b $ S A
ACTION GOTOI0 S3 S4 1 2I1 Accept
I2 S3 S4 5I3 S3 S4 6I4 r3 r3 r3I5 r1I6 r2 r2 r2
I0
I1
I2
I3
I4
I6
I5A
a
S
b
A
A
a
bba
Follow(S) = $Follow(A) = {a,b,$}
String: aabba b $ S A
ACTION GOTOI0 S3 S4 1 2I1 Accept
I2 S3 S4 5I3 S3 S4 6I4 r3 r3 r3I5 r1I6 r2 r2 r2
Stack Input Action
0 aabb$ I0 a, S30a3 aabb$ I3a, S30a3a3 aabb$ I3 b, S40a3a3b4 aabb$ I4b, r30a3a3A6 aabb$ I6b, r20a3A6 aabb$ I6b, r20A2 aabb$ I2b, s40A2b4 aabb$ I4$, r30A2A5 aabb$ I5$,r10s1 aabb$ accept
Shift “a” and goto state 3Reduction means: reduce the previous symbol set to RHS and not reducing the actual symbol at the pointer.Pop number symbols = Length of RHS * 2Below “A” is 3, so 3A = i6