Eliminate left recursion for E: = EE + | EE- | id

How to eliminate left recursion for the next grammar?

E := EE+|EE-|id

Using the general procedure:

A := Aa|b

translates to:

A := b|A'
A' := ϵ| Aa 

Applying this to the original grammar, we get:

A = E, a = (E+|E-) and b = id

Thus:

E := id|E'
E' := ϵ|E(E+|E-)

But this grammar seems wrong because

ϵE+ -> ϵ id +

will be valid but this is an incorrect postfix expression.

+3
source share
1 answer

Your "general procedure" is quoted incorrectly. From the book of the Dragon:

A := Aα | β

becomes

A  := βA′
A′ := αA′ | ϵ

... what gives:

E  := id E′
E′ := (E + | E -) E′ | ϵ
+10
source

Source: https://habr.com/ru/post/1721014/


All Articles