In computer science, Thompson's construction algorithm, also called the McNaughton-Yamada-Thompson algorithm, is a method of transforming a regular expression into an equivalent nondeterministic finite automaton (NFA). This NFA can be used to match strings against the regular expression. This algorithm is credited to Ken Thompson.
Regular expressions and nondeterministic finite automata are two representations of formal languages. For instance, text processing utilities use regular expressions to describe advanced search patterns, but NFAs are better suited for execution on a computer. Hence, this algorithm is of practical interest, since it can compile regular expressions into NFAs. From a theoretical point of view, this algorithm is a part of the proof that they both accept exactly the same languages, that is, the regular languages.
An NFA can be made deterministic by the powerset construction and then be minimized to get an optimal automaton corresponding to the given regular expression. However, an NFA may also be interpreted directly.
To decide whether two given regular expressions describe the same language, each can be converted into an equivalent minimal deterministic finite automaton via Thompson's construction, powerset construction, and DFA minimization. If, and only if, the resulting automata agree up to renaming of states, the regular expressions' languages agree.
The algorithm works recursively by splitting an expression into its constituent subexpressions, from which the NFA will be constructed using a set of rules. More precisely, from a regular expression E, the obtained automaton A with the transition function ? respects the following properties:
The following rules are depicted according to Aho et al. (2007), p. 122. In what follows, N(s) and N(t) are the NFA of the subexpressions s and t, respectively.
The empty-expression ? is converted to
A symbol a of the input alphabet is converted to
The union expression s|t is converted to
State q goes via ? either to the initial state of N(s) or N(t). Their final states become intermediate states of the whole NFA and merge via two ?-transitions into the final state of the NFA.
The concatenation expression st is converted to
The initial state of N(s) is the initial state of the whole NFA. The final state of N(s) becomes the initial state of N(t). The final state of N(t) is the final state of the whole NFA.
The Kleene star expression s* is converted to
An ?-transition connects initial and final state of the NFA with the sub-NFA N(s) in between. Another ?-transition from the inner final to the inner initial state of N(s) allows for repetition of expression s according to the star operator.
With these rules, using the empty expression and symbol rules as base cases, it is possible to prove with mathematical induction that any regular expression may be converted into an equivalent NFA.
Two examples are now given, a small informal one with the result, and a bigger with a step by step application of the algorithm.
The picture below shows the result of Thompson's construction on
(?|a*b). The pink oval corresponds to a, the teal oval corresponds to a*, the green oval corresponds to b, the orange oval corresponds to a*b, and the blue oval corresponds to ?.
As an example, the picture shows the result of Thompson's construction algorithm on the regular expression that denotes the set of binary numbers that are multiples of 3:
The upper right part shows the logical structure (syntax tree) of the expression, with "." denoting concatenation (assumed to have variable arity); subexpressions are named a-q for reference purposes. The left part shows the nondeterministic finite automaton resulting from Thompson's algorithm, with the entry and exit state of each subexpression colored in magenta and cyan, respectively. An ? as transition label is omitted for clarity — unlabelled transitions are in fact ? transitions. The entry and exit state corresponding to the root expression q is the start and accept state of the automaton, respectively.
The algorithm's steps are as follows:
|q:||start converting Kleene star expression|
|b:||start converting union expression|
|p:||start converting Kleene star expression|
|d:||start converting concatenation expression|
|n:||start converting Kleene star expression|
|f:||start converting concatenation expression|
|h:||start converting Kleene star expression|
|h:||finished converting Kleene star expression|
|l:||start converting Kleene star expression|
|j:||start converting concatenation expression|
|l:||finished converting Kleene star expression|
|f:||finished converting concatenation expression|
|n:||finished converting Kleene star expression|
|d:||finished converting concatenation expression|
|p:||finished converting Kleene star expression|
|b:||finished converting union expression|
|q:||finished converting Kleene star expression|
An equivalent minimal deterministic automaton is shown below.
Thompson's is one of several algorithms for constructing NFAs from regular expressions; an earlier algorithm was given by McNaughton and Yamada. Converse to Thompson's construction, Kleene's algorithm transforms a finite automaton into a regular expression.
Glushkov's construction algorithm is similar to Thompson's construction, once the ?-transitions are removed.