GATE CSE · subject-wise

GATE Compiler Design previous year questions

46 questions from GATE papers, newest first, each with the official answer and a worked explanation.

Practise Compiler Design with instant checking
GATE 2026 S1 · Q27MSQ1 mark · no negative· Easy

Consider the following C statements: S1: char *str1 = "Hello; S2: char *str2 = "Hello;"; S3: int *str3 = "Hello"; Which of the following options is/are correct?

char *str1 = "Hello; /* Statement S1 */
char *str2 = "Hello;"; /* Statement S2 */
int *str3 = "Hello"; /* Statement S3 */
  1. (A)S1 and S2 have syntactic errors
  2. (B)S2 has a lexical error and S3 has a syntactic error
  3. (C)S1 has a lexical error and S3 has a semantic error
  4. (D)S1 has a syntactic error and S3 has a semantic error
Answer: C - show solution

S1 opens a string literal that never closes on the line: the lexer cannot form a valid token, a lexical error. S2 is a valid string literal "Hello;" followed by ';': no error. S3 is syntactically a valid declaration but assigns a char* (string literal) to an int*: a type mismatch, found during semantic analysis. Answer: (C) S1 has a lexical error and S3 has a semantic error.

Concept: Classify errors by compiler phase: bad tokens are lexical, bad structure is syntactic, type rules are semantic. The ';' inside quotes in S2 is just a character.

GATE 2026 S1 · Q28MSQ1 mark · no negative· Easy

Which of the following statements is/are true?

  1. (A)LL(1) parser uses backtracking
  2. (B)For a grammar to be LL(1), it must be left-recursive
  3. (C)For a grammar to be LL(1), it must be left-factored
  4. (D)The LL(1) parsers are more powerful than the SLR parsers
Answer: C - show solution

LL(1) is predictive and never backtracks (A false). An LL(1) grammar cannot be left-recursive (B false). Common prefixes make two productions share a FIRST symbol, so an LL(1) grammar must be left-factored (C true). Every LL(1) grammar is not necessarily SLR and vice versa, and LR-family parsers are in general the more powerful; LL(1) is not more powerful than SLR (D false). Answer: (C).

Concept: LL(1) prerequisites: no left recursion, left-factored, disjoint FIRST/FOLLOW sets. Bottom-up parsers handle left recursion that LL(1) cannot.

GATE 2026 S1 · Q42MCQ2 marks · -2/3· Moderate

Consider the following control flow graph. Basic blocks: B1: a = b + i. B2: a = g * k; f = d - f; a = c * 4. B3: t = g * k; b = c + m. B4: x = g * k; y = b + i. B5: z = c + m. Edges: ENTRY → B1; B1 → B2; B1 → B3; B2 → B4; B3 → B4; B4 → B5; B5 → EXIT. Which one of the following options correctly lists the set of redundant expressions (common subexpressions) in the basic blocks B4 and B5? Note: All the variables are integers.

  1. (A)B4: { b + i } B5: { c + m }
  2. (B)B4: { g * k } B5: { c + m }
  3. (C)B4: { g * k, b + i } B5: { }
  4. (D)B4: { g * k } B5: { }
Answer: D - show solution

An expression is redundant at a point if it is available: computed on every path reaching it with no later redefinition of its operands. g * k: computed in B2 and in B3, and neither g nor k is redefined afterwards, so it is available at B4 entry: redundant in B4. b + i: computed in B1, but B3 redefines b, so on the B3 path it is killed: not redundant. c + m: computed in B3 only, not on the path B1 → B2 → B4, so not available at B5: not redundant. Answer: (D) B4: { g * k }, B5: { }.

Concept: Global CSE uses available-expressions analysis, an all-paths (intersection) problem. Traps: b + i looks repeated but b is reassigned; c + m appears earlier but only on one branch.

GATE 2026 S1 · Q53MSQ2 marks · no negative· Moderate

Consider the following two syntax-directed definitions SDD1 and SDD2 for type declarations. SDD1 (grammar G1): D → T V D.type = T.type; V.type = T.type T → int T.type = int T → float T.type = float V → V₁ id V₁.type = V.type; put(id.entry, V.type) V → id put(id.entry, V.type) SDD2 (grammar G2): D → D₁ id D.type = D₁.type; put(id.entry, D₁.type) D → T id D.type = T.type; put(id.entry, T.type) T → int T.type = int T → float T.type = float D is the start symbol, and int, float and id are the three terminals. The non-terminal V₁ is the same as V and the non-terminal D₁ is the same as D. Here, the subscript is used to differentiate the grammar symbols on the two sides of a production. The function put updates the symbol table with the type information for an identifier. Let P and Q be the languages specified by grammars G1 and G2, respectively. Which of the following statements is/are true?

  1. (A)The languages P and Q are the same
  2. (B)SDD2 is S-attributed and contains only synthesized attributes
  3. (C)SDD1 is L-attributed and contains only inherited attributes
  4. (D)The specifications of SDD1 and SDD2 are such that the same entries get added to the symbol table
Answer: A, B, D - show solution

G1: D → T V with V → V id | id gives T id id ... id (one or more ids). G2: D → D id | T id gives the same T id⁺. So P = Q (A true). In SDD2, D.type and T.type are both computed from children only: synthesized, S-attributed (B true). In SDD1, V.type is inherited (from parent D or V), but T.type and D.type are synthesized, so SDD1 is L-attributed but does not contain only inherited attributes (C false). Both put every id with the declared type, so the symbol table gets the same entries (D true). Answer: (A), (B), (D).

Concept: Synthesized attributes flow up from children; inherited ones flow down or across from parent/left siblings. SDD1 mixes both, so 'only inherited' is the trap.

GATE 2026 S2 · Q17MCQ1 mark · -1/3· Easy

In C runtime environment, which one of the following is stored in heap?

  1. (A)A static variable declared inside a function
  2. (B)An array of integers declared inside a function
  3. (C)A dynamically allocated array of integers created using malloc() function call
  4. (D)Return address of a function
Answer: C - show solution

Static variables live in the data/BSS segment. A local array is on the stack in the function's activation record. The return address is also on the stack. Memory obtained with malloc() comes from the heap. Answer: (C).

Concept: Memory layout of a C program: text, data/BSS, stack, heap. The trap is (A): a static local has function scope but static storage duration, so it is in the data segment, not the stack or heap.

GATE 2026 S2 · Q35NAT1 mark · no negative· Moderate

A lexical analyzer uses the following token definitions letter → [A-Za-z] digit → [0-9] id → letter (letter | digit)* number → digit⁺ ws → (blank | tab | newline)⁺ For the string given below, x1 23mm 78 y 7z zz5 14A 8H AaYcD the number of tokens (excluding ws) that will be produced by the lexical analyzer is __________. (answer in integer)

