Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Last active February 1, 2018 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pervognsen/bed78438279114e402cfd932cde32bd0 to your computer and use it in GitHub Desktop.
Save pervognsen/bed78438279114e402cfd932cde32bd0 to your computer and use it in GitHub Desktop.
// Thompson's algorithm for NFA execution.
// Each NFA state is numbered 0 through n-1.
// An n-bit vector represents a superposition of NFA states, called a superstate.
// A state has a superstate as its successor, as a function of the input symbol.
// This induces a superstate-to-superstate successor mapping:
for (next_superstate = 0, successor = successors[symbol]; superstate; superstate >>= 1, successor++)
next_superstate |= *successor & -(superstate & 1);
// Run this, starting with an initial superstate, for each symbol of the input string.
// The final superstate is accepting if any of its states are accepting. This algorithm
// runs in O(m n) time, where m is the length of the input string. The space required for
// the state successor table is O(k n log(n)) words for a k-symbol alphabet, likely k = 256.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment