Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this LR(1) grammar not LALR(1)?

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

like image 566
Jan Vorcak Avatar asked Dec 13 '11 20:12

Jan Vorcak


People also ask

Is LALR and LR 1 Same?

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.

How do you know if a grammar is LALR 1?

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.

Is the grammar LR 1 LALR 1 SLR 1?

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).

When the grammar is said to be LR 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.


1 Answers

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!

like image 108
templatetypedef Avatar answered Sep 23 '22 06:09

templatetypedef