Intermediate Code Generation

GATE CS previous year questions · 3 questions

GATE CS 2025 Set 2 | Question 21

MCQ · 1 mark · −0.33

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?

Options for GATE CS 2025 Set 2 | Question 21

Explanation

Answer: (C)

In one-pass code generation, a jump is often emitted before the address it should jump to is known. Backpatching handles this: the jump is emitted with an empty target, its position is added to a list, and every jump on the list is filled in once the target becomes known.

  • (I) is correct. For Boolean expressions with short-circuit evaluation, the jumps taken when the expression is true or false are collected in truelist and falselist and patched later.
  • (II) is correct. If, if-else and while statements patch their jumps the same way, using these lists and a nextlist, without a second pass.

Answer: C

GATE CS 2024 Set 2 | Question 21

MCQ · 1 mark · −0.33

Consider the following two sets:

Set XSet Y
P. Lexical Analyzer1. Abstract Syntax Tree
Q. Syntax Analyzer2. Token
R. Intermediate Code Generator3. Parse Tree
S. Code Optimizer4. Constant Folding

Which one of the following options is the CORRECT match from Set X to Set Y ?

Options for GATE CS 2024 Set 2 | Question 21

Explanation

Answer: (B)

Match each phase with what it produces or does:

  • P → 2. The lexical analyzer groups the input characters into lexemes and passes them on as tokens.
  • Q → 3. The syntax analyzer checks the token stream against the grammar; the derivation it finds is a parse tree.
  • R → 1. An abstract syntax tree, a compact tree of operators and operands without the parse tree's extra grammar nodes, is a common intermediate representation.
  • S → 4. Constant folding, such as replacing 2 * 3 by 6 at compile time, is a code optimization.

This gives P – 2; Q – 3; R – 1; S – 4. Option C swaps the two trees, and A and D misplace tokens and constant folding.

Answer: B

GATE CS 2024 Set 2 | Question 43

MCQ · 2 marks · −0.66

Consider the following expression: \(x[i] = (p + r) * -s[i] + u/w\). The following sequence shows the list of triples representing the given expression, with entries missing for triples (1), (3), and (6).

(0)+\(p\)\(r\)
(1)
(2)uminus(1)
(3)
(4)/\(u\)\(w\)
(5)+(3)(4)
(6)
(7)=(6)(5)

Which one of the following options fills in the missing entries CORRECTLY?

Options for GATE CS 2024 Set 2 | Question 43

Explanation

Answer: (A)

Triples have no result names: an operand is either a name or the number of an earlier triple.

  • (1) =[] s i reads the element \(s[i]\), the value that (2) negates. The operator []= stores into an array element and produces no value for uminus to use.
  • (3) * (0) (2) multiplies \(p + r\) by \(-s[i]\), which (5) then adds to \(u/w\). The expression contains no subtraction, so the options with – fail here too.
  • (6) []= x i marks the array location \(x[i]\), and (7) assigns the value of (5) to it. Putting (5) inside (6) would lose the index \(i\) and duplicate the work of (7).

Answer: A