GATE / CS ARCHIVE

SUBJECT COLLECTION

Compiler Design · Intermediate Code

15 questions, newest first

GATE CSE has asked 15 questions on Intermediate Code (Compiler Design) from 1992 to 2025, in 12 of the 46 papers; the most recent is in GATE CSE 2025 Set 2. In the last five years it carried 0.4 marks per 100-mark paper on average, against 0.5 since 2003. Since 2014 the questions have been 6 MCQ, 3 NAT and 1 MSQ. 1 group of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
1 mark · MCQ
Report issue

Intermediate Code

Question 21, GATE 2025: For compiler intermediate-code generation, consider: I. backpatching can generate code for Boolean expressions in one pass; II. backpatching can generate code for flow-of-control statements in one pass. Which is correct?
Options: A. I only; B. II only; C. both; D. neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

A branch target may not be known when its branch instruction is first generated. Backpatching emits the instruction with a placeholder target and records its location on a list. Later, when the target's instruction address becomes known, the compiler fills that field without rereading the source.

For a Boolean expression such as a<b && c<d, the compiler can keep true and false lists. A true branch from the first comparison is patched to the start of the second comparison; the final true/false branches are patched to the enclosing statement's destinations. This supports short-circuit Boolean code in one pass, so I is true.

For an if, while, or similar statement, next lists and branch lists are patched when the following block, loop test, or exit becomes known. Thus flow-of-control code can also be generated in one pass, making II true. “One pass” does not prohibit editing previously emitted target fields; that editing is precisely the role of backpatching. Answer: C.

Original paper, page 18 · Answer key

2 marks · MCQ
Report issue

Fill array-load, multiplication and destination triples

Intermediate Code

Question 43, GATE 2024: Expression x[i]=(p+r)*−s[i]+u/w has triples (0) + p r; (1) missing; (2) uminus (1); (3) missing; (4) / u w; (5) + (3) (4); (6) missing; (7) = (6) (5). A fills (1) =[] s i, (3) * (0) (2), (6) []= x i. B uses []= s i, subtraction, =[] x (5). C agrees with A except []= x (5). D uses []= s i, subtraction, =[] x i.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Approach: Follow the expression tree from input values to the final indexed assignment.

1. Derivation. Triple1 must read s[i], denoted =[] s i. Triple2 negates that value. Triple3 multiplies results(0) and(2), forming(p+r)×−s[i]. Triple6 identifies the destination x[i], denoted []= x i, and triple 7 stores the accumulated result(5) there. Only A has all three operations and the correct index i. In C the destination incorrectly uses the computed value(5) as its index.

2. Explanation and checks. A triple refers to the result of an earlier triple by its parenthesized index; (5), for example, denotes the computed right-hand-side value, not the source-language subscript i. Loading s[i] must precede negation. Multiplication then combines p+r with that negative value, followed by addition of u/w. The final destination must still be element i of x. Confusing an array read with a destination reference or substituting the computed value for i changes the program.

Original paper, page 30 · Answer key

1 mark · MSQ
Report issue

Intermediate Code

Question 13, GATE 2021: Which is NOT an intermediate representation of source computation? A three-address code; B abstract syntax tree; C control-flow graph; D symbol table. Multiple selections are permitted.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: D

Three-address code, an abstract syntax tree, and a control-flow graph represent program computations or structure. A symbol table instead records information about names, types, scopes, and bindings. It supports analysis but is not an intermediate representation of the source computation. Thus D.

For an assignment x=a+b, three-address code records the operation, an AST records its expression structure, and a CFG records possible control transfers around it. The symbol table instead stores facts such as x's declared type or scope. A compiler may use all four structures; “used by a compiler” is broader than “intermediate representation.”

Original paper, page 62 · Answer key

1 mark · MCQ
Report issue

Intermediate Code

Question 12, GATE 2017: Convert p=a−b; q=p*c; p=u*v; q=p+q to static single assignment form. Options: A p1=a−b;q1=p1*c;p1=u*v;q1=p1+q1. B p3=a−b;q4=p3*c;p4=u*v;q5=p4+q4. C p1=a−b;q1=p2*c;p3=u*v;q2=p4+q3. D p1=a−b;q1=p*c;p2=u*v;q2=p+q.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. SSA requires each defined variable version to have exactly one assignment. Subscripts need not start at 1, but every use must refer to the correct reaching definition.

