GATE CS Compiler Design Previous Year Questions

60 solved GATE CS questions on Compiler Design, drawn from 10 exam years and grouped by year. Every question shows the official answer and a step-by-step solution.

Want to practise this topic and ask follow-up questions? Explore Success Tracker.

Revision companion

Compiler Design: follow the grammar through each translation phase

Trace how source text becomes tokens, parse trees, and intermediate code. These selected foundations connect lexical analysis, parsing, syntax-directed translation, and basic optimization. Each phase's output is the next phase's input; errors in one phase propagate forward.

Our study notes and original examples support the PYQs below; they are not official exam questions or a replacement for the current syllabus.

Before you start

  • Regular expressions and finite automata for lexical patterns.
  • Context-free grammars, derivations, and parse trees.

Concepts to revise before solving

Lexical analysis

A lexer converts a character stream into tokens using patterns typically described by regular expressions. Longest match and priority rules resolve ambiguity when multiple patterns match. The lexer does not check grammatical structure.

Check yourself: Does the longest-match rule apply before the priority rule?

Top-down parsing and LL(1)

A predictive parser chooses a production using the current non-terminal and lookahead token. An LL(1) grammar requires: no left recursion, and disjoint FIRST sets for alternatives (using FOLLOW when FIRST contains ε). Left recursion must be eliminated; left factoring resolves common prefixes.

Check yourself: Do FIRST sets of alternative productions overlap for any non-terminal?

Bottom-up parsing and LR

LR parsers shift tokens onto a stack and reduce when the top matches a production's right side. SLR uses FOLLOW sets to decide reductions; CLR and LALR use more precise lookahead sets. An LR(1) grammar is unambiguous, but not every unambiguous grammar is LR(1). Shift-reduce and reduce-reduce conflicts indicate the grammar is not in the parser's class.

Check yourself: Is the conflict a genuine grammar ambiguity or a limitation of the parser class?

Syntax-directed translation

Attributes attached to grammar symbols compute values during parsing. Synthesized attributes flow up the parse tree; inherited attributes flow down. An S-attributed definition uses only synthesized attributes and can be evaluated in a single bottom-up pass.

Check yourself: Does the evaluation order respect attribute dependencies?

Intermediate code and basic optimization

Three-address code represents expressions using at most one operator per statement. Basic blocks are maximal sequences with no branches except at entry and exit. Common subexpression elimination avoids recomputing the same value; dead code elimination removes results that are never used.

Check yourself: Is the value from this computation used on any path to the exit?

Mistakes to avoid

Attempting LL(1) parsing on a left-recursive grammar.
Eliminate left recursion first: A → Aα | β becomes A → βA', A' → αA' | ε.
Confusing FIRST and FOLLOW sets.
FIRST(α) is the set of terminals α can begin with. FOLLOW(A) is the set of terminals that can appear after A in any sentential form.
Assuming every reduce-reduce conflict means the grammar is ambiguous.
It means the grammar is not in the parser class (e.g., not SLR). A more powerful parser or grammar transformation may resolve it.

Original teaching example · not a PYQ

Work through the reasoning

Original mini-example: compute FIRST and FOLLOW sets for the grammar S → aAB, A → b | ε, B → c.

  1. FIRST(B) = {c}. FIRST(A) = {b, ε}. FIRST(S) = {a} (the leading terminal of the only production).
  2. FOLLOW(S) = {$} since S is the start symbol.
  3. In S → aAB: FOLLOW(A) includes FIRST(B) = {c}. FOLLOW(B) includes FOLLOW(S) = {$}.
  4. Since A can derive ε, tokens after A come from FIRST(B), which is {c}. So FOLLOW(A) = {c}.

FIRST(S)={a}, FIRST(A)={b,ε}, FIRST(B)={c}. FOLLOW(S)={$}, FOLLOW(A)={c}, FOLLOW(B)={$}.

Try it before reading the answer

Is the grammar S → Sa | b left-recursive? If so, eliminate the left recursion.

Show answer and reasoning

Yes. After elimination: S → bS', S' → aS' | ε.

S → Sa is direct left recursion with α = a and β = b. Applying the transformation: S → βS' and S' → αS' | ε gives S → bS', S' → aS' | ε. This generates the same language: b followed by zero or more a's.

Go deeper with free learning resources