Answer: 13 - show solution

Use longest match. x1 → id (1). 23mm → number 23, id mm (2). 78 → number (1). y → id (1). 7z → number 7, id z (2). zz5 → id (1). 14A → number 14, id A (2). 8H → number 8, id H (2). AaYcD → id (1). Total = 1+2+1+1+2+1+2+2+1 = 13. Answer: 13.

Concept: An id must start with a letter, so a word beginning with digits splits into a number followed by an id. The trap is counting each blank-separated word as one token (giving 9).

GATE 2026 S2 · Q41MCQ2 marks · -2/3· Hard

Consider the canonical LR(0) parsing of the grammar below using terminals {a, b, c} and non-terminals {A, B, C, S} with S as the start symbol. S → ACB A → aA | ε C → cC | ε B → bB | b Which one of the following options gives the number of shift-reduce conflicts that will occur in the LR(0) ACTION table?

  1. (A)2
  2. (B)3
  3. (C)4
  4. (D)5
Answer: D - show solution

Augment with S' → S. I0 = {S' → .S, S → .ACB, A → .aA, A → .}: shift on a and reduce A → ε (LR(0) reduces in every column) - conflict 1. I(a) = {A → a.A, A → .aA, A → .}: shift a vs reduce - conflict 2. I(A) = {S → A.CB, C → .cC, C → .}: shift c vs reduce - conflict 3. I(c) = {C → c.C, C → .cC, C → .}: conflict 4. I(AC) = {S → AC.B, B → .bB, B → .b}: shifts only. I(b) = {B → b.B, B → b., B → .bB, B → .b}: shift b vs reduce B → b - conflict 5. Other states have no conflict. Total = 5. Answer: (D) 5.

Concept: In LR(0), a state with a completed item reduces on every terminal, so any shift in that state conflicts. ε-productions create a completed item in every state that predicts them. The trap is missing the B → b. state or forgetting I0.

GATE 2026 S2 · Q45MCQ2 marks · -2/3· Moderate

Consider the control flow graph given below. ENTRY → B1 B1: a = b + c (successors: B2, B3) B2: d = a + e (successor: B4) B3: e = a + f (successor: B1) B4: g = d + e (successor: EXIT) Which one of the following options is the set of live variables at the exit point of each basic block?

  1. (A)B1:{a, b, c, e, f}, B2:{d, e}, B3:{b, c, e, f}, B4:∅
  2. (B)B1:∅, B2:{d, e}, B3:{a, c, f}, B4:∅
  3. (C)B1:{a, b, c, e, f}, B2:{d, e}, B3:{c, e, f}, B4:∅
  4. (D)B1:∅, B2:{d, e, f}, B3:{a, b, c, e, f}, B4:∅
Answer: A - show solution

in(B) = use(B) ∪ (out(B) - def(B)), out(B) = union of in(successors). B4: out = ∅, in = {d, e}. B2: out = {d, e}, in = {a, e}. B3: in = {a, f} ∪ (out(B3) - {e}), out(B3) = in(B1). B1: in = {b, c} ∪ (out(B1) - {a}), out(B1) = in(B2) ∪ in(B3). Iterate to a fixed point: out(B1) = {a, e} ∪ {a, f, b, c} = {a, b, c, e, f}; in(B1) = {b, c, e, f}; out(B3) = {b, c, e, f}. Answer: (A).

Concept: Backward dataflow analysis with a loop (B3 → B1) needs iteration until nothing changes. The trap is stopping after one pass and missing that e and f flow around the loop, or that e is killed in B3 but live again from B1.

GATE 2025 S1 · Q12MCQ1 mark · -1/3· Easy

Which ONE of the following statements is FALSE regarding the symbol table?

  1. (A)Symbol table is responsible for keeping track of the scope of variables.
  2. (B)Symbol table can be implemented using a binary search tree.
  3. (C)Symbol table is not required after the parsing phase.
  4. (D)Symbol table is created during the lexical analysis phase.
Answer: C - show solution

The symbol table records identifiers with their scope, type and storage information (A true). It can be a hash table, list or BST (B true). Entries are created as the lexer meets identifiers (D true). After parsing, semantic analysis (type checking), intermediate code generation and code generation (storage offsets) all still consult it, so it is needed beyond parsing. Statement (C) is false. Answer: (C).

Concept: The symbol table persists through all compiler phases. The trap is thinking it is a front-end-only structure.

GATE 2025 S1 · Q13MCQ1 mark · -1/3· Easy

Which ONE of the following techniques used in compiler code optimization uses live variable analysis?

  1. (A)Run-time function call management
  2. (B)Register assignment to variables
  3. (C)Strength reduction
  4. (D)Constant folding
Answer: B - show solution

Live variable analysis finds, at each point, the variables whose current values may still be used later. Register allocation uses this: two variables whose live ranges do not overlap can share a register, and a dead variable's register can be freed. Strength reduction and constant folding work on expressions and values, not liveness; call management is a runtime concern. Answer: (B).

Concept: Liveness drives register allocation (interference graphs) and dead-code elimination. Constant folding needs constant propagation, not liveness - a common mix-up.

GATE 2025 S1 · Q46MSQ2 marks · no negative· Easy

Which of the following statement(s) is/are TRUE while computing First and Follow during top down parsing by a compiler?

  1. (A)For a production A → ε, ε will be added to First(A).
  2. (B)If there is any input right end marker, it will be added to First(S), where S is the start symbol.
  3. (C)For a production A → ε, ε will be added to Follow(A).
  4. (D)If there is any input right end marker, it will be added to Follow(S), where S is the start symbol.
Answer: A, D - show solution

Rule for First: if A → ε is a production, ε ∈ First(A): (A) true. Rule for Follow: the end marker $ is placed in Follow(S) for the start symbol: (D) true. $ is never in a First set (it is not derived from any symbol): (B) false. Follow sets contain terminals and $, never ε: (C) false. Answer: (A), (D).

Concept: First may contain ε; Follow may contain $; never the other way round. Swapping the two is the standard trap.

GATE 2025 S1 · Q52NAT2 marks · no negative· Easy

Refer to the given 3-address code sequence. This code sequence is split into basic blocks. The number of basic blocks is ________. (Answer in integer)

1001: i = 1
1002: j = 1
1003: t1 = 10*i
1004: t2 = t1+j
1005: t3 = 8*t2
1006: t4 = t3-88
1007: a[t4] = 0.0
1008: j = j+1
1009: if j <= 10 goto 1003
1010: i = i+1
1011: if i <= 10 goto 1002
1012: i = 1
1013: t5 = i-1
1014: t6 = 88*t5
1015: a[t6] = 1.0
1016: i = i+1
1017: if i <= 10 goto 1013
Answer: 6 - show solution

