Lexical analysis

Thompson's Construction

Build an NFA from a regular expression one fragment at a time.

  • Lecture reference: Lexical Analysis IV · slides 3–6
Regular definitions

One per line, as name = RE; lines starting with // are comments. A name in the expression stands for a fresh copy of its definition.

Tab inserts a tab. Press Escape, then Tab, to move focus out of the editor.
Lecture reference: Lexical Analysis IV · slide 6

The worked example: the ten-state NFA with states A–J.

Consider the regular expression (1 | 0)*1. NFA is ?

Show answer

States A–J, start A, accepting J, and these 11 transitions:

  • A →ε B
  • A →ε H
  • B →ε C
  • B →ε D
  • C →1 E
  • D →0 F
  • E →ε G
  • F →ε G
  • G →ε A
  • H →ε I
  • I →1 J

Construction one step per syntax-tree node

Step 6 of 6
(1 | 0)*1 Concatenation: no new states; ε-move H → I. H is no longer accepting.

Syntax tree numbered in step order

1102|3*415·6

NFA 10 states

  • New in this step
  • Current fragment
  • Operand fragments

Rule

Concatenation A B
ABε

A.final →ε B.start

No new states. A’s final is no longer accepting; only B’s final is.

Here A = (1 | 0)*, B = 1

Lecture reference: Lexical Analysis IV · slide 4