XML technologies / Regular expressions / How regex engines work / NFA, DFA and POSIX / Speed and efficiency
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
If efficiency matters with traditional NFAs—and it does as soon as backtracking is involved—it matters even more with a POSIX NFA, where much more backtracking is possible. A POSIX NFA engine must examine every possible permutation of the regex. Examples under The art of writing regular expressions show how a poorly written regular expression can suffer extreme performance losses.
DFA efficiency
Text-directed processing in DFAs is an excellent way to avoid tedious, time-consuming backtracking. Its speed comes from following all possible matches simultaneously. How is this magical behavior achieved?
NFA: theory and practiceThe mathematical meaning of NFA differs somewhat from what computer science calls an NFA regex engine. Mathematically, an NFA and a DFA should recognize exactly the same text and otherwise have the same properties. In practice, the desire for more features and more expressive regular expressions has created a clear distinction. We will see further differences, but the most important is support for backreferences. A programmer can relatively easily add backreference support to a genuine NFA engine in the mathematical sense. A DFA’s structure rules this out, but an NFA makes it easy. The resulting tool is much more useful, but the change makes the engine decidedly nonregular to a mathematician. What does this mean in practice? Perhaps we should stop calling it an NFA and speak of nonregular expressions, since that describes the new mathematical situation. Nobody does: the name NFA has remained, even though the implementation is no longer an NFA to a mathematician. What does that mean for you as a regex user? Absolutely nothing. You do not care whether mathematics calls the engine regular, nonregular, unregular, irregular, or uncontrolled. Once you understand what to expect from the engine, as this chapter explains, you know all you need to use regular expressions. Readers interested in regex theory are referred to Chapter 3 of Aho, Sethi, and Ullman’s Compilers – Principles, Techniques, and Tools (Addison-Wesley, 1986; German edition: Compilerbau), known as the Dragon Book because of its cover illustration. |
A DFA spends some processing time and memory before examining the string. It analyzes the regex more thoroughly, and differently, than an NFA. Internally, it builds a map or decision structure indicating that an incoming character belongs to one possible match or another. As characters are read from the string, the DFA simply follows the routes marked on this map.
Building the map can take considerable time and memory. Once built for a particular regex, however, it can be used for an unlimited amount of text.
It is like charging an electric car’s batteries. The car sits in the garage connected to its charger. When needed, the energy is immediately available, without a starter motor, engine warm-up, or preheating.
This one-time work before matching, performed once per regex, is called compilation of the regex. A DFA builds the internal map described above. NFAs also compile and build an internal representation, but theirs more closely resembles a small program that the engine later executes.
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