Leaders: the first statement 1001; jump targets 1003, 1002, 1013; statements after jumps 1010, 1012 (1018 does not exist). Sorted leaders: 1001, 1002, 1003, 1010, 1012, 1013. Blocks: [1001], [1002], [1003-1009], [1010-1011], [1012], [1013-1017]. Answer: 6.

Concept: Leader rules: first instruction, every jump target, every instruction following a jump. Commonly missed: 1002 as a target, making 1001 a block by itself.

GATE 2025 S2 · Q21MCQ1 mark · -1/3· Easy

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.
Answer: C - show solution

Backpatching keeps lists of jumps with unfilled targets (truelist, falselist, nextlist) and fills them in when the target label becomes known. This is exactly what lets a compiler generate code for Boolean expressions with short-circuit jumps and for flow-of-control statements (if, while) in a single pass. Both statements are correct. Answer: (C).

Concept: Backpatching exists to avoid a second pass over generated code; the textbook treatment applies it first to Boolean expressions and then to control flow.

GATE 2025 S2 · Q22MCQ1 mark · -1/3· Moderate

Given the following syntax directed translation rules: Rule 1: R → AB {B.i = R.i - 1; A.i = B.i; R.i = A.i + 1;} Rule 2: P → CD {P.i = C.i + D.i; D.i = C.i + 2;} Rule 3: Q → EF {Q.i = E.i + F.i;} Which ONE is the CORRECT option among the following?

  1. (A)Rule 1 is S-attributed and L-attributed; Rule 2 is S-attributed and not L-attributed; Rule 3 is neither S-attributed nor L-attributed
  2. (B)Rule 1 is neither S-attributed nor L-attributed; Rule 2 is S-attributed and L-attributed; Rule 3 is S-attributed and L-attributed
  3. (C)Rule 1 is neither S-attributed nor L-attributed; Rule 2 is not S-attributed and is L-attributed; Rule 3 is S-attributed and L-attributed
  4. (D)Rule 1 is S-attributed and not L-attributed; Rule 2 is not S-attributed and is L-attributed; Rule 3 is S-attributed and L-attributed
Answer: C - show solution

Rule 1: A.i = B.i makes A's attribute depend on its right sibling B, which violates L-attribution; it also has inherited attributes, so it is not S-attributed. Neither. Rule 2: D.i = C.i + 2 is inherited from the left sibling (allowed in L-attributed, not in S-attributed); P.i is synthesized. So L-attributed, not S-attributed. Rule 3: only the head is computed from children: S-attributed and hence also L-attributed. Answer: (C).

Concept: S-attributed means only synthesized attributes. L-attributed allows inherited attributes that depend only on the parent and left siblings. Every S-attributed definition is L-attributed. The trap is missing the right-sibling dependency in Rule 1.

GATE 2025 S2 · Q40MCQ2 marks · -2/3· Hard

Given a Context-Free Grammar G as follows: S → Aa | bAc | dc | bda A → d Which ONE of the following statements is TRUE?

  1. (A)G is neither LALR(1) nor SLR(1)
  2. (B)G is CLR(1), not LALR(1)
  3. (C)G is LALR(1), not SLR(1)
  4. (D)G is LALR(1), also SLR(1)
Answer: C - show solution

FOLLOW(A) = {a, c}. From I0 on d: {S → d.c, A → d.}. SLR reduces A → d on FOLLOW(A), including c, while S → d.c shifts c: shift-reduce conflict, so not SLR(1). In LR(1) this item is A → d. with lookahead a only, so no conflict. After b then d: {S → bd.a, A → d.} with LR(1) lookahead c, while shift is on a: no conflict (SLR would conflict on a). The two d-states have different cores, so LALR merging joins nothing and introduces no conflict. G is LALR(1) but not SLR(1). Answer: (C).

Concept: Classic grammar separating SLR from LALR: FOLLOW sets are too coarse, while LR(1) lookaheads are context-specific. Check that LALR merging only combines states with identical cores.

GATE 2025 S2 · Q51MSQ2 marks · no negative· Hard

Consider two grammars G1 and G2 with the production rules given below: G1: S → if E then S | if E then S else S | a E → b G2: S → if E then S | M M → if E then M else S | c E → b where if, then, else, a, b, c are the terminals. Which of the following option(s) is/are CORRECT?

  1. (A)G1 is not LL(1) and G2 is LL(1).
  2. (B)G1 is LL(1) and G2 is not LL(1).
  3. (C)G1 and G2 are not LL(1).
  4. (D)G1 and G2 are ambiguous.
Answer: C, D - show solution

Not LL(1): in G1 two S-productions begin with "if"; in G2, S → if E then S and S → M both have "if" in FIRST (M → if ...). So neither is LL(1). G1 is the classic dangling-else grammar: "if b then if b then a else a" has two parse trees. G2 is also ambiguous because the else-part of M is an unrestricted S. M derives "if b then c else if b then c". Then w = "if b then if b then c else if b then c else c" parses as S → if b then S with S = M = if b then c else (if b then c else c), and also as S → M = if b then M' else c with M' = "if b then c else if b then c". Answer: (C) and (D).

Concept: Any ambiguous grammar is not LL(1), and common prefixes also break LL(1). G2 looks like the standard matched/unmatched fix, but allowing an unmatched S after else reintroduces ambiguity.

GATE 2024 S1 · Q26MSQ1 mark · no negative· Easy

Which of the following is/are Bottom-Up Parser(s)?

  1. (A)Shift-reduce Parser
  2. (B)Predictive Parser
  3. (C)LL(1) Parser
  4. (D)LR Parser
Answer: A, D - show solution

Shift-reduce parsers build the parse tree from leaves to root, and LR parsers (SLR, LALR, CLR) are shift-reduce parsers, so both are bottom-up. Predictive and LL(1) parsers are top-down. Answer: (A), (D).

Concept: LL = top-down (leftmost derivation), LR = bottom-up (rightmost derivation in reverse). The trap is mixing up the two L's.

GATE 2024 S1 · Q33NAT1 mark · no negative· Moderate

Consider the operator precedence and associativity rules for the integer arithmetic operators given in the table below. Operator Precedence Associativity + Highest Left − High Right ∗ Medium Right / Low Right The value of the expression 3 + 1 + 5 ∗ 2 / 7 + 2 − 4 − 7 − 6 / 2 as per the above rules is __________

Answer: 6 - show solution

