Home › GATE CSE › computerscience › Compiler Design › The MAXIMAL MUNCH rule for lexical analysis says…
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)._
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 whenWhich of these is a BOTTOM-UP (LR) parser feature?An LL(1) grammar requires that, given the leftmost non-terminal and ONE token of lookaheadLL parsers (recursive descent) parse from thePANIC MODE error recovery in a parser works by