Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify whether a grammar is LL(1), LR(0) or SLR(1)?

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 → ε

like image 875
Prashant Bhardwaj Avatar asked Dec 13 '11 21:12

Prashant Bhardwaj


People also ask

How do you know if grammar is SLR?

A grammar is said to be SLR(1) if the following simple LR parser algorithm results in no ambiguity.

When the grammar is said to be LL 1 or LR 1?

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.

What is the difference between SLR 1 and LR 0?

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


1 Answers

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

  • FIRST/FIRST conflicts, where two different productions would have to be predicted for a nonterminal/terminal pair.
  • FIRST/FOLLOW conflicts, where two different productions are predicted, one representing that some production should be taken and expands out to a nonzero number of symbols, and one representing that a production should be used indicating that some nonterminal should be ultimately expanded out to the empty string.
  • FOLLOW/FOLLOW conflicts, where two productions indicating that a nonterminal should ultimately be expanded to the empty string conflict with one another.

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!

like image 122
templatetypedef Avatar answered Sep 24 '22 07:09

templatetypedef