Regex to automata and back
Kleene's theorem in both directions: Thompson's construction turns a regular expression into an epsilon-NFA of linear size (then subset construction and minimisation give the DFA), and state elimination or Arden's rule turns a DFA back into a regular expression; building blocks, state counts, worked conversions and the exponential size of expressions.
Why it exists
Regular expressions, DFAs and NFAs describe the same class of languages (Kleene's theorem). The proof is two algorithms: a regular expression is turned into an automaton (this is how a scanner generator works) and an automaton into a regular expression (this is how GATE asks "write the expression for this DFA"). The state counts of the constructions are standard exam facts.
Prerequisites
You need regular expressions (regular expressions) and the subset construction (subset construction); the final DFA is usually minimised (DFA minimisation).
Precise definitions
Kleene's theorem. For a language the following are equivalent: is the language of a DFA; of an NFA; of an -NFA; of a regular expression.
Chain of constructions:
Thompson's construction builds, by induction on the expression, an -NFA with these properties: exactly one start state (no edge enters it) and exactly one accepting state (no edge leaves it) and at most two edges leaving any state.
- : start accept. : start accept. : two states, no edge.
- Union : a new start state with edges to the starts of and ; edges from their accepting states to a new accepting state.
- Concatenation : an edge from the accepting state of to the start state of (the variant used here; some books identify the two states instead, which saves a state).
- Star : a new start and a new accepting state; edges new start start of , accepting of new accepting, new start new accepting (zero copies), accepting of start of (repeat).
Size. has at most twice as many states as has symbols and operators, and at most twice as many transitions; the NFA is linear in the length of the expression (checked on 300 random expressions).
How it works
The three building blocks
Thompson's construction for a|b: start
Simulated, not typedDouble circle = accepting state. Arrow from nowhere = start state.
Step 0
Start in the start state and everything reachable from it by epsilon moves: {t0, t2, t4}. An NFA can be in several states at once; the string is accepted if SOME choice of moves ends in an accepting state, which is the same as asking whether the set of active states contains an accepting state at the end.
The union machine for has states: the new start, the two machines ( states each) and the new accepting state. Run it on : the closure of the start contains both branches; the branch moves, and the closure after the move reaches the accepting state.
Thompson's construction for ab: start
Simulated, not typedDouble circle = accepting state. Arrow from nowhere = start state.
Step 0
Start in the start state t0. An NFA can be in several states at once; the string is accepted if SOME choice of moves ends in an accepting state, which is the same as asking whether the set of active states contains an accepting state at the end.
Concatenation has states: the two-state machines of and joined by an edge.
Thompson's construction for a*: start
Simulated, not typedDouble circle = accepting state. Arrow from nowhere = start state.
Step 0
Start in the start state and everything reachable from it by epsilon moves: {t0, t1, t2}. An NFA can be in several states at once; the string is accepted if SOME choice of moves ends in an accepting state, which is the same as asking whether the set of active states contains an accepting state at the end.
The star has states: the extra edge start accept lets the empty string through, and the back edge allows repeated 's.
A full example:
Thompson's construction for a(b|c)*: start
Simulated, not typedDouble circle = accepting state. Arrow from nowhere = start state.
Step 0
Start in the start state t0. An NFA can be in several states at once; the string is accepted if SOME choice of moves ends in an accepting state, which is the same as asking whether the set of active states contains an accepting state at the end.
The construction has states: for the first , for the union (two for , two for , a new start and a new accepting state), and more for the start and accepting states of the star; the concatenation adds only an edge. Running it on keeps a small set of states active at every step, which is the NFA behaviour that the subset construction turns into a DFA.
For a feel of the sizes (computed with the construction above): the NFA, the DFA from the subset construction and the minimal DFA have
| expression | Thompson NFA | subset DFA | minimal DFA |
|---|---|---|---|
(The counts of DFA states include the dead state when the language needs one. Other textbooks give other NFA sizes because their rule for concatenation or star differs, for example by identifying states in a concatenation; the quantity GATE asks, the minimal DFA, does not depend on the variant.)
From a DFA to a regular expression: state elimination
- Add a new start state with an edge to the old start, and a new accepting state with edges from every old accepting state (make the old accepting states ordinary).
- Label every edge with a regular expression; parallel edges are merged with .
- Remove the original states one at a time. To remove state : for every pair of states add the label to the edge (joined with any edge already there; the loop term is omitted if has no loop).
- When only and remain, the label of the edge is the regular expression.
Worked on the DFA for "ends with " (, , , , , , accepting) removing , then , then :
From the DFA 'ends with 01' to a regular expression (elimination order q2, q0, q1): start
Computed by a script, not typed| step | edges with their labels |
|---|---|
| start | |
| after removing q2 | |
| after removing q0 | |
| after removing q1 |
Step 0
Remove the states one at a time. The label of the single edge S → F at the end is the regular expression: 1*0(0|10|111*0)*1. A different order of elimination gives a different but equivalent expression; the order q2, q0, q1 gave the shortest of the six.
The result is ; verified equal to the DFA on all strings up to length . A different elimination order gives a different expression: for this DFA the six orders gave six different expressions, all equivalent. The expression is not unique and not automatically short; the order matters.
Arden's rule
An equation system is simpler to solve by hand. Let be the set of strings that lead from the start to state . For the DFA above:
Arden's rule. If , the equation has the unique solution , and the mirror equation has the unique solution . (If the solution is not unique, for example , : both and every language containing solve .) The paths-to-a-state system above has the symbols appended on the right, so the second form applies. Substitute into the others:
The language of the DFA is , again verified against the DFA on all strings up to length .
Other methods
- Derivatives (Brzozowski). The derivative is the expression for ; the distinct derivatives are the states of a DFA directly built from the expression (finitely many, if expressions are simplified).
- The formula . Strings from to using only intermediate states numbered at most : . It is state elimination written as a recurrence.
Size
Converting an expression to an NFA is linear. Converting a DFA to an expression can produce an expression of exponential length in the worst case (quoted, not proved here); that is why GATE gives small DFAs.
Worked examples
EX 1Count Thompson states
EasyFor the NFA has states; for , ; for , ; for , (the two machines joined by an edge). By the size bound an expression with symbols/operators gives at most states. has a syntax tree with symbols, union, star and concatenation, nodes, so the bound is (the count of is per symbol plus for the union plus for the star).
EX 2Which DFA for which expression
Easyover : one state (accepting, looping on both symbols). over : two states (an accepting one looping on and a dead one). : states.
EX 3DFA to expression by elimination
MediumDFA over : (start) , , , ; accepting (strings ending in ). Remove after adding : gets , gets . Then remove : . This is equal to : check (not in either), , , (not in either) and (in both). A more direct answer is .
EX 4Expression to minimal DFA
MediumFor the Thompson NFA has states, the subset construction DFA states and the minimal DFA : the states remember the longest suffix of the input that is a prefix of (, , , ). Accepting is the last. This is the textbook expression-to-scanner pipeline: Thompson, subset construction, minimisation (DFA minimisation).
EX 5Typical questions
GATE level(a) An expression with operators/symbols: the maximum number of states of its Thompson NFA? (the statement of the bound). (b) Does the NFA from Thompson's construction have an edge? Yes (for union and star), and it has exactly one accepting state. (c) "Which expression does this DFA accept?" Solve by Arden's rule or elimination, then test with the shortest strings: , one symbol, two symbols. (d) Are the expressions obtained by different elimination orders equal? Yes as languages, not as strings. (e) Is the subset-construction DFA of a Thompson NFA minimal? Not always (: states against ).
Cheat box
Cheat box
- Regular expression DFA NFA -NFA (Kleene).
- Thompson: one start, one accept, out-degree , at most (symbols + operators) states; edges for union and star.
- Union: new start/accept; concatenation: join the two machines; star: back edge and bypass edge.
- DFA to expression: state elimination , or Arden when .
- Order of elimination changes the expression, not the language. Expression to scanner: Thompson, subset, minimise.
Traps and GATE patterns
Connections
Across subjects
The scanner of a compiler is this pipeline (lexical analysis); closure properties are proved with these constructions (closure properties).
PYQs
11 previous year questions from your book. I re-solved each one myself (11 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs
Which one of the following regular expressions is equivalent to the language accepted by the DFA given below?
- (a). 0*1(0 + 10*1)*
- (b). 0* (10*11) * 0 *
- (c). 0 *1(010*1)* 0*
- (d). 0(1 + 0*10*1)*0 *
Let M be the 5-state NFA with ε-transitions shown in the diagram below Which one of the following regular expressions represents the language accepted by M ?
- (a). (00)* + 1(11)*
- (b). 0* + (1 + 0(00)* )(11)*
- (c). (00)* + (1 + (00)* )(11)*
- (d). 0+ + 1(11)* + 0(11)*
Consider the following definition of a lexical token id for an identifier in a programming language, using extended regular expressions: letter→[A-Za-z] digit→[0-9] id→letter(letter|digit)* Which one of the following Non-deterministic Finite-state Automata with - transitions accepts the set of valid identifiers? (A double-circle denotes a final state)
- (a).
- (b).
- (c).
- (d).
Consider the Deterministic Finite-state Automaton (DFA)
A shown below. The DFA runs on the alphabet {0, 1},
and has the set of states {s, p, q, r}, with s being the start
state and p being the only final state.
Which one of the following regular expressions
correctly describes the language accepted by A?- (a)1(0*11)*
- (b)0(0 + 1)*
- (c)1(0 + 11)*
- (d)1(110*)*
Which one of the following regular expression correctly represents the language of the finite automata given below?
- (a)ab*bab*+ba*aba*
- (b)(ab*b)*ab* + (ba*a)* ba*
- (c)(ab*b+ba*a)(a*+b*)
- (d)(ba*a+ab*b)*(ab*+ba*)
Consider the alphabet Σ = {0, 1}, the null/empty string λ
and the sets of strings X0, X1 and X2 generated by the
corresponding grammar. X0, X1 and X2 are related as
follows :
X0 = 1X1
X1 = 0X1 + 1X2
X2 = 0X1 + {λ}
Which of the following choices precisely represents the
strings in X0?- (a)10(0* + (10)* 1
- (b)10(0* + (10)*)* 1
- (c)1(0* + 10)* 1
- (d)10(0 + 10)* 1 + 110(0 + 10)* 1
Which of the regular expressions given below represent the following DFA? I)0*1(1+00*1)* II)0*1*1+11*0*1 III) (0+1)*1
- (a)I and II only
- (b)I and III only
- (c)II and III only
- (d)I, II, and III
Match the following NFAs with the regular expressions they correspond to: 1. ε + 0(01*1 + 00)*01* 2. ε + 0(10*1 + 00)*0 3. ε + 0(10*1 + 10)*1 4. ε + 0(10*1 + 10)*10*
- (a)P – 2, Q – 1, R – 3, S – 4
- (b)P – 1, Q – 3, R – 2, S – 4
- (c)P – 1, Q – 2, R – 3, S – 4
- (d)P – 3, Q – 2, R – 1, S – 4
Consider the following Finite State Automaton: The language accepted by this automaton is given by the regular expression
- (a)b*ab*ab*ab*
- (b)(a+b)*
- (c)b* a(a+b)*
- (d)b*ab*ab*
Consider the regular expression R = (a + b)*(aa + bb)(a + b)* Which of the following non-deterministic finite automata recognizes the language defined by the regular expression R? Edges labelled λ denote transitions on the empty string.
- (a).
- (b)
- (c).
- (d).
Consider the regular expression R = (a + b)*(aa + bb)(a + b)* Which deterministic finite automaton accepts the language represented by the regular expression R ?
- (a).
- (b)
- (c). THEORY OF COMPUTATION
- (d).
Self-check
Question 1
How many accepting states does a Thompson NFA have?
Show answerHide
Exactly one (and it has no outgoing edge).
Question 2
For , how many states does the NFA built here have, and why?
Show answerHide
: a new start, a two-state machine for each symbol, and a new accepting state.
Question 3
Solve .
Show answerHide
(the standard solution of is ).
Question 4
You eliminate a state with a self-loop between and . What label does the new edge get?
Show answerHide
(joined with any existing edge by ).
Revision card
One-screen revision
- Thompson: one start, one accept, states, union and star use .
- Pipeline: expression, -NFA, subset DFA, minimise.
- Back: state elimination or Arden ( gives when ).
- Elimination order changes the expression, not the language.
References
- ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 1.3 (Lemma 1.55 and Lemma 1.60: regular expression to NFA, DFA to regular expression through generalised NFAs)
- ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 3.2 (from DFAs to regular expressions), Section 3.2.4 (state elimination), Section 3.2.5 (from regular expressions to automata), Theorem 3.4 (Arden-style equations)
- ·Aho, Lam, Sethi, Ullman, Compilers: Principles, Techniques, and Tools (2nd edition), Section 3.7.4 (construction of an NFA from a regular expression, Thompson)
Verification
scripts/enum-checks/toc_regular.py: Thompson NFAs built by scripts/toc_lib.py have one accepting state, at most two edges leaving a state, no edge entering the start state and at most twice as many states as the syntax tree has nodes, and they accept the same strings as Python's re module on 300 random expressions (all strings up to length 7); state elimination in a random order on 300 random DFAs gives an expression equal to the DFA; for 'ends with 01' all six elimination orders give six different but equivalent expressions; the state counts on this page (6, 4, 4, 10 for the building blocks and the example; 14, 5, 4 for (a|b)*abb) were recomputed, and Arden's rule solution of the three equations was checked against the DFA on all strings up to length 12. The Thompson variant used here joins the two parts of a concatenation by an epsilon edge (textbook state counts differ by variant). The exponential lower bound for the size of the expression is quoted, not proved here (needs verification). Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)