1. Gyanastra
  2. Compiler Design
  3. Lexical Analysis
  4. Tokens, Lexemes, and Patterns via Regular Expressions
Free lesson, no account needed 4 min read 7 practice questions after it
Asked in past papers5 times
  • GATE CSE201820162011
  • UGC NET20172015

Compiler Design · Lexical Analysis

Tokens, Lexemes, and Patterns via Regular Expressions

Overview

The lexical analyzer uses patterns, written as regular expressions, to decide which character sequences form which token classes.
Each token class has a pattern describing the shape of every string that belongs to it, and any input string matching that pattern is automatically classified as that token.
The resulting stream of tokens is passed to the syntax analyzer.

Key concepts

  • Pattern: a rule, written as a regular expression, that describes the shape of every string belonging to one token class.
  • Automatic classification: if an input string matches a pattern, the lexical analyzer classifies it as that token without further decisions.
  • Advance specification: patterns must be known to the lexical analyzer before scanning begins; they are supplied as part of its specification, before any input is processed.
  • Identifier token class: an identifier is a letter followed by a letter or a digit, any number of times.
  • Token stream: the output of the lexical analyzer is a stream of tokens, passed to the syntax analyzer.
  • Syntax analyzer input: the syntax analyzer takes the tokens one by one together with a grammar (the grammar of the language) to check the token stream.

How it works

  1. For each token class, write a pattern (regular expression) that describes all strings of that class.
  2. For example, the identifier pattern is: a letter followed by a letter or a digit, any number of times.
  3. Give these patterns to the lexical analyzer in advance, before scanning starts.
  4. While scanning the input, any string that matches a pattern is automatically identified as the token of that class (anything matching the identifier pattern is reported as an identifier).
  5. The lexical analyzer produces a stream of tokens and passes it to the syntax analyzer.
  6. The syntax analyzer takes the tokens one by one, together with the grammar of the language, and checks the token stream.
Figure. Patterns written as regular expressions are supplied before scanning. Any string matching a pattern, such as the identifier pattern, is automatically classified as that token and sent as a token stream to the syntax analyzer.

Formulas

identifier = letter (letter | digit)*
  • letter = any letter
  • digit = any digit
  • ( ... )* = zero or more repetitions of the enclosed item

Revision summary

  • Token classes are recognized by patterns, which are regular expressions.
  • The identifier pattern is a letter followed by any number of letters or digits.
  • Patterns must be supplied to the lexical analyzer before it starts working.
  • A string matching a pattern is automatically classified as that token.
  • The lexical analyzer's output is a stream of tokens, handed to the syntax analyzer.
  • The syntax analyzer consumes tokens one by one together with the grammar.

Previous-year exam insights

  • Lexical analysis is specified by regular expressions (regular grammars) and implemented by finite-state automata (DFA/NFA); syntax analysis is specified by context-free grammars and implemented by pushdown automata.
    Finite-state machines recognize exactly the regular languages.
    Asked inGATE 2011UGC NET 2017
  • When several patterns match a prefix of the input, the lexical analyzer follows the maximal-munch (longest-match) rule: it selects the longest prefix that matches any pattern; ties among equal-length matches are broken by the order in which the patterns are listed (the earlier rule wins).
    Asked inGATE 2018
  • Regular expressions over an alphabet define regular languages; the standard operators are union (| or +), concatenation, Kleene star (*, zero or more), Kleene plus (+, one or more), and optional (? or ε, zero or one).
    Asked inGATE 2018UGC NET 2015
  • A regular expression is converted to an NFA (Thompson's construction) and then to a DFA (subset construction); the DFA is then minimized.
    A DFA can be simulated directly with a transition table, giving linear-time scanning.
    Asked inGATE 2011
  • Lexical analyzers also handle lookahead, backtracking, reserved words vs identifiers (usually via a symbol table), and removal of whitespace and comments; these are still regular operations and need no context-free power.
  • Regular expressions cannot count unboundedly (e.g., balanced parentheses or ana^{n} bnb^{n} are not regular), which is why nested constructs are left to the parser rather than the lexer.
    Asked inUGC NET 2017
Pages 1–2 of 2