Semantic Analysis & Type Checking



Answer & solution
Answer: Each production adds 1 to the sum of its nonterminal children’s red attributes; the given input has E.red=10. (no official key was published)
Every nonterminal node in a parse tree represents one use of a grammar production, hence one reduction in a bottom-up parse. A terminal contributes no reduction by itself; F→2 or F→4 contributes one. Preserve all original val actions and append:
E → E1 * T : E.red = E1.red + T.red + 1 E → T : E.red = T.red + 1 T → F − T1 : T.red = F.red + T1.red + 1 T → F : T.red = F.red + 1 F → 2 : F.red = 1 F → 4 : F.red = 1
These are synthesized because each left-hand attribute depends only on child attributes. No global counter or inherited attribute is needed. In 4−2−4*2, there are four F reductions, four T reductions (two T→F and two T→F−T), and two E reductions (E→T and E→E*T), totaling 10. One bottom-up production sequence is F→4, F→2, F→4, T→F, T→F−T, T→F−T, E→T, F→2, T→F, E→E*T, understood as reductions from right-hand sides to left-hand sides. Parser accept is not an additional grammar reduction, and any augmented start rule is outside the question’s requested reduction to E.