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 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 |
minimizing in Perl) the amount of the subject that matches individual wild
|
23 |
portions of the pattern. This is a "NFA algorithm".
|
24 |
|
25 |
For this set of functions, I tried at first to invent an algorithm that used an
|
26 |
amount of store bounded by a multiple of the number of characters in the
|
27 |
pattern, to save on compiling time. However, because of the greater complexity
|
28 |
in Perl regular expressions, I couldn't do this. In any case, a first pass
|
29 |
through the pattern is needed, in order to find internal flag settings like
|
30 |
(?i). So it works by running a very degenerate first pass to calculate a
|
31 |
maximum store size, and then a second pass to do the real compile - which may
|
32 |
use a bit less than the predicted amount of store. The idea is that this is
|
33 |
going to turn out faster because the first pass is degenerate and the second
|
34 |
can just store stuff straight into the vector. It does make the compiling
|
35 |
functions bigger, of course, but they have got quite big anyway to handle all
|
36 |
the Perl stuff.
|
37 |
|
38 |
The compiled form of a pattern is a vector of bytes, containing items of
|
39 |
variable length. The first byte in an item is an opcode, and the length of the
|
40 |
item is either implicit in the opcode or contained in the data bytes which
|
41 |
follow it. A list of all the opcodes follows:
|
42 |
|
43 |
Opcodes with no following data
|
44 |
------------------------------
|
45 |
|
46 |
These items are all just one byte long
|
47 |
|
48 |
OP_END end of pattern
|
49 |
OP_ANY match any character
|
50 |
OP_SOD match start of data: \A
|
51 |
OP_CIRC ^ (start of data, or after \n in multiline)
|
52 |
OP_NOT_WORD_BOUNDARY \W
|
53 |
OP_WORD_BOUNDARY \w
|
54 |
OP_NOT_DIGIT \D
|
55 |
OP_DIGIT \d
|
56 |
OP_NOT_WHITESPACE \S
|
57 |
OP_WHITESPACE \s
|
58 |
OP_NOT_WORDCHAR \W
|
59 |
OP_WORDCHAR \w
|
60 |
OP_CUT analogue of Prolog's "cut"
|
61 |
OP_EOD match end of data: \Z
|
62 |
OP_DOLL $ (end of data, or before \n in multiline)
|
63 |
|
64 |
|
65 |
Repeating single characters
|
66 |
---------------------------
|
67 |
|
68 |
The common repeats (*, +, ?) when applied to a single character appear as
|
69 |
two-byte items using the following opcodes:
|
70 |
|
71 |
OP_STAR
|
72 |
OP_MINSTAR
|
73 |
OP_PLUS
|
74 |
OP_MINPLUS
|
75 |
OP_QUERY
|
76 |
OP_MINQUERY
|
77 |
|
78 |
Those with "MIN" in their name are the minimizing versions. Each is followed by
|
79 |
the character that is to be repeated. Other repeats make use of
|
80 |
|
81 |
OP_UPTO
|
82 |
OP_MINUPTO
|
83 |
OP_EXACT
|
84 |
|
85 |
which are followed by a two-byte count (most significant first) and the
|
86 |
repeated character. OP_UPTO matches from 0 to the given number. A repeat with a
|
87 |
non-zero minimum and a fixed maximum is coded as an OP_EXACT followed by an
|
88 |
OP_UPTO (or OP_MINUPTO).
|
89 |
|
90 |
|
91 |
Repeating character types
|
92 |
-------------------------
|
93 |
|
94 |
Repeats of things like \d are done exactly as for single characters, except
|
95 |
that instead of a character, the opcode for the type is stored in the data
|
96 |
byte. The opcodes are:
|
97 |
|
98 |
OP_TYPESTAR
|
99 |
OP_TYPEMINSTAR
|
100 |
OP_TYPEPLUS
|
101 |
OP_TYPEMINPLUS
|
102 |
OP_TYPEQUERY
|
103 |
OP_TYPEMINQUERY
|
104 |
OP_TYPEUPTO
|
105 |
OP_TYPEMINUPTO
|
106 |
OP_TYPEEXACT
|
107 |
|
108 |
|
109 |
Matching a character string
|
110 |
---------------------------
|
111 |
|
112 |
The OP_CHARS opcode is followed by a one-byte count and then that number of
|
113 |
characters. If there are more than 255 characters in sequence, successive
|
114 |
instances of OP_CHARS are used.
|
115 |
|
116 |
|
117 |
Character classes
|
118 |
-----------------
|
119 |
|
120 |
OP_CLASS is used for a character class. It is followed by a 32-byte bit map
|
121 |
containing a 1 bit for every character that is acceptable. The bits are counted
|
122 |
from the least significant end of each byte.
|
123 |
|
124 |
|
125 |
Back references
|
126 |
---------------
|
127 |
|
128 |
OP_REF is followed by a single byte containing the reference number.
|
129 |
|
130 |
|
131 |
Repeating character classes and back references
|
132 |
-----------------------------------------------
|
133 |
|
134 |
In both cases, the repeat information follows the base item. The matching code
|
135 |
looks at the following opcode to see if it is one of
|
136 |
|
137 |
OP_CRSTAR
|
138 |
OP_CRMINSTAR
|
139 |
OP_CRPLUS
|
140 |
OP_CRMINPLUS
|
141 |
OP_CRQUERY
|
142 |
OP_CRMINQUERY
|
143 |
OP_CRRANGE
|
144 |
OP_CRMINRANGE
|
145 |
|
146 |
All but the last two are just single-byte items. The others are followed by
|
147 |
four bytes of data, comprising the minimum and maximum repeat counts.
|
148 |
|
149 |
|
150 |
Brackets and alternation
|
151 |
------------------------
|
152 |
|
153 |
A pair of non-identifying (round) brackets is wrapped round each expression at
|
154 |
compile time, so alternation always happens in the context of brackets.
|
155 |
Non-identifying brackets use the opcode OP_BRA, while identifying brackets use
|
156 |
OP_BRA+1, OP_BRA+2, etc. [Note for North Americans: "bracket" to some English
|
157 |
speakers, including myself, can be round, square, or curly. Hence this usage.]
|
158 |
|
159 |
A bracket opcode is followed by two bytes which give the offset to the next
|
160 |
alternative OP_ALT or, if there aren't any branches, to the matching KET
|
161 |
opcode. Each OP_ALT is followed by two bytes giving the offset to the next one,
|
162 |
or to the KET opcode.
|
163 |
|
164 |
OP_KET is used for subpatterns that do not repeat indefinitely, while
|
165 |
OP_KETRMIN and OP_KETRMAX are used for indefinite repetitions, minimally or
|
166 |
maximally respectively. All three are followed by two bytes giving (as a
|
167 |
positive number) the offset back to the matching BRA opcode.
|
168 |
|
169 |
If a subpattern is quantified such that it is permitted to match zero times, it
|
170 |
is preceded by one of OP_BRAZERO or OP_BRAMINZERO. These are single-byte
|
171 |
opcodes which tell the matcher that skipping this subpattern entirely is a
|
172 |
valid branch.
|
173 |
|
174 |
A subpattern with an indefinite maximum repetition is replicated in the
|
175 |
compiled data its minimum number of times (or once with a BRAZERO if the
|
176 |
minimum is zero), with the final copy terminating with a KETRMIN or KETRMAX as
|
177 |
appropriate.
|
178 |
|
179 |
A subpattern with a bounded maximum repetition is replicated up to the maximum
|
180 |
number of times, with BRAZERO or BRAMINZERO before each replication after the
|
181 |
minimum. In effect, (abc){2,5} becomes (abc)(abc)(abc)?(abc)?(abc)?.
|
182 |
|
183 |
|
184 |
Assertions
|
185 |
----------
|
186 |
|
187 |
Assertions are just like other subpatterns, but starting with one of the
|
188 |
opcodes OP_ASSERT or OP_ASSERT_NOT.
|
189 |
|
190 |
|
191 |
Once-only subpatterns
|
192 |
---------------------
|
193 |
|
194 |
These are also just like other subpatterns, but they start with the opcode
|
195 |
OP_ONCE.
|
196 |
|
197 |
|
198 |
Philip Hazel
|
199 |
October 1997
|