This is not my homework, I'm trying to understand LALR(1) grammars. So I found this
S -> aEa | bEb | aFb | bFa
E -> e
F -> e
I wrote the LR items, but I can't figure out why this is an LR(1) grammar and not LALR(1)?
Can anyone help me? Thank you
An LALR(1) parser is an "upgraded" version of an LR(0) parser that keeps track of more precise information to disambiguate the grammar. An LR(1) parser is a significantly more powerful parser that keeps track of even more precise information than an LALR(1) parser.
3 Answers. For LL(1) You have to check by using First and Follow method. For LR(0) and SLR(1) you have to do augmented transition method, and then by making state transition diagram, you have to look where Shift-Reduce and Reduce Reduce conflicts are present and according to that you've to eliminate the parser.
A → a is LALR(1) but not SLR(1). Then, the LALR(1) parsing table can be obtained by merging items with common first components, In this problem, no merging occurs. That is, the final LALR(1) parsing table is the same as the LR(1) one. Thus, the given grammar is LALR(1).
We may inspect the deterministic version of this automaton to determine whether the grammar is LR(1). It is LR(1) only if there are no reduce/reduce or shift/reduce conflicts. Here a shift/reduce conflict happens when a completed item in a state has its "follow token" also occurring as a shift from that state.
Let's begin by constructing LR(1) configurating sets for the grammar:
(1)
S' -> .S [$]
S -> .aEa [$]
S -> .aFb [$]
S -> .bFa [$]
S -> .bEb [$]
(2)
S' -> S. [$]
(3)
S -> a.Ea [$]
S -> a.Fb [$]
E -> .e [a]
F -> .e [b]
(4)
E -> e. [a]
F -> e. [b]
(5)
S -> aE.a [$]
(6)
S -> aEa. [$]
(7)
S -> aF.b [$]
(8)
S -> aFb. [$]
(9)
S -> b.Fa [$]
S -> b.Eb [$]
E -> .e [b]
F -> .e [a]
(10)
E -> e. [b]
F -> e. [a]
(11)
S -> bF.a [$]
(12)
S -> bFa. [$]
(13)
S -> bE.b [$]
(14)
S -> bEb. [$]
If you'll notice, states (4) and (10) have the same core, so in the LALR(1) automaton we'd merge them together to form the new state
(4, 10)
E -> e. [a, b]
F -> e. [a, b]
Which now has a reduce/reduce conflict in it (all conflicts in LALR(1) that weren't present in the LR(1) parser are reduce/reduce, by the way). This accounts for why the grammar is LR(1) but not LALR(1).
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With