Supplemental reading, not an official GATE reading list or an endorsement of these notes.

  • Stanford CS143: Compilers lecture notes

    Free supplemental lecture handouts covering lexical analysis, parsing, semantic analysis, and code generation from Stanford's undergraduate compilers course.

Apply this to the previous-year questions

Previous-year questions by year

This page shows 60 recent questions from the released archive, newest first. For older questions and complete papers, browse all GATE CS papers. Questions can carry more than one subject tag; counts are not marks weightage.

GATE CS 20269 questions

  1. Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]MSQ · +1 marks · Medium
  2. Set 1 Q28Which of the following statements is/are true?MSQ · +1 marks · Medium
  3. Set 1 Q42Consider the control flow graph shown in the figure. [figure] Which one of the following options correctly lists the set of redundant expressions (common…MCQ · +2 marks · Medium
  4. Set 1 Q53Consider the following two syntax-directed definitions SDD1 and SDD2 for type declarations. DD is the start symbol, and intint, floatfloat and idid are the three…MSQ · +2 marks · Medium
  5. Set 2 Q17In C runtime environment, which one of the following is stored in heap?MCQ · +1 marks · Easy
  6. Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] P3 [code] Which one of the following statements is true?MCQ · +1 marks · Easy
  7. Set 2 Q35A lexical analyzer uses the following token definitions - letter→[A−Za−z]letter \to [A-Za-z] - digit→[0−9]digit \to [0-9] - id→letter(letter∣digit)∗id \to letter (letter \mid digit)^* -…NAT · +1 marks · Medium
  8. Set 2 Q41Consider the canonical LR(0)LR(0) parsing of the grammar below using terminals {a,b,c}\{a, b, c\} and non-terminals {A,B,C,S}\{A, B, C, S\} with SS as the start symbol.…MCQ · +2 marks · Hard
  9. Set 2 Q45Consider the control flow graph given below. [figure] Which one of the following options is the set of live variables at the exit point of each basic block?MCQ · +2 marks · Medium

GATE CS 20258 questions

  1. Set 1 Q12Which ONE of the following statements is FALSE regarding the symbol table?MCQ · +1 marks · Easy
  2. Set 1 Q13Which ONE of the following techniques used in compiler code optimization uses live variable analysis?MCQ · +1 marks · Easy
  3. Set 1 Q46Which of the following statement(s) is/are TRUE while computing FirstFirst and FollowFollow during top down parsing by a compiler?MSQ · +2 marks · Easy
  4. Set 1 Q52Refer to the given 3-address code sequence. This code sequence is split into basic blocks. The number of basic blocks is ________. [figure]NAT · +2 marks · Medium
  5. Set 2 Q21Consider the following statements about the use of backpatching in a compiler for intermediate code generation: (I) Backpatching can be used to generate code…MCQ · +1 marks · Easy
  6. Set 2 Q22Given the following syntax directed translation rules: Rule 1: R→AB{B.i=R.i−1;A.i=B.i;R.i=A.i+1;}R \rightarrow AB \{B.i = R.i - 1; A.i = B.i; R.i = A.i + 1;\} Rule 2:…MCQ · +1 marks · Medium
  7. Set 2 Q40Given a Context-Free Grammar GG as follows: S→Aa∣bAc∣dc∣bdaS \to Aa \mid bAc \mid dc \mid bda A→dA \rightarrow d Which ONE of the following statements is TRUE?MCQ · +2 marks · Hard
  8. Set 2 Q51Consider two grammars G1G_1 and G2G_2 with the production rules given below: G1:S→if E then S∣if E then S else S∣aG_1: S \to if\ E\ then\ S \mid if\ E\ then\ S\ else\ S \mid a…MSQ · +2 marks · Hard

