GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Trace printing actions on reductions

3 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Build reductions for the supplied string and execute end-of-production print actions in reduction order. Grammars/strings/actions differ; embedded mid-production actions are separate.

2 marks · MCQ
Report issue

Semantic Analysis & Type Checking

Question 46, GATE 2016: A syntax-directed translation scheme has S→aA {print 1}, S→a {print 2}, A→Sb {print 3}. A bottom-up parser processes input aab. What is the output? A:1 3 2. B:2 2 3. C:2 3 1. D:syntax error.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (8 steps)

Bottom-up parsing of aab first reduces the second a by S→a, printing 2. Then Sb reduces to A, printing 3. Finally aA reduces to S, printing 1. The action order is 2, 3, 1, option C.

Reduction order follows a rightmost derivation in reverse, not the textual order in which productions are listed. A valid derivation is S⇒aA⇒aSb⇒aab. Reversing those steps first collapses the second a into S, then Sb into A, then aA into S. Each semantic action is at the end of its production, so it runs at the corresponding reduction and emits 2,3,1. The successful derivation also rules out syntax error.

Original paper, page 18 · Answer key

5 marks · Descriptive
Report issue

Postfix translation by reduction actions

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

2 marks · MCQ
Report issue

Reduction-time translation

Semantic Analysis & Type Checking

Question 2.10, GATE 1995: Grammar S→xxW {print1}, S→y {print2}, W→Sz {print3}. What is printed by a shift-reduce parse of xxxxyzz?
Tap to enlarge
Your answer
Answer & solution

Answer: A: 23131 (no official key was published)

Step through it (14 steps)

The innermost y reduces to S first, printing 2. That S followed by z reduces to W, printing 3. The preceding xx and this W reduce to S, printing 1. The remaining outer z then permits W→Sz, printing another 3, and the remaining outer xxW reduces to S, printing the last 1. Thus the output is 23131. Actions occur after reductions, giving inside-out order rather than the order of productions in a top-down derivation.

Original paper, page 4