Antlr4中语义谓词的语法 [英] Syntax of semantic predicates in Antlr4

查看:236
本文介绍了Antlr4中语义谓词的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是ANTLR3中的语义谓词"?/em> Bart Kiers很好地概述了Antlr3中的不同语义谓词.

In What is a 'semantic predicate' in ANTLR3? Bart Kiers gives a very well overview about the different semantic predicates in Antlr3.

很遗憾,Antlr4中的语法/语义似乎已更改,因此无法编译:

Too bad the syntax/semantics were seemingly changed in Antlr4, so this does not compile:

end_of_statement
    :   ';'
    |   EOF
    |   {input.LT(1).getType() == RBRACE}? =>
    ;

RBRACE
    : '}'
    ;

有人可以告诉我如何处理 end_of_statement 的第三种情况:接受下一个令牌是否为'}',但不要使用它.

Could someone please tell me how to do the third case of end_of_statement: Accept if the next token is a '}' but do not consume it.

推荐答案

现在只有一种类型的语义谓词,如下所示:

There is now just a single type of semantic predicates, which looks like this:

{ <<boolean-epxression>> }?

抽象类Parserinput属性(生成的解析器从中扩展)现在在其前面带有下划线.

And the input attribute from the abstract class Parser (which your generated parser extends from) now has an underscore in front of it.

因此,在您的情况下,请使用以下ANTLR v3语法:

So, in your case, the following ANTLR v3 syntax:

{input.LT(1).getType() == RBRACE}? =>

在ANTLR v4中看起来像这样:

would look like this in ANTLR v4:

{_input.LT(1).getType() == RBRACE}?

这篇关于Antlr4中语义谓词的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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