Chapter 5:Semantic Analysis

Example: desk calculator

L En print(val(top))
E E + T val(ntop) = val(top-2) + val(top)
E T  
T T * F val(ntop) = val(top-2) * val(top)
T F  
F (E) val(ntop) = val(top-1)
F digit  
   

Before reduction ntop = top - r +1

After code reduction top = ntop

The code fragments (like val(ntop) = val(top-1) ) in the implementation of the desk calculator shown in this slide have been obtained from the semantic rules of slide 17 by replacing each attribute by a position in the val array. When a production with r symbols on then right side is reduced, the value of ntop is set to top V r + 1 . After each code fragment is executed, top is set to ntop .