Lexer antlr3 令牌问题

发布于 2024-08-26 23:46:51 字数 229 浏览 6 评论 0原文

令牌

ENDPLUS: '+' (options (greedy = false;):.) * '+'
       ;

我是否可以构建一个由词法分析器考虑的

PRE: '<<'
       ;

,前提是它前面有一个令牌 PRE 而不包含在 ENDPLUS 中?谢谢。

Can I construct a token

ENDPLUS: '+' (options (greedy = false;):.) * '+'
       ;

being considered by the lexer only if it is preceded by a token PREwithout including in ENDPLUS?

PRE: '<<'
       ;

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

自控 2024-09-02 23:46:51

不,据我所知,这是不可能“开箱即用”的。通过使用属性 input 并调用 LA(int) (向前看)。例如,以下词法分析器规则:

Token
  :  {input.LA(2) == 'b'}? . 
  ;

匹配任何单个字符,只要该单个字符后跟 b。不幸的是,没有 input.LA(-1) 功能可以在令牌流中查看后面的情况。 {...}? 部分称为“语法谓词”,以防您想知道或想用 Google 搜索它。

这里给出了讨论以及有关如何解决该问题的一些指示:http://www.antlr.org/pipermail/antlr-interest/2004-July/008673.html

请注意,它是 {greedy=false;},而不是 <代码>(贪婪=假;)。

No, AFAIK, this is not possible "out of the box". One only has look-ahead-control over the tokens stream in the lexer or parser by using the attribute input and calling LA(int) (look-ahead) on it. For example, the following lexer rule:

Token
  :  {input.LA(2) == 'b'}? . 
  ;

matches any single character as long as that single character is followed by a b. Unfortunately, there's no input.LA(-1) feature to look behind in the token stream. The {...}? part is called a "syntactic predicate" in case you're wondering, or wanting to Google it.

A discussion, and some pointers on how to go about solving it, are given here: http://www.antlr.org/pipermail/antlr-interest/2004-July/008673.html

Note that it's {greedy=false;}, not (greedy=false;).

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