Visualizations
069 / 500

069 · Algorithms

Regex Automaton

Type a regex and watch it compile into an NFA, a DFA and a minimal DFA.

A small recursive descent parser turns the pattern into a syntax tree, and Thompson's construction turns the tree into an NFA whose layout mirrors the pattern: concatenation runs left to right, alternatives stack, stars loop over the top and skip underneath. Subset construction then discovers DFA states one set of NFA states at a time, and Hopcroft's partition refinement colors equivalent states until no input can tell them apart, flying the surviving blocks into the minimal DFA. A test string then runs through all three machines at once: the NFA keeps a glowing token on every state it might be in, spreading along epsilon moves after each character, while each DFA moves a single token. A recursive backtracking matcher counts its own steps alongside, which is where the catastrophic preset earns its name.

Try it. Click the pattern or test field and type (Enter or the up and down arrows switch fields); every keystroke recompiles. Pick a preset below, hover a DFA state to see which NFA states it stands for, and click a machine to replay the test string.

  • Thompson's construction
  • Subset construction
  • Hopcroft minimization
  • Layered graph layout

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build an interactive regular expression visualizer with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Add a text input for a pattern and another for a test string above a full-width canvas that stays sharp on high-DPI screens.
- Write a tiny recursive descent parser for literals, parentheses, | (alternation), and the * + ? operators. It should produce a syntax tree and report the position of any error.
- Compile the tree into an NFA with Thompson's construction: each fragment has a start and an end state, and epsilon edges glue fragments together.
- Lay the NFA out straight from the tree: concatenation goes left to right, alternatives stack vertically, and a star's back edge arcs over the top while its skip edge arcs underneath. Draw states as circles, the accepting state as a double circle, and epsilon edges dashed.
- Simulate the test string by tracking the set of active states (follow epsilon edges after every character) and highlight that set as the string is read one character per half second.

Once that works, make it beautiful:
- Animate a glowing dot along every edge being followed, so you can watch the NFA explore all possibilities at once.
- Add subset construction to build a DFA from sets of NFA states and draw it beside the NFA, with a single token moving through it in step.
- Recompile on every keystroke and show a friendly error marker under the pattern when it does not parse.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as Hopcroft minimization of the DFA, character classes with a symbol-class alphabet, or a step counter that compares the NFA with a backtracking matcher on catastrophic patterns like (a+)+b.
PreviousKintsugiA glazed tea bowl shatters along branching cracks, then is mended with flowing gold. NextLightningBranching lightning that grows from a dielectric breakdown model.

Related visualizations

  • Syntax RailroadType and image A recursive descent parser drives a train through railroad syntax diagrams.
  • Tiny CompilerAlgorithms A whole compiler, from source to running bytecode, rebuilt on every keystroke.
  • Automaton OrchestraEmergence Elementary cellular automata woven into cloth on a loom and played as music.
  • Turing MachineRetro A brass Turing machine runs busy beavers, including all 47 million steps of BB(5).
  • RRT* ParkingAlgorithms A self-driving car grows a tree of curved manoeuvres, then reverses into its spot.
  • Autograd CircuitMachine learning Type a formula and watch reverse-mode autodiff run on a printed circuit board.
  • Huffman GardenAlgorithms Letter frequencies sprout into a Huffman tree drawn as a botanical plate.
  • BiomorphsAlgorithms Dawkins' Blind Watchmaker: breed recursive ink creatures by choosing a child.
  • Symbolic RegressionMachine learning Genetic programming breeds formulas until it rediscovers the law behind the data.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs