The MAXIMAL MUNCH rule for lexical analysis says that when two rules can both match
Athe scanner picks the shortest match
Bthe scanner picks randomly
Cthe scanner reports an error
Dthe scanner picks the longest match
Answer & Solution
Correct answer: D. the scanner picks the longest match
1. MAXIMAL MUNCH (longest-match rule): the scanner consumes as MANY characters as possible to form one token.
2. Example: input `>=`. Both `>` and `>=` are valid tokens. Maximal munch chooses `>=` (longest), the natural choice.
3. Example: input `123.45`. Maximal munch matches the full `123.45` as a NUMBER, not `123` as one number and `.45` separately.
4. Without this rule, scanners would default to shortest tokens, which is rarely what programmers want.
5. Options A, C, D are wrong rules.
_Source: Bob Nystrom, "Crafting Interpreters", Ch 4.6 (Longer Lexemes)._
Keep practising Compiler Design
Browse all Compiler Design questions for GATE CSE, or explore more Computer Science topics.
Related questions
The FOLLOW(A) set in LL parsing isThe FIRST(α) set of a string α in LL parsing isAfter the parser produces an AST, the next compiler phase is typicallyA SHIFT/REDUCE CONFLICT in an LR parser occurs whenLL parsers (recursive descent) parse from thePANIC MODE error recovery in a parser works byWhat is a SYNTAX ERROR?How does a parser typically encode OPERATOR PRECEDENCE in the grammar?