问题描述
在 什么是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>> }?
抽象类Parser
的input
属性(生成的解析器从中扩展)现在在其前面带有下划线.
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中语义谓词的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!