How to determine if the grammar is LL (1), LR (0) or SLR (1)?

How do you determine if a grammar is LL (1), LR (0), or SLR (1)?

Can someone explain this using this example or any other example?

X → Yz | but

Y → bZ | & Epsilon;

Z → & Epsilon;

+45
parsing grammar ll lr
Dec 13 2018-11-21T00:
source share
4 answers

To check if LL (1) is a grammar, one option is to create an LL (1) parsing table and check for any conflicts. These conflicts may be

  • FIRST / FIRST conflicts, where two different settings for a pair with nonterminal / final should be predicted.
  • FIRST / FOLLOW, where two different settings are predicted, one of which means that it is necessary to produce some products and expand them to a non-zero number of characters, and one of them means that production should be used indicating that some non-terminals must be in the final account expanded to an empty string.
  • FOLLOW / FOLLOW conflicts, in which two products indicating that the nonterminal should ultimately be expanded to empty lines conflict with each other.

Try this on your grammar by building FIRST and FOLLOW for each of the non-terminals. 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 construct the following LL (1) parsing table:

  abz $ X a Yz Yz Y bZ eps Z eps 

Since we can build this conflict-free parsing table, LL grammar (1).

To check if the grammar is LR (0) or SLR (1), we start by creating all the configuration sets LR (0) for the grammar. In this case, assuming that X is your starting character, we get the following:

 (1) X' -> .X X -> .Yz X -> .a Y -> . Y -> .bZ (2) X' -> X. (3) X -> Yz (4) X -> Yz. (5) X -> a. (6) Y -> bZ Z -> . (7) Y -> bZ. 

It can be seen from this that the grammar is not LR (0), since in states (1) and (6) there are shift / decrease conflicts. In particular, because we have the abbreviation elements Z →, and Y →., We cannot determine whether to reduce the empty string to these characters or shift some other character. More generally, no grammar with & epsilon -productions is LR (0).

However, this grammar may be SLR (1). To verify this, we increase each contraction using a set of coordinates for specific non-terminals. This returns this set of SLR (1) configuration sets:

 (1) X' -> .X X -> .Yz [$] X -> .a [$] Y -> . [z] Y -> .bZ [z] (2) X' -> X. (3) X -> Yz [$] (4) X -> Yz. [$] (5) X -> a. [$] (6) Y -> bZ [z] Z -> . [z] (7) Y -> bZ. [z] 

Now we no longer have shift conflicts. The conflict in state (1) was eliminated, because we reduce it only when the lookahead is equal to z, which does not conflict with any of the other elements. Similarly, the conflict in (6) went away for the same reason.

Hope this helps!

+80
Dec 13 '11 at 10:03
source share

Unless you have FIRST / FIRST conflicts and FIRST / FOLLOW conflicts, your LL grammar (1).

An example of a FIRST / FIRST conflict:

 S -> Xb | Yc X -> a Y -> a 

When you see only the first input character a, you cannot know whether to use the production S → Xb or S → Yc, because a is in the FIRST set of both X and Y.

An example of a FIRST / FOLLOW conflict:

 S -> AB A -> fe | epsilon B -> fg 

Seeing only the first input character f, you cannot decide whether to use the production A → fe or → epsilon, since f is in both the FIRST set A and in the FOLLOW set A (A can be analyzed as epsilon and B as f )

Please note: if you do not have epsilon production, you cannot have a FIRST / FOLLOW conflict.

+7
Jun 11 '13 at 15:01
source share

The simple answer is: the grammar is called LL (1) if the corresponding parse table LL (1) has the most purely one statement in each table entry.

 Take the simple grammar A -->Aa|b.[A is non-terminal & a,b are terminals] then find the First and follow sets A. First{A}={b}. Follow{A}={$,a}. Parsing table for Our grammar.Terminals as columns and Nonterminal S as a row element. ab $ -------------------------------------------- S | A-->a | | A-->Aa. | -------------------------------------------- 

Since [S, b] contains two Productions, there is confusion about which rule to choose. So this is not LL (1).

Some simple checks to see if the grammar is LL (1) or not. Check 1 . Grammar should not remain recursive. Example: E → E + T. Not LL (1), because it is left recursive. Check 2 . Grammar should be left.

Left factoring is required if two or more variants of grammar rules have a common prefix string. Example: S → A + int | A.

Check 3 . Grammar should not be ambiguous.

 These are some simple checks. 
+2
Aug 05 '15 at 11:45
source share

An LL (1) grammar is a context free, unambiguous grammar that can be analyzed by LL (1) parsers.

In LL (1)

  • The first L denotes the scan input from left to right. The second stand L for the left workout. 1 means using one input character at each step.

To test the grammar of LL (1), you can draw a table of predictive parsing. And if you find several entries in the table, you can say that the grammar is not LL (1).

They are also abbreviated to check whether the grammar is LL (1) or not. Label Technique

+1
May 01 '16 at 12:39
source share



All Articles