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.
FIRST(a)
andFIRST(b)
are disjoint. This implies that they cannot both deriveEMPTY
If
b
can deriveEMPTY
, thena
cannot derive any string that begins withFOLLOW(A)
, that isFIRST(a)
andFOLLOW(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?
Consider your grammar:
This is a shorthand for the three rules:
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 haveS
as start symbol):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:
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?OK, I figure it out, if a grammar contains left-recursive production, like:
Then somehow it must contain another production to "finish" the recursion,say:
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.
An
LL(k)
grammar is one that allows the construction of a deterministic, descent parser with onlyk
symbols of lookahead. The problem with left recursion is that it makes it impossible to determine which rule to apply until the complete input string is examined, which makes the requiredk
potentially infinite.Using your example, choose a
k
, and give the parser an input sequence of lengthn >= k
:A parser cannot decide if it should apply
S->SA
orS->empty
by looking at thek
symbols ahead because the decision would depend on how many timesS->SA
has been chosen before, and that is information the parser does not have.The parser would have to choose
S->SA
exactlyn
times andS->empty
once, and it's impossible to decide which is right by looking at the firstk
symbols in the input stream.To know, a parser would have to both examine the complete input sequence, and keep count of how many times
S->SA
has been chosen, but such a parser would fall outside of the definition ofLL(k)
.Note that unlimited lookahead is not a solution because a parser runs on limited resources, so there will always be a finite input sequence of a length large enough to make the parser crash before producing any output.