GATE CS Compiler Design PYQ with Solutions

Also searched as: GATE CD PYQ · GATE Compiler PYQ

All 44 Compiler Design questions from every GATE Computer Science (CS/CSE) paper from 2021 to 2026, both sets where there were two, with the official answer key and a step-by-step solution. Compiler Design carries about 6.8 marks per paper on average. Questions are grouped chapter-wise, newest paper first.

Compiler Design — average marks per paper
202620252024202320222021
668848

Compiler Design: Parser — GATE CS PYQ (21)

  1. GATE CS 2026 Set 1 · Q28Compiler Designmsq1 mark
    Which of the following statements is/are true?
    • A.
      LL(1) parser uses backtracking
    • B.
      For a grammar to be LL(1), it must be left-recursive
    • C.
      For a grammar to be LL(1), it must be left-factored
      ✓
    • D.
      The LL(1) parsers are more powerful than the SLR parsers

    Answer: C

    Solution

    Answer: C • (A) False: LL(1) is predictive and picks the production from one lookahead symbol, so it never backtracks. • (B) False: left recursion makes a grammar not LL(1). • (C) True: two alternatives with a common prefix share FIRST symbols and cause a table conflict, so the grammar must be left-factored. • (D) False: bottom-up SLR parsers handle grammars LL(1) cannot, such as left-recursive expression grammars.
    Open GATE CS 2026 Set 1 Q28 · discuss this question →
  2. GATE CS 2026 Set 1 · Q53Compiler Designmsq2 marks
    Consider the following two syntax-directed definitions SDD1 and SDD2 for type declarations.
    SDD1 grammar (G1) | Semantic rules
    ------------------+----------------------------------------
    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) | Semantic rules
    ------------------+-----------------------------------------
    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
    is the start symbol, and int, float and id are the three terminals. The non-terminal is the same as and the non-terminal is the same as . 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?
    • A.
      The languages P and Q are the same
      ✓
    • B.
      SDD2 is S-attributed and contains only synthesized attributes
      ✓
    • C.
      SDD1 is L-attributed and contains only inherited attributes
    • D.
      The specifications of SDD1 and SDD2 are such that the same entries get added to the symbol table
      ✓

    Answer: A, B, D

    Solution

    Answer: A, B, D • (A) True: both generate . • (B) True: in SDD2 every attribute (D.type, T.type) is computed from children, so it is S-attributed. • (C) False: SDD1 is L-attributed, but T.type is synthesized (and so is D.type), so it does not contain "only inherited" attributes. • (D) True: both call put once per id with the declared type.
    Open GATE CS 2026 Set 1 Q53 · discuss this question →
  3. GATE CS 2026 Set 2 · Q29Compiler Designmsq1 mark
    Which of the following grammars is/are ambiguous?
    • A.
    • B.
      ✓
    • C.
      ✓
    • D.

    Answer: B, C

    Solution

    Answer: (B), (C) - (A) generates with a unique derivation — unambiguous. - (B) the classic expression grammar has two parse trees for — ambiguous ✓ - (C) generates but a string like has multiple leftmost derivations (via then , or then ) — ambiguous ✓ - (D) right-linear, unique derivation for each — unambiguous.
    Open GATE CS 2026 Set 2 Q29 · discuss this question →
  4. GATE CS 2026 Set 2 · Q41Compiler Designmcq2 marks
    Consider the canonical parsing of the grammar below using terminals and non-terminals with as the start symbol.
    Which one of the following options gives the number of shift-reduce conflicts that will occur in the ACTION table?
    • A.
      2
    • B.
      3
    • C.
      4
    • D.
      5
      ✓

    Answer: D

    Solution

    Answer: (D) The -productions and force reductions in the same states where terminals , (and the following symbols) can be shifted, and adds a shift/reduce clash on . Building the canonical automaton and inspecting each state with a complete item against shiftable terminals yields 5 shift-reduce conflicts in total.
    Open GATE CS 2026 Set 2 Q41 · discuss this question →
  5. GATE CS 2025 Set 1 · Q46Compiler Designmsq2 marks
    Which of the following statement(s) is/are TRUE while computing First and Follow during top-down parsing by a compiler?
    • A.
      For a production , will be added to .
      ✓
    • B.
      If there is any input right end marker, it will be added to , where is the start symbol.
    • C.
      For a production , will be added to .
    • D.
      If there is any input right end marker, it will be added to , where is the start symbol.
      ✓

    Answer: A, D

    Solution

    Answer: (A) and (D) - (A) By definition, if then . True. - (D) The end-of-input marker is always placed in of the start symbol . True. - (B) The end-of-input marker goes into , never into any set. False. - (C) is never added to any set; sets contain only terminals and the end-of-input marker. False.
    Open GATE CS 2025 Set 1 Q46 · discuss this question →
  6. GATE CS 2025 Set 2 · Q22Compiler Designmcq1 mark
    Given the following syntax directed translation rules:
    Rule 1: R -> A B  { B.i = R.i - 1;  A.i = B.i;  R.i = A.i + 1; }
    Rule 2: P -> C D  { P.i = C.i + D.i;  D.i = C.i + 2; }
    Rule 3: Q -> E F  { Q.i = E.i + F.i; }
    Which ONE is the CORRECT option among the following?
    • 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
    • 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
    • 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
      ✓
    • 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

    Solution

    Answer: (C) An SDT is S-attributed if every attribute is synthesized; it is L-attributed if each inherited attribute of a symbol depends only on its parent or on symbols to its left. - Rule 1: it computes inherited attributes (), so not S-attributed. Worse, makes (left symbol) depend on (to its right) → violates L-attributed. So Rule 1 is neither. - Rule 2: is an inherited attribute (so not S-attributed), but it depends on , which is to 's left → L-attributed. - Rule 3: assigns only the synthesized attribute of the head from its children → S-attributed (hence also L-attributed). This matches option (C).
    Open GATE CS 2025 Set 2 Q22 · discuss this question →
  7. GATE CS 2025 Set 2 · Q40Compiler Designmcq2 marks
    Given a CFG : . Which ONE of the following statements is TRUE?
    • A.
      is neither LALR(1) nor SLR(1)
    • B.
      is CLR(1), not LALR(1)
    • C.
      is LALR(1), not SLR(1)
      ✓
    • D.
      is LALR(1), also SLR(1)

    Answer: C

    Solution

    Answer: (C) is LALR(1), not SLR(1) This is a classic example. After reading there is a state with items for reducing and shifting (from ). Under SLR, the reduce is tried on all of , and also allows a shift, causing a shift-reduce conflict — so is not SLR(1). With LR(1)/LALR(1) lookaheads, the reduce is restricted to the specific contexts (lookaheads) that do not include the conflicting , resolving the conflict. So is LALR(1) but not SLR(1).
    Open GATE CS 2025 Set 2 Q40 · discuss this question →
  8. GATE CS 2025 Set 2 · Q51Compiler Designmsq2 marks
    Consider grammars and :
    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
    Which of the following option(s) is/are CORRECT?
    • A.
      is not LL(1) and is LL(1).
    • B.
      is LL(1) and is not LL(1).
    • C.
      and are not LL(1).
      ✓
    • D.
      and are ambiguous.
      ✓

    Answer: C, D

    Solution

    Answer: (C) and (D) (C) Neither grammar is LL(1). In the two -productions both begin with if, giving a FIRST/FIRST conflict. In both and also begin with if, so FIRSTFIRST — again not LL(1). (D) Both grammars exhibit the dangling-else construct and admit more than one parse tree for such inputs (per the official key), so both are ambiguous.
    Open GATE CS 2025 Set 2 Q51 · discuss this question →
  9. GATE CS 2024 Set 1 · Q26Compiler Designmsq1 mark
    Which of the following is/are Bottom-Up Parser(s)?
    • A.
      Shift-reduce Parser
      ✓
    • B.
      Predictive Parser
    • C.
      LL(1) Parser
    • D.
      LR Parser
      ✓

    Answer: A, D

    Solution

    Answer: (A) and (D) Bottom-up parsers build the parse tree from leaves to root using shift-reduce actions: the generic Shift-reduce parser (A) and LR parsers (D). Predictive and LL(1) parsers are top-down.
    Open GATE CS 2024 Set 1 Q26 · discuss this question →
  10. GATE CS 2024 Set 1 · Q33Compiler Designnat1 mark
    With operator rules: (Highest, Left-assoc), (High, Right), (Medium, Right), (Low, Right), the value of is __________

    Answer: 6

    Solution

    Answer: 6 Bind tightest-first. (highest) groups adjacent numbers: and . The expression becomes . Next (high, right-assoc): ... resolving right-to-left with then lowest:
    Carefully applying the given precedence and associativities, the fully parenthesized form evaluates to (integer arithmetic).
    Open GATE CS 2024 Set 1 Q33 · discuss this question →
  11. GATE CS 2024 Set 1 · Q37Compiler Designmcq2 marks
    For the SDD with , , , , and , the value computed for input "MMLK" is
    • A.
      45
      ✓
    • B.
      50
    • C.
      55
    • D.
      65

    Answer: A

    Solution

    Answer: (A) 45 Parse "MMLK" as ="MM", ="L", =, ="K". - ="MM": with innermost : . - ="L": . - . - ="K".
    Open GATE CS 2024 Set 1 Q37 · discuss this question →
  12. GATE CS 2024 Set 1 · Q38Compiler Designmcq2 marks
    Grammar with , , . Terminals . FIRST(S)=\{c,d,f\}, FIRST(T)=\{a,b,\}, FIRST(R)=\{c,\}, FOLLOW(S)=FOLLOW(T)=\{c,f,$\}, FOLLOW(R)=\{f\}. Which option correctly fills the productions?
    • A.
      (1) (2) (3)
      ✓
    • B.
      (1) (2) (3)
    • C.
      (1) (2) (3)
    • D.
      (1) (2) (3)

    Answer: A

    Solution

    Answer: (A) FIRST(S) must be : gives ; the other rule must contribute and . gives FIRST FIRST(R). So (1) . FIRST(T) contains , so is needed: (2) . FIRST(R) and gives FIRST (plus from the rule): (3) . This matches option (A).
    Open GATE CS 2024 Set 1 Q38 · discuss this question →
  13. GATE CS 2024 Set 2 · Q40Compiler Designmcq2 marks
    For , , , fill the partially-filled LL(1) table cells (1),(2),(3),(4). Which combination is correct?
    • A.
      (1) (2) (3) (4)
      ✓
    • B.
      (1) (2) (3) (4)
    • C.
      (1) (2) (3) blank (4) blank
    • D.
      (1) (2) (3) blank (4) blank

    Answer: A

    Solution

    Answer: (A) FIRST (A can be ) and FIRST. So under : (cell 1); under : (cell 2). For : FOLLOW includes and , so appears under (cell 3). For : appears under (cell 4). This matches (A).
    Open GATE CS 2024 Set 2 Q40 · discuss this question →
  14. GATE CS 2024 Set 2 · Q65Compiler Designnat2 marks
    Augmented grammar: ; ; ; . With , the number of items in is ___________

    Answer: 9

    Solution

    Answer: 9 moves the dot past in every item with , giving and , then closure of re-adds all -, -, -productions:
    S' -> S.          S -> S.S
    S -> .SS   S -> .Aa   S -> .bAc   S -> .Bc   S -> .bBa
    A -> .d#   B -> .@
    That is items.
    Open GATE CS 2024 Set 2 Q65 · discuss this question →
  15. GATE CS 2022 · Q13Compiler Designmcq1 mark
    Which one of the following statements is TRUE?
    • A.
      The LALR(1) parser for G cannot have a reduce-reduce conflict if the LR(1) parser for G does not have a reduce-reduce conflict.
    • B.
      Symbol table is accessed only during the lexical analysis phase.
    • C.
      Data flow analysis is necessary for run-time memory management.
    • D.
      LR(1) parsing is sufficient for deterministic context-free languages.
      ✓

    Answer: D

    Solution

    Answer: (D) Every deterministic context-free language has an LR(1) grammar, so LR(1) parsing suffices for DCFLs -> (D) true. (A) is false: merging LR(1) states in LALR(1) can introduce reduce-reduce conflicts even when LR(1) has none. (B) false (symbol table used across many phases). (C) false (data-flow analysis is for optimization, not run-time memory management).
    Open GATE CS 2022 Q13 · discuss this question →
  16. GATE CS 2022 · Q29Compiler Designnat1 mark
    Consider the augmented grammar with as terminals: ; ; ; . If , then contains exactly __________ items.

    Answer: 5

    Solution

    Answer: 5 . Taking on "+" moves the dot past "+" in the second item: . Its closure adds the -productions and then the -productions:
    [S -> S + . R]
    [R -> . R*P]   [R -> . P]
    [P -> . (S)]   [P -> . id]
    That is 5 items.
    Open GATE CS 2022 Q29 · discuss this question →
  17. GATE CS 2021 Set 1 · Q15Compiler Designmcq1 mark
    Consider the following statements. : Every SLR(1) grammar is unambiguous but there are certain unambiguous grammars that are not SLR(1). : For any context-free grammar, there is a parser that takes at most time to parse a string of length . Which one of the following options is correct?
    • A.
      is true and is false
    • B.
      is false and is true
    • C.
      is true and is true
      ✓
    • D.
      is false and is false

    Answer: C

    Solution

    Every SLR(1) grammar is unambiguous, and some unambiguous grammars are not SLR(1) — is true. The CYK algorithm parses any CFG in time — is true.
    Open GATE CS 2021 Set 1 Q15 · discuss this question →
  18. GATE CS 2021 Set 1 · Q36Compiler Designmcq2 marks
    Consider the following grammar (that admits a series of declarations, followed by expressions) and the associated syntax directed translation (SDT) actions, given as pseudo-code:
    P -> D* E*
    D -> int  ID   { record that ID.lexeme is of type int }
    D -> bool ID   { record that ID.lexeme is of type bool }
    E -> E1 + E2   { check that E1.type = E2.type = int; set E.type := int }
    E -> ! E1      { check that E1.type = bool; set E.type := bool }
    E -> ID        { set E.type := int }
    With respect to the above grammar, which one of the following choices is correct?
    • A.
      The actions can be used to correctly type-check any syntactically correct program.
    • B.
      The actions can be used to type-check syntactically correct integer variable declarations and integer expressions.
      ✓
    • C.
      The actions can be used to type-check syntactically correct boolean variable declarations and boolean expressions.
    • D.
      The actions will lead to an infinite loop.

    Answer: B

    Solution

    The rule unconditionally sets type int, ignoring a bool declaration, so boolean expressions are not correctly checked. The actions only reliably type-check integer declarations and integer expressions.
    Open GATE CS 2021 Set 1 Q36 · discuss this question →
  19. GATE CS 2021 Set 1 · Q41Compiler Designmcq2 marks
    Consider the following context-free grammar where the set of terminals is .
    S -> d a T | R f
    T -> a S | b a T | e
    R -> c a T R | e        (e denotes epsilon)
    The following is a partially-filled LL(1) parsing table; the numbered cells (1), (2), (3), (4) are to be filled (blank denotes an empty cell):
          a            b         c          d            f       $
    S                          (1)       S->daT       (2)
    T   T->aS     T->baT      (3)                    T->e      (4)
    R                         R->caTR               R->e
    Which one of the following choices represents the correct combination for the numbered cells in the parsing table ("blank" denotes that the corresponding cell is empty)?
    • A.
      (1) S->Rf (2) S->Rf (3) T->e (4) T->e
      ✓
    • B.
      (1) blank (2) S->Rf (3) T->e (4) T->e
    • C.
      (1) S->Rf (2) blank (3) blank (4) T->e
    • D.
      (1) blank (2) S->Rf (3) blank (4) blank

    Answer: A

    Solution

    Computing FIRST and FOLLOW sets: is nullable with , so is entered under both and (cells 1 and 2). is nullable, and FOLLOW contains and , so is entered in cells 3 and 4. This matches option (A).
    Open GATE CS 2021 Set 1 Q41 · discuss this question →
  20. GATE CS 2021 Set 2 · Q13Compiler Designmcq1 mark
    Consider the following ANSI C program.
    int main() {
        Integer x;
        return 0;
    }
    Which one of the following phases of a compiler will detect the error (if any) in the above program?
    • A.
      Lexical analyzer
    • B.
      Syntax analyzer
    • C.
      Semantic analyzer
      ✓
    • D.
      Machine dependent code optimizer

    Answer: C

    Solution

    Integer is a valid identifier token (passes lexical analysis) and Integer x; is syntactically a valid declaration form (passes parsing). The error — that Integer is not a declared type — is caught during semantic analysis.
    Open GATE CS 2021 Set 2 Q13 · discuss this question →
  21. GATE CS 2021 Set 2 · Q61Compiler Designnat2 marks
    Consider the following augmented grammar with as the set of terminals.
    S' -> S
    S  -> S # c S
    S  -> S S
    S  -> S @
    S  -> < S >
    S  -> a
    S  -> b
    S  -> c
    Let . The number of items in the set is ______.

    Answer: 8

    Solution

    Computing gives the item plus the closure on ; taking on again reproduces the same kernel item with its closure, yielding 8 items in total.
    Open GATE CS 2021 Set 2 Q61 · discuss this question →

