123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- Technical Notes about PCRE
- These are very rough technical notes that record potentially useful information
- about PCRE internals. For information about testing PCRE, see the pcretest
- documentation and the comment at the head of the RunTest file.
- Historical note 1
- Many years ago I implemented some regular expression functions to an algorithm
- suggested by Martin Richards. These were not Unix-like in form, and were quite
- restricted in what they could do by comparison with Perl. The interesting part
- about the algorithm was that the amount of space required to hold the compiled
- form of an expression was known in advance. The code to apply an expression did
- not operate by backtracking, as the original Henry Spencer code and current
- Perl code does, but instead checked all possibilities simultaneously by keeping
- a list of current states and checking all of them as it advanced through the
- subject string. In the terminology of Jeffrey Friedl's book, it was a "DFA
- algorithm", though it was not a traditional Finite State Machine (FSM). When
- the pattern was all used up, all remaining states were possible matches, and
- the one matching the longest subset of the subject string was chosen. This did
- not necessarily maximize the individual wild portions of the pattern, as is
- expected in Unix and Perl-style regular expressions.
- Historical note 2
- By contrast, the code originally written by Henry Spencer (which was
- subsequently heavily modified for Perl) compiles the expression twice: once in
- a dummy mode in order to find out how much store will be needed, and then for
- real. (The Perl version probably doesn't do this any more; I'm talking about
- the original library.) The execution function operates by backtracking and
- maximizing (or, optionally, minimizing in Perl) the amount of the subject that
- matches individual wild portions of the pattern. This is an "NFA algorithm" in
- Friedl's terminology.
- OK, here's the real stuff
- For the set of functions that form the "basic" PCRE library (which are
- unrelated to those mentioned above), I tried at first to invent an algorithm
- that used an amount of store bounded by a multiple of the number of characters
- in the pattern, to save on compiling time. However, because of the greater
- complexity in Perl regular expressions, I couldn't do this. In any case, a
- first pass through the pattern is helpful for other reasons.
- Support for 16-bit and 32-bit data strings
- From release 8.30, PCRE supports 16-bit as well as 8-bit data strings; and from
- release 8.32, PCRE supports 32-bit data strings. The library can be compiled
- in any combination of 8-bit, 16-bit or 32-bit modes, creating up to three
- different libraries. In the description that follows, the word "short" is used
- for a 16-bit data quantity, and the word "unit" is used for a quantity that is
- a byte in 8-bit mode, a short in 16-bit mode and a 32-bit word in 32-bit mode.
- However, so as not to over-complicate the text, the names of PCRE functions are
- given in 8-bit form only.
- Computing the memory requirement: how it was
- Up to and including release 6.7, PCRE worked by running a very degenerate first
- pass to calculate a maximum store size, and then a second pass to do the real
- compile - which might use a bit less than the predicted amount of memory. The
- idea was that this would turn out faster than the Henry Spencer code because
- the first pass is degenerate and the second pass can just store stuff straight
- into the vector, which it knows is big enough.
- Computing the memory requirement: how it is
- By the time I was working on a potential 6.8 release, the degenerate first pass
- had become very complicated and hard to maintain. Indeed one of the early
- things I did for 6.8 was to fix Yet Another Bug in the memory computation. Then
- I had a flash of inspiration as to how I could run the real compile function in
- a "fake" mode that enables it to compute how much memory it would need, while
- actually only ever using a few hundred bytes of working memory, and without too
- many tests of the mode that might slow it down. So I refactored the compiling
- functions to work this way. This got rid of about 600 lines of source. It
- should make future maintenance and development easier. As this was such a major
- change, I never released 6.8, instead upping the number to 7.0 (other quite
- major changes were also present in the 7.0 release).
- A side effect of this work was that the previous limit of 200 on the nesting
- depth of parentheses was removed. However, there is a downside: pcre_compile()
- runs more slowly than before (30% or more, depending on the pattern) because it
- is doing a full analysis of the pattern. My hope was that this would not be a
- big issue, and in the event, nobody has commented on it.
- At release 8.34, a limit on the nesting depth of parentheses was re-introduced
- (default 250, settable at build time) so as to put a limit on the amount of
- system stack used by pcre_compile(). This is a safety feature for environments
- with small stacks where the patterns are provided by users.
- Traditional matching function
- The "traditional", and original, matching function is called pcre_exec(), and
- it implements an NFA algorithm, similar to the original Henry Spencer algorithm
- and the way that Perl works. This is not surprising, since it is intended to be
- as compatible with Perl as possible. This is the function most users of PCRE
- will use most of the time. From release 8.20, if PCRE is compiled with
- just-in-time (JIT) support, and studying a compiled pattern with JIT is
- successful, the JIT code is run instead of the normal pcre_exec() code, but the
- result is the same.
- Supplementary matching function
- From PCRE 6.0, there is also a supplementary matching function called
- pcre_dfa_exec(). This implements a DFA matching algorithm that searches
- simultaneously for all possible matches that start at one point in the subject
- string. (Going back to my roots: see Historical Note 1 above.) This function
- intreprets the same compiled pattern data as pcre_exec(); however, not all the
- facilities are available, and those that are do not always work in quite the
- same way. See the user documentation for details.
- The algorithm that is used for pcre_dfa_exec() is not a traditional FSM,
- because it may have a number of states active at one time. More work would be
- needed at compile time to produce a traditional FSM where only one state is
- ever active at once. I believe some other regex matchers work this way. JIT
- support is not available for this kind of matching.
- Changeable options
- The /i, /m, or /s options (PCRE_CASELESS, PCRE_MULTILINE, PCRE_DOTALL, and some
- others) may change in the middle of patterns. From PCRE 8.13, their processing
- is handled entirely at compile time by generating different opcodes for the
- different settings. The runtime functions do not need to keep track of an
- options state any more.
- Format of compiled patterns
- The compiled form of a pattern is a vector of unsigned units (bytes in 8-bit
- mode, shorts in 16-bit mode, 32-bit words in 32-bit mode), containing items of
- variable length. The first unit in an item contains an opcode, and the length
- of the item is either implicit in the opcode or contained in the data that
- follows it.
- In many cases listed below, LINK_SIZE data values are specified for offsets
- within the compiled pattern. LINK_SIZE always specifies a number of bytes. The
- default value for LINK_SIZE is 2, but PCRE can be compiled to use 3-byte or
- 4-byte values for these offsets, although this impairs the performance. (3-byte
- LINK_SIZE values are available only in 8-bit mode.) Specifing a LINK_SIZE
- larger than 2 is necessary only when patterns whose compiled length is greater
- than 64K are going to be processed. In this description, we assume the "normal"
- compilation options. Data values that are counts (e.g. quantifiers) are two
- bytes long in 8-bit mode (most significant byte first), or one unit in 16-bit
- and 32-bit modes.
- Opcodes with no following data
- These items are all just one unit long
- OP_END end of pattern
- OP_ANY match any one character other than newline
- OP_ALLANY match any one character, including newline
- OP_ANYBYTE match any single unit, even in UTF-8/16 mode
- OP_SOD match start of data: \A
- OP_SOM, start of match (subject + offset): \G
- OP_SET_SOM, set start of match (\K)
- OP_CIRC ^ (start of data)
- OP_CIRCM ^ multiline mode (start of data or after newline)
- OP_NOT_WORD_BOUNDARY \W
- OP_WORD_BOUNDARY \w
- OP_NOT_DIGIT \D
- OP_DIGIT \d
- OP_NOT_HSPACE \H
- OP_HSPACE \h
- OP_NOT_WHITESPACE \S
- OP_WHITESPACE \s
- OP_NOT_VSPACE \V
- OP_VSPACE \v
- OP_NOT_WORDCHAR \W
- OP_WORDCHAR \w
- OP_EODN match end of data or newline at end: \Z
- OP_EOD match end of data: \z
- OP_DOLL $ (end of data, or before final newline)
- OP_DOLLM $ multiline mode (end of data or before newline)
- OP_EXTUNI match an extended Unicode grapheme cluster
- OP_ANYNL match any Unicode newline sequence
-
- OP_ASSERT_ACCEPT )
- OP_ACCEPT ) These are Perl 5.10's "backtracking control
- OP_COMMIT ) verbs". If OP_ACCEPT is inside capturing
- OP_FAIL ) parentheses, it may be preceded by one or more
- OP_PRUNE ) OP_CLOSE, each followed by a count that
- OP_SKIP ) indicates which parentheses must be closed.
- OP_THEN )
-
- OP_ASSERT_ACCEPT is used when
|