Practice tests · GATE

GATE compiler design: 5 practice questions with answers

By the PrepMatic team · Updated · 4 min read

On this page
  1. What GATE tests in compiler design
  2. Practice questions
  3. Answers and explanations
  4. How to prepare compiler design
  5. Take the full topic test on PrepMatic

Compiler Design is a compact section of the GATE CS paper. Most questions are procedural: compute FIRST and FOLLOW sets, build parsing tables or item sets, count tokens or basic blocks, or optimise a small piece of code. Each type has a fixed procedure, so careful practice turns this section into dependable marks. The five original questions below use five of those procedures.

What GATE tests in compiler design

Compiler Design is one of the ten sections in the GATE 2027 CS syllabus, and the topic list was revised, so confirm it on the official syllabus page. The standard content is lexical analysis, parsing (top-down LL and bottom-up LR, SLR and LALR), syntax-directed translation, runtime environments, intermediate code generation and optimisation. Procedural questions dominate: FIRST and FOLLOW sets, parser conflicts, number of states or items, tokens, basic blocks, DAG nodes and the result of an optimisation. The real paper also has MSQ and NAT questions; all five below are MCQ.

Practice questions

Question 1

Consider the grammar S → A B, A → a A | ε, B → b B | c. The set FOLLOW(A) is:

  • A. {b}
  • B. {b, c}
  • C. {a, b, c}
  • D. {b, c, $}

Question 2

The grammar S → a S b | c is augmented with S′ → S. The number of states in the canonical LR(0) collection of item sets is:

  • A. 4
  • B. 5
  • C. 6
  • D. 7

Question 3

The expression (a + b) * (a + b) - c is represented as a directed acyclic graph (DAG) in which common subexpressions are shared. The number of nodes in the DAG is:

  • A. 5
  • B. 7
  • C. 9
  • D. 6

Question 4

Consider this three-address code.

LineInstruction
1x = 0
2i = 1
3if i > 5 goto 11
4t = i * 2
5if t > 6 goto 8
6x = x + t
7goto 9
8x = x - 1
9i = i + 1
10goto 3
11print x

The number of basic blocks is:

  • A. 7
  • B. 8
  • C. 9
  • D. 10

Question 5

A C lexical analyser uses the longest-match (maximal munch) rule. The number of tokens it produces for the statement if (a >= b) x = y+++z; is:

  • A. 11
  • B. 12
  • C. 13
  • D. 14

Answers and explanations

  1. B. A appears only in S → A B, followed by B, so FOLLOW(A) contains FIRST(B) = {b, c}. The production A → a A adds FOLLOW(A) into itself and nothing new. The end marker $ is not included because B can never be empty, so something always follows A.
  2. C. I0 = closure of S′ → .S contains S′ → .S, S → .aSb and S → .c. From I0, the symbol S leads to I1 {S′ → S.}, a to I2 {S → a.Sb, S → .aSb, S → .c} and c to I3 {S → c.}. From I2, S leads to I4 {S → aS.b}, a returns to I2 and c returns to I3. From I4, b leads to I5 {S → aSb.}. The states are I0 to I5, so 6.
  3. D. The leaves are a, b and c (3 nodes). The sum a + b appears twice but is a single shared node. Then there is one node for the multiplication and one for the subtraction. That is 3 + 1 + 1 + 1 = 6. Without sharing, the syntax tree would have 9 nodes.
  4. A. Leaders are the first instruction (line 1), every jump target (lines 11, 8, 9 and 3), and every instruction right after a jump (lines 4, 6, 8 and 11). So the leaders are lines 1, 3, 4, 6, 8, 9 and 11. Each block runs from a leader to just before the next: 1-2, 3, 4-5, 6-7, 8, 9-10 and 11. That is 7 blocks. Line 2 is not a leader because nothing jumps to it.
  5. C. Reading left to right with maximal munch: if, (, a, >=, b, ), x, =, y, ++, +, z, ;. That is 13 tokens. The sequence +++ is read as ++ followed by +, because the lexer takes the longest valid operator first. Counting >= as two tokens is the usual slip.

How to prepare compiler design

  • Learn the FIRST and FOLLOW rules until you can apply them without looking. Write a table with one row per non-terminal and fill it in repeated passes until nothing changes.
  • Practise building LR(0), SLR(1), LALR(1) and LL(1) tables on two or three small grammars. Know which conflicts each parser can and cannot resolve, and the strict hierarchy between them.
  • For counting questions, follow the definition mechanically. Leaders for basic blocks, items for LR automata and maximal munch for tokens each have a clear procedure; work line by line.
  • Study the optimisations by example: constant folding, common-subexpression elimination, dead-code elimination, loop-invariant code motion, and the data-flow equations for liveness and reaching definitions. Add synthesised and inherited attributes.
  • Link it back to theory: lexical analysis uses regular languages and parsing uses context-free languages, so a strong theory of computation base speeds this section.

See the GATE CSE preparation guide for subject order, and the algorithms practice page for another procedure-driven subject. For the official papers, use previous year papers and mock tests.

Take the full topic test on PrepMatic

PrepMatic's topic tests run about 15 questions in 30 minutes, which suits procedural topics where accuracy under time pressure matters. The free assessment shows your weakest subjects first.

Turn the GATE syllabus into a plan you can finish

Take a free GATE assessment on PrepMatic. It shows where you stand in your branch, turns the syllabus into a plan around the hours you actually have, and builds mock tests focused on your weak subjects.

Start free assessment →Free forever plan · No credit card · 2-minute setup

Related

More in GATE

← All practice tests