G27
Self-checkedTier 1 11 min readtoc.regex-fa-conversion

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 LL the following are equivalent: LL is the language of a DFA; of an NFA; of an ε\varepsilon-NFA; of a regular expression.

Chain of constructions:

regular expression→Thompsonε-NFA→subset constructionDFA→state elimination or Ardenregular expression.\text{regular expression} \xrightarrow{\text{Thompson}} \varepsilon\text{-NFA} \xrightarrow{\text{subset construction}} \text{DFA} \xrightarrow{\text{state elimination or Arden}} \text{regular expression}.

Thompson's construction builds, by induction on the expression, an ε\varepsilon-NFA N(r)N(r) 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.

  • N(ε)N(\varepsilon): start →ε\xrightarrow{\varepsilon} accept. N(a)N(a): start →a\xrightarrow{a} accept. N(∅)N(\emptyset): two states, no edge.
  • Union r∣sr \mid s: a new start state with ε\varepsilon edges to the starts of N(r)N(r) and N(s)N(s); ε\varepsilon edges from their accepting states to a new accepting state.
  • Concatenation rsrs: an ε\varepsilon edge from the accepting state of N(r)N(r) to the start state of N(s)N(s) (the variant used here; some books identify the two states instead, which saves a state).
  • Star r∗r^*: a new start and a new accepting state; ε\varepsilon edges new start →\to start of N(r)N(r), accepting of N(r)N(r) →\to new accepting, new start →\to new accepting (zero copies), accepting of N(r)N(r) →\to start of N(r)N(r) (repeat).

Size. N(r)N(r) has at most twice as many states as rr 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 typed
εεaεbεt0t1t2t3t4t5

Double circle = accepting state. Arrow from nowhere = start state.

inputb
active states{t0, t2, t4}

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.

0 / 3
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc2-traces.py

The union machine for a∣ba \mid b has 66 states: the new start, the two machines (22 states each) and the new accepting state. Run it on bb: the closure of the start contains both branches; the bb branch moves, and the closure after the move reaches the accepting state.

Thompson's construction for ab: start

Simulated, not typed
aεbt0t1t2t3

Double circle = accepting state. Arrow from nowhere = start state.

inputab
active states{t0}

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.

0 / 4
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc2-traces.py

Concatenation abab has 44 states: the two-state machines of aa and bb joined by an ε\varepsilon edge.

Thompson's construction for a*: start

Simulated, not typed
εεaεεt0t1t2t3

Double circle = accepting state. Arrow from nowhere = start state.

inputaa
active states{t0, t1, t2}

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.

0 / 5
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc2-traces.py

The star a∗a^* has 44 states: the extra ε\varepsilon edge start →\to accept lets the empty string through, and the back edge allows repeated aa's.

A full example: a(b∣c)∗a (b \mid c)^*

Thompson's construction for a(b|c)*: start

Simulated, not typed
aεεεεεεεbεcεt0t1t2t3t4t5t6t7t8t9

Double circle = accepting state. Arrow from nowhere = start state.

inputabcb
active states{t0}

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.

0 / 9
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc2-traces.py

The construction has 1010 states: 22 for the first aa, 66 for the union b∣cb \mid c (two for bb, two for cc, a new start and a new accepting state), and 22 more for the start and accepting states of the star; the concatenation adds only an ε\varepsilon edge. Running it on abcbabcb 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

expressionThompson NFAsubset DFAminimal DFA
a∣ba \mid b664433
abab444444
a∗a^*443322
a(b∣c)∗a (b \mid c)^*10105533
(a∣b)∗abb(a \mid b)^* abb14145544
(a∣b)∗(a \mid b)^*883311

