Practice tests · GATE
GATE compiler design: 5 practice questions with answers
On this page
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.
| Line | Instruction |
|---|---|
| 1 | x = 0 |
| 2 | i = 1 |
| 3 | if i > 5 goto 11 |
| 4 | t = i * 2 |
| 5 | if t > 6 goto 8 |
| 6 | x = x + t |
| 7 | goto 9 |
| 8 | x = x - 1 |
| 9 | i = i + 1 |
| 10 | goto 3 |
| 11 | print 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
- 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.
- 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.
- 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.
- 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.
- 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 setupRelated
- GATE theory of computation practiceFive GATE-style TOC MCQs on DFA state counts, regular languages, context-free grammars, undecidability and closure properties, with proofs and explanations.
- GATE algorithms practiceFive GATE-style algorithm MCQs on recurrences, Dijkstra, 0/1 knapsack, minimum comparisons and loop analysis, with worked solutions and a preparation plan.
- GATE computer organization practiceFive GATE-style computer organization MCQs on cache tag bits, pipeline CPI, expanding opcodes, DMA cycle stealing and memory access time, with worked answers.
- GATE CSE preparationPlan GATE CS 2027: the revised syllabus section by section, the order to study the nine core subjects, standard books and a 26-week plan from first pass to mocks.
More in GATE
- GATE communications practiceFive GATE EC communications MCQs on AM power, Shannon capacity, PCM bit rate, QAM bandwidth and Hamming code error correction, with complete worked solutions.
- GATE computer networks practiceFive GATE-style computer networks MCQs on subnetting, CSMA/CD frame size, Go-Back-N, TCP congestion control and IP fragmentation, with step-by-step solutions.
- GATE calculus practiceFive GATE-level calculus MCQs on limits, maxima and minima, the mean value theorem, definite integrals and critical points, with full worked solutions.
- GATE analytical aptitude practiceFive GATE-style reasoning MCQs on deduction, number series, seating arrangement, clocks and 3D cube counting, with step-by-step answers and methods.
- GATE control systems practiceFive GATE-style control systems MCQs on damping ratio, Routh stability, steady-state error, root locus breakaway points and gain margin for EE and EC.
- GATE analog circuits practiceFive GATE-style analog circuit MCQs on op-amp summing, BJT bias, MOSFET transconductance, negative feedback gain and Zener regulation, with worked solutions.
- GATE DA probability practiceFive GATE Data Science and AI probability and statistics MCQs on covariance, confidence intervals, the z-test, the central limit theorem and chi-squared tests.
- GATE data structures practiceFive GATE-style data structures MCQs on postfix evaluation, BST deletion, heap insertion, C pointer arithmetic and hashing, with traced solutions for CS and DA.