Chapter 5:Semantic Analysis

Eliminate left recursion

 

E T {R.i = T.val}
  R {E.val = R.s}
R +  
  T {R 1 .i = R.i + T.val}
  R 1 {R.s = R1.s}
R -  
  T {R 1 .i =R.i - T.val}
  R 1 {R.s = R1.s}
R ε {R.s = R.i}
T (E) T.val = E.val}
T num {T.val = num.lexval}
     

To remove left recursion, we use our earlier rule of removing left recursion and rewrite the attribute equations in a systematic manner. We look at a particular solution for this example, a general algorithm will be discussed later. Our old attribute equations are no longer valid. Some new symbols have been added. We say R inherited is T val. What we are trying to do is to take the attribute of the left subtree below. We just keep taking attribute form the left subtree to a right and lower subtree until we hit the last production (R??).