2

I can't seem to understand the difference between LALR(1) and LR(1) except that LALR(1) seems to have fewer states than LR(1) does.

I wonder if anyone has the example to show the difference and some explanation.

Thank you

1 Answer 1

4

There's an example in the Dragon book (Example 4.44; 4.58 if you have the second edition):

S' → S
S  → aAd | bBd | aBe | bAe
A  → c
B  → c

Since the grammar only generates four strings, it's easy enough to create the LR item sets. When you do that, you'll see that there are two sets with the same items but different lookaheads, corresponding to the prefixes ac and bc. There are no conflicts, so the grammar is LR(1).

The LALR algorithm combines states whose items sets are the same, effectively merging their lookaheads. This creates a reduce/reduce conflict, so the grammar is not LALR(1).

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

4 Comments

I have two follow-up questions: 1. can we simplify the grammar to S -> aAd | aBe? If not, why not? 2. Could you elaborate more on what "item sets" and "kernel" mean?
@tanin: no. That's a different grammar (which recognizes the same language but with different derivations/parse trees). Your second question requires a detailed explanation of the LR parsing algorithm, which you can find in any textbook or in various study notes on the internet. I think it's out of scope for a comment on SO...
Sorry, I wasn't clear. I meant whether or not we could simplify the grammar to S -> aAd | aBe, and the newly simplified grammar would still work with LR(1) but not LALR(1)
@Tanin: Ah. No, that grammar is LALR(1).

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.