How do you identify whether a grammar is LL(1), LR(0), or SLR(1)?
Can anyone please explain it using this example, or any other example?
X → Yz | a
Y → bZ | ε
Z → ε
A grammar is said to be SLR(1) if the following simple LR parser algorithm results in no ambiguity.
A grammar whose parsing table has no multiply-defined en- tries is said to be LL(1) which stands for: scanning the input from Left to right producing a Leftmost derivation and using 1 input symbol of lookahead at each step to make parsing action decisions.
The only difference between LR(0) and SLR(1) is this extra ability to help decide what action to take when there are conflicts. Because of this, any grammar that can be parsed by an LR(0) parser can be parsed by an SLR(1) parser. However, SLR(1) parsers can parse a larger number of grammars than LR(0).
To check if a grammar is LL(1), one option is to construct the LL(1) parsing table and check for any conflicts. These conflicts can be
Let's try this on your grammar by building the FIRST and FOLLOW sets for each of the nonterminals. Here, we get that
FIRST(X) = {a, b, z} FIRST(Y) = {b, epsilon} FIRST(Z) = {epsilon}
We also have that the FOLLOW sets are
FOLLOW(X) = {$} FOLLOW(Y) = {z} FOLLOW(Z) = {z}
From this, we can build the following LL(1) parsing table:
a b z $ X a Yz Yz Y bZ eps Z eps
Since we can build this parsing table with no conflicts, the grammar is LL(1).
To check if a grammar is LR(0) or SLR(1), we begin by building up all of the LR(0) configurating sets for the grammar. In this case, assuming that X is your start symbol, we get the following:
(1) X' -> .X X -> .Yz X -> .a Y -> . Y -> .bZ (2) X' -> X. (3) X -> Y.z (4) X -> Yz. (5) X -> a. (6) Y -> b.Z Z -> . (7) Y -> bZ.
From this, we can see that the grammar is not LR(0) because there is a shift/reduce conflicts in state (1). Specifically, because we have the shift item X → .a and Y → ., we can't tell whether to shift the a or reduce the empty string. More generally, no grammar with ε-productions is LR(0).
However, this grammar might be SLR(1). To see this, we augment each reduction with the lookahead set for the particular nonterminals. This gives back this set of SLR(1) configurating sets:
(1) X' -> .X X -> .Yz [$] X -> .a [$] Y -> . [z] Y -> .bZ [z] (2) X' -> X. (3) X -> Y.z [$] (4) X -> Yz. [$] (5) X -> a. [$] (6) Y -> b.Z [z] Z -> . [z] (7) Y -> bZ. [z]
The shift/reduce conflict in state (1) has been eliminated because we only reduce when the lookahead is z, which doesn't conflict with any of the other items.
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