Incomputer science,Thompson's constructionalgorithm, also called theMcNaughton–Yamada–Thompson algorithm,[1] is a method of transforming aregular expression into an equivalentnondeterministic finite automaton (NFA).[2] This NFA can be used tomatch strings against the regular expression. This algorithm is credited toKen Thompson.
Regular expressions and nondeterministic finite automata are two representations offormal 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 cancompile 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, theregular languages.
An NFA can be made deterministic by thepowerset construction and then beminimized to get an optimal automaton corresponding to the given regular expression. However, an NFA may also beinterpreted directly.
To decide whether two given regular expressions describe the same language, each can be converted into an equivalent minimaldeterministic finite automaton via Thompson's construction,powerset construction, andDFA minimization. If, and only if, the resulting automata agreeup to renaming of states, the regular expressions' languages agree.
The algorithm worksrecursively by splitting an expression into its constituent subexpressions, from which the NFA will be constructed using a set of rules.[3] More precisely, from a regular expressionE, the obtained automatonA with the transition functionΔ[clarification needed] respects the following properties:
The following rules are depicted according to Aho et al. (2007),[1] p. 122. In what follows,N(s) andN(t) are the NFA of the subexpressionss andt, respectively.
Theempty-expression ε is converted to
Asymbola of the input alphabet is converted to
Theunion expressions|t is converted to
Stateq goes via ε either to the initial state ofN(s) orN(t). Their final states become intermediate states of the whole NFA and merge via two ε-transitions into the final state of the NFA.
Theconcatenation expressionst is converted to
The initial state ofN(s) is the initial state of the whole NFA. The final state ofN(s) becomes the initial state ofN(t). The final state ofN(t) is the final state of the whole NFA.
TheKleene star expressions* is converted to
An ε-transition connects initial and final state of the NFA with the sub-NFAN(s) in between. Another ε-transition from the inner final to the inner initial state ofN(s) allows for repetition of expressions according to the star operator.
With these rules, using theempty expression andsymbol rules as base cases, it is possible to prove withstructural induction that any regular expression may be converted into an equivalent NFA.[1]
Two examples are now given, a small informal one with the result, and a bigger with a step by step application of the algorithm.

(ε|a*b) using Thompson's construction, step by stepThe picture below shows the result of Thompson's construction on(ε|a*b). The purple oval corresponds toa, the teal oval corresponds toa*, the green oval corresponds tob, the orange oval corresponds toa*b, and the blue oval corresponds toε.

(0|(1(01*(00)*0)*1)*)*As an example, the picture shows the result of Thompson's construction algorithm on the regular expression(0|(1(01*(00)*0)*1)*)* 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 nameda-q for reference purposes. The left part shows the nondeterministic finite automaton resulting from Thompson's algorithm, with theentry andexit state of each subexpression colored inmagenta andcyan, respectively.An ε as transition label is omitted for clarity — unlabelled transitions are in fact ε transitions.The entry and exit state corresponding to the root expressionq is the start and accept state of the automaton, respectively.
The algorithm's steps are as follows:
| q: | start converting Kleene star expression | (0|(1(01*(00)*0)*1)*)* | ||||||||
| b: | start converting union expression | 0|(1(01*(00)*0)*1)* | ||||||||
| a: | convert symbol | 0 | ||||||||
| p: | start converting Kleene star expression | (1(01*(00)*0)*1)* | ||||||||
| d: | start converting concatenation expression | 1(01*(00)*0)*1 | ||||||||
| c: | convert symbol | 1 | ||||||||
| n: | start converting Kleene star expression | (01*(00)*0)* | ||||||||
| f: | start converting concatenation expression | 01*(00)*0 | ||||||||
| e: | convert symbol | 0 | ||||||||
| h: | start converting Kleene star expression | 1* | ||||||||
| g: | convert symbol | 1 | ||||||||
| h: | finished converting Kleene star expression | 1* | ||||||||
| l: | start converting Kleene star expression | (00)* | ||||||||
| j: | start converting concatenation expression | 00 | ||||||||
| i: | convert symbol | 0 | ||||||||
| k: | convert symbol | 0 | ||||||||
| j: | finished converting concatenation expression | 00 | ||||||||
| l: | finished converting Kleene star expression | (00)* | ||||||||
| m: | convert symbol | 0 | ||||||||
| f: | finished converting concatenation expression | 01*(00)*0 | ||||||||
| n: | finished converting Kleene star expression | (01*(00)*0)* | ||||||||
| o: | convert symbol | 1 | ||||||||
| d: | finished converting concatenation expression | 1(01*(00)*0)*1 | ||||||||
| p: | finished converting Kleene star expression | (1(01*(00)*0)*1)* | ||||||||
| b: | finished converting union expression | 0|(1(01*(00)*0)*1)* | ||||||||
| q: | finished converting Kleene star expression | (0|(1(01*(00)*0)*1)*)* | ||||||||
An equivalent minimal deterministic automaton is shown below.

Thompson's is one of several algorithms for constructing NFAs from regular expressions;[5] an earlier algorithm was given by McNaughton and Yamada.[6] 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.
Regular expressions are often used to specify patterns that software is then asked to match. Generating an NFA by Thompson's construction, and using an appropriate algorithm to simulate it, it is possible to create pattern-matching software with performance that is, wherem is the length of the regular expression andn is the length of the string being matched. This is much better than is achieved by many popular programming-language implementations;[7] however, it is restricted to purely regular expressions and does not supportpatterns for non-regular languages like backreferences.
{{cite book}}: CS1 maint: location (link)