字符串递归 Antlr 词法分析器标记
如何在词法分析器中构建一个可以处理内部递归的令牌,如下字符串:
${*anything*${*anything*}*anything*}
?
How do I build a token in lexer that can handle recursion inside as this string:
${*anything*${*anything*}*anything*}
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以在词法分析器规则中使用递归。
以下面的例子为例:
它将被以下语法正确解析:
如 ANTLRWorks 内部的解释器所示:
替代文本http://img185.imageshack.us/img185/5471/recq.png
Yes, you can use recursion inside lexer rules.
Take the following example:
which will be parsed correctly by the following grammar:
as the interpreter inside ANTLRWorks shows:
alt text http://img185.imageshack.us/img185/5471/recq.png
正如 @BartK 在他的文章中巧妙指出的那样,ANTLR 的词法分析器确实支持递归,但您只会在解析器中看到一个标记。如果您需要解释该令牌中的各个部分,您可能希望在解析器中处理它。
IMO,你最好在解析器中做一些事情:
通过做类似上面的事情,你将看到所有必要的部分,并且可以构建 AST 或以其他方式进行相应的处理。
ANTLR's lexers do support recursion, as @BartK adeptly points out in his post, but you will only see a single token within the parser. If you need to interpret the various pieces within that token, you'll probably want to handle it within the parser.
IMO, you'd be better off doing something in the parser:
By doing something like the above, you'll see all the necessary pieces and can build an AST or otherwise handle accordingly.