Practice free →
HomeGATE CSEcomputerscienceCompiler 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)._
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions