1. Gyanastra
  2. Compiler Design
  3. Lexical Analysis
  4. Responsibilities of the Lexical Analyzer
Free lesson, no account needed 4 min read 9 practice questions after it
Asked in past papers9 times
  • GATE CSE20182015201119901987
  • ISRO2017
  • UGC NET201020062005

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 t or m a x.
  • Token: what the later phases see in place of every lexeme. Examples: int is a token, max is a token, the identifier XX is 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:

  1. Converting lexemes into tokens — converting names (the literal text) into tokens. This is the first responsibility.
  2. Removing the comments — comments are eliminated at this phase.
  3. Removing the white spaces — wherever extra white spaces have been given, they are removed.
  4. 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.
Figure. The lexical analyzer sits between the source program and the parser. It reads characters, removes white space and comments, groups the rest into lexemes and returns a token each time the parser asks for the next one. It also enters names in the symbol table and reports lexical errors.

Worked examples

Source text versus tokens:

  • The characters i n t are read as a lexeme and replaced by the token for int.
  • The characters m a x are read as a lexeme and replaced by the token for max.
  • The identifier XX is 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 int and max; 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.
Pages 1–2 of 2