I've seen this algorithm one should be able to use to remove all left recursion. Yet I'm running into problems with this particular grammar:
A -> Cd
B -> Ce
C -> A | B | f
Whatever I try I end up in loops or with a grammar that is still indirect left recursive.
What are the steps to properly implement this algorithm on this grammar?
Figured it out already.
My confusion was that in this order, the algorithm seemed to do nothing, so I figured that must be wrong, and started replacing A -> Cd in the first iteration (ignoring j cannot go beyond i) getting into infinite loops.
1) By reordering the rules:
2) replace C in A -> Cd
3) B not yet in range of j, so leave that and replace direct left recursion of A
4) replace C in B -> Ce
5) not done yet! also need to replace the new rule B -> Ae (production of A is in range of j)
6) replace direct left recursion in productions of B
woohoo! left-recursion free grammar!
Rule is that you first establish some kind of order for non-terminals, and then find all paths where indirect recursion happens.
In this case order would be A < B < C, and possible paths for recursion of non-terminal C would be
and
so new rules for C would be
now you can simply just remove direct left recursion:
and the resulting non-recursive grammar would be: