Flex(词法分析器)中是否可以进行反向引用?
我习惯于在可以使用括号捕获引用的语言中使用正则表达式。我在 flex 中看到的唯一接近的就是 yytext 变量。但它的内容是完整匹配的正则表达式,而不仅仅是其中的一部分。
在flex中不能使用反向引用吗?如果是这样,我该怎么办?我找不到有关该主题的任何内容...
我正在谈论这个: http://en.wikipedia.org/wiki/Flex_lexical_analysisr
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 about the subject...
I'm talking about this:
http://en.wikipedia.org/wiki/Flex_lexical_analyser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过更多搜索后,我不相信这是可能的......如果有人知道其他情况,请告诉我。
After searching even more I don't believe it's possible... If anyone knows otherwise, let me know please.
这对于 OP 来说可能已经太晚了,没有任何用处。但可能会对未来的读者有所帮助。
不,Flex-lexer 仍然不支持回溯,而且我认为它永远不会支持。这是因为,与许多其他流行的正则表达式引擎将正则表达式转换为 NFA(这些称为正则表达式定向正则表达式引擎)不同,Flex 在内部将它们转换为 DFA(这些称为文本定向正则表达式引擎)。这样做是因为 DFA 比 NFA 更快。这样做的结果是,DFA 不进行回溯。为了正确地将正则表达式与反向引用匹配,您需要回溯。
考虑这个简单的示例
(.*)\1
。这会匹配像catcat
这样的双字,否则会失败。为了在不回溯的情况下匹配这一点,DFA 至少应该知道捕获组的长度,但事实并非如此。复杂的模式会让事情变得更加复杂,并且无法用 DFA 来处理。我在某处读到这是 NP 完全问题。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 likecatcat
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.