/ binds loosest and is right-associative, so the expression is (3 + 1 + 5 ∗ 2) / ((7 + 2 − 4 − 7 − 6) / 2). Left part: + binds before ∗, so (3 + 1 + 5) ∗ 2 = 18. Middle: + first gives 9 − 4 − 7 − 6, and right-associative − gives 9 − (4 − (7 − 6)) = 9 − 3 = 6. Then 6 / 2 = 3 and 18 / 3 = 6. Answer: 6.

Concept: Split at the lowest-precedence operator first, applying its associativity, then recurse. The trap is evaluating − left to right.

GATE 2024 S1 · Q37MCQ2 marks · -2/3· Easy

Consider the following syntax-directed definition (SDD). S → DHTU { S.val = D.val + H.val + T.val + U.val; } D → "M" D1 { D.val = 5 + D1.val; } D → ϵ { D.val = −5; } H → "L" H1 { H.val = 5 ∗ 10 + H1.val; } H → ϵ { H.val = −10; } T → "C" T1 { T.val = 5 ∗ 100 + T1.val; } T → ϵ { T.val = −5; } U → "K" { U.val = 5; } Given "MMLK" as the input, which one of the following options is the CORRECT value computed by the SDD (in the attribute S.val)?

  1. (A)45
  2. (B)50
  3. (C)55
  4. (D)65
Answer: A - show solution

Parse MMLK as D = MM, H = L, T = ϵ, U = K. D.val = 5 + 5 + (−5) = 5. H.val = 50 + (−10) = 40. T.val = −5. U.val = 5. S.val = 5 + 40 − 5 + 5 = 45. Answer: (A) 45.

Concept: Evaluate synthesized attributes bottom-up, remembering the ϵ productions contribute their own (negative) values. The trap is ignoring the ϵ values and getting 65.

GATE 2024 S1 · Q38MCQ2 marks · -2/3· Moderate

Consider the following grammar G, with S as the start symbol. The grammar G has three incomplete productions denoted by (1), (2), and (3). S → daT | (1) T → aS | bT | (2) R → (3) | ϵ The set of terminals is {a, b, c, d, f}. The FIRST and FOLLOW sets of the different non-terminals are as follows. FIRST(S) = {c, d, f}, FIRST(T) = {a, b, ϵ}, FIRST(R) = {c, ϵ} FOLLOW(S) = FOLLOW(T) = {c, f, $}, FOLLOW(R) = {f} Which one of the following options CORRECTLY fills in the incomplete productions?

  1. (A)(1) S → Rf (2) T → ϵ (3) R → cTR
  2. (B)(1) S → fR (2) T → ϵ (3) R → cTR
  3. (C)(1) S → fR (2) T → cT (3) R → cR
  4. (D)(1) S → Rf (2) T → cT (3) R → cR
Answer: A - show solution

ϵ ∈ FIRST(T) and c ∉ FIRST(T), so (2) is T → ϵ, ruling out (C) and (D). S → fR would put R at the end of S, so FOLLOW(R) would include FOLLOW(S) = {c, f, $}, not just {f}; with S → Rf, FIRST(S) = {d} ∪ {c, f} and FOLLOW(R) = {f}. With R → cTR: FOLLOW(T) gets FIRST(R) − ϵ = {c} and, since R is nullable, FOLLOW(R) = {f}; with FOLLOW(S) from T → aS this matches {c, f, $}. Answer: (A).

Concept: Work backwards from the given sets: ϵ in FIRST forces an ϵ production, and a small FOLLOW set means the non-terminal is never at the end of a longer right side. The trap is choosing S → fR, which gives the right FIRST(S) but the wrong FOLLOW(R).

GATE 2024 S1 · Q39MCQ2 marks · -2/3· Moderate

Consider the following pseudo-code. Which one of the following options CORRECTLY specifies the number of basic blocks and the number of instructions in the largest basic block, respectively?

L1:  t1 = -1
L2:  t2 = 0
L3:  t3 = 0
L4:  t4 = 4 * t3
L5:  t5 = 4 * t2
L6:  t6 = t5 * M
L7:  t7 = t4 + t6
L8:  t8 = a[t7]
L9:  if t8 <= max goto L11
L10: t1 = t8
L11: t3 = t3 + 1
L12: if t3 < M goto L4
L13: t2 = t2 + 1
L14: if t2 < N goto L3
L15: max = t1
  1. (A)6 and 6
  2. (B)6 and 7
  3. (C)7 and 7
  4. (D)7 and 6
Answer: D - show solution

Leaders: L1 (first), L3 (target of L14), L4 (target of L12), L11 (target of L9), and the instructions after each jump: L10, L13, L15. Blocks: L1-L2 (2), L3 (1), L4-L9 (6), L10 (1), L11-L12 (2), L13-L14 (2), L15 (1). That is 7 blocks, the largest with 6 instructions. Answer: (D) 7 and 6.

Concept: Leaders are the first instruction, every jump target, and every instruction following a jump. The trap is missing L3 as a separate leader, which merges it into a 7-instruction block.

GATE 2024 S2 · Q21MCQ1 mark · -1/3· Easy

Consider the following two sets: Set X Set Y P. Lexical Analyzer 1. Abstract Syntax Tree Q. Syntax Analyzer 2. Token R. Intermediate Code Generator 3. Parse Tree S. Code Optimizer 4. Constant Folding Which one of the following options is the CORRECT match from Set X to Set Y ?

  1. (A)P - 4; Q - 1; R - 3; S - 2
  2. (B)P - 2; Q - 3; R - 1; S - 4
  3. (C)P - 2; Q - 1; R - 3; S - 4
  4. (D)P - 4; Q - 3; R - 2; S - 1
Answer: B - show solution

The lexical analyzer produces tokens (P-2). The syntax analyzer builds the parse tree (Q-3). The intermediate code generator works with the abstract syntax tree as an intermediate representation (R-1). Constant folding is a code optimization (S-4). Answer: (B) P - 2; Q - 3; R - 1; S - 4.

Concept: Phases of a compiler and their artefacts. Parse tree versus AST is the usual confusion: the parser gives the concrete parse tree, the AST serves as an intermediate representation.

GATE 2024 S2 · Q29MSQ1 mark · no negative· Moderate

Which of the following statements is/are FALSE?

  1. (A)An attribute grammar is a syntax-directed definition (SDD) in which the functions in the semantic rules have no side effects
  2. (B)The attributes in a L-attributed definition cannot always be evaluated in a depth-first order
  3. (C)Synthesized attributes can be evaluated by a bottom-up parser as the input is parsed
  4. (D)All L-attributed definitions based on LR(1) grammar can be evaluated using a bottom-up parsing strategy
Answer: B, D - show solution

