| 1 |
nigel |
41 |
Technical Notes about PCRE |
| 2 |
|
|
-------------------------- |
| 3 |
|
|
|
| 4 |
|
|
Many years ago I implemented some regular expression functions to an algorithm |
| 5 |
|
|
suggested by Martin Richards. These were not Unix-like in form, and were quite |
| 6 |
|
|
restricted in what they could do by comparison with Perl. The interesting part |
| 7 |
|
|
about the algorithm was that the amount of space required to hold the compiled |
| 8 |
|
|
form of an expression was known in advance. The code to apply an expression did |
| 9 |
|
|
not operate by backtracking, as the Henry Spencer and Perl code does, but |
| 10 |
|
|
instead checked all possibilities simultaneously by keeping a list of current |
| 11 |
|
|
states and checking all of them as it advanced through the subject string. (In |
| 12 |
|
|
the terminology of Jeffrey Friedl's book, it was a "DFA algorithm".) When the |
| 13 |
|
|
pattern was all used up, all remaining states were possible matches, and the |
| 14 |
|
|
one matching the longest subset of the subject string was chosen. This did not |
| 15 |
|
|
necessarily maximize the individual wild portions of the pattern, as is |
| 16 |
|
|
expected in Unix and Perl-style regular expressions. |
| 17 |
|
|
|
| 18 |
|
|
By contrast, the code originally written by Henry Spencer and subsequently |
| 19 |
|
|
heavily modified for Perl actually compiles the expression twice: once in a |
| 20 |
|
|
dummy mode in order to find out how much store will be needed, and then for |
| 21 |
|
|
real. The execution function operates by backtracking and maximizing (or, |
| 22 |
|
|
optionally, minimizing in Perl) the amount of the subject that matches |
| 23 |
|
|
individual wild portions of the pattern. This is an "NFA algorithm" in Friedl's |
| 24 |
|
|
terminology. |
| 25 |
|
|
|
| 26 |
nigel |
43 |
For the set of functions that forms PCRE (which are unrelated to those |
| 27 |
|
|
mentioned above), I tried at first to invent an algorithm that used an amount |
| 28 |
|
|
of store bounded by a multiple of the number of characters in the pattern, to |
| 29 |
|
|
save on compiling time. However, because of the greater complexity in Perl |
| 30 |
|
|
regular expressions, I couldn't do this. In any case, a first pass through the |
| 31 |
|
|
pattern is needed, in order to find internal flag settings like (?i) at top |
| 32 |
|
|
level. So PCRE works by running a very degenerate first pass to calculate a |
| 33 |
|
|
maximum store size, and then a second pass to do the real compile - which may |
| 34 |
|
|
use a bit less than the predicted amount of store. The idea is that this is |
| 35 |
|
|
going to turn out faster because the first pass is degenerate and the second |
| 36 |
|
|
pass can just store stuff straight into the vector. It does make the compiling |
| 37 |
|
|
functions bigger, of course, but they have got quite big anyway to handle all |
| 38 |
|
|
the Perl stuff. |
| 39 |
nigel |
41 |
|
| 40 |
|
|
The compiled form of a pattern is a vector of bytes, containing items of |
| 41 |
|
|
variable length. The first byte in an item is an opcode, and the length of the |
| 42 |
|
|
item is either implicit in the opcode or contained in the data bytes which |
| 43 |
|
|
follow it. A list of all the opcodes follows: |
| 44 |
|
|
|
| 45 |
|
|
Opcodes with no following data |
| 46 |
|
|
------------------------------ |
| 47 |
|
|
|
| 48 |
|
|
These items are all just one byte long |
| 49 |
|
|
|
| 50 |
|
|
OP_END end of pattern |
| 51 |
|
|
OP_ANY match any character |
| 52 |
|
|
OP_SOD match start of data: \A |
| 53 |
|
|
OP_CIRC ^ (start of data, or after \n in multiline) |
| 54 |
|
|
OP_NOT_WORD_BOUNDARY \W |
| 55 |
|
|
OP_WORD_BOUNDARY \w |
| 56 |
|
|
OP_NOT_DIGIT \D |
| 57 |
|
|
OP_DIGIT \d |
| 58 |
|
|
OP_NOT_WHITESPACE \S |
| 59 |
|
|
OP_WHITESPACE \s |
| 60 |
|
|
OP_NOT_WORDCHAR \W |
| 61 |
|
|
OP_WORDCHAR \w |
| 62 |
|
|
OP_EODN match end of data or \n at end: \Z |
| 63 |
|
|
OP_EOD match end of data: \z |
| 64 |
|
|
OP_DOLL $ (end of data, or before \n in multiline) |
| 65 |
nigel |
43 |
OP_RECURSE match the pattern recursively |
| 66 |
nigel |
41 |
|
| 67 |
|
|
|
| 68 |
|
|
Repeating single characters |
| 69 |
|
|
--------------------------- |
| 70 |
|
|
|
| 71 |
|
|
The common repeats (*, +, ?) when applied to a single character appear as |
| 72 |
|
|
two-byte items using the following opcodes: |
| 73 |
|
|
|
| 74 |
|
|
OP_STAR |
| 75 |
|
|
OP_MINSTAR |
| 76 |
|
|
OP_PLUS |
| 77 |
|
|
OP_MINPLUS |
| 78 |
|
|
OP_QUERY |
| 79 |
|
|
OP_MINQUERY |
| 80 |
|
|
|
| 81 |
|
|
Those with "MIN" in their name are the minimizing versions. Each is followed by |
| 82 |
|
|
the character that is to be repeated. Other repeats make use of |
| 83 |
|
|
|
| 84 |
|
|
OP_UPTO |
| 85 |
|
|
OP_MINUPTO |
| 86 |
|
|
OP_EXACT |
| 87 |
|
|
|
| 88 |
|
|
which are followed by a two-byte count (most significant first) and the |
| 89 |
|
|
repeated character. OP_UPTO matches from 0 to the given number. A repeat with a |
| 90 |
|
|
non-zero minimum and a fixed maximum is coded as an OP_EXACT followed by an |
| 91 |
|
|
OP_UPTO (or OP_MINUPTO). |
| 92 |
|
|
|
| 93 |
|
|
|
| 94 |
|
|
Repeating character types |
| 95 |
|
|
------------------------- |
| 96 |
|
|
|
| 97 |
|
|
Repeats of things like \d are done exactly as for single characters, except |
| 98 |
|
|
that instead of a character, the opcode for the type is stored in the data |
| 99 |
|
|
byte. The opcodes are: |
| 100 |
|
|
|
| 101 |
|
|
OP_TYPESTAR |
| 102 |
|
|
OP_TYPEMINSTAR |
| 103 |
|
|
OP_TYPEPLUS |
| 104 |
|
|
OP_TYPEMINPLUS |
| 105 |
|
|
OP_TYPEQUERY |
| 106 |
|
|
OP_TYPEMINQUERY |
| 107 |
|
|
OP_TYPEUPTO |
| 108 |
|
|
OP_TYPEMINUPTO |
| 109 |
|
|
OP_TYPEEXACT |
| 110 |
|
|
|
| 111 |
|
|
|
| 112 |
|
|
Matching a character string |
| 113 |
|
|
--------------------------- |
| 114 |
|
|
|
| 115 |
|
|
The OP_CHARS opcode is followed by a one-byte count and then that number of |
| 116 |
|
|
characters. If there are more than 255 characters in sequence, successive |
| 117 |
|
|
instances of OP_CHARS are used. |
| 118 |
|
|
|
| 119 |
|
|
|
| 120 |
|
|
Character classes |
| 121 |
|
|
----------------- |
| 122 |
|
|
|
| 123 |
|
|
OP_CLASS is used for a character class, provided there are at least two |
| 124 |
|
|
characters in the class. If there is only one character, OP_CHARS is used for a |
| 125 |
|
|
positive class, and OP_NOT for a negative one (that is, for something like |
| 126 |
|
|
[^a]). Another set of repeating opcodes (OP_NOTSTAR etc.) are used for a |
| 127 |
|
|
repeated, negated, single-character class. The normal ones (OP_STAR etc.) are |
| 128 |
|
|
used for a repeated positive single-character class. |
| 129 |
|
|
|
| 130 |
nigel |
43 |
OP_CLASS is followed by a 32-byte bit map containing a 1 bit for every |
| 131 |
|
|
character that is acceptable. The bits are counted from the least significant |
| 132 |
|
|
end of each byte. |
| 133 |
nigel |
41 |
|
| 134 |
|
|
|
| 135 |
|
|
Back references |
| 136 |
|
|
--------------- |
| 137 |
|
|
|
| 138 |
nigel |
53 |
OP_REF is followed by two bytes containing the reference number. |
| 139 |
nigel |
41 |
|
| 140 |
|
|
|
| 141 |
|
|
Repeating character classes and back references |
| 142 |
|
|
----------------------------------------------- |
| 143 |
|
|
|
| 144 |
|
|
Single-character classes are handled specially (see above). This applies to |
| 145 |
|
|
OP_CLASS and OP_REF. In both cases, the repeat information follows the base |
| 146 |
|
|
item. The matching code looks at the following opcode to see if it is one of |
| 147 |
|
|
|
| 148 |
|
|
OP_CRSTAR |
| 149 |
|
|
OP_CRMINSTAR |
| 150 |
|
|
OP_CRPLUS |
| 151 |
|
|
OP_CRMINPLUS |
| 152 |
|
|
OP_CRQUERY |
| 153 |
|
|
OP_CRMINQUERY |
| 154 |
|
|
OP_CRRANGE |
| 155 |
|
|
OP_CRMINRANGE |
| 156 |
|
|
|
| 157 |
|
|
All but the last two are just single-byte items. The others are followed by |
| 158 |
|
|
four bytes of data, comprising the minimum and maximum repeat counts. |
| 159 |
|
|
|
| 160 |
|
|
|
| 161 |
|
|
Brackets and alternation |
| 162 |
|
|
------------------------ |
| 163 |
|
|
|
| 164 |
nigel |
43 |
A pair of non-capturing (round) brackets is wrapped round each expression at |
| 165 |
nigel |
41 |
compile time, so alternation always happens in the context of brackets. |
| 166 |
nigel |
53 |
|
| 167 |
nigel |
43 |
Non-capturing brackets use the opcode OP_BRA, while capturing brackets use |
| 168 |
nigel |
41 |
OP_BRA+1, OP_BRA+2, etc. [Note for North Americans: "bracket" to some English |
| 169 |
nigel |
43 |
speakers, including myself, can be round, square, curly, or pointy. Hence this |
| 170 |
|
|
usage.] |
| 171 |
nigel |
41 |
|
| 172 |
nigel |
53 |
Originally PCRE was limited to 99 capturing brackets (so as not to use up all |
| 173 |
|
|
the opcodes). From release 3.5, there is no limit. What happens is that the |
| 174 |
|
|
first ones, up to EXTRACT_BASIC_MAX are handled with separate opcodes, as |
| 175 |
|
|
above. If there are more, the opcode is set to EXTRACT_BASIC_MAX+1, and the |
| 176 |
|
|
first operation in the bracket is OP_BRANUMBER, followed by a 2-byte bracket |
| 177 |
|
|
number. This opcode is ignored while matching, but is fished out when handling |
| 178 |
|
|
the bracket itself. (They could have all been done like this, but I was making |
| 179 |
|
|
minimal changes.) |
| 180 |
|
|
|
| 181 |
nigel |
41 |
A bracket opcode is followed by two bytes which give the offset to the next |
| 182 |
|
|
alternative OP_ALT or, if there aren't any branches, to the matching KET |
| 183 |
|
|
opcode. Each OP_ALT is followed by two bytes giving the offset to the next one, |
| 184 |
|
|
or to the KET opcode. |
| 185 |
|
|
|
| 186 |
|
|
OP_KET is used for subpatterns that do not repeat indefinitely, while |
| 187 |
|
|
OP_KETRMIN and OP_KETRMAX are used for indefinite repetitions, minimally or |
| 188 |
|
|
maximally respectively. All three are followed by two bytes giving (as a |
| 189 |
|
|
positive number) the offset back to the matching BRA opcode. |
| 190 |
|
|
|
| 191 |
|
|
If a subpattern is quantified such that it is permitted to match zero times, it |
| 192 |
|
|
is preceded by one of OP_BRAZERO or OP_BRAMINZERO. These are single-byte |
| 193 |
|
|
opcodes which tell the matcher that skipping this subpattern entirely is a |
| 194 |
|
|
valid branch. |
| 195 |
|
|
|
| 196 |
|
|
A subpattern with an indefinite maximum repetition is replicated in the |
| 197 |
|
|
compiled data its minimum number of times (or once with a BRAZERO if the |
| 198 |
|
|
minimum is zero), with the final copy terminating with a KETRMIN or KETRMAX as |
| 199 |
|
|
appropriate. |
| 200 |
|
|
|
| 201 |
|
|
A subpattern with a bounded maximum repetition is replicated in a nested |
| 202 |
|
|
fashion up to the maximum number of times, with BRAZERO or BRAMINZERO before |
| 203 |
|
|
each replication after the minimum, so that, for example, (abc){2,5} is |
| 204 |
nigel |
53 |
compiled as (abc)(abc)((abc)((abc)(abc)?)?)?. The 99 and 200 bracket limits do |
| 205 |
|
|
not apply to these internally generated brackets. |
| 206 |
nigel |
41 |
|
| 207 |
|
|
|
| 208 |
|
|
Assertions |
| 209 |
|
|
---------- |
| 210 |
|
|
|
| 211 |
|
|
Forward assertions are just like other subpatterns, but starting with one of |
| 212 |
|
|
the opcodes OP_ASSERT or OP_ASSERT_NOT. Backward assertions use the opcodes |
| 213 |
|
|
OP_ASSERTBACK and OP_ASSERTBACK_NOT, and the first opcode inside the assertion |
| 214 |
|
|
is OP_REVERSE, followed by a two byte count of the number of characters to move |
| 215 |
nigel |
49 |
back the pointer in the subject string. When operating in UTF-8 mode, the count |
| 216 |
|
|
is a character count rather than a byte count. A separate count is present in |
| 217 |
|
|
each alternative of a lookbehind assertion, allowing them to have different |
| 218 |
|
|
fixed lengths. |
| 219 |
nigel |
41 |
|
| 220 |
|
|
|
| 221 |
|
|
Once-only subpatterns |
| 222 |
|
|
--------------------- |
| 223 |
|
|
|
| 224 |
|
|
These are also just like other subpatterns, but they start with the opcode |
| 225 |
|
|
OP_ONCE. |
| 226 |
|
|
|
| 227 |
|
|
|
| 228 |
|
|
Conditional subpatterns |
| 229 |
|
|
----------------------- |
| 230 |
|
|
|
| 231 |
|
|
These are like other subpatterns, but they start with the opcode OP_COND. If |
| 232 |
|
|
the condition is a back reference, this is stored at the start of the |
| 233 |
nigel |
53 |
subpattern using the opcode OP_CREF followed by two bytes containing the |
| 234 |
nigel |
41 |
reference number. Otherwise, a conditional subpattern will always start with |
| 235 |
|
|
one of the assertions. |
| 236 |
|
|
|
| 237 |
|
|
|
| 238 |
|
|
Changing options |
| 239 |
|
|
---------------- |
| 240 |
|
|
|
| 241 |
|
|
If any of the /i, /m, or /s options are changed within a parenthesized group, |
| 242 |
|
|
an OP_OPT opcode is compiled, followed by one byte containing the new settings |
| 243 |
|
|
of these flags. If there are several alternatives in a group, there is an |
| 244 |
|
|
occurrence of OP_OPT at the start of all those following the first options |
| 245 |
|
|
change, to set appropriate options for the start of the alternative. |
| 246 |
|
|
Immediately after the end of the group there is another such item to reset the |
| 247 |
|
|
flags to their previous values. Other changes of flag within the pattern can be |
| 248 |
|
|
handled entirely at compile time, and so do not cause anything to be put into |
| 249 |
|
|
the compiled data. |
| 250 |
|
|
|
| 251 |
|
|
|
| 252 |
|
|
Philip Hazel |
| 253 |
nigel |
53 |
August 2001 |