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 labelRewrite 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 thumbnever attach a star-loop to a node that has other traffic.

📐 Worked example —

StageEdge labels presentRule applied
0
1 · · concatenation (twice)
2 · · union ⟹ parallel edges
3 · · · concatenation on
4star rules on and each ⟹ , middle state + loop
5 · · onlysplit 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

⚠️ 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