Question

The following grammar has left recursion.

X is the start simbol.

X -> Xa | Zb
Z -> Zc | d | Xa

How to remove it? Please explain it step by step.

Was it helpful?

Solution

-- remove Z left recursion--

X -> Xa | Zb
Z -> dZ' | XaZ'
Z' -> cZ' | empty

--next remove Z --

X -> Xa | dZ'b | XaZ'b
Z' -> cZ' | empty

--next factorize X--

X -> XaX' | dZ'b
X'-> Z'b | empty
Z'-> cZ' | empty

--next remove X recursion--

X -> dZ'bX''
X'' -> a X'X'' | empty
X' -> Z'b | empty
Z' -> cZ' | empty
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top