chapter

    Compiler Design Practice Questions 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
    Level 3: Exam Standard

    A compiler is structured with a clear separation between the front-end and back-end. The front-end is further divided into lexical, syntax, and semantic analysis. Suppose a new source language is introduced that has the EXACT same lexical and syntactic rules as the original language , but entirely different semantic rules (e.g., type checking and scope resolution). What is the MINIMUM number of front-end phases that must be completely rewritten to support ?

    Question 2
    Level 3: Exam Standard

    Consider the following context-free grammar with start symbol and the set of terminals .

    What is the number of blank (error) cells in the LL(1) parsing table for this grammar?

    Question 3
    Level 3: Exam Standard

    Let be an arbitrary context-free grammar. Let , , , and denote the number of states in the canonical collections of LR(0), SLR(1), LALR(1), and CLR(1) items of , respectively.

    Which ONE of the following tuples is IMPOSSIBLE for any context-free grammar ?

    Question 4
    Level 3: Exam Standard

    Consider the syntax-directed translation of . The relevant semantic rules are:

    1. Translate
    2. Emit

    Assume is $300$ before translating . The translation of generates 2 instructions. The translation of generates 3 instructions, and contains the index $304$.

    What is the target address backpatched into the instruction at index $304$, and what is the quad index of the unconditional emitted in step 5?

    Question 5
    Level 3: Exam Standard

    Consider an activation tree for a program execution where the sequence of procedure calls (preorder traversal) is exactly .

    Which one of the following CANNOT be the corresponding sequence of procedure returns (postorder traversal) for this same activation tree?

    Question 6
    Level 3: Exam Standard

    Consider a control flow graph with basic blocks and an Exit block. is the entry block and branches to and ; both and transfer control to ; transfers control to ; transfers control to and to Exit. The block summaries are

    and no variables are live after the program terminates, that is . The maximum, over the five basic blocks, of the number of variables in is

    Free preview ends here

    Login to view the complete practice 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 Practice Questions 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 Practice Questions

    1202 practice questions for Compiler Design in GATE CS, sorted chapter by chapter and graded from basic to exam level, each with a full solution.

    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 MCQ

    A compiler is structured with a clear separation between the front-end and back-end. The front-end is further divided into lexical, syntax, and semantic analysis. Suppose a new source language is introduced that has the EXACT same lexical and syntactic rules as the original language , but entirely different semantic rules (e.g., type checking and scope resolution). What is the MINIMUM number of front-end phases that must be completely rewritten to support ?

    1. A.

      1

    2. B.

      2

    3. C.

      3

    4. D.

      4

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: The front-end of a compiler is responsible for analyzing the source code. It is logically divided into phases that each check a specific aspect: lexical (tokens), syntax (grammar structure), and semantic (meaning and type consistency).

    Step 1: Analyze the changes in the new language . The problem states that lexical rules (how characters form tokens) and syntactic rules (how tokens form valid sentences/parse trees) are EXACTLY the same.

    Step 2: Since lexical and syntax rules are unchanged, the Lexical Analyzer and Syntax Analyzer can be reused without modification. They will produce the exact same parse tree for as they did for .

    Step 3: The semantic rules (type checking, scope resolution) are entirely different. The Semantic Analyzer is the phase that traverses the parse tree to enforce these meaning-based rules.

    Step 4: Therefore, only the Semantic Analysis phase must be completely rewritten to implement the new semantic rules.

    Step 5: The minimum number of front-end phases to rewrite is 1.

    Answer: 1

    Question 2 · Top-Down and Predictive Parsing MCQ

    Consider the following context-free grammar with start symbol and the set of terminals .

    What is the number of blank (error) cells in the LL(1) parsing table for this grammar?

    1. A.

      2

    2. B.

      3

    3. C.

      4

    4. D.

      6

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a construction and counting question. We must find the total number of cells in the table and subtract the number of filled cells.

    Step 1: Determine table dimensions.

    Non-terminals: (4 rows).

    Terminals: a, b, c, d, \$ (5 columns).

    Total cells = .

    Step 2: Compute FOLLOW sets.

    \text{FOLLOW}(S) = \{\\}$.

    \text{FOLLOW}(X) = (\text{FIRST}(YZ) \setminus \{\epsilon\}) \cup \text{FOLLOW}(S) = \{b, c, d, \\}$.

    \text{FOLLOW}(Y) = (\text{FIRST}(Z) \setminus \{\epsilon\}) \cup \text{FOLLOW}(S) = \{d, \\}$.

    \text{FOLLOW}(Z) = \text{FOLLOW}(S) = \{\\}$.

    Step 3: Count filled cells for each row.

    For : . fills M[S, a], M[S, b], M[S, c], M[S, d], M[S, \]$. (5 cells).

    For : fills . fills M[X, b], M[X, c], M[X, d], M[X, \]$. (5 cells).

    For : fills . fills . fills M[Y, d], M[Y, \]$. (4 cells).

    For : fills . fills M[Z, \]$. (2 cells).

    Step 4: Calculate blank cells.

    Total filled = .

    Blank cells = .

    (Specifically, and are blank).

    Answer: 4

    Question 3 · Bottom-Up and LR Parsing MCQ

    Let be an arbitrary context-free grammar. Let , , , and denote the number of states in the canonical collections of LR(0), SLR(1), LALR(1), and CLR(1) items of , respectively.

    Which ONE of the following tuples is IMPOSSIBLE for any context-free grammar ?

    1. A.

      (8, 8, 8, 8)

    2. B.

      (8, 8, 10, 14)

    3. C.

      (8, 8, 14, 10)

    4. D.

      (8, 8, 8, 14)

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: the four parser classes have a strict relationship on their state counts, derived from how each is constructed from the previous one.

    Step 1: Relate LR(0) and SLR(1) states.

    • SLR(1) uses the exact same automaton as LR(0); it only changes the ACTION table by using FOLLOW sets for reduce actions.
    • Therefore, always. All four options satisfy this (8 = 8).

    Step 2: Relate LALR(1) and CLR(1) states.

    • CLR(1) builds the full LR(1) automaton with exact per-item lookaheads.
    • LALR(1) is obtained by merging CLR(1) states that have the same core (same LR(0) items) but different lookaheads.
    • Merging can only reduce or maintain the number of states, never increase it.
    • Therefore, always.

    Step 3: Check each option against .

    • A: — possible (e.g., an LR(0) grammar).
    • B: — possible (SLR(1) grammar with some LALR/CLR state splitting).
    • C: — violates . IMPOSSIBLE.
    • D: — possible (LALR merges all CLR states back to 8).

    Answer: Option C.

    Question 4 · Syntax-Directed Translation and Intermediate Representations MCQ

    Consider the syntax-directed translation of . The relevant semantic rules are:

    1. Translate
    2. Emit

    Assume is $300$ before translating . The translation of generates 2 instructions. The translation of generates 3 instructions, and contains the index $304$.

    What is the target address backpatched into the instruction at index $304$, and what is the quad index of the unconditional emitted in step 5?

    1. A.

      300 and 305

    2. B.

      302 and 305

    3. C.

      300 and 306

    4. D.

      302 and 306

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: A while loop must re-evaluate the condition on every iteration. Therefore, all backward jumps (both the explicit end-of-body goto and the fall-through patches from ) must target , not the start of the body .

    Exam route:

    Step 1: Initial . Rule 1 sets .

    Step 2: Translate . It uses quads 300 and 301. becomes 302.

    Step 3: is 302.

    Step 4: Translate . It uses quads 302, 303, and 304. becomes 305. We are given .

    Step 5: Rule 5 emits at the current , which is 305. So, quad 305 is goto 300.

    Step 6: Rule 6 backpatches () to (300).

    Answer: The target address for index 304 is 300, and the unconditional goto is at quad 305.

    Question 5 · Runtime Environments and Procedure Calls MCQ

    Consider an activation tree for a program execution where the sequence of procedure calls (preorder traversal) is exactly .

    Which one of the following CANNOT be the corresponding sequence of procedure returns (postorder traversal) for this same activation tree?

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: The call sequence is the preorder traversal and the return sequence is the postorder traversal of the exact same activation tree. A pair of sequences is valid only if they can be generated by the same tree structure.

    Step 1: The root must be the first in preorder () and last in postorder (). All options satisfy this.

    Step 2: Check Option A: . This corresponds to a linear chain . Preorder: . Postorder: . Valid.

    Step 3: Check Option B: . This implies has children and . has children and . Preorder of this tree: . Postorder: . Valid.

    Step 4: Check Option C: . This implies has child . has children and . has child . Preorder: . Postorder: . Valid.

    Step 5: Check Option D: . This implies has children and . has child . has child . The preorder of this specific tree would be . This contradicts the given call sequence .

    Step 6: Since Option D requires a different preorder traversal, it cannot be the return sequence for the given call sequence.

    Answer: D

    Question 6 · Basic Blocks, Data-Flow Analysis and Code Optimization MCQ

    Consider a control flow graph with basic blocks and an Exit block. is the entry block and branches to and ; both and transfer control to ; transfers control to ; transfers control to and to Exit. The block summaries are

    and no variables are live after the program terminates, that is . The maximum, over the five basic blocks, of the number of variables in is

    1. A.

      2

    2. B.

      3

    3. C.

      4

    4. D.

      6

    Correct Answer:

    B

    Step-by-Step Solution

    Pattern: this is a backward data-flow fixed point on a cyclic CFG, recognisable because per-block and summaries, a loop structure, and the Exit boundary are given.

    Why this method: and are mutually recursive through the loop, so the system must be solved iteratively starting from the block closest to Exit.

    Insight: the loop between and propagates liveness back and forth until a fixed point is reached, but the boundary restricts what can flow out of .

    Exam route: compute first, then iterate through until the sets stop growing. Track the size of each set and find the maximum.

    Learning route:

    Step 1: .

    Step 2: Pass 1.

    .

    Assume initially. Then .

    .

    .

    Step 3: Pass 2.

    .

    .

    .

    .

    Step 4: The sets for and have converged. (size 2), (size 3).

    Step 5: Now compute and .

    .

    (size 3).

    .

    (size 3).

    Step 6: Now compute .

    .

    (size 2).

    Step 7: The sizes are . The maximum is 3.

    The tempting wrong path: a student ignores the boundary condition and assumes contains all variables . Then , size 6. This inflates all subsequent sets, making the maximum 6. It breaks at Step 1: liveness is a backward analysis whose boundary is the empty set, because after termination no instruction reads anything.

    Generalization: for liveness, always initialise at Exit with . Assuming variables are live at Exit is a forward-analysis habit that inflates the result.

    Verification: at the entry of , the set is . is live because it is read in . and are live because they are read in and not defined in . is not live because it is defined in before can read it.

    Answer: B