Compiler Design · Lexical Analysis
Responsibilities of the Lexical Analyzer
Overview
The lexical analyzer is the compiler phase that reads the source program character by character and turns it into a stream of tokens.
It is the only phase that ever sees the raw text, so it also handles comment and white-space removal and records line numbers for error reporting.
Key concepts
- Lexical analyzer: the only phase of the compiler that actually reads the source text, character by character.
Once this phase is over, the raw letters of the program (for example the letters I, N, T or M, A, X) are never seen again. - Lexeme: the actual literal text of a word in the source, e.g. the characters
i n torm a x. - Token: what the later phases see in place of every lexeme. Examples:
intis a token,maxis a token, the identifierXXis a token. - Line-number tracking: because reading is done character by character, only this phase knows what line number each lexeme belongs to.
How it works
The lexical analyzer reads the program line by line and character by character. Its main responsibilities are:
- Converting lexemes into tokens — converting names (the literal text) into tokens. This is the first responsibility.
- Removing the comments — comments are eliminated at this phase.
- Removing the white spaces — wherever extra white spaces have been given, they are removed.
- Tracking line numbers — recording the line number each lexeme belongs to, which is why an error message can say that a syntax error occurs at a particular line number. This would not be possible otherwise.
Drawing the diagram…
Worked examples
Source text versus tokens:
- The characters
i n tare read as a lexeme and replaced by the token forint. - The characters
m a xare read as a lexeme and replaced by the token formax. - The identifier
XXis a token.
White space: even if the entire C program is written in one line, there will be no errors at all; a program written completely without any spaces (apart from where separation between words is required) still works fine.
A space is needed between words such as int and max (otherwise they would merge), but after that no space is needed.
Line numbers: an error reported at a particular line number is possible only because the lexical analyzer reads the program line by line and records the line number of each lexeme.
Revision summary
- The lexical analyzer is the only phase that reads the actual text, character by character.
- After this phase, raw letters are never seen again; every lexeme is replaced by a token.
- Memorise the four responsibilities: (1) convert lexemes into tokens, (2) remove comments, (3) remove white spaces, (4) know the line number of each lexeme.
- Extra white spaces and line breaks do not cause errors.
- A space is only needed to separate adjacent words such as
intandmax; without it they would merge. - Line-numbered error messages exist only because of this phase.
Previous-year exam insights
- The lexical analyzer's output is a stream (sequence) of tokens, usually passed to the parser; it may also enter identifiers into the symbol table and is typically implemented as a deterministic finite automaton (scanner).
Asked inISRO 2017UGC NET 2010GATE 2011 - Keywords are recognized during lexical analysis (they are matched as reserved words by the scanner, usually via a reserved-word table), not during parsing.
Asked inGATE 2011 - The module that scans the source text character by character is the lexical analyzer (scanner); the parser performs syntax analysis, checking validity and syntactic structure.
Asked inGATE 1987UGC NET 2006UGC NET 2005 - When several token patterns match, the scanner follows the maximal-munch (longest-match) rule: it takes the longest prefix matching any pattern; ties are broken by the pattern listed first (priority order).
Asked inGATE 2018 - Lexical analysis is the phase that groups characters into tokens (uniform symbols); it does not check syntax or semantics.
Asked inUGC NET 2010UGC NET 2006UGC NET 2005 - Besides tokenizing, the scanner strips comments and whitespace and keeps line/column information for error messages; it reports lexical errors such as illegal characters and malformed tokens.