ANTLR:在语法中定义新通道
我知道可以在 ANTLR 语法中的默认和隐藏令牌通道之间切换,但假设我想要第三个通道。如何在语法中定义新的令牌通道?例如,假设我想要一个名为 ALTERNATIVE 的频道。
I know it is possible to switch between the default and hidden token channels in an ANTLR grammar, but lets say I want a third channel. How can I define a new token channel in the gramar? For instance, lets say I want a channel named ALTERNATIVE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们只是
中的
类final int
令牌,因此您可以简单地在词法分析器中引入一个额外的
int
,如下所示:相关的问答:如何让 Antlr 解析器规则从默认和隐藏中读取频道
They're just
final int
's in theToken
class, so you could simply introduce an extra
int
in your lexer like this:A related Q&A: How do I get an Antlr Parser rule to read from both default AND hidden channel
对于 C 目标,您可以使用
This 会在每个规则之后重置,因此如果您希望通道分配在规则之间持续存在,您可能必须覆盖 nextTokenStr()。
For the C target you can use
This gets reset after every rule so if you want a channel assignment to persist across rules you may have to override nextTokenStr().