Constructing an NFA for the Regular Expression
We want an NFA with -moves (if needed) whose language equals the regular expression
Intuitively, this language is the set of all binary strings over that contain the substring “00” somewhere. Formally, denotes all binary strings, and the concatenation forces an occurrence of while the starred parts allow any prefix/suffix around it.
A standard way to construct such an NFA from a regular expression is Thompson’s construction producing an equivalent epsilon-NFA. Thompson’s construction is guaranteed to produce an NFA that recognizes the same language as the input regex .
Below we build an -NFA directly by composing NFAs for the subexpressions using the Thompson rules for union, concatenation, and Kleene star .
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩ ↩2
Key subexpressions (for reuse)
Let:
Then the full expression is .
We will use Thompson’s construction rules:
- Union: create a new start/end with -branches to both operands.
- Concatenation: connect final states of the first part to the start of the second via an link.
- Kleene star: add transitions that allow skipping the sub-NFA (zero repetitions) and looping for more repetitions.
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩ ↩2 ↩3
Step-by-step Thompson-style construction for $(0+1)^*00(0+1)^*$
- 1Step 1
Work over . Target regex is , i.e., prefix in , then “00”, then suffix in .
- 2Step 2
Create small NFAs for single symbols using Thompson’s base rule: a start state with a transition on that symbol to an end state .
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩
-
- 3Step 3
Construct union by adding a new entry state with transitions to the NFA for and to the NFA for , then merge into a new exit state via transitions .
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩
-
- 4Step 4
Apply the star rule: add transitions that allow (i) skipping the starred sub-NFA (for zero repetitions) and (ii) looping from the inner end back to the inner start .
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩
-
- 5Step 5
Create two single-symbol NFAs for ‘0’, then concatenate them by connecting the first’s end to the second’s start via an transition .
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩
-
- 6Step 6
Take the NFA for , connect its unique accept state to the start of the NFA with an transition, then connect the accept state of to the start of the final NFA with an transition (concatenation rule) .
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩
-
- 7Step 7
Rename states to avoid collisions when assembling the three big pieces. Then specify (for ) and . The resulting NFA is equivalent to the original regex by Thompson’s construction correctness guarantee .
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩
-
Concrete -NFA
We present one explicit construction with states:
- start
- internal
- accept
All transitions not mentioned go to .
-transitions
- From : to and
- Looping structure inside the first :
- Connecting between parts:
- (first to the middle)
- (finish to second star)
- Star exit/skip behavior:
- Looping structure inside the second :
- (repeat the “ chunk”)
- (alternate path to ensure closure behavior)
Symbol transitions (on input or )
- For the union “” chunks (used by both stars):
- Continue union/chunk after consuming one symbol:
Middle “” piece
- First 0 of the :
- Second 0 of the :
- Then to connect to second star occurs via:
Note: This is a valid Thompson-style -NFA pattern. Thompson’s construction guarantees language equivalence to the regex ; different state labelings or exact wiring that preserves the Thompson-rule structure can yield isomorphic NFAs.
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩
Why this NFA matches exactly the “contains 00” language
The two components can generate any binary prefix and any binary suffix due to the Kleene-star wiring that permits zero or more repetitions . The segment forces two consecutive symbols through the chained transitions .
A string is accepted iff there exists a split: which is exactly “somewhere in the string there is a substring.”
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩
Tip: Verify by reasoning about substrings
Before converting, interpret as “any prefix, then 00, then any suffix”. After building the NFA, check that the only way to reach accept requires consuming two consecutive 0s in the middle.
Warning: Don’t forget ε-closure when simulating
If your NFA has ε-transitions (Thompson’s construction does), acceptance simulation must consider the ε-closure of the current state set before and after consuming each input symbol. Thompson’s construction is defined using ε-transitions .
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩
Optional structure map (how the machine is composed)
Each block is itself an -NFA built from Thompson rules:
Footnotes
-
Thompson's construction - Wikipedia - Rules for converting regular expressions into equivalent ε-NFAs (union, concatenation, Kleene star) with correctness via structural induction. ↩
Construction roadmap
Parse the regex
1Identify Kleene stars, union , and concatenation around the fixed substring ."
Build base NFAs
2Create NFAs for symbols 0 and 1, and for union via Thompson’s union rule."
Apply Kleene star
3Wrap the union NFA with the star rule to get ."
Concatenate components
4Place between two copies of using Thompson concatenation links."
Assemble final ε-NFA
5Rename states to avoid collisions and list transitions; language equivalence follows from Thompson’s construction."
Knowledge Check
What language does describe over ?