E → E + A | A
A → A B | B
B → B # | C
C → a | b
We will eliminate left recursion step-by-step from each non-terminal using the standard rule:
If a production has the form:
A → A α | β
, then
Convert to:A → β A'
A' → α A' | ε
Step 1: Eliminate Left Recursion from
E → E + A | A
This has direct left recursion.
Match the rule:
α = + A
(left recursive part)β = A
(non-left recursive part)
Apply the rule:
E → A E’
E’ → + A E’ | ε
Step 2: Eliminate Left Recursion from
A → A B | B
This is also direct left recursion.
Match the rule:
α = B
β = B
Apply the rule:
A → B A'
A' → B A' | ε
Step 3: Eliminate Left Recursion from
B → B # | C
Again, this is direct left recursion.
Match the rule:
α = #
β = C
Apply the rule:
B → C B'
B' → # B' | ε
Step 4: C → a | b
No left recursion here. Keep as it is C → a | b
Final Grammar after Removing Left Recursion:
E → A E’
E’ → + A E’ | ε
A → B A’
A’ → B A’ | ε
B → C B’
B’ → # B’ | ε
C → a | b
Summary Table Format (Easy to remember):
Original Rule | Left Recursion Eliminated Form |
---|---|
E → E + A | A | E → A E’ E’ → + A E’ | ε |
A → A B | B | A → B A’ A’ → B A’ | ε |
B → B # | C | B → C B’ B’ → # B’ | ε |
C → a | b | (No change) C → a | b |