GATE / CS ARCHIVE

GATE CSE 2003 / QUESTION 58

Output order of embedded translation actions

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

GATE 2003, Q58 2 marks · MCQ
Report issue

Semantic Analysis & Type Checking

Question 58, GATE 2003: A translation scheme is S→TR; R→+ T {print('+');} R | ε; T→num {print(num.val);}. Token num represents an integer, and num.val is its value. For input 9+5+2, what is printed?
A. 9+5+2
B. 95+2+
C. 952++
D. ++952
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step through it (19 steps)

1. Expand S→TR. The initial T consumes 9 and immediately prints 9.
2. The first R consumes + and invokes T on 5, printing 5. Its following action prints + before the recursive R begins.
3. That recursive R consumes +, invokes T on 2 and prints 2, then prints +. Its final R chooses ε and prints nothing.
4. Concatenating the actions yields 9,5,+,2,+, or 95+2+, B. Moving the plus action to after the recursive R would change the result, but that is not this scheme.

Solution: The initial T prints 9. Each R expansion reads +, processes T (which prints the next number), prints +, and only then processes the remaining R. Hence the execution order is 9,5,+,2,+, yielding 95+2+, option B. The action is before the recursive R, so it does not postpone both plus signs to the end.

Original paper, page 8