ANTLR:在语法中定义新通道

发布于 2024-11-30 17:47:42 字数 96 浏览 7 评论 0原文

我知道可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2024-12-07 17:47:43

它们只是 中的 final int令牌
,因此您可以简单地在词法分析器中引入一个额外的 int,如下所示:

grammar T;

@lexer::members {
  public static final int ALTERNATIVE = HIDDEN + 1;
}

// parser rules ...

FOO
  :  'foo' {$type=ALTERNATIVE;}
  ;

// other lexer rules ...

相关的问答:如何让 Antlr 解析器规则从默认和隐藏中读取频道

They're just final int's in the Token class
, so you could simply introduce an extra int in your lexer like this:

grammar T;

@lexer::members {
  public static final int ALTERNATIVE = HIDDEN + 1;
}

// parser rules ...

FOO
  :  'foo' {$type=ALTERNATIVE;}
  ;

// other lexer rules ...

A related Q&A: How do I get an Antlr Parser rule to read from both default AND hidden channel

慕巷 2024-12-07 17:47:43

对于 C 目标,您可以使用

//This must be assigned somewhere
@lexer::context {
  ANTLR3_UINT32 defaultChannel;
}

TOKEN : 'blah' {$channel=defaultChannel;};

This 会在每个规则之后重置,因此如果您希望通道分配在规则之间持续存在,您可能必须覆盖 nextTokenStr()。

For the C target you can use

//This must be assigned somewhere
@lexer::context {
  ANTLR3_UINT32 defaultChannel;
}

TOKEN : 'blah' {$channel=defaultChannel;};

This gets reset after every rule so if you want a channel assignment to persist across rules you may have to override nextTokenStr().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文