Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a LL grammar be left-recursive?

In the dragon book, LL grammar is defined as follows:

A grammar is LL if and only if for any production A -> a|b, the following two conditions apply.

  1. FIRST(a) and FIRST(b) are disjoint. This implies that they cannot both derive EMPTY

  2. If b can derive EMPTY, then a cannot derive any string that begins with FOLLOW(A), that is FIRST(a) and FOLLOW(A) must be disjoint.

And I know that LL grammar can't be left recursive, but what is the formal reason? I guess left-recursive grammar will contradict rule 2, right? e.g., I've written following grammar:

S->SA|empty
A->a

Because FIRST(SA) = {a, empty} and FOLLOW(S) ={$, a}, then FIRST(SA) and FOLLOW(S) are not disjoint, so this grammar is not LL. But I don't know if it is the left-recursion make FIRST(SA) and FOLLOW(S) not disjoint, or there is some other reason? Put it in another way, is it true that every left-recursive grammar will have a production that will violate condition 2 of LL grammar?

like image 775
wangshuaijie Avatar asked Apr 23 '13 09:04

wangshuaijie


People also ask

Can an LL 1 grammar be left recursive?

If a grammar contain left recursion it can not be LL(1) Eg - S -> Sa | b S -> Sa goes to FIRST(S) = b S -> b goes to b, thus b has 2 entries hence not LL(1) 3. If a grammar is ambiguous then it can not be LL(1) 4.

Is left recursion a problem for LL parsers?

Left recursion often poses problems for parsers, either because it leads them into infinite recursion (as in the case of most top-down parsers) or because they expect rules in a normal form that forbids it (as in the case of many bottom-up parsers, including the CYK algorithm).

Why left recursive grammar Cannot be used in top-down parsing?

Left recursion is a problem in top-down parsers because top down parsers use left-most derivation to derive the required string by using the start symbol of the grammar. Due to this reason top-down parsers might go into infinite loop with left recursive grammar.

How do you check if a grammar is left recursive or not?

A Grammar G (V, T, P, S) is left recursive if it has a production in the form. A → A α |β. Left Recursion can be eliminated by introducing new non-terminal A such that. This type of recursion is also called Immediate Left Recursion.


2 Answers

OK, I figure it out, if a grammar contains left-recursive production, like:

S->SA

Then somehow it must contain another production to "finish" the recursion,say:

S->B

And since FIRST(B) is a subset of FIRST(SA), so they are joint, this violates condition 1, there must be conflict when filling parse table entries corresponding to terminals both in FIRST(B) and FIRST(SA). To summarize, left-recursion grammar could cause FIRST set of two or more productions to have common terminals, thus violating condition 1.

like image 60
wangshuaijie Avatar answered Sep 18 '22 21:09

wangshuaijie


Consider your grammar:

S->SA|empty
A->a

This is a shorthand for the three rules:

S -> SA
S -> empty
A -> a

Now consider the string aaa. How was it produced? You can only read one character at a time if you have no lookahead, so you start off like this (you have S as start symbol):

S -> SA
S -> empty
A -> a

Fine, you have produced the first a. But now you cannot apply any more rules because there is no more non-terminals. You are stuck!

What you should have done was this:

S -> SA
S -> SA
S -> SA
S -> empty
A -> a
A -> a
A -> a

But you don't know this without reading the entire string. You would need an infinite amount of lookahead.

In a general sense, yes, every left-recursive grammar can have ambiguous strings without infinite lookahead. Look at the example again: There are two different rules for S. Which one should we use?

like image 20
Emil Vikström Avatar answered Sep 20 '22 21:09

Emil Vikström