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