(A) True - the textbook definition of an attribute grammar. (B) False - L-attributed definitions are exactly those that can always be evaluated in one depth-first, left-to-right traversal. (C) True - synthesized attributes are computed at each reduction. (D) False - L-attributed definitions on LL(1) grammars can be evaluated bottom-up (using marker non-terminals), but not every L-attributed definition on an LR(1) grammar can. The FALSE statements are B and D. Answer: (B), (D).

Concept: The question asks for FALSE statements. Key facts: L-attributed means depth-first evaluable; bottom-up evaluation of inherited attributes is guaranteed only for L-attributed SDDs on LL grammars.

GATE 2024 S2 · Q40MCQ2 marks · -2/3· Moderate

Consider the following context-free grammar where the start symbol is S and the set of terminals is {a,b,c,d}. S → AaAb | BbBa A → cS | ε B → dS | ε The following is a partially-filled LL(1) parsing table. a b c d $ S S → AaAb S → BbBa (1) (2) A A → ε (3) A → cS B (4) B → ε B → dS Which one of the following options represents the CORRECT combination for the numbered cells in the parsing table? Note: In the options, "blank" denotes that the corresponding cell is empty.

  1. (A)(1) S → AaAb (2) S → BbBa (3) A → ε (4) B → ε
  2. (B)(1) S → BbBa (2) S → AaAb (3) A → ε (4) B → ε
  3. (C)(1) S → AaAb (2) S → BbBa (3) blank (4) blank
  4. (D)(1) S → BbBa (2) S → AaAb (3) blank (4) blank
Answer: A - show solution

FIRST(A) = {c, ε}, FIRST(B) = {d, ε}. FIRST(AaAb) = {c, a}, FIRST(BbBa) = {d, b}. So M[S,c] = S → AaAb (1) and M[S,d] = S → BbBa (2). FOLLOW(A): A is followed by a and by b in AaAb, so FOLLOW(A) = {a, b} and A → ε goes in columns a and b: (3) = A → ε. FOLLOW(B) = {b, a} from BbBa, so (4) = B → ε. Answer: (A).

Concept: Filling an LL(1) table: productions go under FIRST of their right side, and ε-productions under FOLLOW of the left side. Forgetting the second occurrence of A (followed by b) leaves (3) blank wrongly.

GATE 2024 S2 · Q43MCQ2 marks · -2/3· Moderate

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?

  1. (A)(1) =[] s i (3) * (0) (2) (6) []= x i
  2. (B)(1) []= s i (3) − (0) (2) (6) =[] x (5)
  3. (C)(1) =[] s i (3) * (0) (2) (6) []= x (5)
  4. (D)(1) []= s i (3) − (0) (2) (6) =[] x i
Answer: A - show solution

(1) must fetch s[i] for uminus: an indexed read, written =[] s i. (2) negates it. (3) multiplies (p + r) by −s[i]: * (0) (2). (4) and (5) add u/w. The assignment to x[i] is split into an indexed-store triple that computes the location, []= x i, and triple (7) = (6) (5) assigns the value (5) to it. Answer: (A).

Concept: In triples, an array store x[i] = y needs two triples: []= x i, then an assignment using it. The read s[i] uses =[]. Option C puts (5) inside (6), duplicating (7).

GATE 2024 S2 · Q65NAT2 marks · no negative· Moderate

