Sub topic 4
From Regular Expression to NFA to DFA
Assume you know basic regular expressions, finite automata, sets, and graph traversal. This chapter shows how a token pattern becomes an automaton that a lexer can run.
Why convert a regular expression?
A regular expression is convenient for describing a language. An automaton is convenient for recognizing input one character at a time.
The usual conversion has two stages:
- Convert the regular expression to an NFA.
- Convert the NFA to a DFA.
An NFA, or nondeterministic finite automaton, may have several possible next states and may change state without reading input. A DFA, or deterministic finite automaton, has exactly one next state for each state and input symbol.
Building an NFA with Thompson's construction
Thompson's construction creates a small NFA for each part of the expression and joins those pieces with epsilon transitions. An epsilon transition consumes no input.
| Regular expression | NFA construction | Column |
|---|---|---|
| `a` | One transition labeled `a` | |
| `R S` | Connect the accepting state of `R` to the start of `S` | |
| `R \ | S` | Split with epsilon transitions, then join both branches |
| `R*` | Add paths that skip `R` or repeat it |
For example, a|b starts at state 0. Epsilon transitions lead to an a branch and a b branch. Both branches then lead to the accepting state.
Converting an NFA to a DFA
The subset construction treats each DFA state as a set of NFA states.
For each set:
- Compute its epsilon-closure, which contains every state reachable using only epsilon transitions.
- For each input symbol, find all reachable NFA states.
- Take the epsilon-closure of that result.
- Make that set a DFA state if it is new.
- Mark a DFA state accepting if its set contains an NFA accepting state.
The empty set can be added as a dead state when a complete DFA is required.
from collections import deque
nfa = {
0: {"ε": {1, 3}},
1: {"a": {2}},
2: {"ε": {5}},
3: {"b": {4}},
4: {"ε": {5}},
5: {},
}
start, accept = 0, 5
alphabet = {"a", "b"}
def epsilon_closure(states):
result = set(states)
work = list(states)
while work:
state = work.pop()
for nxt in nfa[state].get("ε", set()):
if nxt not in result:
result.add(nxt)
work.append(nxt)
return frozenset(result)
def move(states, symbol):
return {
nxt for state in states
for nxt in nfa[state].get(symbol, set())
}
dfa_start = epsilon_closure({start})
dfa_states = {dfa_start}
queue = deque([dfa_start])
transitions = {}
while queue:
current = queue.popleft()
for symbol in alphabet:
target = epsilon_closure(move(current, symbol))
transitions[(current, symbol)] = target
if target not in dfa_states:
dfa_states.add(target)
queue.append(target)
for state in dfa_states:
print(state, "accepting" if accept in state else "non-accepting")The resulting DFA recognizes exactly the strings recognized by the original regular expression. Its states may contain several NFA states, so the DFA can have up to 2^n states for an NFA with n states.
Exercise
In the subset construction, when is a DFA state accepting?
Show answer
When its corresponding set of NFA states contains at least one accepting NFA state.