Is each grammar LL (1) also LR (1)?

Is each grammar LL (1) also LR (1)?

+6
compiler-construction lr
source share
1 answer

Yes, because both LL and LR analyze data from left to right; and since LL (1) looks at only one token, it must be LR (1). This is also true for LR (k), where k> 1, since the grammar LR (k) can be transformed into the grammar LR (1).

The difference between LR and LL grammars is that LR produces the rightmost output, where, since LL produces the leftmost output. Thus, this means that the LR parser can actually parse a larger set than the LL grammar because it is created from leaves.

Suppose we have the settings as follows:

A -> "(" A ")" | "(" ")" 

Then LL (1) will parse the string (()) :

 (()) -> A -> "(" A ")" -> "(" "(" ")" ")" 

Where, when LR (1) will analyze as follows:

 Input Stack Action (()) 0 ()) 0 '(' )) 0 '(' '(' ) 0 '(' '(' ')' Reduce using A -> "(" ")" ) 0 '(' A - 0 '(' A ')' Reduce using A -> "(" A ")" - 0 A Accept 

See more details: http://en.wikipedia.org/wiki/LL_parsing

+8
source share

All Articles