Chapter 5:Semantic Analysis

Algorithm .

. If the reduction is to a marker M j and the marker belongs to a production

A M1 X1 . Mn X n then

Ai is in position top-2j+2

X 1.i is in position top-2j+3

X1.s is in position top-2j+4

. If reduction is to a non terminal A by production A M 1 X 1 . M n Xn then compute A s and push on the stack

If the reduction is to a marker Mj and the marker belongs to a production A -> M1X1.MnXn then A.i is in position top-2j+2, X1.i is in position top-2j+3 and X1.s is in position top-2j+4. If reduction is to a non-terminal A by production A -> M1X1.MnXn, then compute A.s and push on the stack.

This algorithm is valid only if the input grammar is LL(1), or else we might not be sure that we were reducing to one particular marker non- terminal, and thus could not locate the proper attributes, or even know what formula to apply in general. To reduce the number of markers, we apply the following simplifications:

(a) If Xj has no inherited attribute, we need not use marker Mj and we incorporate the expected positions for the attributes on the stack accordingly.

(b) If X1->i exists, but is computed by a copy rule X1->i A.i, then we can omit M1, since A.i will already be located just below X1 on the stack, and this value can serve for X.i as well.