2. First create p3=a−b; then q4=p3*c must use that version. The reassignment p=u*v needs a different version, p4.

3. The last sum uses new p and old q: q5=p4+q4. This is option B. A repeats definitions, C uses undefined versions, and D leaves unversioned references.

Original paper, page 7 · Answer key

2 marks · NAT
Report issue

Intermediate Code

Question 43, GATE 2017: Grammar: stmt→if expr then expr else expr; stmt | δ; expr→term relop term | term; term→id | number; id→a|b|c; number→[0–9]. δ is the empty statement and relop a relational operator. A program contains ten if terminals. Find its number of control-flow paths, counting the two branches of one if as two paths.
Tap to enlarge
Answer & solution

Official answer: 1024.0

Our answer: 1024

1. The stmt grammar appends one if statement followed by another stmt. Thus ten if terminals describe ten sequential binary choices, not a nesting with an unspecified shape.

2. After the first if there are two structural paths. Each subsequent if doubles the existing path count because every current path can take either its then or else branch.

3. Therefore P0=1 and Pn=2Pn−1, giving P10=210=1024. This counts syntactic control-flow graph paths, as the question’s example does; repeated or constant conditions can make some paths infeasible for actual executions.

Original paper, page 19 · Answer key

1 mark · NAT
Report issue

Intermediate Code

Question 19, GATE 2016: Find the minimum total number of variable names in static single-assignment form for this straight-line code, including incoming variables:
x=u−t;
y=x*v;
x=y+w;
y=t−z;
y=x*y;
Tap to enlarge
Answer & solution

Official answer: 10.0 : 10.0

Our answer: 10

The five incoming values u, t, v, w, z each need one name. The two definitions of x and three of y need five distinct SSA names: x1=u−t; y1=x1*v; x2=y1+w; y2=t−z; y3=x2*y2. Total 5+5=10 variables, counting incoming values as requested.

In SSA, each name has exactly one definition, but it can be read many times. Hence the two reads of t share one incoming name, while the successive writes to y cannot share a name. When renaming, every use must refer to its most recent preceding definition: the final product uses x2 and y2, not the original names. There are no branches here, so no φ-functions are necessary. Five new defined names plus five distinct incoming names is the requested total.

Original paper, page 9 · Answer key

2 marks · NAT
Report issue

Intermediate Code

Question 47, GATE 2015: Find the least temporary-variable count for static-single-assignment three-address code for q+r/3+s−t*5+u*v/w.
Tap to enlarge
Answer & solution

Official answer: 8

One valid translation is t1=r/3; t2=q+t1; t3=t2+s; t4=t*5; t5=t3−t4; t6=u*v; t7=t6/w; t8=t5+t7. There are eight binary-operation nodes and each three-address instruction computes only one. SSA forbids assigning a later intermediate into an already used temporary name, so the intended unoptimized translation needs eight names, including its final result. Reusing one scratch register would not be SSA.

Solution: The expression has eight binary operations: the divisions r/3 and (u*v)/w, multiplications t*5 and u*v, and four outer additive/subtractive operations. A three-address translation with a fresh temporary for every operation therefore uses eight temporaries, including the expression result.

Original paper, page 17 · Answer key

1 mark · MCQ
Report issue

Intermediate Code

Question 28, GATE 2015: About AST and CFG, which is true? A successors always correspond to later source code; B neither can contain a cycle; C maximum successors can depend on input program; D each node corresponds to at most one statement.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

AST node arity can vary with program constructs, and CFG successor counts can vary with multiway branches such as switch. Thus C under the general AST/basic-block CFG representation. CFGs can contain loop cycles, successors need not follow source order, and a basic block can contain several statements.

An AST can represent a construct with a variable number of operands or children, while a CFG can give a multiway branch many successor blocks. Thus a fixed universal successor count does not follow from the general names. CFG loop back-edges directly disprove B, and also disprove A’s assumption that all successors appear later in source. A basic-block CFG node can contain several sequential statements, disproving D. Different fixed-arity normalized intermediate representations are possible, but are not specified here.

