Sub topic 8
From regular expression to automaton
This chapter assumes you know DFAs, NFAs with ε-moves, ε-closure and subset construction, and the regular expression operators (union, concatenation, star). Here you will turn any regular expression into an ε-NFA mechanically, then into a DFA when you need one. This is the direction of the equivalence proof that GATE questions use most often.
The goal and the plan
Every regular expression denotes a regular language. To prove it, we build for each expression R an ε-NFA that accepts exactly L(R). The standard method is Thompson's construction. It works by structural induction on the expression:
- Handle the base cases: ∅, ε, and a single symbol.
- Handle the three operators, assuming ε-NFAs already exist for the sub-expressions.
Every fragment has exactly one start state and exactly one accepting state. The start state has no incoming edges from inside the fragment, and the accepting state has no outgoing edges. This invariant is what makes the pieces safe to glue together.
The base cases
| Expression | Fragment |
|---|---|
| ∅ | Start state and accepting state with no transition between them |
| ε | Start state, one ε-edge to the accepting state |
| a | Start state, one edge labeled a to the accepting state |
The three operator rules
Let N(R1) and N(R2) be the fragments for R1 and R2.
- Union R1 | R2. Add a new start state with ε-edges to the start states of both fragments. Add a new accepting state, with ε-edges from both old accepting states into it.
- Concatenation R1R2. Connect the accepting state of N(R1) to the start state of N(R2) with an ε-edge. The start of N(R1) is the new start, and the accept of N(R2) is the new accept. Many textbooks merge those two states into one instead. Both versions are correct, and the merged one has one fewer state per concatenation.
- Star R1\*. Add a new start and a new accepting state. Add ε-edges from the new start to the old start and to the new accept (this allows zero repetitions). Add ε-edges from the old accept to the new accept and back to the old start (this allows more repetitions).
The star fragment is the one learners get wrong most often, so here it is drawn out.
The edge from s straight to t is what makes ε belong to L(R\*). The edge from the old accept back to the old start allows repetition.
Size and shape guarantees
Thompson's construction has predictable bounds, and GATE questions often rely on them.
- The ε-edge version has at most 2 states per symbol occurrence and 2 per union or star. Concatenation adds none.
- Each state has at most two outgoing edges. If an edge is labeled with a symbol, it is the only outgoing edge of that state.
- The total size is linear in the length of the expression.
Take (a|b)\*abb. It has 5 symbol occurrences (10 states), one union (+2) and one star (+2), for 14 states with ε-edge concatenation. With merged concatenation, subtract 3 and you get 11.
A different construction, the Glushkov (position) automaton, avoids ε-edges entirely. It has one state per symbol occurrence plus a start state, so n + 1 states for n occurrences. For (a|b)\*abb that is 6 states. It is not the textbook default, but it shows that the 14-state figure is a property of the construction and not of the language.
Implementing the construction
The code below builds the fragments exactly as described and simulates them using ε-closure. States are integers. An edge label of None means ε.
from itertools import count
ids = count()
class NFA:
def __init__(self, start, accept, edges):
self.start = start
self.accept = accept
self.edges = edges # list of (src, label, dst); label None = epsilon
def states(self):
s = {self.start, self.accept}
for a, _, b in self.edges:
s.add(a)
s.add(b)
return s
def symbol(c):
s, t = next(ids), next(ids)
return NFA(s, t, [(s, c, t)])
def union(a, b):
s, t = next(ids), next(ids)
edges = a.edges + b.edges + [
(s, None, a.start), (s, None, b.start),
(a.accept, None, t), (b.accept, None, t),
]
return NFA(s, t, edges)
def concat(a, b):
edges = a.edges + b.edges + [(a.accept, None, b.start)]
return NFA(a.start, b.accept, edges)
def star(a):
s, t = next(ids), next(ids)
edges = a.edges + [
(s, None, a.start), (s, None, t),
(a.accept, None, a.start), (a.accept, None, t),
]
return NFA(s, t, edges)
def eclosure(nfa, S):
seen = set(S)
stack = list(S)
while stack:
q = stack.pop()
for src, lab, dst in nfa.edges:
if src == q and lab is None and dst not in seen:
seen.add(dst)
stack.append(dst)
return seen
def accepts(nfa, w):
cur = eclosure(nfa, {nfa.start})
for ch in w:
step = {dst for src, lab, dst in nfa.edges if src in cur and lab == ch}
cur = eclosure(nfa, step)
return nfa.accept in cur
# (a|b)*abb
body = star(union(symbol("a"), symbol("b")))
m = concat(concat(concat(body, symbol("a")), symbol("b")), symbol("b"))
print(len(m.states())) # 14
print(accepts(m, "aabb")) # True
print(accepts(m, "abab")) # False
print(accepts(m, "abb")) # TrueThe result agrees with the plain NFA for the same language from the earlier chapters. This NFA is bigger, but it was produced with no thinking about the language at all. That is the point of the construction.
Going on to a DFA
Thompson's NFA has ε-moves, so converting it to a DFA uses subset construction with ε-closure:
- The DFA start state is the ε-closure of the NFA start state.
- For a DFA state S and symbol c, the next state is the ε-closure of all states reachable from S on c.
- A DFA state is accepting if it contains the NFA accept state.
For (a|b)\*abb, the 14-state NFA reduces to a DFA with 4 reachable states after minimization. The unminimized subset construction may produce more states, since closure sets that differ only in ε-reachable states can still be distinct. Always check whether a question asks for the subset-construction result or the minimal DFA.
Common errors
- Forgetting that R\* accepts ε. Without the ε-edge from the new start to the new accept, the NFA rejects the empty string.
- Merging states in a union. Combining the two start states directly can create paths that mix the branches. Use a fresh start state.
- Counting only symbols. The state bound counts operators too. Union and star each add 2 states.
- Treating ∅ and ε alike. The fragment for ε connects start to accept, and the fragment for ∅ does not. Concatenating with ∅ gives ∅, and concatenating with ε changes nothing.
- Stopping at the NFA when a DFA is asked for. An NFA with ε-edges is not a DFA, even though it is a valid finite automaton.
When to build, and when to reason directly
On GATE questions you rarely draw a full Thompson NFA. For counting or equivalence questions, it is faster to reason about what the language needs the machine to remember. For (a|b)\*abb the DFA must track the longest suffix matching a prefix of abb, which gives 4 states. Use the mechanical construction when the expression is a nested mix of operators, where intuition fails, or when the question refers explicitly to the construction's structure, such as the number of ε-transitions or states.
Exercise
How many states does the Thompson NFA (with ε-edge concatenation, as in the code above) have for the expression (a|b)*? How many ε-edges does it have?
Show answer
There are 2 symbol occurrences (4 states), one union (+2) and one star (+2), so 8 states. The ε-edges are 4 from the union (2 out of the new start, 2 into the new accept) and 4 from the star, for 8 in total. Concatenation is not used here.
Exercise
Why must the star construction add a new start state instead of reusing the start state of N(R) as the start state of N(R*)?
Show answer
The old start state may have incoming edges from inside N(R), for instance after the back-edge from the old accept state is added. If it were also the start of the whole fragment, those loops could be entered at the start and used to accept strings outside L(R*), or could carry paths from the surrounding expression back into the loop. A fresh start state with no incoming edges keeps the fragment's entry and exit separate.
Exercise
Give the number of states of the Glushkov (position) automaton for (ab|a)*b, and explain why it needs no ε-edges.
Show answer
The expression has 4 symbol occurrences (a, b, a, b), so the automaton has 4 + 1 = 5 states: one per position plus a start state. Each transition goes from one position directly to a position that may follow it in some word, so the ε-edges that Thompson's construction uses for wiring are not needed.