ACompressed recap
🧾
A lexical error means no pattern fits the input at the current position: illegal characters, unterminated strings or comments. The lexer reports it and recovers, usually by panic mode (deleting characters until a token can start) or by a small local repair. Lex/Flex generate a lexer from a file of patterns with actions; conflicts are resolved by longest match and then the earliest rule; yylex() returns tokens, yytext holds the lexeme.
BMerged rule card
1.3 Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured
- Lexical error: no pattern matches a prefix of the remaining input (illegal char, unterminated string/comment, over-long name).
- Recovery: panic mode (delete until a token can start); local repairs (delete, insert, replace, transpose); minimum-distance (rare).
- Lex file: declarations %% rules (pattern {action}) %% functions.
- lex.l → Lex → lex.yy.c → C compiler → lexer; yylex(), yytext, yyleng, yylval.
- Conflicts: longest match, then earliest rule; catch-all . last.
CInterleaved GATE-style questions
Q1MCQ1 markLex sections
The three sections of a Lex file, in order, are:
- (A) rules, declarations, functions
- (B) declarations, rules, auxiliary functions
- (C) functions, rules, declarations
- (D) patterns, tokens, parser
Q2MCQ2 marksLex conflicts
Rules in order: [a-z]+ → ID; while → WHILE. Input while produces:
- (A) WHILE
- (B) ID
- (C) an error
- (D) ID followed by WHILE
Q3MSQ1 markLexical errors
Which are lexical errors in C?
- (A)
int x = 12;` (a backtick) - (B)
/* comment never closed - (C)
whlie (x) y = 1; - (D)
"textat the end of a line
Q4NAT2 marksLex simulation
Rules: if → IF; [a-z]+ → ID; [0-9]+ → NUM; blanks skipped; . → ?. How many tokens are printed for if ifx 42 x9?
Numerical answer: ________
Q5MCQ1 markLex output
Lex produces:
- (A) a parser in C
- (B) a C file containing yylex()
- (C) machine code directly
- (D) a symbol table
Q6NAT1 markToken count
How many tokens are in printf("sum = %d\n", a + b); // show?
Numerical answer: ________
Q7MCQ1 markRecovery
Minimum-distance error correction is rarely used because:
- (A) it is too expensive and may guess the wrong fix
- (B) it never finds a fix
- (C) it only works for keywords
- (D) it requires Lex
Q8MCQ2 marksPattern simulation
Patterns in priority order: A = ab, B = a(b|c)*, C = (a|b)+c. What happens for aabcb?
- (A) C(aabc), then a lexical error at the final b
- (B) B(a) B(abc) then B(b)
- (C) A(ab) C(cb)
- (D) C(aabcb)
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (B)1 mark
Declarations %% rules %% auxiliary functions.
- (A) is wrong: Declarations come first.
- (C) is wrong: Functions come last.
- (D) is wrong: Lex has no parser section.
Re-read: Lesson 1.3 — Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured
Q2 Answer: (B)2 marks
Both match all 5 characters; the earlier rule ([a-z]+) wins. This is why keyword rules must be listed first.
- (A) is wrong: WHILE is listed second.
- (C) is wrong: The input matches.
- (D) is wrong: Only one token is produced.
Re-read: Lesson 1.3 — Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured
Q3 Answer: (A) (B) (D)1 mark
A stray backtick, an unclosed comment and an unclosed string literal fit no pattern. whlie is a valid identifier, so (C) is a syntax error, not a lexical one.
Re-read: Lesson 1.3 — Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured
Q4 Answer: 52 marks
IF(if) ID(ifx) NUM(42) ID(x) NUM(9) → 5.
Re-read: Lesson 1.3 — Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured
Q5 Answer: (B)1 mark
lex.yy.c with the function yylex().
- (A) is wrong: Parsers come from other generators.
- (C) is wrong: The C file still needs a C compiler.
- (D) is wrong: The symbol table is filled by the generated lexer at run time.
Re-read: Lesson 1.3 — Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured
Q6 Answer: 91 mark
printf · ( · "sum = %d\n" · , · a · + · b · ) · ; = 9.
Re-read: Lesson 1.1.2 — Counting tokens in C code: whitespace, comments, string literals, ++ and >=
Q7 Answer: (A)1 mark
Searching for the fewest edits is costly, and the nearest valid text may not be what was meant.
- (B) is wrong: It always finds some fix.
- (C) is wrong: It applies to any input.
- (D) is wrong: It is independent of Lex.
Re-read: Lesson 1.3 — Lexical errors and recovery; how a lexer generator (Lex/Flex) is structured
Q8 Answer: (A)2 marks
At position 1, C takes aabc (4 characters; B takes only a). The last b matches no pattern: C(aabc) — error at position 5.
- (B) is wrong: Longest match takes aabc, and b alone fits no pattern.
- (C) is wrong: A does not match at position 1 longest.
- (D) is wrong: C must end in c.
Re-read: Lesson 1.2.2 — Transition diagrams; longest match and priority rules; lookahead