Chapter 5:Semantic Analysis

 

INPUT STATE Val PRODUCTION
3*5+4n      
*5+4n digit 3  
*5+4n F 3 F digit
*5+4n T 3 T F
5+4n T* 3 -  
+4n T*digit 3 - 5  
+4n T*F 3 - 5 F digit
+4n T 15 T T * F
+4n E 15 E T
4n E+ 15 -  
n E+digit 15 - 4  
n E+F 15 - 4 F digit
n E+T 15 - 4 T F
n E 19 E E +T

This slide shows the evaluation of the synthesized attributes of 3*5+4n by an LR parser during a bottom up pass. Initial sequence of events on seeing the input : The parser shifts the state corresponding to token digit (taken as digit here) onto the stack. In the second move, the parser reduces by the production F -> digit. It implements the semantic rule F.val = digit.lexval. In the third move, the parser reduces by T -> F. No code fragment is associated so the val array is unchanged. Rest of the implementation goes on similar lines. After each reduction, the top of the val stack contains the attribute value associated with the left side of the reducing production.