Start learning →

GATE CSE Compiler Design — practice questions

Explore 23 free Compiler Design questions in GATE CSE Computer Science. Open each question or exercise to read its content, reference answers and available explanation.

Practise Compiler Design with Quill →

Compiler Design questions and exercises with answers

Showing questions 1–23 of 23.

The SCANNER (or LEXER) is the FIRST phase of a compiler. Its job is toRead the answer and worked solution →The DIFFERENCE between a TOKEN and a LEXEME isRead the answer and worked solution →Which formalism is most commonly used to specify the LEXICAL structure of programming language tokens?Read the answer and worked solution →The MAXIMAL MUNCH rule for lexical analysis says that when two rules can both matchRead the answer and worked solution →How are RESERVED WORDS (keywords like 'if', 'while') typically handled by a scanner?Read the answer and worked solution →A scanner usually IGNORES which characters?Read the answer and worked solution →An IDENTIFIER in most C-family languages follows the regexRead the answer and worked solution →A FINITE AUTOMATON used in the scanner is typicallyRead the answer and worked solution →If a programmer writes `1.5e3` in a C-style language, the scanner should produceRead the answer and worked solution →In an LL(1) language, the scanner produces tokens that the parser examinesRead the answer and worked solution →Which is a token (TYPE) emitted by a typical C-style scanner?Read the answer and worked solution →A PARSER converts a stream of tokens intoRead the answer and worked solution →RECURSIVE DESCENT parsers translate each grammar rule intoRead the answer and worked solution →Recursive descent is a TOP-DOWN parsing strategy. Its main weakness isRead the answer and worked solution →An AMBIGUOUS grammar is one whereRead the answer and worked solution →How does a parser typically encode OPERATOR PRECEDENCE in the grammar?Read the answer and worked solution →What is a SYNTAX ERROR?Read the answer and worked solution →PANIC MODE error recovery in a parser works byRead the answer and worked solution →LL parsers (recursive descent) parse from theRead the answer and worked solution →A SHIFT/REDUCE CONFLICT in an LR parser occurs whenRead the answer and worked solution →After the parser produces an AST, the next compiler phase is typicallyRead the answer and worked solution →The FIRST(α) set of a string α in LL parsing isRead the answer and worked solution →The FOLLOW(A) set in LL parsing isRead the answer and worked solution →

Worked practice questions

The SCANNER (or LEXER) is the FIRST phase of a compiler. Its job is to

  1. execute the source code
  2. optimise the program
  3. produce machine code directly
  4. produce a token stream
Show answer and solution

Correct answer: D. produce a token stream

1. The scanner reads the source code as a sequence of CHARACTERS. 2. It groups those characters into meaningful chunks called TOKENS — the 'words' and 'punctuation' of the programming language. 3. Output: a stream of tokens (each with a type, lexeme, literal, line number). 4. Subsequent phases (parser, semantic analyser, code generator) work on this token stream instead of raw text. 5. Options A, C, D describe later phases (interpreter, code gen, optimiser). _Source: Bob Nystrom, "Crafting Interpreters", Ch 4 (Scanning, intro)._

Open this question →

The DIFFERENCE between a TOKEN and a LEXEME is

  1. they are synonyms
  2. lexeme = substring; token adds type
  3. a token is shorter than a lexeme
  4. lexemes are reserved words only
Show answer and solution

Correct answer: B. lexeme = substring; token adds type

1. LEXEME: the specific raw substring of source code that forms a token, e.g. 'while', '123', 'x', '+='. 2. TOKEN: a richer structure containing the lexeme PLUS: - TYPE (e.g. KEYWORD_WHILE, NUMBER, IDENTIFIER, PLUS_EQUAL) - LITERAL value (for numbers, strings — parsed into typed value) - SOURCE LOCATION (line and column) 3. Example: '123' is the lexeme; the token is `(NUMBER, 123, line=4)`. 4. Options A, C, D mischaracterise. _Source: Bob Nystrom, "Crafting Interpreters", Ch 4.2 (Lexemes and Tokens)._

Open this question →

Which formalism is most commonly used to specify the LEXICAL structure of programming language tokens?

  1. Turing machines
  2. Context-free grammars
  3. Regular expressions
  4. Predicate logic
Show answer and solution

Correct answer: C. Regular expressions

1. Lexical structure is REGULAR: tokens like identifiers, numbers, keywords can be described by REGULAR EXPRESSIONS. 2. Regex → NFA → DFA. The DFA becomes the scanner table. 3. Tools like lex, flex, ANTLR use this exact pipeline. 4. Context-free grammars (option B) are used for SYNTAX (parser), not lexical analysis. Turing machines and predicate logic are far more powerful than needed for tokenisation. _Source: Bob Nystrom, "Crafting Interpreters", Ch 4.3 (Regular Languages and Expressions)._

Open this question →

How to practise

  1. Choose a Compiler Design question and try to work out your answer.
  2. Open the question to check the correct answer and read its worked solution.
  3. Continue in the app for bite-sized lessons, explanations and review with Quill. You can start without creating an account.

More Computer Science topics