Similar questions 3 more →
Semantic Analysis & Type Checking

Answer & solution
Official answer: A
Step through it (14 steps)
Approach: Split the input according to the grammar and evaluate synthesized attributes from each terminating production upward.
1. D consumes the two Ms: D→M D1→M M D2→M M ε. The bottom value is −5, so D1.val=5−5=0 and D.val=5+0=5.
2. H consumes the single L. Its recursive rule contributes 50, then its epsilon rule contributes −10. Hence H.val=50−10=40.
3. There is no C between L and K, so T immediately uses T→ε. This still assigns T.val=−5; generating no terminal symbols does not mean an attribute value of zero.
4. U consumes K and has U.val=5. The start rule sums the values: S.val=5+40−5+5=45.
Answer: A. The three distinct epsilon base values are part of the semantics and must be applied even when their terminal run is empty.