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?
FIRST[1](SA)
is{a}
. – HomophileLA(S->SA)
andLA(S->e)
both containa
. See my answer for a more intuitive explanation. – Homophile