GATE / CS ARCHIVE

GATE CSE 1996 / QUESTION 20

Postfix translation by reduction actions

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

GATE 1996, Q20 5 marks · Descriptive
Report issue

Semantic Analysis & Type Checking

Question 20, GATE 1996: Use the SDTS E→E+E {print “+”}, E→E*E {print “.”}, E→id {print id.name}, E→(E) {no action}. An LR parser executes a production's action immediately after reducing by it. Draw the parse tree and give the translation of (a+b)*(c+d).
Tap to enlarge
Answer & solution

Answer: Literal output ab+cd+. (final dot is emitted); root multiplication with two parenthesized addition subtrees. (no official key was published)

The root production is E→E*E. Its left E derives (E), whose inner E derives E+E with identifier leaves a and b. Its right E derives (E), whose inner E derives E+E with identifier leaves c and d. Thus a textual description of the full parse tree is E(E('(',E(E(id a),'+',E(id b)),')'), '*', E('(',E(E(id c),'+',E(id d)),')')). Parentheses are terminal children of their E→(E) nodes, not emitted characters.

Actions run on reduction, after the right-hand-side subexpressions have been recognized. Reducing id a prints a, reducing id b prints b, and reducing their addition prints +. The parenthesized left operand therefore emits a b +. The E→(E) reduction emits nothing. Similarly the right operand emits c d +. Only after both operand E nodes exist can the root multiplication reduce and print the dot specified by its action.

The literal complete output is a b + c d + ., or ab+cd+. when concatenated without separators. The final dot is an emitted multiplication token, not sentence punctuation. It is postfix notation: each operation follows its operands. As a check, a postfix evaluator pushes a and b, combines them by +, pushes c and d, combines them by +, then multiplies the two resulting values. The grammar is ambiguous in general for unparenthesized mixed operators, but these explicit parentheses fix the intended tree for this particular sentence. Conventional postfix ab+cd+* expresses the same multiplication, but it is not the exact string emitted by the printed SDTS and must not replace it silently.

Original paper, page 9