XML technologies / Regular expressions / How regex engines work / NFA, DFA and POSIX / Comparing DFA and NFA engines
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Both DFAs and NFAs have strengths and weaknesses, discussed below.
DFA versus NFA: compilation
Before applying the regex, both compile it into an internal form suited to their algorithm. NFA compilation is generally faster and uses less memory, with little difference between traditional and POSIX NFAs.
DFA versus NFA: search speed
Simple literal tests normally run at similar speeds. DFA speed during this phase is independent of regex complexity; NFA speed depends directly on it.
A traditional NFA tries every permutation and backtracking possibility before concluding failure. That is why the entire chapter The art of writing regular expressions explains writing efficient NFA regexes. Matching can take almost forever, though a traditional NFA at least stops when it finds a match.
A POSIX NFA tries every possibility to find the longest match. Finding an immediate match may therefore take as long as failure, sometimes very long. Efficient POSIX NFA regexes are doubly important.
This slightly overstates matters: optimizations can shorten NFA searches. You have seen one: an initial ˹^˼ anchor avoids testing later positions (see The transmission advances to the next character). Further optimizations appear under The art of writing regular expressions.
DFAs generally need few optimizations because they are inherently fast. Their compilation often does more work than many NFAs devote to optimization.
Some modern DFAs defer compilation time and memory costs until a potential match begins. Much of the previously built map may never be needed. Building only required parts on demand saves time and memory. This is called lazy evaluation, or deferred evaluation. However, search speed may then depend on regex complexity.
DFA versus NFA: match results
A DFA or POSIX NFA finds the leftmost-longest match. A traditional NFA may find that or something else. A given implementation is deterministic for a fixed regex and subject, but another implementation might differ. Nearly all NFAs I have used behave as described here, though no algorithm or standard guarantees it.
DFA versus NFA: features
NFAs can support many features unavailable to DFAs:
Capturing text with parenthesized subexpressions, including backreferences and determining where those subexpressions matched.
Lookaround and other compound zero-width assertions (see Lookahead; lookbehind). (Note: lex has a feature called trailing context, exactly like positive zero-width lookahead, but usable only at the regex's end.)
Lazy quantifiers and ordered alternation. A DFA could easily find the shortest overall match, although no tool directly supports this. But local lazy constructs or ordered alternatives cannot be implemented by a DFA.
Possessive quantifiers (see Possessive quantifiers) and atomic groups (see Atomic parentheses).
DFA speed with NFA features: regex nirvana?DFAs cannot support capturing parentheses or backreferences, but hybrid approaches combine the techniques. The box NFA theory and practice explains how NFAs evolved beyond theoretical ideals; DFAs could naturally do the same. Their architecture makes it harder, not impossible. GNU grep uses a simple, effective approach: a DFA where possible, an NFA for backreferences. GNU awk does similarly, using GNU grep's fast DFA for simple success tests, finding only the leftmost-shortest match. More demanding cases use an NFA. Thus GNU awk supports backreferences through its additional Henry Spencer's regex engine in Tcl (recall his early contributions; see Henry Spencer’s regex package) is remarkable. It supports lookaround, captures, and lazy quantifiers like an NFA, yet finds the POSIX leftmost-longest match (see NFA, DFA and POSIX) without some NFA problems discussed under The art of writing regular expressions. Truly impressive. |
DFA versus NFA: implementation difficulty
Simple engines of either type are easy to understand and build, but limited. Demands for speed, memory efficiency, and features naturally produce complex programs.
By source length, the NFA in ed from Unix Version 7 (January 1979) used under 350 lines; the entire grep program had 478. Henry Spencer's freely available Version 8 routines (1986) had 1,900 lines; Tom Lord's rx NFA package (1992) had 9,700 lines (rx is used, among others, in GNU sed ).
The Version 7 DFA had just over 400 lines; Henry Spencer's complete POSIX DFA package (1992) had 4,500.
GNU egrep version 2.4.2 combines both engines in about 8,300 lines. Tcl's hybrid DFA/NFA implementation has about 9,500.
Simple does not necessarily mean feature-poor. Once, needing regexes in Pascal after years without using it, I quickly built a simple NFA engine. It has few extras and is not fast, but its full NFA dialect makes it useful.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln