Example for LL (1) grammar, which is NOT LALR?

Now I am studying parsers in my compilation theory course. I need to find an example for a grammar that is in LL (1) but not in LALR. I know that this must exist. Please help me come up with the simplest example of this problem.

+6
parsing compilation
source share
2 answers

Some searches lead this example for non-LALR (1) grammar, which is LL (1):

S ::= '(' X | E ']' | F ')' X ::= E ')' | F ']' E ::= A F ::= A A ::= Ξ΅ 

The LALR (1) construct fails because there is a reduction reduction conflict between E and F. In the set of LR (0) states, there is a state consisting of

 E ::= A . ; F ::= A . ; 

which is necessary for both S and X contexts. LALR (1) lookahead sets for these elements, thus mixing tokens originating from S and X productions. This is different for LR (1), where different states exist for these cases.

With LL (1), decisions are made by looking for FIRST sets of alternatives, where ')' and ']' are always found in different alternatives.

+13
source share

From the Dragon Book (Second Edition, p. 242):

A grammar class that can be analyzed using LR methods is its own super-set of grammar classes that can be analyzed using predictive or LL methods. For the grammar, which should be LR (k), we must be able to recognize the appearance of the right side of the work in the right form, with k lookahead input characters. This requirement is much less stringent than for LL (k) grammars, where we must be able to recognize the use of the work, seeing only the first k characters of what is obtained from the right side. Thus, it is not surprising that LR grammars can describe more languages ​​than LL grammars.

+1
source share

All Articles