Overview
| Artifact ID: | 344daa0ec358ddaa7be11f19e9c416f82e6d3367 |
|---|---|
| Ticket: | 1115587fffffffffffffffffffffffffffffffff
Regexp backreference fail with a * closure |
| User & Date: | tgl on 2012-02-14 11:35:59 |
Changes
- icomment:
Some notes about fixing the more general issue ... For context, realize that Tcl's regex engine is a hybrid DFA/NFA implementation. Mostly it relies on the DFA logic, which has fast and stable performance because it never has to backtrack. But there are two significant regex features that DFA can't handle: capturing parentheses and back-references. To deal with those things, the regex compilation code builds a tree of "subRE"s, which will be searched at runtime in NFA style. The existing types of subREs are: * plain: matches any string described by a DFA. * backref: matches any string that is equal to a string identified by a previous capture (or actually, any string that is N repetitions of such a string, but I digress). * capture: matches any string that matches its child subRE, then saves the match for later output or backref-matching. * concatenation: matches any string that matches the concatenation of its two child subREs. * alternation: matches any string that matches either of its two child subREs. concatenation implies searching, since it has to consider each possible division of the presented string into two parts to see if they can match its two child subREs. Similarly, alternation requires searching, although it has only two choices to consider not N. To reduce unnecessary searching, Henry Spencer made the compilation code create a DFA for each subRE node, not only the "plain" nodes. The DFA describes what could possibly match the subRE in as much detail as DFA can manage (which is actually everything except the effects of back-refs). At runtime, the subRE tree searching code first executes the associated DFA, and gives up immediately if it finds no match. Otherwise it starts working through the possible match candidates. Now, if you're a programmer, it probably struck you as odd that the subRE types listed above don't include any iteration capability. How did Henry expect to deal with quantified regexes? The answer appears in the "it's quantifier time" portion of parseqatom(). He transforms the general case x{m,n} as follows: * first, turn x{0,...} into x{1,...}|empty * next, knowing we have m>0 and n>0, turn x{m,n} into x{m-1,n-1}x, with capturing parens in only the second x The reference to capturing parens is telling --- he wasn't thinking about back-refs when he wrote this. What the code is actually doing is pushing all the "messy" stuff (that is, subREs possibly involving capture or backref) into the second child of the concatenation subRE it creates here. The first child subRE, for "x{m-1,n-1}", is always implemented as a plain DFA node. Now, that's fine for messiness that consists of capturing parens; capturing parens don't affect match behavior, so there's no need to consider them in the first child subRE. (What this implementation effectively says is that if you write capturing parens inside a quantifier, say "(x)+", what will be captured is the text that matched the last iteration of the quantifier. No objection from me, though I do not see this behavioral detail documented in the re_syntax man page ... and come to think of it, I wonder whether the POSIX regex spec has anything to say about the subject.) BUT: that logic falls down completely when you consider back-refs. A back-ref absolutely does affect match behavior, but what the first child subRE has to work with is only a copy of the DFA for the back-ref's referent expression. That is, if you write something like (\w+)(\s+\1)+ (intended to match 2 or more repetitions of the same word), the actual behavior you get is equivalent to (\w+)(\s+\w+)*(\s+\1) ie, the last word is checked to be equal to the first, but any words in the middle are only checked to be words, because the subRE for "x{m-1,n-1}" is only a DFA for "(\s+\w+)*". Note that this only comes into play when the back-ref is a part of a larger quantified expression --- a quantifier attached directly to a back-ref uses a different code path, which has a problem with "\n*" as I documented earlier, but not this more general bug. If you work through the examples mentioned so far, I think they are all explained by one issue or the other. I'm inclined to think that the way to attack this is to invent an "iteration" subRE type, drop the two compilation-time transforms mentioned above in favor of just plopping an iteration subRE atop any messy expression that has to be quantified, and do the search work straightforwardly at runtime. However, this looks like a sizable amount of work that will result in a very nontrivial patch. So before starting, I'm soliciting some feedback about whether this seems like a sane solution that would get accepted. - login: "tgl"
- mimetype: "text/plain"
- username: "tgl"