You are on page 1of 13

LALR PARSER

Prepared by
VISHWA DEEPAK
04CS1029
Dec 18, 2021 Vishwa Deepak 04CS1029 1
There are three issues which arise
here—
 Whether LALR is equivalent to
SLR.
 Shift/Reduce conflict in the
cases of LR(1) and LALR.
 Reduce/Reduce conflict in the
cases of LR(1) and LALR.

Dec 18, 2021 Vishwa Deepak 04CS1029 2


Taking an example Grammar

 S’ -> S
 S -> Bbb
 S -> aab
 S -> bBa
 B -> a

Dec 18, 2021 Vishwa Deepak 04CS1029 3


Items using SLR PARSER
 I0 : S’ -> .S
 S -> .Bbb
 S -> .aab
 S -> .bBa
 B -> .a
 I1: S’ -> S.
 I2: S -> B.bb
 I3: S -> a.ab
 B -> a.
Dec 18, 2021 Vishwa Deepak 04CS1029 4
 I4: S -> b.Ba
 B -> .a
 I5: S-> Bb.b
 I6: S -> aa.b
 I7: S -> bB.a
 I8: B -> a.
 I9: S-> Bbb.
 I10: S -> aab.
 I11: S -> bBa.

Underlined item shows shift/reduce


conflict.
Dec 18, 2021 Vishwa Deepak 04CS1029 5
Items using LR(1) PARSER
 I0 : S’ -> .S , $
 S -> .Bbb ,$
 S -> .aab , $
 S -> .bBa , $
 B -> .a ,a|b
 I1: S’ -> S. ,$
 I2: S -> B.bb , $
 I3: S -> a.ab ,$
 B -> a. , b
 I4: S -> b.Ba , $
 B -> .a ,a
Dec 18, 2021 Vishwa Deepak 04CS1029 6
 I5: S-> Bb.b ,$
 I6: S -> aa.b ,$
 I7: S -> bB.a ,$
 I8: B -> a. ,a
 I9: S-> Bbb. ,$
 I10: S -> aab. ,$
 I11: S -> bBa. ,$

In the underlined item , we find that


there is no shift / reduce conflict.
Here LALR = LR(1)
& Number of states in SLR parser =
Number of states in LR(1) parser.
Dec 18, 2021 Vishwa Deepak 04CS1029 7
NEXT GRAMMAR (EXAMPLE)
#To discuss reduce/reduce
conflict
 S’-> S

 S-> aAd

 S-> bBd

 S-> aBe

 S-> bAe

 A-> c

 B-> c

Dec 18, 2021 Vishwa Deepak 04CS1029 8


Items using LR(1) PARSER
 I0 : S’-> .S , $
 S-> . aAd, $
 S-> . bBd, $
 S-> . aBe, $
 S-> . bAe, $
 I1: S’-> S ., $
 I2: S-> a . Ad, $
 S-> a . Be, $
 A-> .c, d
 B->.c, e
 I3: S-> b . Bd, $
 S-> b . Ae, $
 A->.c, e
 B->.c,d

Dec 18, 2021 Vishwa Deepak 04CS1029 9


 I4: S->aA.d, $
 I5: S-> aB.e,$
 I6: A->c. , d
 B->c. , e
 I7: S->bB.d, $
 I8: S->bA.e, $
 I9: B->c. , d
 A->c. , e
 I10: S->aAd. , $
 I11: S->aBe., $
 I12: S->bBd., $
 I13: S->aBe., $

The underlined items are of our interest.


There is no reduce/reduce conflict in the
underlined items, due to different follows.
Dec 18, 2021 Vishwa Deepak 04CS1029 10
LR and LALR parsing tables

LR parser LALR parser


Item d e Item d e
I1 .. .. I1 .. ..
I2 .. .. I2 .. ..
.. .. .. .. .. ..
I6 r6 r7 I69 r6/r7 r6/r7
.. .. .. .. .. ..
I9 r7 r6 I10 .. ..
.. .. .. .. .. ..
Dec 18, 2021 Vishwa Deepak 04CS1029 11
 In the last slide we found that
even there was no R/R conflict
in LR(1) parser, it appeared in
LALR parser.
 This shows that LALR is less
potent than LR(1) parser ,since
it gains R/R conflict.
 LALR is free of S/R conflict , if
LR(1) is. But SLR may be
having this conflict. So, SLR is
least potent among all the three.
Dec 18, 2021 Vishwa Deepak 04CS1029 12
CONCLUSION

SHIFT-REDUCE CONFLICT present


in SLR

Some of them are solved in….
LR (1)

All those solved are preserved in…
LALR

Dec 18, 2021 Vishwa Deepak 04CS1029 13

You might also like