Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing left recursion

The following grammar has left recursion

E= E+T|T
T= T*F|F
F= a|b|c

How to remove it? Is there any general procedure for it?

like image 270
Prasoon Saurav Avatar asked Apr 16 '10 10:04

Prasoon Saurav


People also ask

Why do we need to remove left recursion?

Removing left recursion Left recursion often poses problems for parsers, either because it leads them into infinite recursion (as in the case of most top-down parsers) or because they expect rules in a normal form that forbids it (as in the case of many bottom-up parsers, including the CYK algorithm).

What is the problem with left recursion in compiler design?

In the production rule above, the variable in the left side occurs at the first position on the right side production, due to which the left recursion occurs. If we have a left recursion in our grammar, then it leads to infinite recursion, due to which we cannot generate the given string.


1 Answers

Yes, there is a general procedure, see e.g. wikipedia.

E = TE'
E'= (e) | +TE'
T = FT'
T'= (e) | *FT'
F = a | b | c

// (e) is "epsilon" which is defined as the empty string

It should be mentioned that this alters the associativity of + and * from left to right. That is, where before a + b + c was parsed as (a + b) + c, it is now parsed as a + (b + c).

This is not a problem for addition and multiplication, but it would be a problem for subtraction and division, for example.

Wikipedia article has more detail on the left-recursion removal procedure and its intricacies.

like image 108
polygenelubricants Avatar answered Oct 17 '22 11:10

polygenelubricants