GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Trace recursive macro expansion

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

How these are solved

Both present the same recursive M1/M2 macros and ask which can loop. However 1992 additionally requires an example call;

2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Termination of recursive macro expansion

Compiler Phases, Linking & Loading

Question 2.16, GATE 1996: Macro M1 recursively invokes M1(X+1) when X=0, otherwise emits WORD X. M2 instead recursively invokes M2(X) when X=0. Which can recurse indefinitely?
Question 2.16, GATE 1996: Macro M1 recursively invokes M1(X+1) when X=0, otherwise emits WORD X. M2 instead recursively invokes M2(X) when X=0. Which can recurse indefinitely?
Tap to enlarge
Your answer
Answer & solution

Answer: A: (ii) only (no official key was published)

For M1 invoked with X=0, the recursive call receives 1. Its condition is then false, so it emits WORD 1 and stops. For any nonzero initial value it stops immediately. M2 invoked with X=0 calls itself with the same argument, so the condition never changes and expansion repeats indefinitely. Thus only the second definition has the nontermination problem, option A. This reasoning uses expansion-time integer parameter evaluation; the recursion is in the macro processor, not a runtime subroutine.

Original paper, page 5

Descriptive
Report issue

A recursive macro that fails to progress

Compiler Phases, Linking & Loading

Question 3(i), GATE 1992: M1 calls M1(X+1) when X=0 and otherwise emits WORD X; M2 calls M2(X) when X=0 and otherwise emits WORD X+1. Which loops forever, and give a call.
Question 3(i), GATE 1992: M1 calls M1(X+1) when X=0 and otherwise emits WORD X; M2 calls M2(X) when X=0 and otherwise emits WORD X+1. Which loops forever, and give a call.
Tap to enlarge
Answer & solution

Answer: M2 0 (no official key was published)

For M1 0, the recursive invocation receives 1. Its zero test fails, so it emits a word and stops; a nonzero initial argument likewise stops directly. In M2 0, the recursive call receives exactly 0 again, so every expansion repeats the same zero test and invocation with no progress. This causes unbounded expansion until a practical processor limit intervenes. The nonzero WORD branch cannot rescue that call because it is never reached. The analysis assumes expansion-time arithmetic and equality tests on X as intended.

Original paper, page 4