Introduction

Regular expressions and finite automata define the same class of languages. One direction builds an ε\varepsilon-NFA from an expression (Thompson's construction). The other eliminates states from a DFA (or NFA) until a single regular expression remains. Together with the subset construction, this closes the loop: RE ⇔\Leftrightarrow ε\varepsilon-NFA ⇔\Leftrightarrow NFA ⇔\Leftrightarrow DFA.

The working tasks are building an NFA from an RE, an RE from a small DFA, and proving a language regular by exhibiting either description.

Prerequisites

Core concepts

Theorem

For a language L⊆Σ∗L \subseteq \Sigma^*, the following are equivalent:

  1. L=L(r)L = L(r) for some regular expression rr.
  2. L=L(N)L = L(N) for some ε\varepsilon-NFA NN.
  3. L=L(N)L = L(N) for some NFA NN.
  4. L=L(M)L = L(M) for some DFA MM.

Thompson fragments

Thompson's construction builds an ε\varepsilon-NFA with exactly one start state and one accept state, and with no edges into the start or out of the accept, by composing fragments:

ConstructFragment idea
Symbol aastart →a\xrightarrow{a} accept
ε\varepsilonstart →ε\xrightarrow{\varepsilon} accept
∅\emptysetstart and accept with no path between them
r+sr+snew start ε\varepsilon-splits to the two starts; both accepts ε\varepsilon-merge to a new accept
rsrsaccept of rr ε\varepsilon-linked to start of ss
r∗r^*new start ε\varepsilon to rr's start and to new accept; rr's accept ε\varepsilon back to rr's start and to new accept

The resulting NFA has O(∣r∣)O(|r|) states. Convert to a DFA by subset construction if needed.

State elimination (Arden / GNFA)

To get an RE from an automaton:

  1. Convert the DFA/NFA to a GNFA (generalized NFA) whose edge labels are regular expressions: add a new start with ε\varepsilon to the old start, and ε\varepsilon from every old accept to a new unique accept; replace missing edges by ∅\emptyset.
  2. While more than two states remain, pick a state qq (not the new start or accept) and eliminate it: for every pair of states i,ji,j with edges i→Riqq→Rqjji \xrightarrow{R_{iq}} q \xrightarrow{R_{qj}} j and loop q→Rqqqq \xrightarrow{R_{qq}} q, replace the i→ji \to j label RijR_{ij} by Rij+RiqRqq∗RqjR_{ij} + R_{iq} R_{qq}^* R_{qj}.
  3. When only start ss and accept ff remain, the label on s→fs \to f is the RE.

Arden's lemma is the algebraic twin: if Q=αQ+βQ = \alpha Q + \beta and ε∉L(α)\varepsilon \notin L(\alpha), then Q=α∗βQ = \alpha^* \beta.

How it works

RE to DFA pipeline

Text
RE-TO-DFA(r)
1  N <- THOMPSON(r)          // epsilon-NFA
2  M <- SUBSET-CONSTRUCT(N)  // DFA
3  optionally MINIMIZE(M)
4  return M

DFA to RE pipeline

Text
DFA-TO-RE(M)
1  G <- TO-GNFA(M)
2  while G has a state q other than start and accept
3      ELIMINATE(G, q)
4  return the single edge label from start to accept

Different elimination orders yield different but equivalent expressions.

Algorithms and pseudocode

Thompson for concatenation, union and star follows the fragment table. State count: every symbol, ε\varepsilon or ∅\emptyset fragment has 2 states, union and star add 2 new states each, and concatenation adds none, so the NFA has at most 2∣r∣2|r| states, where ∣r∣|r| counts symbols and operators. Textbooks differ on whether concatenation merges the two glued states, so a question asking for an exact Thompson state count must fix the convention.

State elimination makes polynomially many algebraic label updates, but explicitly expanded expressions can grow exponentially. Elimination is manageable by hand on small machines; expression size matters to the actual running time.

Mathematical explanation

Correctness sketch of Thompson

By induction on the expression: each fragment accepts exactly L(r)L(r), and composing fragments preserves the inductive semantics of ++, concatenation and star because ε\varepsilon-edges only glue without reading input.

Correctness of elimination

When qq is removed, every path i→q→⋯→q→ji \to q \to \cdots \to q \to j that looped on qq a total of k≥0k \ge 0 times becomes a single RE edge RiqRqq∗RqjR_{iq} R_{qq}^* R_{qj}. Summing with the old i→ji \to j paths leaves the language of labels from start to accept unchanged.

Arden's lemma

Solving X=AX+BX = AX + B for languages with ε∉A\varepsilon \notin A gives the unique solution X=A∗BX = A^* B. Proof: unfold X=A(AX+B)+B=A2X+AB+B=⋯X = A(AX+B)+B = A^2 X + AB + B = \cdots, and the ε∉A\varepsilon \notin A condition prevents infinite descent from hiding extra strings.

Worked examples

Example 1: Thompson for (a+b)∗abb(a+b)^* abb

Build fragments for aa, bb, union, star, then concatenate a, b, b. With the ε\varepsilon-glued concatenation of the fragment table the NFA has 14 states: 2 for each of the five symbol occurrences, 2 for the union and 2 for the star. The machine accepts strings ending with abb. Subset construction followed by minimization gives the 4-state DFA that tracks progress towards abb.

Thompson construction fragments for union, concat and star
Figure 1. Thompson fragments: symbol a; union r+s with epsilon split and merge; concatenation rs; star r* with bypass and loop epsilon edges.

Example 2: state elimination on a 2-state DFA

DFA for "even number of 0s": states AA (start, accept) and BB; alphabet {0,1}\{0,1\}; A→1AA \xrightarrow{1} A, A→0BA \xrightarrow{0} B, B→0AB \xrightarrow{0} A, B→1BB \xrightarrow{1} B. AA means "even 0s so far", BB means "odd".

Eliminate BB: the path A→0B→1∗B→0AA \xrightarrow{0} B \xrightarrow{1^*} B \xrightarrow{0} A contributes 01∗00 1^* 0, and the direct loop contributes 11. The new self-loop on AA is 1+01∗01 + 01^*0, and since AA is both start and accept the answer is (1+01∗0)∗(1 + 01^*0)^*.

One wrong edge changes the language. If B→0BB \xrightarrow{0} B and B→1AB \xrightarrow{1} A instead, eliminating BB gives (1+00∗1)∗(1 + 00^*1)^*, which is "empty, or ends in 1". The two machines agree on 1, 00 is in the first language only, and 01 is in the second only. Always check the edges out of the eliminated state before writing the loop label.

State elimination on a small DFA yielding a regular expression
Figure 2. Two-state DFA for even number of 0s; after eliminating state B the remaining start-to-accept label is the regular expression (1+01*0)*.

Example 3: RE for "contains 01"

(0+1)∗01(0+1)∗(0+1)^* 01 (0+1)^*. Thompson yields an ε\varepsilon-NFA; subset construction and minimization give a 3-state DFA: "no progress", "last symbol was 0" and the accepting sink "seen 01". Three states are necessary: prefixes ε\varepsilon, 0, and 01 are pairwise distinguishable. Continuation 1 distinguishes the first two; the empty continuation distinguishes 01 from either of them.

Example 4: from RE a(a+b)*b to an NFA then DFA

Thompson produces an epsilon-NFA with a chain: read a, then loop on a+b via star fragment, then read b. After subset construction and removing unreachable sets, the DFA tracks: have we read the leading a? are we in the middle? have we just closed with b? Membership: a string is accepted iff it starts with a, ends with b, and has length at least 2. Checks: ab yes; aab yes; aaabbb yes; a no; b no; ba no.

Example 5: proving two REs equivalent

To show (0+1)∗01(0+1)∗=(0+1)∗0(0+1)∗1(0+1)∗(0+1)^* 01 (0+1)^* = (0+1)^* 0 (0+1)^* 1 (0+1)^*: the second form asks for some 0 followed later by some 1. Take the last 0 before that 1; the symbol after it is a 1, so the factor 01 occurs. The converse is immediate. The automaton method gives the same answer: both expressions minimize to the same 3-state DFA of Example 3.

Example 6: state elimination on the divisible-by-3 DFA

Use the DFA of Deterministic Finite Automata (residues q0,q1,q2q_0, q_1, q_2, start and accept q0q_0). Eliminate q2q_2 first: it is entered only from q1q_1 on 0, loops on 1, and leaves to q1q_1 on 0, so q1q_1 gains the loop 01∗00 1^* 0. Now eliminate q1q_1: it is entered from q0q_0 on 1, loops on 01∗00 1^* 0, and returns to q0q_0 on 1, so q0q_0 gains the loop 1(01∗0)∗11 (0 1^* 0)^* 1. Together with the 0-loop on q0q_0:

L=(0+1(01∗0)∗1)∗.L = \bigl(0 + 1 (0 1^* 0)^* 1\bigr)^*.

11 (3), 110 (6) and 1001 (9) match; 10 (2) and 111 (7) do not. Eliminating in the order q1q_1 first gives a longer but equivalent expression.

Example 7: Arden's lemma on the even-number-of-0s DFA

Write one equation per state for the set of strings that lead to it (ε\varepsilon for the start state):

A=ε+A 1+B 0,B=A 0+B 1.A = \varepsilon + A\,1 + B\,0, \qquad B = A\,0 + B\,1 .

The second equation has the form B=B 1+A 0B = B\,1 + A\,0; the right-sided Arden rule (X=Xα+βX = X\alpha + \beta with ε∉L(α)\varepsilon \notin L(\alpha) gives X=βα∗X = \beta \alpha^*) yields B=A 0 1∗B = A\,0\,1^*. Substitute: A=ε+A(1+01∗0)A = \varepsilon + A(1 + 0 1^* 0), so A=(1+01∗0)∗A = (1 + 0 1^* 0)^*, matching the elimination answer. Since AA is the only accepting state, this is the language.

Arden's condition matters. If ε∈L(α)\varepsilon\in L(\alpha), uniqueness is no longer guaranteed: the equation may have multiple solutions. For α=ε\alpha=\varepsilon and β⊊Σ∗\beta\subsetneq\Sigma^*, every X⊇βX\supseteq\beta is a solution; if β=Σ∗\beta=\Sigma^*, only X=Σ∗X=\Sigma^* is possible.

Regular grammars

A right-linear grammar has rules A→wBA \to wB or A→wA \to w with w∈Σ∗w \in \Sigma^*; a left-linear one has A→BwA \to Bw or A→wA \to w. Both generate exactly the regular languages. From a DFA: one variable per state, a rule p→a qp \to a\,q for each edge δ(p,a)=q\delta(p,a) = q, and p→εp \to \varepsilon for each accepting pp. For the reverse direction, first normalize to single-terminal rules A→aBA\to aB, A→aA\to a, with optional unit/epsilon rules. Then use one state per normalized variable plus a final state; general word-labelled edges for longer ww must instead be expanded into paths with intermediate states.

Mixing is not allowed. A grammar that uses right-linear and left-linear rules together can be non-regular: S→aTS \to aT, T→Sb∣bT \to Sb \mid b is "linear" but generates {anbn∣n≥1}\{a^n b^n \mid n \ge 1\}.

IMPERATIVE RULE. When asked only for an NFA, stop after Thompson. When asked for a DFA, always finish subset construction (and minimize if the question says minimal).

Important properties and observations

  • Every RE has an equivalent DFA; every DFA has an equivalent RE.
  • Thompson NFAs are sparse and easy to draw by hand for exam REs.
  • Elimination order changes the RE text, not the language.
  • ∅\emptyset-edges in a GNFA act as absent paths.
  • Converting RE →\to minimal DFA is the standard way to prove two REs equivalent.

Common misconceptions.

  • "State elimination needs a DFA." It works on NFAs too after converting labels to REs.
  • "Thompson always gives a DFA." It gives an ε\varepsilon-NFA.
  • "The RE from elimination is unique." Only unique up to language equality.

GATE examination perspective

  • DRAW THOMPSON CLEANLY. One start, one accept per fragment; only ε\varepsilon for glue.
  • ELIMINATE SYSTEMATICALLY. Update every pair i,ji,j when removing qq; do not forget the loop Rqq∗R_{qq}^*.
  • SIMPLIFY AFTERWARD. Apply r+r=rr+r=r, ∅∗=ε\emptyset^*=\varepsilon, distributivity.
  • EQUIVALENCE QUESTIONS. Prefer RE →\to minimal DFA for both sides, then compare.
  • READ THE TABLE, NOT THE SHAPE. Two-state machines for "ends in 1" and "even number of 0s" look alike; one edge decides which language it is.
  • ELIMINATION ORDER. Eliminate the state with the fewest in-out pairs first; the final expression differs by order, the language does not.
  • ARDEN SIDES. For "strings leading to a state" equations the variable is on the left of the symbol (X=Xα+βX = X\alpha + \beta, solution βα∗\beta\alpha^*); for "strings accepted from a state" it is on the right (X=αX+βX = \alpha X + \beta, solution α∗β\alpha^*\beta).
  • LINEAR IS NOT REGULAR. Only purely right-linear or purely left-linear grammars are guaranteed regular.

Summary

Sources

  1. J. E. Hopcroft, R. Motwani, J. D. Ullman, Introduction to Automata Theory, Languages, and Computation, 3rd ed., Pearson Addison-Wesley: Section 3.2 (finite automata and regular expressions, including 3.2.2 on eliminating states and 3.2.3 on converting regular expressions to automata).
  2. M. Sipser, Introduction to the Theory of Computation, 3rd ed., Cengage Learning: Section 1.3 (regular expressions, equivalence with finite automata).
  3. P. Linz, An Introduction to Formal Languages and Automata, 6th ed., Jones & Bartlett Learning: Sections 3.2 (regular expressions and regular languages) and 3.3 (regular grammars, right- and left-linear grammars).

Next steps