Compiler Design: Intermediate Code & Code Optimization — GATE CS PYQ (11)

  1. GATE CS 2026 Set 1 · Q42Compiler Designmcq2 marks
    Consider the control flow graph shown in the figure. 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.
    GATE CS 2026 Set 1 Question 42 figure
    • A.
      B4: B5:
    • B.
      B4: B5:
    • C.
      B4: B5:
    • D.
      B4: B5:
      ✓

    Answer: D

    Solution

    Answer: (D) An expression is redundant at a point only if it is available on every path reaching that point: computed earlier, with no operand redefined since.
    Expression  | Path via B2                        | Path via B3                    | Redundant?
    ------------+------------------------------------+--------------------------------+-----------
    g * k at B4 | computed (a = g*k), g, k unchanged | computed (t = g*k)             | Yes
    b + i at B4 | available from B1                  | B3 redefines b, which kills it | No
    c + m at B5 | never computed on this path        | computed in B3                 | No
    So B4 has {g*k} and B5 has none. The traps are , killed by in B3, and , which is computed on only one path.
    Open GATE CS 2026 Set 1 Q42 · discuss this question →
  2. GATE CS 2026 Set 2 · Q45Compiler Designmcq2 marks
    Consider the control flow graph given below. Which one of the following options is the set of live variables at the exit point of each basic block?
    GATE CS 2026 Set 2 Question 45 figure
    • A.
      B1:{a, b, c, e, f}, B2:{d, e}, B3:{b, c, e, f}, B4:
      ✓
    • B.
      B1:, B2:{d, e}, B3:{a, c, f}, B4:
    • C.
      B1:{a, b, c, e, f}, B2:{d, e}, B3:{c, e, f}, B4:
    • D.
      B1:, B2:{d, e, f}, B3:{a, b, c, e, f}, B4:

    Answer: A

    Solution

    Answer: (A) Working backwards (a variable is live at a point if it is used later before being redefined): - At B4 exit (program end): . - B3 () loops back to B1, which uses ; is used in B2/B4 and again in B3 — live out of B3 . - B2 () flows to B4 which uses — live out . - B1 () reaches B2 and B3 which together use — live out . This matches option (A).
    Open GATE CS 2026 Set 2 Q45 · discuss this question →
  3. GATE CS 2025 Set 1 · Q13Compiler Designmcq1 mark
    Which ONE of the following techniques used in compiler code optimization uses live variable analysis?
    • A.
      Run-time function call management
    • B.
      Register assignment to variables
      ✓
    • C.
      Strength reduction
    • D.
      Constant folding

    Answer: B

    Solution

    Answer: (B) Register assignment to variables Live variable analysis computes, for each program point, which variables are live (will be used before being redefined). This is exactly the information needed for register allocation: two variables that are never simultaneously live can share a register, and dead values need not be kept in registers. Strength reduction and constant folding are algebraic/value optimizations; run-time call management is unrelated to liveness.
    Open GATE CS 2025 Set 1 Q13 · discuss this question →
  4. GATE CS 2025 Set 1 · Q52Compiler Designnat2 marks
    Refer to the given 3-address code sequence, 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

    Solution

    Answer: 6 Leaders are: the first statement, any jump target, and any statement following a conditional/unconditional jump. - First statement: 1001. - Jump targets: 1003, 1002, 1013. - Statements right after a jump: 1010 (after 1009), 1012 (after 1011). Leaders . Each leader begins a basic block:
    B1: 1001
    B2: 1002
    B3: 1003–1009
    B4: 1010–1011
    B5: 1012
    B6: 1013–1017
    That is 6 basic blocks.
    Open GATE CS 2025 Set 1 Q52 · discuss this question →
  5. GATE CS 2025 Set 2 · Q21Compiler Designmcq1 mark
    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 expressions 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?
    • A.
      Only (I) is correct.
    • B.
      Only (II) is correct.
    • C.
      Both (I) and (II) are correct.
      ✓
    • D.
      Neither (I) nor (II) is correct.

    Answer: C

    Solution

    Answer: (C) Both (I) and (II) are correct. Backpatching keeps lists of incomplete jump targets and fills them in once the target label is known. This lets a compiler generate code for Boolean expressions (true/false exit jumps) and for flow-of-control statements (if-else, while) in a single pass. Both (I) and (II) hold.
    Open GATE CS 2025 Set 2 Q21 · discuss this question →
  6. GATE CS 2024 Set 1 · Q39Compiler Designmcq2 marks
    For the given 15-line three-address pseudo-code (with gotos to L4, L3, L11), the number of basic blocks and the number of instructions in the largest basic block, respectively, are
    • A.
      6 and 6
    • B.
      6 and 7
    • C.
      7 and 7
    • D.
      7 and 6
      ✓

    Answer: D

    Solution

    Answer: (D) 7 and 6 Leaders: L1 (first); jump targets L3, L4, L11; and lines right after a jump: L10 (after L9), L13 (after L12), L15 (after L14). Leaders -> 7 basic blocks:
    B1: L1-L2    B2: L3     B3: L4-L9   B4: L10
    B5: L11-L12  B6: L13-L14  B7: L15
    Largest block is B3 = L4..L9 = 6 instructions. So 7 blocks, largest 6.
    Open GATE CS 2024 Set 1 Q39 · discuss this question →
  7. GATE CS 2024 Set 2 · Q43Compiler Designmcq2 marks
    For , fill the missing triples (1),(3),(6). Which option is correct?
    • A.
      (1) =[] s i (3) * (0)(2) (6) []= x i
      ✓
    • B.
      (1) []= s i (3) - (0)(2) (6) =[] x (5)
    • C.
      (1) =[] s i (3) * (0)(2) (6) []= x (5)
    • D.
      (1) []= s i (3) - (0)(2) (6) =[] x i

    Answer: A

    Solution

    Answer: (A) The three-address form of uses the triples: (0) + p r ; (1) =[] s i (fetch s[i]) ; (2) uminus (1) ; (3) * (0)(2) ; (4) / u w ; (5) + (3)(4) ; (6) []= x i (address of x[i]) ; (7) = (6)(5). So the missing entries are (1) =[] s i, (3) * (0)(2), and (6) []= x i - option (A).
    Open GATE CS 2024 Set 2 Q43 · discuss this question →
  8. GATE CS 2021 Set 1 · Q60Compiler Designnat2 marks
    Consider the following C code segment:
    a = b + c;
    e = a + 1;
    d = b + c;
    f = d + 1;
    g = e + f;
    In a compiler, this code segment is represented internally as a directed acyclic graph (DAG). The number of nodes in the DAG is ______.

    Answer: 6

    Solution

    Leaves: b, c, and the constant 1 (3 nodes). b+c is a common subexpression shared by a and d (1 node). a+1 (= e, and d+1=f share the same value as they add 1 to the same node) and e+f add internal nodes, for a total of 6 distinct nodes.
    Open GATE CS 2021 Set 1 Q60 · discuss this question →
  9. GATE CS 2021 Set 2 · Q23Compiler Designmsq1 mark
    In the context of compilers, which of the following is/are NOT an intermediate representation of the source program?
    • A.
      Three address code
    • B.
      Abstract Syntax Tree (AST)
    • C.
      Control Flow Graph (CFG)
    • D.
      Symbol table
      ✓

    Answer: D

    Solution

    Three-address code, the AST and the CFG are all intermediate representations of the program. The symbol table is an auxiliary data structure storing identifier information, not an intermediate representation of the source program.
    Open GATE CS 2021 Set 2 Q23 · discuss this question →
  10. GATE CS 2021 Set 2 · Q40Compiler Designmcq2 marks
    Consider the following ANSI C code segment:
    z = x + 3 + y->f1 + y->f2;
    for (i = 0; i < 200; i = i + 2) {
        if (z > i) {
            p = p + x + 3;
            q = q + y->f1;
        } else {
            p = p + y->f2;
            q = q + x + 3;
        }
    }
    Assume that y points to a struct (allocated on the heap) with two fields f1 and f2, and that the local variables x, y, z, p, q, i are allotted registers. Common sub-expression elimination (CSE) is applied. The number of addition operations and dereference operations (of the form y->f1 or y->f2) in the optimized code, respectively, are:
    • A.
      403 and 102
    • B.
      203 and 2
    • C.
      303 and 102
    • D.
      303 and 2
      ✓

    Answer: D

    Solution

    The dereferences y->f1 and y->f2 are loop-invariant and computed once (2 total) after CSE. Counting the additions that remain after eliminating the common subexpression x+3 across the loop's 100 iterations gives 303 additions. Hence 303 and 2.
    Open GATE CS 2021 Set 2 Q40 · discuss this question →
  11. GATE CS 2021 Set 2 · Q48Compiler Designmcq2 marks
    For a statement in a program, in the context of liveness analysis, the following sets are defined: : the set of variables used in . : the set of variables that are live at the entry of . : the set of variables that are live at the exit of . Consider a basic block that consists of two statements, followed by . Which one of the following statements is correct?
    • A.
      ✓
    • B.
    • C.
    • D.

    Answer: A

    Solution

    Within a straight-line basic block, the point after is exactly the point before , so the variables live at the exit of equal those live at the entry of : .
    Open GATE CS 2021 Set 2 Q48 · discuss this question →

