Designing Compilers: Eliminating left recursion, special case

60 Views Asked by At

Eliminating left recursion is a technique in designing compilers, as discussed here

Examples that I have seen were like this:

S -> Aa | b
A -> Sb | Ag

that is, one expression has just one of the non terminal and also not itself. I know how to solve this kind of left recursion example and I have found several tutorials on this.

What about if in the first expression we also have a non terminal that is the left non terminal, I mean something like the grammar below:

A -> Ba | Aa | c
B -> Bb | Ab | d

That is, in both expressions we have a recursion to both A and B.

In the first example I provided, we simply substitute "S" with "Aa | b" in the second expression so we will obtain " A -> Aab | bb | Aag | bg " and then we can simply solve it with the rules defined in the tutorial that I have linked above.

But for the second expression, substitution wont work. So in this case, what is the best way to solve it?

Thank you in advance.