Your original grammar is ambiguous:
L → L : L
L → id R
Suppose we have id : id : id
. (For simplicity, I'm just letting R
derive the empty string, but any valid derivation of the R
s would end up with the same ambiguity.) That needs to be derived from S → L → L1 : L2
, but from there, either L1
or L2
can derive L : L
. In effect, the two cases correspond to :
associating to the left or associating to the right, but with longer sequences of :
-separated id R
clauses, the number of possible parses increases exponentially.
Your rewrite does not eliminate this ambiguity, it just makes it harder to see.
In short, you need to decide how :
associates. It probably makes most sense for it to associate to the left, but an LL grammar really only handles right-association. (That's not a huge problem since you can reassociate the AST easily enough, but with an LR parser generator you wouldn't have to do any of this.) If we rewrite L
as right-associative, we automatically eliminate the left-recursion problem:
S → L
L → L'
L → L' : L
L' → id R
(Really, L'
is not very useful in this case; it would have been almost as simple to write L → id R | id R L
. But in either case, you end up needing to factor out the common prefix.)