14

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

0

1 Answer 1

27
+50

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!

Sign up to request clarification or add additional context in comments.

8 Comments

well, you helped me a lot, I realised one mistake a do in an analysis in my pdf, but anyway, I found this here and I wanted to prove it to my self, my result is also that it's a valid LARL(1) grammar, so I guess there is a mistake on that website? am I right?
I guess that would be a mistake on the website, since we just built the LALR automaton and had bison check it for us. I would assume that the intent was to find a LALR but not SLR grammar.
well, I figured it out, you miss rules E->e and F->e in your analysis, that's why bison didn't have any error output
@JanVorcak- Answer updated. Can you take a look at this and see if it explains things better?
Is there a typo in the second line of state 10? It reads E -> e. [a], I think it should be F -> e. [a]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.