Original paper, page 35 · Answer key

2 marks · MCQ
Report issue

Intermediate Code

Question 44, GATE 2015: Intermediate code:1 i=1; 2 j=1; 3 t1=5*i; 4 t2=t1+j; 5 t3=4*t2; 6 t4=t3; 7 a[t4]=−1; 8 j=j+1; 9 if j≤5 goto 3; 10 i=i+1; 11 if i<5 goto 2. CFG node/edge counts using entry/exit nodes: A5, 7; B6, 7; C5, 5; D7, 8.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Our answer: B with explicit entry and exit nodes

Leaders split the code into blocks[1], [2], [3..9], [10..11]. Add separate entry and exit nodes: 6 nodes. Edges are entry→B1, B1→B2, B2→B3, B3→B3, B3→B4, B4→B2, B4→exit: 7. Thus B. Without explicit entry/exit nodes, counts differ; the official choice uses them.

Leaders are statement 1, start 2(target of 11), start 3(target of 9), and 10(after conditional 9). Group straight-line instructions until each branch: B1=[1], B2=[2], B3=[3…9], B4=[10…11]. B3 has a self-loop when j≤5 and a fallthrough to B4; B4 returns to B2 when i<5 and otherwise exits. Add entry→B1, B1→B2, B2→B3 and the two B4 edges: seven edges over four blocks plus entry/exit=6 nodes. State the node-count convention rather than hiding these extra nodes.

Original paper, page 41 · Answer key

2 marks · MCQ
Report issue

Intermediate Code

Question 34, GATE 2014: A compiler accesses X[i][j][k] using byte-offset code t0=i*1024; t1=j*32; t2=k*4; t3=t1+t0; t4=t3+t2; t5=X[t4]. Integers are 32 bits, characters 8 bits. Which declaration matches? A int X[32][32][8]; B int X[4][1024][32]; C char X[4][32][8]; D char X[32][16][2].
Tap to enlarge
Your answer
Answer & solution

Official answer: A

1. Set up the reasoning. For a row-major three-dimensional array with element size e and trailing dimensions d2,d3, the offset is e·(i·d2·d3+j·d3+k). Match its three coefficients to the generated code.

2. Derive the result. The byte strides show 4-byte int elements, 8 elements in the last dimension(32/4), and 32 in the middle dimension(1024/32). The first dimension is not recoverable from strides alone, but among the listed declarations only int X[32][32][8] matches. Thus A.

3. Check the interpretation. The innermost coefficient identifies e=4 bytes; j then gives d3=8 and i gives d2=32. The leading dimension is not encoded in these strides, so select the matching offered declaration without claiming that 32 as its first dimension was derived.

Original paper, page 38 · Answer key

2 marks · MCQ
Report issue

Three-address code from synthesized expression locations

Intermediate Code

Question 59, GATE 2003: A syntax-directed definition uses:
S→id := E {gen(id.place '=' E.place);}
E→E1+E2 {t=newtemp(); gen(t '=' E1.place '+' E2.place); E.place=t;}
E→id {E.place=id.place;}
gen emits code and newtemp returns a fresh temporary t1,t2,... on each call. For X=Y+Z, what three-address code is generated?
A. X=Y+Z
B. t1=Y+Z; X=t1
C. t1=Y; t2=t1+Z; X=t2
D. t1=Y; t2=Z; t3=t1+t2; X=t3
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Reduce identifier Y using E→id: set its place to Y without generating an instruction. Reduce Z the same way, with place Z.
2. At the addition node, call newtemp once to obtain t1. Emit t1=Y+Z and make the addition expression's place t1.
3. At the assignment node, use X as the destination and the completed expression place as source. Emit X=t1.
4. The ordered output is precisely B. No identifier-copy actions occur in the definition, ruling out C and D; the addition action cannot be bypassed to get A.

Solution: An identifier expression uses the identifier’s existing place and emits no copy. The addition allocates one temporary and emits t₁=Y+Z, after which its place is t₁. The assignment then emits X=t₁. Thus the two-instruction sequence in B follows directly; no temporaries are needed merely to refer to Y and Z.

Original paper, page 8

