GATE CSE Lexical Analysis PYQs
Lexical Analysis in GATE CS is consistently tested as a 1-mark foundational topic covering tokenization mechanics, formal automaton models, error classification… Read the guide
- Questions
- 7
- Asked
- since 2011
- Total marks
- 7
- Marks / paper
- 0.3
7 GATE CSE Lexical Analysis PYQs with solutions, from Compiler Design. Browse all GATE CSE PYQs.
Questions
Every official paper that asked this topic, newest first.
- GATE CSE 2026 Set 1 · Q27Consider the following C statements:
char *str1 = "Hello; /* Statement S1 */ char *str2 = "Hello;"; /* Statement S2 */ int *str3 = "Hello"; /* Statement S3 */Which of the following options is/are correct?MSQ1 markEasySolve - GATE CSE 2026 Set 2 · Q35A lexical analyzer uses the following token definitions • • • • • For the string given below, …NAT1 markMediumSolve
- GATE CSE 2025 Set 1 · Q12Which ONE of the following statements is FALSE regarding the symbol table?MCQ1 markEasySolve
- GATE CSE 2024 Set 2 · Q21Consider the following two sets: Which one of the following options is the CORRECT match from Set X to Set Y ?MCQ1 markEasyDiagramSolve
- GATE CSE 2023 · Q19Consider the following definition of a lexical token
idfor an identifier in a programming language, using extended regular expressions: …MCQ1 markEasySolve - GATE CSE 2011 · Q1In a compiler, keywords of a language are recognized duringMCQ1 markEasySolve
- GATE CSE 2011 · Q19The lexical analysis for a modern computer language such as Java needs the power of which one of the following machine models in a necessary and sufficient sense?MCQ1 markEasySolve
What GATE tests in Lexical Analysis
Concepts, patterns, formulas and traps distilled from past papers.
Lexical Analysis in GATE CS is consistently tested as a 1-mark foundational topic covering tokenization mechanics, formal automaton models, error classification, and compiler data structure lifecycle. Recent years have shifted from purely definitional phase-matching questions toward practical token segmentation using the maximal munch rule and formal construction of NFA/DFA models from regular expressions.
Core concepts
- Tokens, Patterns, and Lexemes
- Maximal Munch / Longest Match rule
- Finite State Automata as the computational engine for lexical analysis
- NFA with epsilon-transitions constructed from regular expressions
- Lexical errors (e.g., unclosed string literals, unrecognized characters) vs Syntactic and Semantic errors
- Symbol table initialization during lexical analysis and its cross-phase persistence
- Handling of whitespace and comments
Recurring question patterns
Token Counting with Maximal Munch (Longest Match)
CommonNAT1 marks2026Given regular expression definitions for tokens (identifiers, numbers, whitespace) and an input string, compute the exact number of tokens recognized by applying greedy prefix matching.
Error Classification Across Compiler Phases
OccasionalMSQ1 marks2026Given snippets of code with errors (e.g., unclosed string literals, invalid identifiers, type mismatches, missing brackets), classify which phase detects which error (Lexical, Syntactic, or Semantic).
Automata and Regular Expressions for Tokens
CommonMCQ1 marks2023, 2011Mapping token definitions (extended regular expressions) to corresponding Finite State Automata (NFA with -transitions or DFA) or identifying the theoretical power needed (FSA).
Role of Lexer and Symbol Table Interactions
CommonMCQ1 marks2025, 2024, 2011Conceptual verification of the lexical analyzer's duties (keyword identification, token stream generation) and symbol table management (creation at lexical phase, persistence across code generation).
Formulas GATE uses
Identifier Regular Definition
Used to specify valid identifier lexical patterns and evaluate corresponding NFA/DFA transitions.
Maximal Munch Token Split
Used when scanning an input stream without whitespace delimiters where a prefix matches one token class and the remainder starts a new token (e.g., splitting 23mm into number 23 and id mm).
Examiner traps in Lexical Analysis
- Treating hybrid strings like `23mm` or `14A` as a single invalid token or single identifier rather than applying maximal munch to produce two separate tokens (`number` + `id`).
- Confusing lexical errors (malformed tokens like unclosed string literals) with syntactic errors (grammar rule violations) or semantic errors (type mismatches).
- Assuming the symbol table is discarded after parsing/syntax analysis; it is required through intermediate code generation, semantic analysis, and target code generation.
- Selecting an NFA that fails to allow alternating transitions between character classes (e.g., getting trapped in a `letter`-only or `digit`-only loop).
- Forgetting that whitespace and comments typically do not produce output tokens unless explicitly asked.
How questions are asked
- Count total tokens produced by maximal munch for a given string
- Classify error types in C statements as Lexical, Syntactic, or Semantic
- Identify correct NFA/DFA diagram recognizing a token regular expression
- Match compiler phases to output artifacts or optimization techniques
- Select true/false conceptual statements about the symbol table and lexer
Paper trends
Transition from direct recall questions (e.g., matching phases or identifying FSA as the formal model) to constructive and evaluation-based questions (tracing maximal munch splits and NFA transitions).
2026, 2023, 2011
Introduction of error localization across phases using MSQ format and token-counting using NAT format.
2026
Difficulty notes
Easy items test theoretical models (FSA), symbol table lifecycle, standard phase matching, or straightforward error identification. Medium items test edge cases in token segmentation under greedy scanning (maximal munch) involving adjacent alphanumeric strings or recognizing precise NFA state transitions.