GATE CS 20249 questions

  1. Set 1 Q26Which of the following is/are Bottom-Up Parser(s)?MSQ · +1 marks · Easy
  2. Set 1 Q37Consider the following syntax-directed definition (SDD). | Production | Semantic Rules | | ------ | ------ | | S → DHTU |…MCQ · +2 marks · Medium
  3. Set 1 Q38Consider the following grammar GG, with SS as the start symbol. The grammar GG has three incomplete productions denoted by (1), (2), and (3).…MCQ · +2 marks · Medium
  4. Set 1 Q39Consider the following pseudo-code. [code] Which one of the following options CORRECTLY specifies the number of basic blocks and the number of instructions in…MCQ · +2 marks · Medium
  5. Set 2 Q21Consider the following two sets: | Set X | Set Y | | :--- | :--- | | P. Lexical Analyzer | 1. Abstract Syntax Tree | | Q. Syntax Analyzer | 2. Token | | R.…MCQ · +1 marks · Easy
  6. Set 2 Q29Which of the following statements is/are FALSE?MSQ · +1 marks · Medium
  7. Set 2 Q40Consider the following context-free grammar where the start symbol is SS and the set of terminals is {a,b,c,d}\{a,b,c,d\}. S→AaAb∣BbBaS \rightarrow AaAb \mid BbBa…MCQ · +2 marks · Medium
  8. Set 2 Q43Consider the following expression: x[i]=(p+r)∗−s[i]+u/wx[i] = (p + r) * -s[i] + u/w. The following sequence shows the list of triples representing the given expression, with…MCQ · +2 marks · Medium
  9. Set 2 Q65Consider the following augmented grammar, which is to be parsed with a SLR parser. The set of terminals is {a,b,c,d,#,@}\{a, b, c, d, \#, @\} S′→SS' \rightarrow S…NAT · +2 marks · Hard

GATE CS 20235 questions

  1. Set 1 Q11Consider 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…MCQ · +1 marks · Easy
  2. Set 1 Q19Consider the following definition of a lexical token id for an identifier in a programming language, using extended regular expressions: **letter**…MCQ · +1 marks · Easy
  3. Set 1 Q36Consider the following program: [code] Which one of the following options represents the activation tree corresponding to the main function?MCQ · +2 marks · Easy
  4. Set 1 Q37Consider the control flow graph shown. Which one of the following choices correctly lists the set of live variables at the exit point of each basic block?…MCQ · +2 marks · Hard
  5. Set 1 Q60Consider the syntax directed translation given by the following grammar and semantic rules. Here N,I,FN, I, F and BB are non-terminals. NN is the starting…NAT · +2 marks · Medium

GATE CS 20223 questions

  1. Set 1 Q13Which one of the following statements is TRUE?MCQ · +1 marks · Easy
  2. Set 1 Q29Consider the augmented grammar with {+,∗,(,),id}\{+, *, (, ), id\} as the set of terminals. S′→SS' \to S S→S+R∣RS \to S + R \mid R R→R∗P∣PR \to R * P \mid P…NAT · +1 marks · Medium
  3. Set 1 Q65Consider the following grammar along with translation rules. [figure] Here # and % are operators and id is a token that represents an integer and *id.val*…NAT · +2 marks · Hard

GATE CS 202110 questions

  1. Set 1 Q14Consider the following statements. S1S_1: The sequence of procedure calls corresponds to a preorder traversal of the activation tree. S2S_2: The sequence of…MCQ · +1 marks · Easy
  2. Set 1 Q15Consider the following statements. S1S_1: Every SLR(1) grammar is unambiguous but there are certain unambiguous grammars that are not SLR(1). S2S_2: For any…MCQ · +1 marks · Medium
  3. Set 1 Q36Consider the following grammar (that admits a series of declarations, followed by expressions) and the associated syntax directed translation (SDT) actions,…MCQ · +2 marks · Medium
  4. Set 1 Q41Consider the following context-free grammar where the set of terminals is {a,b,c,d,f}\{a, b, c, d, f\}. S→daT∣RfS \rightarrow d a T \mid R f…MCQ · +2 marks · Medium
  5. Set 1 Q60Consider the following C code segment: [code] In a compiler, this code segment is represented internally as a directed acyclic graph (DAG). The number of nodes…NAT · +2 marks · Medium
  6. Set 2 Q13Consider the following ANSI C program: [code] Which one of the following phases in a seven-phase C compiler will throw an error?MCQ · +1 marks · Easy
  7. Set 2 Q23In the context of compilers, which of the following is/are NOT an intermediate representation of the source program?MSQ · +1 marks · Easy
  8. Set 2 Q40Consider the following ANSI C code segment: [code] Assume that the variable y points to a struct (allocated on the heap) containing two fields f1 and f2,…MCQ · +2 marks · Hard
  9. Set 2 Q48For a statement SS in a program, in the context of liveness analysis, the following sets are defined: USE(S)USE(S) : the set of variables used in SS IN(S)IN(S) :…MCQ · +2 marks · Easy
  10. Set 2 Q61Consider the following augmented grammar with {#,@,<,>,a,b,c}\{\#, @, <, >, a, b, c\} as the set of terminals. \begin{array}{l} S' \rightarrow S \\ S \rightarrow S \# c S…NAT · +2 marks · Medium

GATE CS 20204 questions

  1. Set 1 Q19Consider the following statements. I. Symbol table is accessed only during lexical analysis and syntax analysis. II. Compilers for programming languages that…MCQ · +1 marks · Medium
  2. Set 1 Q20Consider the language L={an∣n≥0}∪{anbn∣n≥0}L = \{a^n \mid n \geq 0\} \cup \{a^n b^n \mid n \geq 0\} and the following statements. I. LL is deterministic context-free. II. LL is…MCQ · +1 marks · Medium
  3. Set 1 Q34Consider the following grammar. S→aSB∣dS \to aSB \mid d B→bB \to b The number of reduction steps taken by a bottom-up parser while accepting the string aaadbbbaaadbbb is…NAT · +1 marks · Medium
  4. Set 1 Q43Consider the productions A → PQ and A → XY. Each of the five non-terminals A,P,Q,X,A, P, Q, X, and YY has two attributes: ss is a synthesized attribute,…MCQ · +2 marks · Medium

GATE CS 20194 questions

  1. Set 1 Q13Which one of the following kinds of derivation is used by LR parsers?MCQ · +1 marks · Easy
  2. Set 1 Q29Consider the grammar given below: S→AaS \to Aa A → BD B→b∣ϵB \to b \mid \epsilon D→d∣ϵD \to d \mid \epsilon Let a, b, d, and $ be indexed as follows: | a | b |…NAT · +1 marks · Medium
  3. Set 1 Q46Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let X1,X2,X3,X4,X5X_1, X_2, X_3, X_4, X_5, and X6X_6 be the…MCQ · +2 marks · Medium
  4. Set 1 Q53Consider the augmented grammar given below: S′→SS' \to S S→⟨L⟩∣idS \to \langle L \rangle \mid \text{id} L→L,S∣SL \to L, S \mid S Let…NAT · +2 marks · Medium

GATE CS 20183 questions

  1. Set 1 Q18Which one of the following statements is FALSE?MCQ · +1 marks · Easy
  2. Set 1 Q47A lexical analyzer uses the following patterns to recognize three tokens T1T_1, T2T_2, and T3T_3 over the alphabet {a,b,c}\{a,b,c\}.
    T1:a?(b∣c)∗aT_1: a? (b|c)^* a…
    MCQ · +2 marks · Hard
  3. Set 1 Q48Consider the following parse tree for the expression a#b$c$d#e#f, involving two binary operators $ and #. [figure]MCQ · +2 marks · Medium

GATE CS 20175 questions

  1. Set 1 Q12Consider the following intermediate program in three address code [code] Which one of the following corresponds to a static single assignment form of the…MCQ · +1 marks · Easy
  2. Set 1 Q17Consider the following grammar: | | | |---|---| | P→xQRSP \rightarrow xQRS | | | Q→yz∣zQ \rightarrow yz \mid z | | | R→w∣ϵR \rightarrow w \mid \epsilon | | |…MCQ · +1 marks · Easy
  3. Set 1 Q43Consider the following grammar: [code] where relop is a relational operator (e.g., <, >, ...), ò refers to the empty statement, and if, then,…NAT · +2 marks · Medium
  4. Set 1 Q52Consider the expression (a−1)∗(((b+c)/3)+d))(a-1)*(((b+c)/3)+d)). Let X be the minimum number of registers required by an optimal code generation (without any register spill)…NAT · +2 marks · Medium
  5. Set 2 Q5Match the following according to input (from the left column) to the compiler phase (in the right column) that processes it: | Input | Compiler Phase | | :---…MCQ · +1 marks · Easy

Other GATE CS topics

Continue learning with Success Tracker

Keep working on Compiler Design

Reading a solution is a useful start. In Success Tracker, you can attempt questions yourself, review mistakes and return to the topics that need another pass.

AI-powered practice· Unlimited practice on eligible plans
PYQs with solutions
Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
Practice that adapts
Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
AI doubt support
Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.

Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.

This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.