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

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

问题描述

ANTLR3 中的语义谓词"是什么? 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天全站免登陆