2 marks · MCQ
Report issue

Postfix and addition association

Intermediate Code

Question 2.21, GATE 1995: Convert A+B*(C+D)/F+D*E to postfix using ordinary precedence and left associativity.
Tap to enlarge
Your answer
Answer & solution

Answer: Exact: ABCD+*F/+DE*+; listed B reassociates the additions (no official key was published)

First convert C+D to CD+, multiply by B to obtain BCD+*, and divide by F to obtain BCD+*F/. Ordinary left-associative addition then combines A with that term, before adding DE*, yielding ABCD+*F/+DE*+. Listed B is ABCD+*F/DE*++, which instead parses A+(B*(C+D)/F+D*E). These have the same value over exact arithmetic because addition is associative, explaining the intended B, but they are not identical parse-tree conversions and can differ with floating-point rounding. The distinction is retained rather than labeling the printed postfix exactly left associative.

Original paper, page 5

Descriptive
Report issue

Build an arithmetic syntax tree

Intermediate Code

Question 9(a), GATE 1995: Translate a * −(b+c) into an abstract syntax tree.
Tap to enlarge
Answer & solution

Answer: mul(a, neg(add(b,c))) (no official key was published)

The parentheses force addition of b and c first. The minus sign immediately before the parentheses is unary negation, since it has only the parenthesized expression as operand. Multiplication then combines a with that negated result.

The tree is:
        *
       / \
      a  unary−
            |
            +
           / \
          b   c

Equivalently, its exact parenthesized representation is mul(a,neg(add(b,c))). Leaves are identifiers and internal nodes are operations. The abstract syntax tree need not contain separate parenthesis nodes because the tree structure already records grouping. It is important not to turn the unary minus into a binary subtraction with a missing left child. A code generator could evaluate add(b,c), negate its result, and finally multiply by a; the tree expresses that dependency regardless of the eventual instruction order.

Original paper, page 6

Report issue

Purpose of an intermediate representation

Intermediate Code

Question 1.12, GATE 1994: Why generate intermediate code for an abstract machine: easier lexing, availability of syntax-directed rules, frontend portability, or inability to generate real machine code directly?
Tap to enlarge
Your answer
Answer & solution

Answer: C: frontend portability (no official key was published)

An intermediate representation separates language analysis from target-machine details. The same frontend can translate a source language into that representation, while different backends generate code for different processors. This improves portability and reuse. Direct generation of machine code is possible, so D is false. Syntax-directed translation can generate an IR, but its existence is not the main abstract-machine benefit, and lexical analysis does not become fundamentally easier merely because an IR is chosen.

Original paper, page 2

Descriptive
Report issue

Eager Boolean expression quadruples

Intermediate Code

Question 11(b), GATE 1992: Generate three-address quadruples in compiler generation order for (a+b) > (c+d) or a > c and b < d. Use usual precedence; partial/short-circuit evaluation is forbidden.
Tap to enlarge
Answer & solution

Answer: Seven quadruples, ending in t7 = t3 or (t4 and t5); all operands are evaluated. (no official key was published)

The parse is ((a+b)>(c+d)) or ((a>c) and (b<d)), because arithmetic binds before comparison and and binds before or. Generate code in a left-to-right postorder traversal, storing each intermediate value in a fresh temporary. A quadruple is (operator, argument1, argument2, result):

1. (+, a, b, t1)
2. (+, c, d, t2)
3. (>, t1, t2, t3)
4. (>, a, c, t4)
5. (<, b, d, t5)
6. (and, t4, t5, t6)
7. (or, t3, t6, t7)

Thus t1=a+b, t2=c+d, and t3 is the first Boolean comparison. Independently t4 and t5 compute both comparisons in the second arm; t6 combines them, and t7 is the final Boolean value. All comparison and Boolean results are represented as Boolean values (or conventional 0/1 encodings), not jump destinations. Every operation executes even if t3 is already true or t4 is false. A jump-based short-circuit sequence would violate the explicit prohibition on partial evaluation.

There are seven internal nodes in the expression tree and one quadruple per node. Temporary names and the order of independent child computations can differ in valid implementations; this is one conventional compiler generation order, not a uniquely mandated register allocation.

Original paper, page 8