GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 19(b)

Synthesize reduction-count attributes

Compiler Design · Semantic Analysis & Type Checking · Full GATE CSE 2000 paper

GATE 2000, Q19(b) Descriptive
Report issue

Semantic Analysis & Type Checking

Question 19(b), GATE 2000: Grammar/SDTS: E→E1*T {E.val=E1.val*T.val}; E→T {E.val=T.val}; T→F−T1 {T.val=F.val−T1.val}; T→F {T.val=F.val}; F→2 {F.val=2}; F→4 {F.val=4}. Attributes are evaluated immediately after bottom-up reductions. Add synthesized attributes to compute E.red, the total reductions without changing the grammar.
Question 19(b), GATE 2000: Grammar/SDTS: E→E1*T {E.val=E1.val*T.val}; E→T {E.val=T.val}; T→F−T1 {T.val=F.val−T1.val}; T→F {T.val=F.val}; F→2 {F.val=2}; F→4 {F.val=4}. Attributes are evaluated immediately after bottom-up reductions. Add synthesized attributes to compute E.red, the total reductions without changing the grammar.
Question 19(b), GATE 2000: Grammar/SDTS: E→E1*T {E.val=E1.val*T.val}; E→T {E.val=T.val}; T→F−T1 {T.val=F.val−T1.val}; T→F {T.val=F.val}; F→2 {F.val=2}; F→4 {F.val=4}. Attributes are evaluated immediately after bottom-up reductions. Add synthesized attributes to compute E.red, the total reductions without changing the grammar.
Tap to enlarge
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.

Original paper, page 9