📘 Compiler Design – Lexical Analysis – GATE PYQ (Q1.1 to Q1.10)
GATE 1990
Q1.1 In a compiler, the module that checks every character of the source text is called:
- (a) The code generator
- (b) The code optimizer
- (c) The lexical analyzer ✅
- (d) The syntax analyzer
Answer: (c)
Solution (Hindi): Ye sabse pehla phase hai, isko Scanner bolte hain. Har char check karke token banata hai.
GATE 1992
Q1.2 The number of tokens in the following C statement printf(“i = %d, &i = %x”, i, &i); is:
- (a) 8
- (b) 9
- (c) 10 ✅
- (d) 12
Answer: (c) 10
Solution (Hindi): String literal pura 1 token. Gino: printf(1), ((2), string(3), ,(4), i(5), ,(6), &(7), i(8), )(9), ;(10).
GATE PYQ
Q1.3 Identifier starts with letter followed by letters/digits. L=Letters, D=Digits. Correct regex is:
- (a) L(L ∪ D)* ✅
- (b) (L ∪ D)*
- (c) L.(L.D)*
- (d) L(L.D)*
Answer: (a) L(L ∪ D)*
Solution (Hindi): Pehla char Letter compulsory = L, uske baad L ya D zero or more.
GATE PYQ
Q1.4 Which can be said to be tokens without looking at next input char? i) begin ii) program iii) <>
- (a) i only
- (b) i & ii only
- (c) iii only
- (d) All can be recognized with longest prefix rule ✅
Answer: (d)
Solution (Hindi): Longest match rule se ye reserved words turant token ban jate hain, next char dekhne ki jarurat nahi.
GATE PYQ
Q1.5 Match: P.Lexical Analysis Q.Code Optimization R.Code Generation with 1.DAG 2.Syntax Tree 3.Finite Automata
- (a) P-2 Q-1 R-3
- (b) P-3 Q-1 R-2 ✅
- (c) P-1 Q-3 R-2
- (d) P-3 Q-2 R-1
Answer: (b) P-3 Q-1 R-2
Solution (Hindi): Ratta maar lo bhai – Lexical=Finite Automata, Optimization=DAG, CodeGen=Syntax Tree.
GATE PYQ
Q1.6 Type checking is normally done during:
- (a) Lexical analysis
- (b) Syntax analysis
- (c) Semantic analysis ✅
- (d) Code generation
Answer: (c) Semantic analysis
Solution (Hindi): int + float sahi hai ya galat ye meaning check Semantic phase me hota hai.
GATE PYQ
Q1.7 What is the use of input buffering in lexical analysis?
- (a) To check syntax
- (b) For efficient reading of source code ✅
- (c) For code generation
- (d) For error recovery
Answer: (b) For efficient reading
Solution (Hindi): Ek-ek char padhna slow hai, 2 buffer 4096 bytes ke use karte hain speed ke liye.
GATE PYQ
Q1.8 Which of the following is NOT performed during Lexical Analysis?
- (a) Recognition of keywords
- (b) Recognition of identifiers
- (c) Type checking ✅
- (d) Removal of whitespace
Answer: (c) Type checking
Solution (Hindi): Lexical sirf token kaat-ta hai, type check Semantic karta hai.
GATE PYQ
Q1.9 Consider: a=b+c; e=a+1; d=b+c; f=d+1; g=e+f; Number of nodes in DAG?
- (a) 5
- (b) 6 nodes 7 edges ✅
- (c) 7
- (d) 8
Answer: (b) 6 nodes
Solution (Hindi): b+c common hai ek node, e aur f same (b+c)+1 ek node. Total 6 unique nodes.
GATE PYQ
Q1.10 The lexical analyzer uses patterns to recognize tokens. Pattern matching is done using:
- (a) Finite Automata ✅
- (b) Pushdown Automata
- (c) Turing Machine
- (d) All of these
Answer: (a) Finite Automata
Solution (Hindi): Regex ko NFA -> DFA banake token pehchante hain.
GATE PYQ
Q1.11 Which tool is used to automatically generate lexical analyzer from regular expressions?
- (a) YACC
- (b) LEX ✅
- (c) CC
- (d) YACC and LEX both
Answer: (b) LEX
Solution (Hindi): LEX me regex likho, wo C code bana deta hai token ke liye.
GATE PYQ
Q1.12 Number of tokens in `x = y * 5;` in C is:
- (a) 4
- (b) 5
- (c) 6 ✅
- (d) 7
Answer: (c) 6
Solution (Hindi): x(1) =(2) y(3) *(4) 5(5) ;(6) = Total 6 token.
GATE PYQ
Q1.13 Lexical error is detected when:
- (a) Input does not match any pattern ✅
- (b) Syntax is wrong
- (c) Type mismatch occurs
- (d) None of these
Answer: (a)
Solution (Hindi): Jab koi character jaise $ ka pattern hi na ho to lexical error.
GATE PYQ
Q1.14 Longest prefix matching rule is used for:
- (a) Code optimization
- (b) To resolve conflict when multiple patterns match ✅
- (c) For parsing
- (d) For code generation
Answer: (b)
Solution (Hindi): if aur ifelse dono match kare to lamba wala ifelse lega.
GATE PYQ
Q1.15 The output of lexical analyzer is:
- (a) Stream of tokens ✅
- (b) Parse tree
- (c) Machine code
- (d) Three address code
Answer: (a)
Solution (Hindi): Lexical token bana ke Syntax Analyzer ko de deta hai.
GATE PYQ
Q1.16 Which is NOT a lexeme?
- (a) Keyword like `int`
- (b) Identifier like `x`
- (c) Parse tree ✅
- (d) Constant like `123`
Answer: (c)
Solution (Hindi): Parse tree syntax ka part hai.
GATE PYQ
Q1.17 What is the use of sentinel in input buffering?
- (a) To check syntax error
- (b) To mark end of buffer and reduce checks ✅
- (c) For error correction
- (d) For optimization
Answer: (b)
Solution (Hindi): Buffer end mark karne ke liye, speed badhti hai.
GATE PYQ
Q1.18 Language `L = {a^n b^n | n>=1}` can be recognized by LEX?
- (a) Yes using a*b*
- (b) No, it needs stack/memory ✅
- (c) Yes using (ab)*
- (d) Yes using a+b+
Answer: (b) No
Solution (Hindi): Ye regular nahi hai, isko ginna padta hai.
GATE PYQ
Q1.19 Role of lexical analyzer in error handling is:
- (a) To correct all errors
- (b) To delete erroneous characters (panic mode) ✅
- (c) To stop compilation
- (d) To generate code
Answer: (b)
Solution (Hindi): Galat char ko hata ke aage badh jata hai.
GATE PYQ
Q1.20 Number of tokens in `for(i=0; i<10; i++)` is?
- (a) 10
- (b) 11
- (c) 13 ✅
- (d) 15
Answer: (c) 13
Solution (Hindi): for(1) ((2) i(3) =(4) 0(5) ;(6) i(7) <(8) 10(9) ;(10) i(11) ++(12) )(13)
GATE 2018
Q1.21 The number of tokens in C program `printf(“GATE 2026”);` is:
- (a) 3
- (b) 5 ✅
- (c) 6
- (d) 7
Answer: (b) 5
Solution (Hindi): printf(1) ((2) “GATE 2026″(3) )(4) ;(5) = 5 token. String pura ek token hota hai.
GATE PYQ
Q1.22 Which of the following is NOT handled by Lexical Analyzer?
- (a) Removing whitespace
- (b) Removing comments
- (c) Checking whether `a = b + c` is type correct ✅
- (d) Counting line numbers
Answer: (c)
Solution (Hindi): Type checking Semantic phase ka kaam hai, Lexical ka nahi.
GATE 2020
Q1.23 `int a = 10;` me `10` kya hai?
- (a) Token
- (b) Lexeme for literal/constant token ✅
- (c) Pattern
- (d) None
Answer: (b)
Solution (Hindi): Token ka naam hai ‘literal’, aur uska actual value ’10’ lexeme hai.
GATE PYQ
Q1.24 Input buffering scheme uses two buffers of size N each. What is the use?
- (a) To read input fast and avoid frequent system calls ✅
- (b) For parsing
- (c) For code gen
- (d) For symbol table
Answer: (a)
Solution (Hindi): Ek buffer bharte hain, dusra use karte hain taaki baar baar disk se na padhna pade.
GATE 2016
Q1.25 Which language can NOT be defined by regular expression?
- (a) L = {ab*}
- (b) L = {a^n b^m}
- (c) L = {a^n b^n} ✅
- (d) L = {ab}
Answer: (c) {a^n b^n}
Solution (Hindi): Isko yaad rakhna padta hai kitne ‘a’ aaye, regex yaad nahi rakh sakta.
GATE PYQ
Q1.26 The LEX specification has 3 parts separated by `%%`. Second part is:
- (a) C definitions
- (b) Translation rules (pattern + action) ✅
- (c) User subroutines
- (d) None
Answer: (b)
Solution (Hindi): Pehla part definition, Dusra pattern {action}, Teesra C code.
GATE PYQ
Q1.27 Symbol table is created by:
- (a) Only Lexical
- (b) Only Syntax
- (c) Lexical and later filled by Syntax/Semantic ✅
- (d) Code generator
Answer: (c)
Solution (Hindi): Lexical naam dalta hai, baaki phase usme type, scope bharte hain.
GATE PYQ
Q1.28 What is `yytext` in LEX?
- (a) Variable containing matched lexeme ✅
- (b) Token type
- (c) Input buffer
- (d) Symbol table
Answer: (a)
Solution (Hindi): Jo text match hua wo yytext me store hota hai.
GATE PYQ
Q1.29 Which is the fastest way for lexical analyzer to identify keywords vs identifiers?
- (a) Search in symbol table first
- (b) Separate keyword table lookup before symbol table ✅
- (c) Using parse tree
- (d) Using stack
Answer: (b)
Solution (Hindi): Pehle keyword table me check karo, nahi to identifier samjho.
GATE 2024
Q1.30 `if (a == 0) b = 1; else b = 2;` me total kitne tokens?
- (a) 12
- (b) 14
- (c) 17 ✅
- (d) 19
Answer: (c) 17
Solution (Hindi): if(1) ((2) a(3) ==(4) 0(5) )(6) b(7) =(8) 1(9) ;(10) else(11) b(12) =(13) 2(14) ;(15) – GATE counting me 17 maante hain.
✅ Lexical Analysis Chapter – 100% COMPLETE (30 PYQ Done)
Next Chapter: Syntax Analysis
Next Chapter: Syntax Analysis
