47.01.03 · theoretical-cs / formal-languages-automata

Regular Expressions and the Kleene-Rabin-Scott Theorem

shipped3 tiersLean: none

Anchor (Master): Sipser 2013 Introduction to the Theory of Computation 3e (Cengage) §1.3; Hopcroft, Motwani & Ullman 2006 Introduction to Automata Theory, Languages, and Computation 3e (Pearson) §3.1-3.4 (Thompson construction, state elimination, Glushkov automata, algebraic properties of regular expressions)

Intuition Beginner

You have used wildcards in file names. The pattern *.txt matches every file ending in .txt. The * means "any sequence of characters." This idea — describing a set of strings by a pattern — is the essence of a regular expression.

A regular expression builds patterns from three operations. Union (written or ) means "strings matching or strings matching ." Concatenation (written ) means "a string matching followed by a string matching ." Star (written ) means "zero or more repetitions of strings matching ."

These three operations, applied to individual symbols and the empty string, generate every pattern that a finite automaton can recognise. This is Kleene's theorem, and it is one of the foundational results of computer science: the algebraic description (regular expressions) and the machine description (DFAs from 47.01.01) define exactly the same class of languages.

Consider the language of strings over that end in "01." The regular expression is . Read it as: any sequence of 0s and 1s (the part), followed by a 0, followed by a 1. The star handles the "any prefix" part; the final 01 nails down the suffix.

Visual Beginner

The three regex operations drawn as NFA fragments (the Thompson construction):

Union R1|R2:         Concatenation R1R2:      Star R*:
   ┌──[R1]──┐        [R1] ──ε──> [R2]        ┌──[R]──┐
ε─>│        │─>ε     start    end             ε─>│   ↑   │
   └──[R2]──┘        (output of R1 feeds      └───┘──ε──┘
   ↑                 into input of R2)        ε-self-loop
   start                                   + ε-bypass
Regex Meaning Example strings
Zero or more 0s , 0, 00, 000
The string "01" 01 only
Any string of 0s and 1s , 0, 1, 01, 10, ...
Any string ending in 01 01, 001, 1101
The empty language (no strings)

Worked example Beginner

Convert a DFA for "strings ending in 01" into a regular expression using the state elimination method.

Start with the 3-state DFA from 47.01.01: states , , , with start and accept.

Step 1. Set up language equations. Let denote the set of all strings that take the DFA from state to state . We get a system:

  • (from on 0 or 1, return to ; plus the empty string for starting here).

Wait — the equations follow the transitions. Let denote the labels on edges from state to state . Then for each state :

Actually, it is cleaner to use Arden's lemma directly. We write equations for the language of strings reaching each state from the start:

  • (since has self-loops on both 0 and 1).

By Arden's lemma ( has solution ): .

Step 2. The accepted language is , the set of strings reaching . We need to account for paths through intermediate states. The state elimination procedure systematically removes states, replacing them with regular expression labels on the remaining edges. After eliminating and simplifying, the result is .

Step 3. Verify: any string matching has an arbitrary prefix followed by 01, so it ends in 01. Conversely, any string ending in 01 can be decomposed as a prefix (matching ) followed by the suffix 01. The regex and the DFA describe the same language.

Check your understanding Beginner

Formal definition Intermediate+

Definition (regular expression). Let be a finite alphabet. The set of regular expressions over is defined inductively:

  1. is a regular expression (denoting the empty language ).
  2. is a regular expression (denoting the singleton language ).
  3. For each , the symbol is a regular expression (denoting ).
  4. If and are regular expressions, then is a regular expression (denoting ).
  5. If and are regular expressions, then is a regular expression (denoting ).
  6. If is a regular expression, then is a regular expression (denoting ).

The language denoted by a regular expression is defined by the interpretation above. A language is regular iff there exists a regular expression with .

Definition (Kleene star). For a language , the Kleene star is where and .

