IAE Academy

GATE 2025 CS2 Q.21: solution and answer

GATE 2025 · CS2Q.21 · 1 mark · MCQ

Consider the following statements about the use of backpatching in a compiler for intermediate code generation:
(I) Backpatching can be used to generate code for Boolean expression in one pass.
(II) Backpatching can be used to generate code for flow-of-control statements in one pass.
Which ONE of the following options is CORRECT?

  1. (A)

    Only (I) is correct.

  2. (B)

    Only (II) is correct.

  3. (C)

    Both (I) and (II) are correct.

  4. (D)

    Neither (I) nor (II) is correct.

See the question in the paper

Answer and solution

ANSWER

Option (C): both (I) and (II) are correct.

Official answer: CSource: Official answer key, GATE 2025 CS2 (IIT Roorkee)

  1. Problem: in one-pass code generation, a jump to a label that is not yet known cannot be filled in when the jump is emitted.

  2. Backpatching fixes this. Each jump with an unknown target is put on a list, using makelist(i). The list is filled in later with backpatch(list, label), once the target address is known.

  3. Boolean expressions: for the translation keeps and . The marker records the index of the next instruction. Then , and . So statement (I) is true.

  4. Flow-of-control: for we do and . The same pattern handles if-else, while and statement sequences. So statement (II) is true.

  5. Both statements hold, so the answer is (C).

KEY IDEA

Backpatching leaves jump targets blank, records them on truelist, falselist or nextlist, and fills them in once the target is known. This lets a single pass generate code for both Boolean expressions and flow-of-control statements.

WATCH OUT FOR

Thinking backpatching is only for flow-of-control statements, or only for Boolean expressions. It is used for both, and Boolean expressions are where truelist and falselist first appear.

From the GATE 2025 CS2 paper. Every question of this paper · Download the full paper · All 2 Intermediate code questions, with notes