如何在JFlex中使用正则表达式捕获组? [英] How do I use regular expression capturing groups with JFlex?

查看:110
本文介绍了如何在JFlex中使用正则表达式捕获组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管此问题与JFlex有关,但它可能也适用于其他扫描仪生成器,例如lex,flex。

Although this question is about JFlex, it probably applies to other scanner generators such as lex, flex as well.

如果我有规则,该如何创建捕获规则中一部分的捕获组,并使用捕获组的结果作为规则匹配时调用的代码的参数?

If I have some rule, how can I create a capturing group in part of that rule and use the result of that captured group as an argument to the code that gets called upon the rule matching?

例如,我有一个简单的规则来匹配SGML标签:

For example, let's say I had a simple rule to match an SGML tag:

"<"[a-zA-Z]+">"    {return new Token(Type.OPEN_TAG);}

如何捕获内部字符部分([

How could I capture the inner character part([a-zA-Z]+) and use it as an argument in my Token constructor?

编辑:我知道我可以简单地使用yytext()来获取整个匹配的值,然后将代码中的各个部分分开,但这似乎会使事情变得比所需的复杂。

I'm aware I could simply use yytext() to get the whole matched value and then separate the parts elsewhere in code, but that seems like it would be making things more complicated than they need to be.

推荐答案

扫描仪生成器通常不支持捕获组,老实说,我从未在扫描仪生成器中看到过对它们的有效需求。通常,我们通常会用其他RegEx引擎中的捕获组做的大多数事情都可以在解析器中或通过操作中的简单代码更好地处理。

Scanner generators generally don't support capturing groups, and to be honest, I have never seen a valid need for them in a scanner generator. Most things you would normally us the capturing groups for in other RegEx engines are better handled in the parser or by a simple piece of code in the action.

类似于以下内容

"<"[a-zA-Z]+">"    {
                     String matchedText = yytext();
                     String label = matchedText.substring(1, matchedText.length() - 1);
                     return new Token(Type.OPEN_TAG, label);
                   }






实施小组捕获往往会干扰了扫描仪生成器执行的许多优化,以减小转换表的大小。我从未使用过JFlex,但我似乎还记得关于Flex支持某种有限形式的回溯并向前/向后看的东西,但是如果使用,则会发出有关性能的警告。


Implementing group capturing tends to interfere with many of the optimisations performed by the scanner generator to reduce the size of the transition table. I have never used JFlex but I seem to remember something about flex supporting some limited form of backtracking and look ahead/behind, but would then issue warnings about performance if used.

这篇关于如何在JFlex中使用正则表达式捕获组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