Consider the following augmented grammar, which is to be parsed with a SLR parser. The set of terminals is {a,b,c,d,#,@} S′ → S S → SS | Aa | bAc | Bc | bBa A → d# B → @ Let I0 = CLOSURE({S′ → •S}). The number of items in the set GOTO(I0, S) is ___________

Answer: 9 - show solution

I0 contains S′ → •S, S → •SS, S → •Aa, S → •bAc, S → •Bc, S → •bBa, A → •d#, B → •@. GOTO(I0, S) moves the dot over S in S′ → •S and S → •SS, giving the kernel S′ → S• and S → S•S. Closure of S → S•S adds all S-items S → •SS, S → •Aa, S → •bAc, S → •Bc, S → •bBa, and then A → •d# and B → •@. Total = 2 + 5 + 2 = 9. Answer: 9.

Concept: GOTO = advance the dot, then take the closure. Forgetting to close over A and B (reached via •Aa and •Bc) gives 7.

GATE 2023 · Q11MCQ1 mark · -1/3· Easy

Consider the following statements regarding the front-end and back-end of a compiler. S1: The front-end includes phases that are independent of the target hardware. S2: The back-end includes phases that are specific to the target hardware. S3: The back-end includes phases that are specific to the programming language used in the source code. Identify the CORRECT option.

  1. (A)Only S1 is TRUE.
  2. (B)Only S1 and S2 are TRUE.
  3. (C)S1, S2, and S3 are all TRUE.
  4. (D)Only S1 and S3 are TRUE.
Answer: B - show solution

The front end (lexical, syntax, semantic analysis, intermediate code generation) depends on the source language but not on the target machine, so S1 is true. The back end (target-specific optimisation, code generation, register allocation) depends on the target hardware, so S2 is true. The back end works on the intermediate representation and is independent of the source language, so S3 is false. Answer: (B) Only S1 and S2 are TRUE.

Concept: The front-end/back-end split exists so one front end can target many machines and one back end can serve many languages. The trap is S3: language specifics belong to the front end.

GATE 2023 · Q19MCQ1 mark · -1/3· Moderate

Consider the following definition of a lexical token id for an identifier in a programming language, using extended regular expressions: letter → [A-Za-z], digit → [0-9], id → letter (letter | digit)*. Which one of the following Non-deterministic Finite-state Automata with ϵ-transitions accepts the set of valid identifiers? (State 0 is the start state; F marks a final state.)

  1. (A)States 0, 1(F). 0 --letter--> 1; 0 --ϵ--> 1; 1 --letter, digit--> 1.
  2. (B)States 0, 1(F), 2(F), 3(F). 0 --letter--> 1; 1 --ϵ--> 2; 1 --ϵ--> 3; 2 --letter--> 2; 3 --digit--> 3.
  3. (C)States 0, 1(F), 2, 3, 4, 5, 6. 0 --letter--> 1; 1 --ϵ--> 2; 2 --letter--> 3; 3 --ϵ--> 6; 1 --ϵ--> 4; 4 --digit--> 5; 5 --ϵ--> 6; 6 --ϵ--> 1.
  4. (D)States 0, 1(F), 2, 3. 0 --letter--> 1; 1 --letter--> 2; 2 --digit--> 3; 3 --ϵ--> 1.
Answer: C - show solution

(A) has an ϵ-move from the start straight to the final state, so it accepts the empty string and strings starting with a digit such as "9". (B) commits after the first letter to only letters or only digits, so "a1b" is rejected. (D) after the first letter accepts only repeated "letter digit" pairs, so "ab" is rejected. (C) is the Thompson construction: after one letter, the final state 1 can loop through either a letter branch or a digit branch and return via ϵ to 1, any number of times, giving letter(letter | digit)*. Answer: (C).

Concept: Tests turning a regular definition into an ϵ-NFA. Watch for ϵ-edges that skip the mandatory first letter and for loops that cannot mix letters and digits.

GATE 2023 · Q36MCQ2 marks · -2/3· Moderate

Consider the following program. Which one of the following options represents the activation tree corresponding to the main function? (Each tree is written as parent -> children, children listed left to right.)

int main()
{
    f1();
    f2(2);
    f3();
    return(0);
}

int f1()
{
    return(1);
}

int f2(int X)
{
    f3();
    if (X==1)
        return f1();
    else
        return (X*f2(X-1));
}

int f3()
{
    return(5);
}
  1. (A)main -> f1, f2, f3; that f2 -> f3, f2; the inner f2 -> f3, f1
  2. (B)main -> f1, f2, f3; f2 -> f3, f1
  3. (C)main -> f1; f1 -> f2; f2 -> f3, f1
  4. (D)main -> f1, f2, f3; f2 -> f3, f2, f1
Answer: A - show solution

main calls f1(), f2(2), f3() in that order, so its children are f1, f2, f3. f2(2) first calls f3(); since X = 2 ≠ 1 it then calls f2(1). So f2(2) has children f3, f2. f2(1) calls f3(), then, since X == 1, calls f1(). So f2(1) has children f3, f1. f1 and f3 make no calls. Answer: (A).

Concept: An activation tree has one node per call, with children in call order. The trap is (D), which flattens the recursive call f2(1) and its callees into one level.

GATE 2023 · Q37MCQ2 marks · -2/3· Moderate

Consider the control flow graph shown. B1: i = m - 1; j = n; a = 10. B2: i = i + 1; j = j - 1. B3: a = 20. B4: i = a + 1. Edges: ENTRY -> B1, B1 -> B2, B2 -> B3, B2 -> B4, B3 -> B4, B4 -> B2, B4 -> EXIT. Which one of the following choices correctly lists the set of live variables at the exit point of each basic block?

  1. (A)B1: {}, B2: {a}, B3: {a}, B4: {a}
  2. (B)B1: {i, j}, B2: {a}, B3: {a}, B4: {i}
  3. (C)B1: {a, i, j}, B2: {a, i, j}, B3: {a, i}, B4: {a}
  4. (D)B1: {a, i, j}, B2: {a, j}, B3: {a, j}, B4: {a, i, j}
Answer: D - show solution

Use IN(B) = USE(B) ∪ (OUT(B) - DEF(B)). B2: USE {i, j}, DEF {i, j}. B3: DEF {a}. B4: USE {a}, DEF {i}. OUT(B4) = IN(B2) ∪ IN(EXIT) = IN(B2). Iterate to a fixed point: IN(B2) = {i, j} ∪ (OUT(B2) - {i, j}); OUT(B2) = IN(B3) ∪ IN(B4); IN(B4) = {a} ∪ (OUT(B4) - {i}); IN(B3) = OUT(B3) - {a} = IN(B4) - {a}. Fixed point: IN(B2) = {a, i, j}, so OUT(B4) = {a, i, j}, IN(B4) = {a, j}, OUT(B3) = {a, j}, IN(B3) = {j}, OUT(B2) = {a, j}, OUT(B1) = {a, i, j}. Answer: (D) B1: {a, i, j}, B2: {a, j}, B3: {a, j}, B4: {a, i, j}.

Concept: Backward data-flow analysis with the loop B4 -> B2 carrying i and j back. The trap is ignoring the back edge, which makes i and j look dead at the end of B4.

GATE 2023 · Q60NAT2 marks · no negative· Moderate

Consider the syntax directed translation given by the following grammar and semantic rules. Here N, I, F and B are non-terminals. N is the starting non-terminal, and #, 0 and 1 are lexical tokens corresponding to input letters "#", "0" and "1", respectively. X.val denotes the synthesized attribute (a numeric value) associated with a non-terminal X. I1 and F1 denote occurrences of I and F on the right hand side of a production, respectively. For the tokens 0 and 1, 0.val = 0 and 1.val = 1. N → I # F N.val = I.val + F.val I → I1 B I.val = (2 × I1.val) + B.val I → B I.val = B.val F → B F1 F.val = (1/2)(B.val + F1.val) F → B F.val = (1/2) B.val B → 0 B.val = 0.val B → 1 B.val = 1.val The value computed by the translation scheme for the input string 10#011 is ________. (Rounded off to three decimal places)

Answer: 2.374 to 2.376 - show solution

Integer part "10": I(1) = 1, then I(10) = 2 × 1 + 0 = 2. Fraction part "011" is right-recursive: F(1) = 1/2 × 1 = 0.5; F(11) = 1/2 × (1 + 0.5) = 0.75; F(011) = 1/2 × (0 + 0.75) = 0.375. N.val = 2 + 0.375 = 2.375 (binary 10.011). Answer: 2.375 (key range 2.374 to 2.376).

Concept: The scheme converts a binary fixed-point string to decimal: left recursion doubles for integer digits, right recursion halves for fractional digits. Evaluate F from the rightmost digit.

GATE 2022 · Q13MCQ1 mark · -1/3· Moderate

Which one of the following statements is TRUE?

  1. (A)The LALR(1) parser for a grammar G cannot have reduce-reduce conflict if the LR(1) parser for G does not have reduce-reduce conflict.
  2. (B)Symbol table is accessed only during the lexical analysis phase.
  3. (C)Data flow analysis is necessary for run-time memory management.
  4. (D)LR(1) parsing is sufficient for deterministic context-free languages.
Answer: D - show solution

(A) False: merging LR(1) states with the same core to form LALR(1) can create reduce-reduce conflicts that LR(1) did not have. (B) False: the symbol table is used by later phases too (semantic analysis, code generation). (C) False: data flow analysis is a compile-time optimization tool, not needed for run-time memory management. (D) True: every deterministic context-free language has an LR(1) grammar, so LR(1) parsing suffices. Answer: (D).

Concept: LR(1) grammars generate exactly the DCFLs. Remember LALR merging can introduce reduce-reduce (never shift-reduce) conflicts.

GATE 2022 · Q29NAT1 mark · no negative· Easy

Consider the augmented grammar with {+, *, (, ), id} as the set of terminals. S' → S S → S + R | R R → R * P | P P → (S) | id If I₀ is the set of two LR(0) items {[S' → S.], [S → S. + R]}, then goto(closure(I₀), +) contains exactly __________ items.

Answer: 5 - show solution

