0% found this document useful (0 votes)
172 views5 pages

Lalr - Parser: S' - S S - BBB S - Aab S - Bba B - A

1. This document discusses the differences between SLR, LR(1), and LALR parsing. 2. It shows that LALR parsing is equivalent to LR(1) parsing in that it avoids shift-reduce conflicts, but LALR can have reduce-reduce conflicts even if LR(1) does not for a given grammar. 3. While LALR parsing is less powerful than LR(1) in some cases, it is more powerful than SLR parsing since it avoids shift-reduce conflicts where SLR has them.

Uploaded by

kislaybokaro
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views5 pages

Lalr - Parser: S' - S S - BBB S - Aab S - Bba B - A

1. This document discusses the differences between SLR, LR(1), and LALR parsing. 2. It shows that LALR parsing is equivalent to LR(1) parsing in that it avoids shift-reduce conflicts, but LALR can have reduce-reduce conflicts even if LR(1) does not for a given grammar. 3. While LALR parsing is less powerful than LR(1) in some cases, it is more powerful than SLR parsing since it avoids shift-reduce conflicts where SLR has them.

Uploaded by

kislaybokaro
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Vishwa Deepak 04cs1029

LALR -PARSER
There are three issues which arise here—
1. Whether LALR is equivalent to SLR.
2. Shift/Reduce conflict in the cases of LR(1) and LALR.
3. Reduce/Reduce conflict in the cases of LR(1) and LALR.

Let’s take an example grammar :

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

First of all we work with the SLR parser.


Generating the items ->

I0 : S’ -> .S
S -> .Bbb
S -> .aab
S -> .bBa
B -> .a
I1: S’ -> S.
I2: S -> B.bb
I3: S -> a.ab
B -> a.
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.

In the underlined item, there is a shift/reduce conflict. To avoid this conflict, we work
with LR(1) parser.

1
Vishwa Deepak 04cs1029

Generating the items of 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
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 we see that LALR = LR(1), since no merging was required .


As well as we find that no. of states in SLR and LR(1) are same, which may not be the
same in other cases.

*************************************************************

Now, Let’s take another example.


In this example it is shown that reduce/reduce conflict may arise in LALR parser , even if
it is not present in LR(1) parser.
We are given the following grammar to parse using LALR parser. First of all , this
grammar will be parsed using LR(1) parser, and after that it will be fed to LALR parser.

S’-> S
S-> aAd
S-> bBd
S-> aBe
S-> bAe
A-> c

2
Vishwa Deepak 04cs1029

B-> c

Generating the LR (1) items for the above grammar,


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
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. We see that when we make the Parsing table for
LR (1), we will get something like this…

3
Vishwa Deepak 04cs1029

The LR (1) Parsing Table. (partly filled)

a ……………… d e

I1 . .
I2 . .
. . .
. . .
.
I6 ………………. r6 r7
.. . .
. . .
. .
. . .
I9 …………………. r7 r6
.

This table on reduction to the LALR parsing table, comes up in the forms of-

The LALR Parsing table. ( partly filled)

a ……………… d e

I1 . .
I2 . .
. . .
. . .
.
I69 ………………. r6/r7 r7/r6
.. . .
. . .
. .
. . .
I9 …………………. r7 r6

4
Vishwa Deepak 04cs1029

So, we find that the LALR gains reduce-reduce conflict whereas the corresponding LR (1)
counterpart was void of it. This is a proof enough that LALR is less potent than LR (1).
But, since we have already proved that the LALR is void of shift-reduce conflicts (given that
the corresponding LR(1) is devoid of the same), whereas SLR (or LR (0)) is not necessarily
void of shift-reduce conflict, the LALR grammar is more potent than the SLR grammar.

SHIFT-REDUCE CONFLICT present in SLR

 Some of them are solved in….

LR (1)

 All those solved are preserved in…

LALR

So, we have answered all the queries on LALR that we raised intuitively.

You might also like