You are on page 1of 3

SLR table and LALR parse tables (resolving conflicts)

Consider the augmented grammar G:


0. 1. 2. 3. 4. 5. S --> S$ S --> Aa S --> dAb S --> dca S --> cb A --> c

LR(0) Itemsets
State I0 Item S --> .S$ S --> .Aa S --> .dAb S --> .dca S --> .cb A --> .c S --> S.$ S --> A.a S --> d.Ab S --> d.ca A --> .c S --> c.b A--> c. S --> Aa. S --> dA.b S --> dc.a A--> c. S --> cb. S --> dAb. S --> dca. Notes read on S goes to state 1 complete state for A; read on A goes to state 2 read on d goes to state 3 ditto read on c goes to state 4 ditto accept read on a goes to state 5 complete state for A; read on A goes to 6 read on c goes to state 7 ditto read on b goes to state 8 reduce on rule 5 reduce on rule 1 read on b goes to state 9 read on a goes to state 10 reduce on rule 5 reduce on rule 4 reduce on rule 2 reduce on rule 3

I1 I2 I3

I4

I5 I6 I7

I8 I9 I10

Conflicts
Inconsistent state any state that has a completed item [A .] with anything else there too, e.g., [A .] . You get a conflict because, for the given example, the first item puts a reduce in the parse table and the second item puts in a shift. This is called a shift/reduce conflict or error. It is also possible to get a reduce/reduce conflict. In the previous grammar, there is a conflict in state 4 and in state 7 (they are inconsistent). When in state zero, read on c, takes us to state 4. In state 4, after reading a c, you might be in the middle of a rule. But on the other hand, you might also be at the end of a rule. The conflict arises because when you get a c, you dont know which one it will be. Similarly, when in state 3, read on c, takes us to state 7. You also dont know if you are in the middle of some rule or at the end of another rule on a c read. Example grammar: 0. 1. 2. 3. 4. 5. S --> S$ S --> Aa S --> dAb S --> dca S --> cb A --> c

FOLLOW(S) = {$} FOLLOW(A) = {a, b}

The SLR parse table:


a 0 1 2 3 4 5 6 7 8 9 10 b c s4 d s3 $ accept s5 s7 r5 s8/r5 r1 s10/r5 s9 r5 r4 r2 r3 6 S 1 A 2

Ways to resolve inconsistencies (conflicts)


1. Increase k LR(k) LR(1) items (k grows exponentially) 2. Use LR(1) items, but try to reduce the number of states 3. Use LR(0), but add look-ahead

LR(1) Itemsets
State I0 Item S --> .S$ , $ S --> .Aa , $ S --> .dAb , $ S --> .dca , $ S --> .cb , $ A --> .c , a S --> S.$ , $ S --> A.a , $ S --> d.Ab , $ S --> d.ca , $ A --> .c , b S --> c.b , $ A--> c. , a S --> Aa. , $ S --> dA.b , $ S --> dc.a , $ A--> c. , b S --> cb. , $ S --> dAb. , $ S --> dca. , $ Notes complete on S; read on S goes to state 1 complete on A; FIRST($$) = $; read on A goes to state 2 FIRST($$) = $; read on d goes to state 3 FIRST($$) = $; ditto read on d FIRST($$) = $; read on c goes to state 4 FIRST(a$) = a; ditto read on c accept read on a goes to state 5 complete on A; read on A goes to 6 read on c goes to state 7 FIRST(b$) = b; ditto read on c read on b goes to state 8 reduce on rule 5 reduce on rule 1 read on b goes to state 9 read on a goes to state 10 reduce on rule 5 reduce on rule 4 reduce on rule 2 reduce on rule 3

I1 I2 I3

I4

I5 I6 I7 I8 I9 I10

The LR(1) parse table = LALR table (because no merging of core sets occurs)
a 0 1 2 3 4 5 6 7 8 9 10 b c s4 d s3 $ accept s5 s7 r5 s8 r1 s10 s9 r5 r4 r2 r3 6 S 1 A 2

You might also like