closure(I₀) = I₀ (the dots are at the end or before the terminal +). goto on + gives S → S + .R. Its closure adds R → .R * P, R → .P, and then for P: P → .(S), P → .id. Total = 5 items. Answer: 5.

Concept: goto moves the dot over the symbol, then closure adds productions for every non-terminal right after a dot, repeatedly. The trap is forgetting the P items reached through R → .P.

GATE 2022 · Q65NAT2 marks · no negative· Moderate

Consider the following grammar along with translation rules. S → S₁ # T {S.val = S₁.val * T.val} S → T {S.val = T.val} T → T₁ % R {T.val = T₁.val ÷ R.val} T → R {T.val = R.val} R → id {R.val = id.val} Here # and % are operators and id is a token that represents an integer and id.val represents the corresponding integer value. The set of non-terminals is {S, T, R, P} and a subscripted non-terminal indicates an instance of the non-terminal. Using this translation scheme, the computed value of S.val for root of the parse tree for the expression 20 # 10 % 5 # 8 % 2 % 2 is _____________.

Answer: 80 - show solution

% is generated lower in the tree (by T), so it binds tighter than #; both are left-recursive, hence left-associative. # means multiply and % means divide. Group: (20) # (10 % 5) # ((8 % 2) % 2). T values: 20; 10 ÷ 5 = 2; (8 ÷ 2) ÷ 2 = 2. S = (20 × 2) × 2 = 80. Answer: 80.

Concept: Precedence comes from grammar depth, associativity from left or right recursion. The trap is evaluating left to right ignoring precedence, or treating % as modulus.

GATE 2018 · Q18MCQ1 mark · -1/3· Easy

Which one of the following statements is FALSE?

  1. (A)Context-free grammar can be used to specify both lexical and syntax rules.
  2. (B)Type checking is done before parsing.
  3. (C)High-level language programs can be translated to different Intermediate Representations.
  4. (D)Arguments to a function can be passed using the program stack.
Answer: B - show solution

(A) True: lexical rules are regular, and every regular language is context-free. (B) False: type checking is part of semantic analysis, which works on the parse tree produced by the parser, so it is done after parsing. (C) True: three-address code, syntax trees, SSA and so on. (D) True: activation records on the stack commonly hold arguments. Answer: (B).

Concept: Compiler phase order: lexical analysis, parsing, semantic analysis (type checking), IR generation. The trap is option (A); CFGs can describe tokens because regular is a subset of context-free, even though lexers use regular expressions in practice.

GATE 2018 · Q47MCQ2 marks · -2/3· Moderate

A lexical analyzer uses the following patterns to recognize three tokens T₁, T₂, and T₃ over the alphabet {a,b,c}. T₁: a?(b|c)*a T₂: b?(a|c)*b T₃: c?(b|a)*c Note that 'x?' means 0 or 1 occurrence of the symbol x. Note also that the analyzer outputs the token that matches the longest possible prefix. If the string bbaacabc is processed by the analyzer, which one of the following is the sequence of tokens it outputs?

  1. (A)T₁T₂T₃
  2. (B)T₁T₁T₃
  3. (C)T₂T₁T₃
  4. (D)T₃T₃
Answer: D - show solution

Input bbaacabc. Longest matches from the start: T₂ matches "bb" (length 2); T₁ matches "bba" ((b|c)* = bb, then a; length 3); T₃ matches "bbaac" (c? empty, (b|a)* = bbaa, then c; length 5). Longest is T₃ on "bbaac". Remaining "abc": T₁ matches only "a" (length 1), T₂ matches "ab" (length 2), T₃ matches "abc" ((b|a)* = ab, then c; length 3). Longest is T₃. Output: T₃T₃. Answer: (D).

Concept: Maximal munch: at each position try all patterns and take the longest match. The trap is committing to the first pattern that matches (T₁ or T₂ on "bb...") instead of checking which one reaches furthest.

GATE 2018 · Q48MCQ2 marks · -2/3· Moderate

Consider the following parse tree for the expression a#b$c$d#e#f, involving two binary operators $ and #. (Parse tree, described: the root is # with left child a and right child a second #. That second # has left child a $ node and right child a third # whose children are e and f. The $ node has left child another $ (with children b and c) and right child d. In bracket form the tree is a # (((b $ c) $ d) # (e # f)).) Which one of the following is correct for the given parse tree?

  1. (A)$ has higher precedence and is left associative; # is right associative
  2. (B)# has higher precedence and is left associative; $ is right associative
  3. (C)$ has higher precedence and is left associative; # is left associative
  4. (D)# has higher precedence and is right associative; $ is left associative
Answer: A - show solution

Operators lower in the tree are applied first. The $ operations (b $ c, then $ d) sit below every #, so $ binds tighter: $ has higher precedence. b$c$d is grouped as (b $ c) $ d, so $ is left associative. The # operations group as a # (X # (e # f)), each # taking the rest of the expression on its right, so # is right associative. Answer: (A).

Concept: Reading precedence and associativity from a parse tree: deeper means higher precedence; a left-leaning chain means left associative, a right-leaning chain means right associative. The trap is reading associativity of # from the top-level split alone.

GATE 2016 Set1 · Q29NAT1 mark · no negative· Moderate

Consider the following code segment. The minimum number of total variables required to convert the above code segment to static single assignment form is ________.

x = u - t;
y = x * v;
x = y + w;
y = t - z;
y = x * y;
Answer: 10 - show solution

In SSA each variable is assigned exactly once, so every assignment gets a fresh name: x1 = u - t y1 = x1 * v x2 = y1 + w y2 = t - z y3 = x2 * y2 Variables used: the inputs u, t, v, w, z (5) plus x1, y1, x2, y2, y3 (5). Total = 10. Answer: 10.

Concept: SSA renames every definition. The trap is forgetting to count the original input variables (giving 5) or trying to reuse a temporary name.

GATE 2016 Set1 · Q55NAT2 marks · no negative· Moderate

The attributes of three arithmetic operators in some programming language are given below. Operator Precedence Associativity Arity + High Left Binary - Medium Right Binary * Low Left Binary The value of the expression 2 - 5 + 1 - 7 * 3 in this language is ________.

Answer: 9 - show solution

* has the lowest precedence, so it is applied last: (2 - 5 + 1 - 7) * 3. + has the highest precedence: 5 + 1 = 6, giving 2 - 6 - 7. - is right-associative: 2 - (6 - 7) = 2 - (-1) = 3. Then 3 * 3 = 9. Answer: 9.

Concept: Parsing with non-standard precedence and associativity. The trap is applying the usual rules (giving -30) or treating - as left-associative (giving -33 after multiplying).

GATE 2016 Set1 · Q56MCQ2 marks · -2/3· Moderate

