For homework, I was given the following grammar:
S: D
D: AbBb | BaAb
A: ε
B: ε
I computed it using LL(1) just fine. The first sets were:
S: a, b
D: a,b
A: ε
B: ε
The follow sets were:
S: $
D: $
A: b
B: a,b
When I made my parsing table, the example string "ab" parsed just fine. However, when I tried to parse the same exact grammar using LR(1), I ran into errors.
For item set 0 I got the following: (the , separates the look ahead terminals)
Item set 0:
S: .D, $
D: .AbBb, $
D: .BaAb, $
A: ., b
B: ., a,b
If you make the table, you will clearly see that there is a reduce-reduce conflict between A and B in item set 0. If I'm asked to parse the string "ab", the parser won't know whether to reduce my empty to A or to reduce it to B. What am I doing wrong? I was always told that LR(1) can actually parse more grammars than LL(1), so what is the deal here? I would appreciate if somebody could help me out because it's driving me nuts. Thanks
They state you've generated looks like its from an
SLR(1)
parser.If you use
LR(1)
or evenLALR(1)
, you will find there are no conflicts. Here, for example, is state 0 of theLALR(1)
machine, as generated by Bison:State 0
While it is certainly true that
LL(1) ⊂ LR(1)
, there is no simple relationship betweenLL(1)
and eitherSLR(1)
orLALR(1)
. (I'm talking about grammars here. For sets of languages, the relationships are simpler.) Your grammar is a reasonably simple example of anLL(1)
grammar which is notSLR(1)
; for an example of anLL(1)
grammar which is notLALR(1)
, see this answer.