Exercise 1: A Lexical Error Inside an Unreachable Branch — Possible Solution ==================================================================== THE PROGRAM ------------------------------ if (false) { var x = "unterminated; } print "fine"; RESULT ------------------------------ [line 1] LexError: unterminated string "fine" is never printed. The program fails before a single statement runs. WHY THIS WORKS AS AN ANSWER ------------------------------ tokenize() processes the ENTIRE source string in one pass, from the first character to the last, before Parser ever sees a single token. It has no concept of "this text is inside an if(false) block" -- that information doesn't exist yet at the lexing stage; branches, blocks, and control flow are all things the PARSER discovers by reading the token stream tokenize() already finished producing. The unterminated string literal starting at `"unterminated` is scanned character by character regardless of what keywords or braces surround it in the source text, and the missing closing quote is detected the moment the scanner reaches the end of the file (or the next line, depending on the string-scanning rule) without finding one. This is exactly the same "whole-program, before-any-execution" logic the chapter used for parse errors, just one stage earlier. Parsing needs a complete, valid token stream to build a correct AST from -- that's why a parse error inside an unreachable branch still halts everything. Lexing needs to produce that complete token stream in the first place, and it has even less awareness of "reachability" than parsing does, since reachability is a control-flow concept and lexing happens before any control-flow structure has been recognized at all. Both stages share the same underlying reason a runtime error doesn't: neither one ever asks "will this code actually run" -- that question literally cannot be answered until the AST exists and the interpreter starts walking it, which is one stage (lexing) or two stages (lexing and parsing) later than where these two error types are detected.