Every programming language you have ever used relies on a rule that regular expressions cannot enforce: brackets must balance. Write an opening ( and the language demands a matching ) somewhere later — regardless of what appears in between and regardless of how deeply things nest. A regular expression has no memory of how deep you are. It simply cannot count.
The fix is surprisingly small. Take a finite automaton — the machine behind every regex engine — and bolt on a stack. The result is a pushdown automaton (PDA). When the machine reads an opening bracket it pushes a marker; when it reads a closing bracket it pops one. At the end, if the stack is empty, the brackets balanced.
That tiny addition — one infinite stack — is precisely enough to jump from regular languages to context-free languages (CFLs), the second tier of the Chomsky hierarchy. Context-free languages include the grammars of nearly every programming language, arithmetic expressions, HTML nesting, and a great deal of natural language structure. Regular expressions, no matter how clever, cannot capture them. A PDA — proven by Noam Chomsky and colleagues in the late 1950s — captures them exactly.
Comments
Loading comments...