Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is every LL(1) grammar also an LR(1)?

Is every LL(1) grammar also an LR(1)?

like image 855
siddharth Avatar asked Nov 14 '10 01:11

siddharth


People also ask

Does every LL one grammar need not be SLR 1?

Statement 1:Every SLR(1) grammar is unambiguous but there are certain unambiguous grammars that are not SLR(1). As you can see in the diagram Not all Unambiguous grammars are SLR(1) but All SLR(1) grammars are Unambiguous. This Statement is True.

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 an LL 1 grammar when the grammar is said to be LL 1 grammar?

LL(1) GRAMMARS AND LANGUAGES. A context-free grammar G = (VT, VN, S, P) whose parsing table has no multiple entries is said to be LL(1).


1 Answers

Yes, since both LL and LR parse the data from Left to Right; and since LL(1) looks ahead only one token it must necessarily be an LR(1). This is also true for LR(k), where k > 1, since an LR(k) grammar can be transformed into a LR(1) grammar.

The difference between LR and LL grammars comes in that LR produces the rightmost derivation, where as LL produces the leftmost derivation. So this means that an LR parser can in fact parse a greater set than an LL grammar as it builds up from the leaves.

Lets say we have productions as follows:

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

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

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

Where as the LR(1) will parse as follows:

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

For more info see: http://en.wikipedia.org/wiki/LL_parsing

like image 80
Mike Avatar answered Nov 08 '22 15:11

Mike