开发者

Are back references possible in flex (lexical analyser)?

I'm used to play with regexp in languages where I can use parenthesis to capture references. The only thing near that in flex that I'm seeing is the yytext variable. But it's contents are the full matched regexp and not just some part of it.

Isn't the use of back references in flex possible? If so, how can I do it? I can't find anything abou开发者_如何学编程t the subject...

I'm talking about this: http://en.wikipedia.org/wiki/Flex_lexical_analyser


After searching even more I don't believe it's possible... If anyone knows otherwise, let me know please.


This may be too late to be any use for OP. But may help future readers.

No, still Flex-lexer doesn't support backtracking and I don't think it will ever. This is because, unlike many other popular regex engines which turns regex into NFAs (these are known as regex-directed regex engines), Flex internally converts them into DFAs (these are known as text-directed regex engines). This is done because DFAs are faster than NFAs. Consequence of this is that, DFAs don't do backtracking. And to properly match a regex with backreferences, you need backtracking.

Consider this simple example (.*)\1. This matches double words like catcat and fails otherwise. To match this without backtracking, DFA should know the length of the captured group at least, which is not the case. Things get more complicated with complex patterns, and they are impossible to handle with DFAs. I somewhere read that this is NP complete problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