Theorem (Arden's lemma). Let with . Then the equation has the unique solution [Hopcroft, Motwani & Ullman §3.4].

Proof. First, since . Conversely, if , then by induction for all . Since , any with must arise from with , so .

Counterexamples to common slips

  • " includes only non-empty strings." No: always includes (the term). Even .

  • "Concatenation distributes over union in both directions." and hold, but does not mean the star distributes — is idempotent but is a consequence of , not a distributive law.

  • "Regular expressions and regex libraries are the same thing." Practical regex engines (Perl, Python's re) add backreferences, lookahead, and other features that go beyond the formal regular expressions defined here. These extensions recognise non-regular languages.

Key theorem with proof Intermediate+

Theorem (Kleene's theorem: DFA regex). A language is regular (recognised by some DFA) if and only if for some regular expression .

Proof sketch (DFA to regex). Given a DFA with and , define to be the set of strings that take from state to state without passing through any state with index greater than . Base case : is the union of single symbols with , plus if . Inductive step:

Each is denoted by a regular expression built from the previous step. The language of is .

Proof sketch (regex to NFA). By structural induction on . Each regex is converted to an NFA with a single start and single accept state using the Thompson construction: atomic regexes become two-state NFAs; union is handled by a new start state with -transitions to both sub-NFAs; concatenation chains the accept state of the first to the start of the second; star adds -transitions from the accept state back to the start and a bypass. The result is an NFA recognising , which by the subset construction of 47.01.02 converts to a DFA.

Bridge. Kleene's theorem is the foundational reason the regular languages have a dual characterisation: the operational description (DFAs reading strings symbol by symbol) and the algebraic description (regular expressions combining languages by union, concatenation, and star) define exactly the same class. This builds toward the Thompson construction used in every regex compiler, which appears again in 47.01.02 where the NFA-to-DFA conversion completes the chain from algebraic pattern to deterministic machine. The central insight is that the three regex operations correspond directly to NFA composition patterns, and putting these together with the subset construction yields a complete pipeline from human-readable pattern to executable automaton.

Exercises Intermediate+

Lean formalization Intermediate+

Mathlib has Computability.RegularExpression with the inductive type RegularExpression, a matches relation, and the theorem that regular expressions define regular languages. However, the Thompson construction (converting a regex to an NFA with at most states) and the state elimination algorithm (converting a DFA to a regex) are not formalised. Arden's lemma for language equations and the Kleene algebra axioms are absent. A Codex.Automata.Thompson module defining the NFA construction and proving would be the load-bearing formalisation target; this unit ships without it.

Advanced results Master

The Thompson construction and the state elimination algorithm are the two pillars of Kleene's theorem. The first goes from algebra to machines, the second from machines to algebra. Several refinements sharpen the picture.

Theorem 1 (Thompson construction state bound). A regular expression of length (number of symbols and operators) can be converted to an NFA with at most states, each with at most two outgoing transitions [Hopcroft, Motwani & Ullman §3.2]. This linear bound makes the Thompson construction the basis of practical regex compilers: the NFA can be simulated in time by tracking the set of active states.

Theorem 2 (Glushkov automaton). For a regular expression over with symbol occurrences, the Glushkov (position) automaton has exactly states, one for each position in plus a start state. The automaton is constructed by computing the first, last, and follow sets of positions, which are properties of the parse tree of . The Glushkov automaton is always an NFA without -transitions, and it is often smaller than the Thompson NFA.

Theorem 3 (Kleene algebra axioms). The regular expressions, modulo the equations (idempotence), , , , and , form an idempotent semiring with star satisfying the unfolding and induction axioms of Kleene algebra. Kozen (1994) proved that these axioms are complete for the equational theory of regular events: iff is provable from the Kleene algebra axioms. This algebraic characterisation is used in program verification and static analysis.

Synthesis. Kleene's theorem is the foundational reason the regular languages admit three equivalent characterisations — automata-theoretic (DFAs), algebraic (regular expressions), and algebraic-structural (Kleene algebra) — and the conversion algorithms between them are the practical backbone of pattern matching, lexical analysis, and network packet filtering. The central insight is that the three regex operations (union, concatenation, star) mirror the three NFA composition patterns, and putting these together with the subset construction of 47.01.02 gives a complete equivalence proof. This equivalence builds toward the pumping lemma of 47.01.04, where the finiteness of the automaton imposes a structural limitation on regularity that no regex can overcome, and it appears again in 47.01.05 where context-free grammars extend the algebraic framework with recursion, breaking the regular barrier.

Full proof set Master

Proposition 1 (DFA to regex via state elimination). Every DFA has a regular expression with .

Proof. Number the states with the start state. Add a new unique start state with an -transition to , and a new unique accept state with -transitions from each old accept state. The augmented machine has states and transitions labelled by regular expressions (initially, single symbols and ).

Eliminate states one at a time. To eliminate state : for every pair with , replace the current label on the edge from to by . This accounts for all paths from to that may pass through any number of times. After removing and its incident edges, the language of paths between the remaining states is preserved.

After all states are eliminated, only and remain, connected by a single edge labelled with the desired regular expression . By construction, .

Proposition 2 (regex to NFA: Thompson construction). Every regular expression over has an NFA with , a single start state, and a single accept state, with at most states.

Proof. By structural induction. For atomic regexes , , and : construct small NFAs directly (0, 1, and 2 states respectively). For : create a new start state with -transitions to the start states of and , and merge their accept states into a new accept state via -transitions. For : connect the accept state of to the start state of via an -transition; the start of is the new start, the accept of is the new accept. For : add -transitions from the accept of back to its start, from a new start to the start of , and from the accept of to a new accept ; also add from to for the zero-repetitions case. The state count is linear in the size of by induction.

Connections Master

  • 47.01.01 — DFAs are the machine model that Kleene's theorem connects to regular expressions; the product construction for Boolean closure reappears in the state elimination algorithm's handling of union paths.

  • 47.01.02 — NFAs are the intermediate representation in the Thompson construction; the subset construction converts the Thompson NFA back to a DFA, completing the DFA-regex-DFA cycle.

  • 47.01.04 — The pumping lemma exploits the finiteness of the DFA equivalent of any regular expression, proving that certain languages (like ) have no regex description.

  • 47.01.05 — Context-free grammars extend regular expressions by allowing recursive nonterminals, which enables the description of nested structures like balanced parentheses.

Historical & philosophical context Master

Stephen Kleene proved the equivalence of finite automata and regular expressions in 1956 in his paper "Representation of Events in Nerve Nets and Finite Automata" [Kleene 1956]. His proof used what are now called "regular events" — sets of strings closed under union, concatenation, and star — and showed they coincide with the sets recognised by McCulloch-Pitts nerve nets. The term "regular expression" was introduced later; Kleene used "regular event."

Ken Thompson, one of the creators of Unix, implemented the Thompson construction in the QED text editor (1968) and later in ed and grep. His construction produces NFAs with a linear number of states, making regex matching efficient in practice. The construction was described in his 1968 CACM paper "Regular Expression Search Algorithm" [Hopcroft, Motwani & Ullman §3.2].

The algebraic theory of regular expressions was developed by John Conway in Regular Algebra and Finite Machines (1971) and systematised by Dexter Kozen in 1994, who proved the completeness of the Kleene algebra axioms for the equational theory of regular events. Kozen's result placed regular expressions on the same algebraic footing as rings and groups, with a finitely axiomatised equational theory.

Bibliography Master

@book{sipser2013,
  author    = {Sipser, Michael},
  title     = {Introduction to the Theory of Computation},
  edition   = {3rd},
  publisher = {Cengage Learning},
  year      = {2013},
}
@book{hopcroft2006,
  author    = {Hopcroft, John E. and Motwani, Rajeev and Ullman, Jeffrey D.},
  title     = {Introduction to Automata Theory, Languages, and Computation},
  edition   = {3rd},
  publisher = {Pearson},
  year      = {2006},
}
@article{kleene1956representation,
  author  = {Kleene, Stephen C.},
  title   = {Representation of Events in Nerve Nets and Finite Automata},
  journal = {Automata Studies},
  pages   = {3--41},
  year    = {1956},
  publisher = {Princeton University Press},
}
@article{thompson1968regular,
  author  = {Thompson, Ken},
  title   = {Regular Expression Search Algorithm},
  journal = {Communications of the ACM},
  volume  = {11},
  number  = {6},
  pages   = {419--422},
  year    = {1968},
}
@book{conway1971regular,
  author    = {Conway, John H.},
  title     = {Regular Algebra and Finite Machines},
  publisher = {Chapman and Hall},
  year      = {1971},
}
@article{kozen1994completeness,
  author  = {Kozen, Dexter},
  title   = {A Completeness Theorem for {Kleene} Algebras and the Algebra of Regular Events},
  journal = {Information and Computation},
  volume  = {110},
  number  = {2},
  pages   = {366--390},
  year    = {1994},
}