Practice free →
HomeGATE CSEcomputerscienceCompiler Design › The scanner usually emits an EOF (end-of-file) t…

The scanner usually emits an EOF (end-of-file) token after the last real token. Why?

Ato mark file corruption
BEOF is never emitted; the parser checks input length
Cto track file size
Dto inform the parser to stop requesting more tokens, simplifying parser logic
Answer & Solution
Correct answer: D. to inform the parser to stop requesting more tokens, simplifying parser logic
1. Many parsers use a CURRENT TOKEN and check it against expected token types. 2. Without an EOF token, the parser would need a separate 'end of input' check — making code less uniform. 3. By emitting an EOF token, the parser can treat 'end of input' just like any other token. The parser knows it's done when it sees EOF after the top-level rule. 4. EOF also helps error recovery: the parser knows when there's nothing more to consume. 5. Other options are wrong. _Source: Bob Nystrom, "Crafting Interpreters", Ch 4.4 (Scanner Class — EOF emission)._
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions