Chapter 5:Semantic Analysis

Removal of left recursion

Suppose we have translation scheme:

A A1 Y {A = g(A 1 ,Y)}
A X {A = f(X)}

After removal of left recursion it becomes

A X {R.in = f(X)}
R {A.s =R.s}
R Y {R1 .in = g(Y,R)}
R1 {R.s = R1 .s}
R ε {R.s = R.i}

The general scheme for transforming left-recursive translation for predictive parsing is shown below: Suppose we have the following translation scheme :

A -> A1Y {A.s = g(A1.s, Y.s)}
A -> X {A.s = f(X.s)}

Each grammar symbol has a synthesized attribute written using the lower case letter s. (f and g are arbitrary functions.) After removing left recursion from the above grammar and taking semantic actions into account, the transformed scheme becomes:

A -> X {R.in = f(X.s)}
R {A.s = R.s}
R -> Y {R1.in = g(Y.s,R.in)}
R1 {R.s = R1.s}
R ->ε {R.s = R.in}