Converting Regular Expressions to NFA
Context: FIT2014_MOC · leg 1 of the Kleene’s Theorem cycle · Assignment 1 hand skill Problem it solves: given a regular expression, build an NFA recognising the same language.
Quick Revision
- 🎯 Trigger: a regex to turn into a machine ➔ start with one edge labelled the whole expression, then rewrite edges until every label is a single letter or .
- ⚡ Key Constraint: the rule needs two -transitions and a fresh middle state — putting the -loop on the left or right node is wrong in general.
🔧 The procedure
Start with: a Start state and a Final state joined by a single edge labelled with the entire regular expression. Repeat: apply the rewrite rules below until every edge is labelled by a letter or .
| Edge label | Rewrite to |
|---|---|
| delete the edge (no transition at all) | |
| a single edge labelled (brackets are only grouping) | |
| two edges in series through a new intermediate state: | |
| two parallel edges between the same pair, labelled and | |
| with a self-loop labelled on the new middle state |
⭐ Why needs both -transitions
stateDiagram-v2 direction LR qin --> mid: ε mid --> mid: R mid --> qout: ε
- The danger ➔ if the -loop were placed on the left node, any other edge arriving there (say or ) could enter the loop, letting the machine match — strings the original expression never described. Symmetrically for the right node with , .
- The fix ➔ the fresh middle state is reachable only via the two -edges, so the loop is sealed off from the surrounding structure.
- Rule of thumb ➔ never attach a star-loop to a node that has other traffic.
📐 Worked example —
| Stage | Edge labels present | Rule applied |
|---|---|---|
| 0 | — | |
| 1 | · · | concatenation (twice) |
| 2 | · ∥ · | union ⟹ parallel edges |
| 3 | · · · | concatenation on |
| 4 | star rules on and | each ⟹ , middle state + loop |
| 5 | · · only | split the loop by concatenation |
- Order is free ➔ any rule may be applied to any eligible edge; the resulting NFAs differ in shape but all recognise the same language.
✍️ Practice
Practice: Convert into an NFA with only letter/ labels.
Reference solution
- Concatenation ⟹ three edges in series: , then , then .
- Star on the middle edge ⟹ into a fresh state, a self-loop labelled , out.
- Union on the loop ⟹ two parallel loops labelled and .
- Concatenation on each ⟹ and through new middle states.
- Key move: apply star before union here, so the two -edges seal the loop before it is split into branches.
⚠️ Common Mistakes
- 💡 deletes, it does not become a state ➔ an edge labelled is removable because no string matches it.
- 💡 Don’t skip the middle state for ➔ attaching the loop directly to an existing node lets neighbouring edges leak into the loop.
- 💡 Union means parallel, not sequential ➔ becomes two edges between the same pair of states; chaining them would encode .
- 💡 Stop only when every label is a letter or ➔ a leftover multi-letter label like means the conversion is unfinished.
🧠 Active Recall
Why does the rule introduce a new state and two -transitions rather than looping on an existing node?
Answer
- Short answer: an existing node may have other incoming or outgoing edges ( on the left; on the right). A loop attached there could be entered from those edges, so the machine would accept strings like that the original expression does not describe.
- Why: Isolation of the loop ➔ the fresh middle state is reachable only through the two -edges, guaranteeing that repetitions of occur exactly where the star sits in the expression.