--- code/trunk/doc/pcrematching.3 2007/02/24 21:41:34 91 +++ code/trunk/doc/pcrematching.3 2007/05/29 15:18:18 168 @@ -26,7 +26,7 @@ .sp there are three possible answers. The standard algorithm finds only one of -them, whereas the DFA algorithm finds all three. +them, whereas the alternative algorithm finds all three. . .SH "REGULAR EXPRESSIONS AS TREES" .rs @@ -41,8 +41,8 @@ .SH "THE STANDARD MATCHING ALGORITHM" .rs .sp -In the terminology of Jeffrey Friedl's book \fIMastering Regular -Expressions\fP, the standard algorithm is an "NFA algorithm". It conducts a +In the terminology of Jeffrey Friedl's book "Mastering Regular +Expressions", the standard algorithm is an "NFA algorithm". It conducts a depth-first search of the pattern tree. That is, it proceeds along a single path through the tree, checking that the subject matches what is required. When there is a mismatch, the algorithm tries any alternatives at the current point, @@ -63,15 +63,16 @@ matched by portions of the pattern in parentheses. This provides support for capturing parentheses and back references. . -.SH "THE DFA MATCHING ALGORITHM" +.SH "THE ALTERNATIVE MATCHING ALGORITHM" .rs .sp -DFA stands for "deterministic finite automaton", but you do not need to -understand the origins of that name. This algorithm conducts a breadth-first -search of the tree. Starting from the first matching point in the subject, it -scans the subject string from left to right, once, character by character, and -as it does this, it remembers all the paths through the tree that represent -valid matches. +This algorithm conducts a breadth-first search of the tree. Starting from the +first matching point in the subject, it scans the subject string from left to +right, once, character by character, and as it does this, it remembers all the +paths through the tree that represent valid matches. In Friedl's terminology, +this is a kind of "DFA algorithm", though it is not implemented as a +traditional finite state machine (it keeps multiple states active +simultaneously). .P The scan continues until either the end of the subject is reached, or there are no more unterminated paths. At this point, terminated paths represent the @@ -92,11 +93,20 @@ matches that start at later positions. .P There are a number of features of PCRE regular expressions that are not -supported by the DFA matching algorithm. They are as follows: +supported by the alternative matching algorithm. They are as follows: .P 1. Because the algorithm finds all possible matches, the greedy or ungreedy nature of repetition quantifiers is not relevant. Greedy and ungreedy -quantifiers are treated in exactly the same way. +quantifiers are treated in exactly the same way. However, possessive +quantifiers can make a difference when what follows could also match what is +quantified, for example in a pattern like this: +.sp + ^a++\ew! +.sp +This pattern matches "aaab!" but not "aaa!", which would be matched by a +non-possessive quantifier. Similarly, if an atomic group is present, it is +matched as if it were a standalone pattern at the current point, and the +longest match is then "locked in" for the rest of the overall pattern. .P 2. When dealing with multiple paths through the tree simultaneously, it is not straightforward to keep track of captured substrings for the different matching @@ -107,21 +117,25 @@ not supported, and cause errors if encountered. .P 4. For the same reason, conditional expressions that use a backreference as the -condition are not supported. +condition or test for a specific group recursion are not supported. .P -5. Callouts are supported, but the value of the \fIcapture_top\fP field is +5. Because many paths through the tree may be active, the \eK escape sequence, +which resets the start of the match when encountered (but may be on some paths +and not on others), is not supported. It causes an error if encountered. +.P +6. Callouts are supported, but the value of the \fIcapture_top\fP field is always 1, and the value of the \fIcapture_last\fP field is always -1. .P -6. +7. The \eC escape sequence, which (in the standard algorithm) matches a single -byte, even in UTF-8 mode, is not supported because the DFA algorithm moves -through the subject string one character at a time, for all active paths +byte, even in UTF-8 mode, is not supported because the alternative algorithm +moves through the subject string one character at a time, for all active paths through the tree. . -.SH "ADVANTAGES OF THE DFA ALGORITHM" +.SH "ADVANTAGES OF THE ALTERNATIVE ALGORITHM" .rs .sp -Using the DFA matching algorithm provides the following advantages: +Using the alternative matching algorithm provides the following advantages: .P 1. All possible matches (at a single point in the subject) are automatically found, and in particular, the longest match is found. To find more than one @@ -130,17 +144,18 @@ .P 2. There is much better support for partial matching. The restrictions on the content of the pattern that apply when using the standard algorithm for partial -matching do not apply to the DFA algorithm. For non-anchored patterns, the -starting position of a partial match is available. +matching do not apply to the alternative algorithm. For non-anchored patterns, +the starting position of a partial match is available. .P -3. Because the DFA algorithm scans the subject string just once, and never -needs to backtrack, it is possible to pass very long subject strings to the -matching function in several pieces, checking for partial matching each time. +3. Because the alternative algorithm scans the subject string just once, and +never needs to backtrack, it is possible to pass very long subject strings to +the matching function in several pieces, checking for partial matching each +time. . -.SH "DISADVANTAGES OF THE DFA ALGORITHM" +.SH "DISADVANTAGES OF THE ALTERNATIVE ALGORITHM" .rs .sp -The DFA algorithm suffers from a number of disadvantages: +The alternative algorithm suffers from a number of disadvantages: .P 1. It is substantially slower than the standard algorithm. This is partly because it has to search for all possible matches, but is also because it is @@ -148,10 +163,24 @@ .P 2. Capturing parentheses and back references are not supported. .P -3. The "atomic group" feature of PCRE regular expressions is supported, but -does not provide the advantage that it does for the standard algorithm. -.P -.in 0 -Last updated: 06 June 2006 -.br -Copyright (c) 1997-2006 University of Cambridge. +3. Although atomic groups are supported, their use does not provide the +performance advantage that it does for the standard algorithm. +. +. +.SH AUTHOR +.rs +.sp +.nf +Philip Hazel +University Computing Service +Cambridge CB2 3QH, England. +.fi +. +. +.SH REVISION +.rs +.sp +.nf +Last updated: 29 May 2007 +Copyright (c) 1997-2007 University of Cambridge. +.fi