(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

  1. Add a new start state SS with an ε\varepsilon edge to the old start, and a new accepting state FF with ε\varepsilon edges from every old accepting state (make the old accepting states ordinary).
  2. Label every edge with a regular expression; parallel edges are merged with ∣\mid.
  3. Remove the original states one at a time. To remove state qq: for every pair of states p→q→rp \to q \to r add the label (p→q) (q→q)∗ (q→r)(p \to q)\,(q \to q)^*\,(q \to r) to the edge p→rp \to r (joined with any edge already there; the loop term is omitted if qq has no loop).
  4. When only SS and FF remain, the label of the edge S→FS \to F is the regular expression.

Worked on the DFA for "ends with 0101" (q0→1q0q_0 \xrightarrow{1} q_0, q0→0q1q_0 \xrightarrow{0} q_1, q1→0q1q_1 \xrightarrow{0} q_1, q1→1q2q_1 \xrightarrow{1} q_2, q2→0q1q_2 \xrightarrow{0} q_1, q2→1q0q_2 \xrightarrow{1} q_0, q2q_2 accepting) removing q2q_2, then q0q_0, then q1q_1:

From the DFA 'ends with 01' to a regular expression (elimination order q2, q0, q1): start

Computed by a script, not typed
stepedges 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.

0 / 4
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc2-traces.py

The result is 1∗0(0∣10∣111∗0)∗11^* 0 (0 \mid 10 \mid 1 1 1^* 0)^* 1; verified equal to the DFA on all strings up to length 1212. 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 XiX_i be the set of strings that lead from the start to state ii. For the DFA above:

X0=ε∣X01∣X21,X1=X00∣X10∣X20,X2=X11.X_0 = \varepsilon \mid X_0 1 \mid X_2 1, \qquad X_1 = X_0 0 \mid X_1 0 \mid X_2 0, \qquad X_2 = X_1 1.

Arden's rule. If ε∉A\varepsilon \notin A, the equation X=AX∣BX = A X \mid B has the unique solution X=A∗BX = A^* B, and the mirror equation X=XA∣BX = X A \mid B has the unique solution X=BA∗X = B A^*. (If ε∈A\varepsilon \in A the solution is not unique, for example A=εA = \varepsilon, B=bB = b: both {b}\{b\} and every language containing bb solve X=X∣bX = X \mid b.) The paths-to-a-state system above has the symbols appended on the right, so the second form applies. Substitute X2X_2 into the others:

X1=X00∣X1(0∣10)⇒X1=X00(0∣10)∗,X0=ε∣X0(1∣0(0∣10)∗11)⇒X0=(1∣0(0∣10)∗11)∗.X_1 = X_0 0 \mid X_1 (0 \mid 10) \Rightarrow X_1 = X_0 0 (0 \mid 10)^*, \qquad X_0 = \varepsilon \mid X_0 (1 \mid 0 (0 \mid 10)^* 1 1) \Rightarrow X_0 = (1 \mid 0 (0 \mid 10)^* 11)^*.

The language of the DFA is X2=X11=(1∣0(0∣10)∗11)∗ 0(0∣10)∗ 1X_2 = X_1 1 = (1 \mid 0 (0 \mid 10)^* 11)^* \, 0 (0 \mid 10)^* \, 1, again verified against the DFA on all strings up to length 1212.

Other methods

  • Derivatives (Brzozowski). The derivative r/ar / a is the expression for {w:aw∈L(r)}\{w : aw \in L(r)\}; the distinct derivatives are the states of a DFA directly built from the expression (finitely many, if expressions are simplified).
  • The formula RijkR_{ij}^{k}. Strings from ii to jj using only intermediate states numbered at most kk: Rijk=Rijk−1∣Rikk−1(Rkkk−1)∗Rkjk−1R_{ij}^{k} = R_{ij}^{k-1} \mid R_{ik}^{k-1} (R_{kk}^{k-1})^* R_{kj}^{k-1}. 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

Easy

For aa the NFA has 22 states; for a∣ba \mid b, 66; for a∗a^*, 44; for abab, 44 (the two machines joined by an ε\varepsilon edge). By the size bound an expression with ss symbols/operators gives at most 2s2s states. a(b∣c)∗a (b \mid c)^* has a syntax tree with 33 symbols, 11 union, 11 star and 11 concatenation, 66 nodes, so the bound is 2⋅6=12≥102 \cdot 6 = 12 \ge 10 (the count of 1010 is 22 per symbol plus 22 for the union plus 22 for the star).

EX 2Which DFA for which expression

Easy

(a∣b)∗(a \mid b)^* over {a,b}\{a, b\}: one state (accepting, looping on both symbols). a∗a^* over {a,b}\{a, b\}: two states (an accepting one looping on aa and a dead one). (a∣b)∗abb(a \mid b)^* abb: 44 states.

EX 3DFA to expression by elimination

Medium

DFA over {a,b}\{a, b\}: pp (start) →aq\xrightarrow{a} q, p→bpp \xrightarrow{b} p, q→aqq \xrightarrow{a} q, q→bpq \xrightarrow{b} p; accepting qq (strings ending in aa). Remove pp after adding S→εpS \xrightarrow{\varepsilon} p: S→qS \to q gets b∗ab^* a, q→qq \to q gets a∣bb∗aa \mid b b^* a. Then remove qq: b∗a(a∣bb∗a)∗b^* a (a \mid b b^* a)^*. This is equal to (a∣b)∗a(a \mid b)^* a: check ε\varepsilon (not in either), aa, baba, abab (not in either) and baabaa (in both). A more direct answer is (a∣b)∗a(a \mid b)^* a.

EX 4Expression to minimal DFA

Medium

For (a∣b)∗abb(a \mid b)^* abb the Thompson NFA has 1414 states, the subset construction 55 DFA states and the minimal DFA 44: the states remember the longest suffix of the input that is a prefix of abbabb (ε\varepsilon, aa, abab, abbabb). 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 nn operators/symbols: the maximum number of states of its Thompson NFA? 2n2n (the statement of the bound). (b) Does the NFA from Thompson's construction have an ε\varepsilon 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: ε\varepsilon, 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 (a∣ba \mid b: 44 states against 33).

Cheat box

Cheat box

  • Regular expression ↔\leftrightarrow DFA ↔\leftrightarrow NFA ↔\leftrightarrow ε\varepsilon-NFA (Kleene).
  • Thompson: one start, one accept, out-degree ≤2\le 2, at most 2×2 \times (symbols + operators) states; ε\varepsilon 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 (p→q)(q→q)∗(q→r)(p \to q)(q \to q)^*(q \to r), or Arden X=AX∣B⇒X=A∗BX = AX \mid B \Rightarrow X = A^* B when ε∉A\varepsilon \notin A.
  • Order of elimination changes the expression, not the language. Expression to scanner: Thompson, subset, minimise.

Traps and GATE patterns

Connections

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

GATE 2024 · Set 1 · MCQQ15 · page 199 of the book
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 *
GATE 2024 · Set 2 · MCQ · 2 marksQ16 · page 199 of the book
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)*
GATE 2023 · MCQQ18 · page 199 of the book
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).
GATE 2023 · MCQ · 1 markQ20 · page 200 of the book
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*)*
GATE 2022 · MCQ · 1 markQ21 · page 200 of the book
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*)
GATE 2015 · Set 2 · MCQ · 2 marksQ42 · page 202 of the book
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
GATE 2014 · Set 1 · MCQ · 2 marksQ48 · page 203 of the book
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
GATE 2008 · MCQQ62 · page 207 of the book
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
GATE 2007 · MCQ · 2 marksQ66 · page 208 of the book
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*
GATE 2007 · MCQ · 2 marksQ69 · page 209 of the book
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).
GATE 2007 · MCQQ70 · page 209 of the book
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 answer

Exactly one (and it has no outgoing edge).

Question 2

For r=a∣br = a \mid b, how many states does the NFA built here have, and why?

Show answer

66: a new start, a two-state machine for each symbol, and a new accepting state.

Question 3

Solve X=X0∣1X = X 0 \mid 1.

Show answer

X=10∗X = 1 0^* (the standard solution of X=XA∣BX = XA \mid B is BA∗B A^*).

Question 4

You eliminate a state with a self-loop cc between p→aqp \xrightarrow{a} q and q→brq \xrightarrow{b} r. What label does the new edge p→rp \to r get?

Show answer

ac∗ba c^* b (joined with any existing p→rp \to r edge by ∣\mid).

Revision card

One-screen revision

  • Thompson: one start, one accept, ≤2n\le 2n states, union and star use ε\varepsilon.
  • Pipeline: expression, ε\varepsilon-NFA, subset DFA, minimise.
  • Back: state elimination or Arden (X=XA∣BX = XA \mid B gives BA∗BA^* when ε∉A\varepsilon \notin A).
  • 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