Compiler Phases: Which Analyzer Checks Every Character?
In a compiler, the phase that checks every character of the source text is the lexical analyzer (also called the lexer or scanner). It reads the input stream character-by-character and groups characters into tokens (like keywords, identifiers, numbers, operators) using patterns described by the language’s lexical grammar. This behavior is consistent with standard compiler design: after tokenization, the syntax analyzer works at the token level to verify the program’s grammar, while the code generator/optimizer occur later and do not scan raw characters.
Therefore, the correct option is: (i) the lexical analyzer.
Key learning terms: lexical analyzer token syntax analyzer code generator code optimizer
Compiler Design: Lexical Analysis (Lexer/Scanner) Explained
How the correct option is determined
- 1Step 1
The only phase described as operating directly on the raw source stream (characters) is the lexical analyzer.
- 2Step 2
The lexer reads source text and forms tokens; later phases consume tokens rather than individual characters.
- 3Step 3
The syntax analyzer checks grammar over tokens, and code generator/optimizer work after parsing, producing/transforming code.
- 4Step 4
Choose option (i) the lexical analyzer.
Why the lexical analyzer matches the question
A lexer’s job is to transform a character stream into tokens—so it must examine the input at the character level to recognize token boundaries (e.g., distinguishing while from an identifier while1, or recognizing == as one operator instead of two = characters). After this, the syntax analyzer performs grammatical checks based on the token stream.
Meanwhile:
- syntax analyzer operates on token sequences and ensures the program matches grammar rules (e.g., via parsing tables or recursive descent).
- code generator produces target code from intermediate representation.
- code optimizer rewrites code to improve performance/size.
Exam shortcut
If a question mentions characters (not tokens), you’re almost always looking for the lexical analyzer.
Common confusion to avoid
Don’t mix up levels: the lexer works on characters; the syntax analyzer works on tokens.
Compiler Phases and Their Typical Inputs
Which phase consumes characters vs. tokens vs. intermediate/code forms
Quick FAQ
Knowledge Check
In a compiler, ________ checks every character of the source text.