Skip to content

Thompson's Construction

A compositional conversion of a classical regular expression into a language-equivalent ε-NFA.

Version
v1 · 2026-10-03 · History
Domain-specific #
13667
Aliases
Thompson Nfa Construction

Core Idea

Thompson's construction recursively converts a classical regular expression into an ε-NFA accepting the same language. Literal fragments are combined by local rules for concatenation, alternation, and repetition; the fragments retain a composable entry/exit interface. A textbook variant has one explicit start and final state per fragment, while an implementation can instead keep patchable outgoing arrows until completion.[1][2]

How would you explain it like I'm…

Snap-Together Pattern Maps

Imagine you want a machine that says 'yes' when it sees a certain pattern of letters. Thompson's construction builds that machine out of small snap-together pieces, one for each little part of the pattern, like building with toy blocks. When all the pieces are snapped together, the machine says 'yes' to exactly the words that fit the pattern.

Turning Patterns into Machines

A regular expression is a short code that describes a pattern of letters, like 'cat or dog' or 'a followed by any number of b's.' Thompson's construction turns such a pattern into a machine made of states (circles) and arrows that checks whether a word matches. It builds small machine pieces for single letters, then joins them for 'this then that', 'this or that', and 'repeat this'. Some arrows are free jumps that don't use up a letter. The finished machine accepts exactly the words the pattern describes, and its size grows only in step with the pattern's length.

Regex-to-NFA Construction

Thompson's construction converts a classical regular expression into an equivalent nondeterministic finite automaton with ε-moves (ε-NFA), where ε-moves are transitions taken without consuming input. It works compositionally: there is a small fragment for each literal symbol, and rules for combining fragments for concatenation, alternation (or), and Kleene star (repetition). At every step, each fragment has a usable entry and exit and recognizes exactly the language of its subexpression. The finished ε-NFA recognizes exactly the original expression's language, and its size grows linearly with the expression's length. Textbooks often give each fragment one start and one accept state, but implementations like Russ Cox's instead track a start state and a list of dangling exit arrows to patch later; both express the same idea.

 

Thompson's construction translates a classical regular expression into an equivalent ε-NFA by structural induction on the expression. Base fragments recognize single literal symbols, and composition rules build fragments for concatenation, alternation, and Kleene star, linking subfragments with ε-transitions. The invariant maintained at each step is that every fragment exposes a usable entry/exit interface and recognizes exactly the language of its subexpression, so the completed automaton recognizes exactly the language of the full expression. The resulting automaton's size is linear in the length of the expression. A common textbook presentation gives every fragment exactly one explicit start state and one explicit final state. Russ Cox's implementation instead represents a fragment as a start state plus a list of unconnected exit arrows, patching those arrows during composition and adding a single match state at the end. Since both realize the same compositional scheme, having one explicit accept state in every intermediate fragment is a presentation choice, not a requirement of the construction.

Scope of Application

A text-search pattern may be compiled once and matched over many inputs. Compiler lexers can compile a regular expression for each token class and join the resulting machines at a new ε-branching start. NFA simulation, DFA conversion, longest-match choice, and lexer rule priority happen downstream; they do not define the construction.[2][3]

Clarity

The necessary roles are a classical regular-expression source, composable automaton fragments, operator-local wiring rules, and an ε-NFA whose language equals the source's. Backreference syntax lies outside these rules; some fixed-choice backreference patterns still denote regular languages, while a variable-length case such as (a*)b\1 requires matching the same arbitrary count on both sides of b and is nonregular. The textbook NFA size is linear in expression length, but that does not imply matching a text of length \(m\) against a length-\(n\) expression costs \(O(m+n)\); cited NFA simulation can take \(O(mn)\).[1][2]

Manages Complexity

The fragment interface means each regex syntax-tree node can be compiled locally while preserving a global language invariant. For a(b|c)*, build symbols, branch between b and c, add the star bypass and loop, and concatenate after a. That provides a small machine without reasoning separately about every accepted string.[1][3]

Abstract Reasoning

If \(L(N_R)=L(R)\) for fragments corresponding to subexpressions, correct concatenation, alternation, and star wiring preserve the matching language operation. Induction over the source expression then gives \(L(N)=L(R)\) for the completed automaton. The regex and ε-NFA differ in representation, not in the strings they denote.[1]

Knowledge Transfer

The conversion transfers from a single search pattern to per-token lexer patterns. What transfers is the formal operator composition, not how the resulting machine is run or optimized. Modern ε-NFA diagrams are a formal reconstruction of Thompson's historical machine-code technique; early Unix grep should not be assumed to have used this matching algorithm.[2][3]

Relationships to Other Abstractions

Local relationship map for Thompson's ConstructionParents appear above the current abstraction, mutual partners to the right, and children below. Node labels state whether each abstraction is prime or domain-specific; colors identify relation types.Thompson'sConstructionDOMAINDomain-specific abstraction: Regular Expression — presupposesRegularExpressionDOMAIN

Current abstraction Thompson's Construction Domain-specific

Parents (1) — outgoing typed relations

  • Thompson's Construction presupposes Regular Expression Domain-specific

    The construction recursively compiles a regular expression's literals and operators.

    Condition / exception classical regular-language source

Hierarchy path (1) — routes to 1 parentless root

Neighborhood in Abstraction Space

Thompson's Construction sits in a sparse region of the domain-specific corpus (62nd percentile for distinctiveness): few abstractions share its structure, so a faithful description tends to retrieve it precisely.

Family — Type Systems & Functional Constructs (20 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-11-14

References

[1] Alfred Aho and Rajeev Verma, Columbia COMS W3261, “Lecture 4: Regular Expressions and Finite Automata”, §2, directly checked. registry ↩a ↩b ↩c ↩d

[2] Russ Cox, “Regular Expression Matching Can Be Simple And Fast” (2007), compiling, performance, and history sections, directly checked. registry ↩a ↩b ↩c ↩d

[3] NYU compiler lecture, “Compilers Lecture #4”, §§3.7.4 and 3.8, directly checked. registry ↩a ↩b ↩c