chapter

    Compiler Design Pyqs for Gate Cs

    GATE CS Compiler Design: 6 chapters, 43 previous year questions (100% of Compiler Design), 1202 practice questions and one solved question from each chapter.

    A question from this chapter

    Question 1
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    A lexical analyzer uses the following token definitions

    •
    •
    •
    •
    •

    For the string given below,


    the number of tokens (excluding ) that will be produced by the lexical analyzer is __________. (answer in integer)
    Question 2
    2026 Slot Set1 PYQ
    Level 3: Exam Standard

    Which of the following statements is/are true?

    Question 3
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    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?
    Question 4
    2026 Slot Set1 PYQ
    Level 3: Exam Standard
    Consider the following two syntax-directed definitions SDD1 and SDD2 for type declarations.

    SDD1SDD2
    Grammar
    (G1)
    Semantic RulesGrammar
    (G2)
    Semantic Rules






    is the start symbol, and , and 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 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?
    Question 5
    2023 PYQ
    Level 3: Exam Standard
    Consider the following program:

    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);
    }

    Which one of the following options represents the activation tree corresponding to the main function?
    Question 6
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the control flow graph given below.
    ENTRY B1 a = b + c B2 d = a + e B3 e = a + f B4 g = d + e EXIT Which one of the following options is the set of live variables at the exit point of each basic block?
    Free preview ends here

    Login to view the complete previous-year questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Compiler Design PYQs for GATE CS

    GATE CS Compiler Design: 6 chapters, 43 previous year questions (100% of Compiler Design), 1202 practice questions and one solved question from each chapter.

    About Compiler Design Previous Year Questions (PYQs)

    43 previous year questions from Compiler Design in GATE CS, grouped by chapter with the exam year, answer key and step-by-step solution for each.

    Compiler Design Weightage in GATE CS

    Compiler Design accounts for 43 of 43 Compiler Design previous year questions in our bank (100%), about 4.3 per paper across 10 papers.

    Compiler Design Chapter Matrix

    ChapterTopicsPYQsShare of unit PYQsPractice questions
    Compiler Phases, Lexical Analysis and Semantic ProcessingCompiler Phases, Front-end, Back-end and Symbol Tables, Lexical Tokens, Regular Expressions and Finite Automata, Semantic Analysis and Syntax-directed Type Checking, Lexical, Syntax and Semantic Error Classification921%253
    Top-down and Predictive ParsingFirst and Follow Set Computation, Ll(1) Grammar Conditions and Predictive Parsing, Ll(1) Parsing Table Construction614%172
    Bottom-up and Lr ParsingBottom-up Parser Types and Lr Grammar Classes, Operator Precedence and Shift-reduce Parsing, Lr Item Closure and Goto Computation, Lr Parsing Conflicts and Grammar Ambiguity819%233
    Syntax-directed Translation and Intermediate RepresentationsBackpatching and Control-flow Code Generation, Syntax-directed Definitions and Attribute Evaluation, Intermediate Representations and Triple Code921%243
    Runtime Environments and Procedure CallsActivation Trees and Procedure Call Sequences25%56
    Basic Blocks, Data-flow Analysis and Code OptimizationBasic Blocks and Control-flow Graph Construction, Live Variables and Data-flow Analysis, Dag Optimization, Common Subexpressions and Available Expressions921%245

    More from Compiler Design

    One Solved Question from Each Compiler Design Chapter

    Question 1 · Compiler Phases, Lexical Analysis and Semantic Processing · 2026_Set2 NAT
    A lexical analyzer uses the following token definitions

    •
    •
    •
    •
    •

    For the string given below,


    the number of tokens (excluding ) that will be produced by the lexical analyzer is __________. (answer in integer)
    Correct Answer:

    13

    Step-by-Step Solution

    Key idea: Apply the maximal munch rule (longest match) to tokenize the input string based on the given regular expressions.

    Step 1: The input string is separated by whitespace. We process each space-separated chunk.

    Step 2: x1 starts with a letter and is followed by a digit. Matches id. (1 token)

    Step 3: 23mm starts with digits. The longest match for number is 23. The remaining mm matches id. (2 tokens)

    Step 4: 78 matches number. (1 token)

    Step 5: y matches id. (1 token)

    Step 6: 7z starts with a digit. 7 matches number. z matches id. (2 tokens)

    Step 7: zz5 starts with a letter, followed by letters/digits. Matches id. (1 token)

    Step 8: 14A starts with digits. 14 matches number. A matches id. (2 tokens)

    Step 9: 8H starts with a digit. 8 matches number. H matches id. (2 tokens)

    Step 10: AaYcD starts with a letter, followed by letters. Matches id. (1 token)

    Step 11: Sum the tokens: 1 + 2 + 1 + 1 + 2 + 1 + 2 + 2 + 1 = 13.

    Answer: 13

    Question 2 · Top-Down and Predictive Parsing · 2026_Set1 MSQ

    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

    Correct Answer:

    ["C"]

    Step-by-Step Solution

    Insight: LL(1) parsers are predictive and non-backtracking. They require grammars to be free of left recursion and left-factored to avoid FIRST set conflicts.

    Exam route: Evaluate each statement against the core properties of LL(1) grammars and parser hierarchy.

    Learning route:

    Step 1: Evaluate Option A. LL(1) parsers are predictive; they use the parsing table to make deterministic choices without backtracking. FALSE.

    Step 2: Evaluate Option B. Left recursion causes infinite loops in top-down parsing and guarantees FIRST-FIRST conflicts. It must be eliminated, not required. FALSE.

    Step 3: Evaluate Option C. If a grammar has common prefixes (e.g., ), their FIRST sets will overlap, violating the LL(1) disjointness condition. Left factoring is the necessary transformation to resolve this. TRUE.

    Step 4: Evaluate Option D. The parser power hierarchy is LL(1) SLR LALR CLR. SLR is strictly more powerful than LL(1). FALSE.

    Conclusion: Only Option C is true.

    Question 3 · Bottom-Up and LR Parsing · 2026_Set2 MCQ
    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?
    1. A.

      2

    2. B.

      3

    3. C.

      4

    4. D.

      5

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: Epsilon productions in LR(0) immediately create reduce items in the initial state, which clash with any shift items for the same non-terminal, causing shift-reduce conflicts.

    Exam route: Construct the LR(0) items state by state. Count the exact (state, terminal) pairs where both a shift and a reduce action are valid in the ACTION table.

    Learning route:

    1. State I0: Contains S' -> .S, S -> .ACB, A -> .aA, and A -> . (from ). The dot is before A, not C, so C productions are not yet in I0.
    • A -> . is a reduce item (places reduce in all columns).
    • A -> .aA is a shift item on 'a'.
    • Result: 1 SR conflict (on 'a').
    1. State I1 = GOTO(I0, a): Contains A -> a.A, A -> .aA, A -> ..
    • Shift on 'a', reduce A. Result: 1 SR conflict (on 'a').
    1. State I2 = GOTO(I0, A): Contains S -> A.CB, C -> .cC, C -> ..
    • Shift on 'c', reduce C. Result: 1 SR conflict (on 'c').
    1. State I3 = GOTO(I2, c): Contains C -> c.C, C -> .cC, C -> ..
    • Shift on 'c', reduce C. Result: 1 SR conflict (on 'c').
    1. State I4 = GOTO(I2, C): Contains S -> AC.B, B -> .bB, B -> .b.
    • Only shift items (on 'b'). Result: 0 conflicts.
    1. All other states (like GOTO on S, B) have only reduce items or only shift items, yielding 0 SR conflicts.
    2. Total SR conflicts = 1 + 1 + 1 + 1 = 4.
    Question 4 · Syntax-Directed Translation and Intermediate Representations · 2026_Set1 MSQ
    Consider the following two syntax-directed definitions SDD1 and SDD2 for type declarations.

    SDD1SDD2
    Grammar
    (G1)
    Semantic RulesGrammar
    (G2)
    Semantic Rules






    is the start symbol, and , and 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 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

    Correct Answer:

    ["A","B","D"]

    Step-by-Step Solution

    Insight: This is an attribute classification and language equivalence question. The key is to check each SDD for synthesized vs. inherited attributes and trace the symbol table updates.

    Exam route: Evaluate each statement against the definitions of S-attributed and L-attributed, and manually trace a sample string like "int id1 id2".

    Learning route:

    Statement A: "The languages P and Q are the same."

    G1 generates: D → T V → int V → int V1 id → int id id.

    G2 generates: D → D1 id → T id id → int id id.

    Both generate a type followed by one or more identifiers. The languages are identical. TRUE.

    Statement B: "SDD2 is S-attributed and contains only synthesized attributes."

    In SDD2, D.type depends on D1.type or T.type (children). T.type depends on terminals. All attributes are synthesized. TRUE.

    Statement C: "SDD1 is L-attributed and contains only inherited attributes."

    In SDD1, V.type is inherited (from T), and V1.type is inherited (from V). However, T.type and D.type are synthesized. Since it contains synthesized attributes, "only inherited" is FALSE.

    Statement D: "The specifications of SDD1 and SDD2 are such that the same entries get added to the symbol table."

    For "int a b":

    SDD1: V.type = int. V1.type = int. put(b.entry, int), then put(a.entry, int).

    SDD2: D1.type = int. put(a.entry, int). Then put(b.entry, int).

    The same entries (a:int, b:int) are added. TRUE.

    Verification: Re-checking SDD1 confirms T.type is synthesized, invalidating Statement C. Statements A, B, and D hold.

    Question 5 · Runtime Environments and Procedure Calls · 2023 MCQ
    Consider the following program:

    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);
    }

    Which one of the following options represents the activation tree corresponding to the main function?
    1. A. mainf1f2f3f3f2f3f1
    2. B. mainf1f2f3f3f1
    3. C. mainf1f2f3f1
    4. D. mainf1f2f3f3f2f1
    Correct Answer:

    A

    Step-by-Step Solution

    Insight: Trace the execution step-by-step, drawing a new node for every function invocation, especially noting the recursive call in f2.

    Exam route:

    • main calls f1, f2(2), f3. So main has 3 children: f1, f2, f3.
    • f2(2) calls f3, then since 2!=1, it calls f2(1). So f2(2) has 2 children: f3, f2(1).
    • f2(1) calls f3, then since 1==1, it calls f1. So f2(1) has 2 children: f3, f1.
    • Match this tree structure with the options. Option A matches perfectly.

    Learning route:

    1. Start at main. The code shows sequential calls: f1(), f2(2), f3(). Thus, the main node has three children in order: f1, f2, f3.
    2. Examine the first f2 invocation, f2(2). Inside f2, it first calls f3(). Then it checks X==1. Since X=2, it goes to the else branch and evaluates X*f2(X-1), which invokes f2(1). Thus, the f2 node (for X=2) has two children: f3 and f2 (for X=1).
    3. Examine the second f2 invocation, f2(1). Inside this call, it first calls f3(). Then it checks X==1. Since X=1, it evaluates return f1(), invoking f1(). Thus, this second f2 node has two children: f3 and f1.
    4. The complete activation tree is: main has children [f1, f2, f3]. The first f2 has children [f3, f2]. The second f2 has children [f3, f1].
    5. Option A depicts exactly this hierarchy. Option B misses the recursive f2 call entirely. Option D incorrectly makes f3, f2, and f1 all direct children of the first f2, ignoring the nesting of the recursive call.
    Question 6 · Basic Blocks, Data-Flow Analysis and Code Optimization · 2026_Set2 MCQ
    Consider the control flow graph given below.
    ENTRY B1 a = b + c B2 d = a + e B3 e = a + f B4 g = d + e 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:

    Correct Answer:

    A

    Step-by-Step Solution

    Insight: Global liveness analysis in the presence of loops requires synthesizing backward data-flow equations with fixed-point iteration to resolve back-edge dependencies.

    Exam route: Compute IN/OUT for each block backwards. OUT(B4) = \emptyset implies IN(B4) = \{d, e\}. Backtrack to B2, B3, and B1, testing the given options for consistency.

    Learning route:

    1. This is a global data-flow question, recognizable by the CFG with a back edge and a request for live variables at block exits.
    2. The trigger is the loop (back edge B3 to B1), which requires solving a system of backward equations to a fixed point.
    3. Step 1: EXIT: OUT(B4) = \emptyset.
    4. Step 2: B4: g = d + e. USE=\{d, e\}, DEF=\{g\}. IN(B4) = (OUT(B4) - \{g\}) \cup \{d, e\} = \{d, e\}.
    5. Step 3: B2: d = a + e. USE=\{a, e\}, DEF=\{d\}. Successor is B4. OUT(B2) = IN(B4) = \{d, e\}. IN(B2) = (OUT(B2) - \{d\}) \cup \{a, e\} = \{e\} \cup \{a, e\} = \{a, e\}.
    6. Step 4: B3: e = a + f. USE=\{a, f\}, DEF=\{e\}. Successor is B1 (back edge). OUT(B3) = IN(B1). IN(B3) = (OUT(B3) - \{e\}) \cup \{a, f\}.
    7. Step 5: B1: a = b + c. USE=\{b, c\}, DEF=\{a\}. Successors are B2 and B3. OUT(B1) = IN(B2) \cup IN(B3) = \{a, e\} \cup IN(B3).
    8. Step 6: Solve fixed point. Assume IN(B1) initially \emptyset. Then OUT(B3) = \emptyset, IN(B3) = \{a, f\}. OUT(B1) = \{a, e\} \cup \{a, f\} = \{a, e, f\}. IN(B1) = (OUT(B1) - \{a\}) \cup \{b, c\} = \{e, f\} \cup \{b, c\} = \{b, c, e, f\}.
    9. Step 7: Iterate again with IN(B1) = \{b, c, e, f\}. OUT(B3) = \{b, c, e, f\}. IN(B3) = (\{b, c, e, f\} - \{e\}) \cup \{a, f\} = \{a, b, c, f\}. OUT(B1) = \{a, e\} \cup \{a, b, c, f\} = \{a, b, c, e, f\}. IN(B1) = (\{a, b, c, e, f\} - \{a\}) \cup \{b, c\} = \{b, c, e, f\}. Fixed point reached.
    10. Step 8: Final OUT sets: OUT(B1) = \{a, b, c, e, f\}, OUT(B2) = \{d, e\}, OUT(B3) = \{b, c, e, f\}, OUT(B4) = \emptyset. This matches Option A. My verified answer matches the current answer.