Consider the following Syntax Directed Translation Scheme (SDTS), with non-terminals {S, A} and terminals {a, b}. S → aA { print 1 } S → a { print 2 } A → Sb { print 3 } Using the above SDTS, the output printed by a bottom-up parser, for the input aab is:

  1. (A)1 3 2
  2. (B)2 2 3
  3. (C)2 3 1
  4. (D)syntax error
Answer: C - show solution

The derivation of aab is S ⇒ aA ⇒ aSb ⇒ aab. A bottom-up parser performs the reductions in reverse of this rightmost derivation: 1. Reduce the second a to S by S → a: print 2. 2. Reduce Sb to A by A → Sb: print 3. 3. Reduce aA to S by S → aA: print 1. Output: 2 3 1. Answer: (C).

Concept: Bottom-up parsing executes actions at reductions, i.e. the reverse rightmost derivation. The trap is printing in top-down (derivation) order, giving 1 3 2.

GATE 2016 Set2 · Q29MCQ1 mark · -1/3· Easy

Match the following: (P) Lexical analysis (i) Leftmost derivation (Q) Top down parsing (ii) Type checking (R) Semantic analysis (iii) Regular expressions (S) Runtime environments (iv) Activation records

  1. (A)P ↔ i, Q ↔ ii, R ↔ iv, S ↔ iii
  2. (B)P ↔ iii, Q ↔ i, R ↔ ii, S ↔ iv
  3. (C)P ↔ ii, Q ↔ iii, R ↔ i, S ↔ iv
  4. (D)P ↔ iv, Q ↔ i, R ↔ ii, S ↔ iii
Answer: B - show solution

Tokens are specified by regular expressions (P-iii). Top-down parsers build a leftmost derivation (Q-i). Type checking is part of semantic analysis (R-ii). Runtime environments manage activation records on the stack (S-iv). Answer: (B).

Concept: Compiler phases and their core tools. Top-down gives leftmost derivation; bottom-up gives a rightmost derivation in reverse.

GATE 2016 Set2 · Q55MCQ2 marks · -2/3· Moderate

Which one of the following grammars is free from left recursion?

  1. (A)S → AB A → Aa | b B → c
  2. (B)S → Ab | Bb | c A → Bd | ε B → e
  3. (C)S → Aa | B A → Bb | Sc | ε B → d
  4. (D)S → Aa | Bb | c A → Bd | ε B → Ae | ε
Answer: B - show solution

(A): A → Aa is immediate left recursion. (C): S → Aa and A → Sc give S ⇒ Aa ⇒ Sca, indirect left recursion. (D): A → Bd and B → Ae give A ⇒ Bd ⇒ Aed, indirect left recursion. (B): S starts with A, B or c; A starts with B or ε; B starts with e. No nonterminal can derive a sentential form starting with itself. Answer: (B).

Concept: Check indirect left recursion by following leftmost symbols through other rules, including through nullable prefixes. Looking only for A → A... misses (C) and (D).

GATE 2016 Set2 · Q56MCQ2 marks · -2/3· Moderate

A student wrote two context-free grammars G1 and G2 for generating a single C-like array declaration. The dimension of the array is at least one. For example, int a[10][3]; The grammars use D as the start symbol, and use six terminal symbols int ; id [ ] num. Grammar G1 Grammar G2 D → int L; D → int L; L → id [E L → id E E → num] E → E[num] E → num] [E E → [num] Which of the grammars correctly generate the declaration mentioned above?

  1. (A)Both G1 and G2
  2. (B)Only G1
  3. (C)Only G2
  4. (D)Neither G1 nor G2
Answer: A - show solution

G1: L → id [E and E → num] | num] [E generate id [num] followed by zero or more [num] groups, e.g. int id [num] [num] ; for int a[10][3];. G2: E → E[num] | [num] generates one or more [num] groups (left-recursive), so int id [num][num] ; is also produced. Both generate exactly the declarations with at least one dimension. Answer: (A) Both G1 and G2.

Concept: Grammars can split brackets across productions in unusual ways; derive a sample string to check. Left recursion (G2) does not make a grammar wrong, only unsuitable for top-down parsing.

GATE 2014 Set1 · Q27MCQ1 mark · -1/3· Easy

Which one of the following is FALSE?

  1. (A)A basic block is a sequence of instructions where control enters the sequence at the beginning and exits at the end.
  2. (B)Available expression analysis can be used for common subexpression elimination.
  3. (C)Live variable analysis can be used for dead code elimination.
  4. (D)x = 4 ∗ 5 ⇒ x = 20 is an example of common subexpression elimination.
Answer: D - show solution

A is the definition of a basic block - true. B: an expression available at a point need not be recomputed, which is exactly common subexpression elimination - true. C: an assignment to a variable that is not live afterwards is dead and can be removed - true. D: replacing 4 ∗ 5 by 20 at compile time is constant folding, not common subexpression elimination - false. Answer: (D).

Concept: Matching each data-flow analysis to the optimisation it enables. The trap is confusing constant folding (evaluate constant expressions at compile time) with CSE (reuse an already computed value).

GATE 2014 Set3 · Q27MCQ1 mark · -1/3· Easy

One of the purposes of using intermediate code in compilers is to

  1. (A)make parsing and semantic analysis simpler.
  2. (B)improve error recovery and error reporting.
  3. (C)increase the chances of reusing the machine-independent code optimizer in other compilers.
  4. (D)improve the register allocation.
Answer: C - show solution

Intermediate code separates the front end (language-specific) from the back end (machine-specific). Optimisations written on the intermediate representation are independent of both the source language and the target machine, so the same optimiser can be reused across compilers. Parsing and error reporting happen before IR generation, and register allocation is a back-end concern. Answer: (C).

Concept: Purpose of an IR: retargetability and reuse of machine-independent optimisation. The trap is option D; register allocation is done on target code, not because of the IR.

GATE 2014 Set3 · Q28MCQ1 mark · -1/3· Easy

Which of the following statements are CORRECT? 1) Static allocation of all data areas by a compiler makes it impossible to implement recursion. 2) Automatic garbage collection is essential to implement recursion. 3) Dynamic allocation of activation records is essential to implement recursion. 4) Both heap and stack are essential to implement recursion.

  1. (A)1 and 2 only
  2. (B)2 and 3 only
  3. (C)3 and 4 only
  4. (D)1 and 3 only
Answer: D - show solution

Recursion needs a fresh activation record for each active call. With purely static allocation there is one record per procedure, so recursion is impossible (1 true) and dynamic allocation of activation records is required (3 true). A stack alone suffices; no heap or garbage collector is needed (2 and 4 false). Answer: (D) 1 and 3 only.

Concept: Run-time storage organisation for recursion. The trap is statement 4: a heap is needed for dynamic data, not for recursion.