Compiler Design: Basics & Lexical — GATE CS PYQ (9)

  1. GATE CS 2026 Set 1 · Q27Compiler Designmsq1 mark
    Consider the following C statements:
    char *str1 = "Hello;        /* Statement S1 */
    char *str2 = "Hello;";      /* Statement S2 */
    int  *str3 = "Hello";       /* Statement S3 */
    Which of the following options is/are correct?
    • A.
      S1 and S2 have syntactic errors
    • B.
      S2 has a lexical error and S3 has a syntactic error
    • C.
      S1 has a lexical error and S3 has a semantic error
      ✓
    • D.
      S1 has a syntactic error and S3 has a semantic error

    Answer: C

    Solution

    Answer: C • S1: the string literal is never closed, so the lexer cannot form a valid token. That is a lexical error. • S2: "Hello;" is a valid string, so there is no error. • S3: the tokens and syntax are fine, but a char* is assigned to an int*. That type mismatch is caught during type checking, so it is a semantic error.
    Open GATE CS 2026 Set 1 Q27 · discuss this question →
  2. GATE CS 2026 Set 2 · Q35Compiler Designnat1 mark
    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 ) that will be produced by the lexical analyzer is __________. (answer in integer)

    Answer: 13

    Solution

    An id must start with a letter; a number is a run of digits. Scanning (maximal munch): x1→id(1); 23mm→23+mm(2); 78→number(1); y→id(1); 7z→7+z(2); zz5→id(1); 14A→14+A(2); 8H→8+H(2); AaYcD→id(1). Total .
    Open GATE CS 2026 Set 2 Q35 · discuss this question →
  3. GATE CS 2025 Set 1 · Q12Compiler Designmcq1 mark
    Which ONE of the following statements is FALSE regarding the symbol table?
    • A.
      Symbol table is responsible for keeping track of the scope of variables.
    • B.
      Symbol table can be implemented using a binary search tree.
    • C.
      Symbol table is not required after the parsing phase.
      ✓
    • D.
      Symbol table is created during the lexical analysis phase.

    Answer: C

    Solution

    Answer: (C) — this statement is FALSE. The symbol table is used well beyond parsing — in semantic analysis (type checking, scope resolution), intermediate code generation, optimization and final code generation. So "not required after the parsing phase" is false. The other statements are true: the symbol table tracks scope (A), can be implemented with a BST/hash table (B), and entries begin to be created as early as lexical analysis (D).
    Open GATE CS 2025 Set 1 Q12 · discuss this question →
  4. GATE CS 2024 Set 2 · Q21Compiler Designmcq1 mark
    Match Set X to Set Y: P. Lexical Analyzer, Q. Syntax Analyzer, R. Intermediate Code Generator, S. Code Optimizer <-> 1. Abstract Syntax Tree, 2. Token, 3. Parse Tree, 4. Constant Folding.
    • A.
      P-4; Q-1; R-3; S-2
    • B.
      P-2; Q-3; R-1; S-4
      ✓
    • C.
      P-2; Q-1; R-3; S-4
    • D.
      P-4; Q-3; R-2; S-1

    Answer: B

    Solution

    Answer: (B) P-2; Q-3; R-1; S-4 Lexical analyzer produces Tokens (2); syntax analyzer produces the Parse Tree (3); intermediate code generation works from the Abstract Syntax Tree (1); code optimization includes Constant Folding (4).
    Open GATE CS 2024 Set 2 Q21 · discuss this question →
  5. GATE CS 2024 Set 2 · Q29Compiler Designmsq1 mark
    Which statements is/are FALSE?
    • A.
      An attribute grammar is an SDD whose semantic-rule functions have no side effects
    • B.
      The attributes in an L-attributed definition cannot always be evaluated in depth-first order
      ✓
    • C.
      Synthesized attributes can be evaluated by a bottom-up parser as the input is parsed
    • D.
      All L-attributed definitions based on an LR(1) grammar can be evaluated using bottom-up parsing
      ✓

    Answer: B, D

    Solution

    Answer: (B) and (D) - (B) is FALSE: L-attributed definitions are specifically designed so that all attributes CAN be evaluated in a single depth-first (left-to-right) traversal. - (D) is FALSE: not every L-attributed SDD on an LR grammar can be evaluated bottom-up (inherited attributes can be problematic); only the S-attributed subset is always bottom-up evaluable. - (A) and (C) are TRUE statements.
    Open GATE CS 2024 Set 2 Q29 · discuss this question →
  6. GATE CS 2023 · Q11Compiler Designmcq1 mark
    Consider statements about a compiler. S1: The front-end includes phases independent of the target hardware. S2: The back-end includes phases specific to the target hardware. S3: The back-end includes phases specific to the programming language of the source code. Identify the CORRECT option.
    • A.
      Only S1 is TRUE.
    • B.
      Only S1 and S2 are TRUE.
      ✓
    • C.
      S1, S2, and S3 are all TRUE.
    • D.
      Only S1 and S3 are TRUE.

    Answer: B

    Solution

    Answer: (B) Only S1 and S2 are TRUE The front-end (lexical/syntax/semantic analysis, IR generation) is machine-independent -> S1 true. The back-end (code generation, machine-specific optimization) is target-hardware specific -> S2 true. The back-end is NOT source-language specific (it works on IR) -> S3 false.
    Open GATE CS 2023 Q11 · discuss this question →
  7. GATE CS 2023 · Q19Compiler Designmcq1 mark
    Given letter -> [A-Za-z], digit -> [0-9], id -> letter (letter | digit)*, which NFA with epsilon-transitions (shown as options A-D) accepts the set of valid identifiers?
    GATE CS 2023 Question 19 figure
    • A.
      (A)
    • B.
      (B)
    • C.
      (C)
      ✓
    • D.
      (D)

    Answer: C

    Solution

    Answer: (C) A valid identifier matches letter (letter | digit)*. The correct NFA reads exactly one letter to reach the accepting (double-circle) state, then has an epsilon-structured loop that consumes any number of further letters or digits and returns to the accepting state. Option (C) is the automaton whose epsilon-transitions correctly implement the (letter | digit)* loop after the first letter. The others either accept the empty string or wire the epsilon-loop so it does not faithfully realize (letter | digit)*.
    Open GATE CS 2023 Q19 · discuss this question →
  8. GATE CS 2023 · Q60Compiler Designnat2 marks
    For the SDT computing a signed binary value (N -> I # F), the value computed for input "10#011" (rounded to three decimals) is __________

    Answer: 2.374 to 2.376

    Solution

    Answer: 2.375 before # is the integer part "10" ; after # is the fraction ".011" . .
    Open GATE CS 2023 Q60 · discuss this question →
  9. GATE CS 2022 · Q65Compiler Designnat2 marks
    Grammar with rules: ; ; ; ; (# and % are operators, id an integer). The computed for the root of the parse tree of is __________.

    Answer: 80

    Solution

    Answer: 80 Rules: (so # means multiply) and (so % means divide), left-associative with % binding tighter than #. Expression groups as :
    10 % 5         = 2
    8 % 2 % 2      = (8/2)/2 = 2
    20 * 2 * 2     = 80
    Open GATE CS 2022 Q65 · discuss this question →

Compiler Design: Runtime Environment — GATE CS PYQ (3)

  1. GATE CS 2023 · Q36Compiler Designmcq2 marks
    For the given program (main calls f1(); f2(2); f3();, where f2 calls f3() then recurses), which option represents the activation tree of main?
    GATE CS 2023 Question 36 figure
    • A.
      (A)
      ✓
    • B.
      (B)
    • C.
      (C)
    • D.
      (D)

    Answer: A

    Solution

    Answer: (A) main calls f1, then f2(2), then f3. Inside f2(2): it first calls f3, then (since X!=1) calls f2(1); f2(1) calls f3 then f1. So under f2 the children are f3 and f2(1), and under f2(1) are f3 and f1. The activation tree with main's children {f1, f2, f3}, f2's children {f3, f2}, and the inner f2's children {f3, f1} matches option (A).
    Open GATE CS 2023 Q36 · discuss this question →
  2. GATE CS 2023 · Q37Compiler Designmcq2 marks
    For the control flow graph shown (blocks B1-B4, with B1: i=m-1, j=n, a=10; B2: i=i+1, j=j-1; B3: a=20; B4: i=a+1), which choice lists the live variables at the EXIT of each basic block?
    GATE CS 2023 Question 37 figure
    • A.
      B1: {}, B2: {a}, B3: {a}, B4: {a}
    • B.
      B1: {i,j}, B2: {a}, B3: {a}, B4: {i}
    • C.
      B1: {a,i,j}, B2: {a,i,j}, B3: {a,i}, B4: {a}
    • D.
      B1: {a,i,j}, B2: {a,j}, B3: {a,j}, B4: {a,i,j}
      ✓

    Answer: D

    Solution

    Answer: (D) B1: {a,i,j}, B2: {a,j}, B3: {a,j}, B4: {a,i,j} Compute live variables backward (a variable is live at a point if it is used later before being redefined). Tracing the uses — B4 uses (and loops back using ), B2 uses , B3 defines used in B4 — gives the live-out sets B1:{a,i,j}, B2:{a,j}, B3:{a,j}, B4:{a,i,j}, matching option (D).
    Open GATE CS 2023 Q37 · discuss this question →
  3. GATE CS 2021 Set 1 · Q14Compiler Designmcq1 mark
    Consider the following statements. : The sequence of procedure calls corresponds to a preorder traversal of the activation tree. : The sequence of procedure returns corresponds to a postorder traversal of the activation tree. Which one of the following options is correct?
    • A.
      is true and is false
    • B.
      is false and is true
    • C.
      is true and is true
      ✓
    • D.
      is false and is false

    Answer: C

    Solution

    In an activation tree, calls happen in preorder (a procedure is entered before its callees) and returns happen in postorder (a procedure returns after all its callees). Both statements are true.
    Open GATE CS 2021 Set 1 Q14 · discuss this question →

Take a Compiler Design chapter-wise test

Pick any chapters and attempt their PYQs with a timer in the GATE interface, with instant analysis — free.

Create free account →

More GATE CS PYQ subject-wise