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

问题描述

您如何将用 ANTLR 3 编写的这部分代码翻译成 ANTLR 4?

How would you translate this portion of code written in ANTLR 3 into ANTLR 4?

expr: (Identifier '.')=> (refIdentifier)
  | (Identifier '!')=> (refIdentifier)
  | (Identifier '=>')=> (lambdaExpression);

我的意思是这种语义谓词现在似乎不存在.我可以用什么代替?

I mean this kind of semantic predicate does not seem to exist now. What could I use Instead?

推荐答案

在 ANTLR v4 中,不再有门控语义谓词{ ... }?=>,并且也不再有句法谓词( ... )=>,因为v4中使用的解析算法可以解决歧义(需要不再需要这样的谓词).所以,这应该适合你:

In ANTLR v4, there are no longer gated semantic predicates, { ... }?=>, and there are also no longer syntactic predicates, ( ... )=>, because the parsing algorithm used in v4 can resolve the ambiguities (the need for such predicates are no longer needed). So, this should just work for you:

expr
 : refIdentifier
 | refIdentifier
 | lambdaExpression
 ;

请注意,v4 中只有一种谓词:语义谓词{ ... }?.例如,如果您需要检查令牌的内容,您可以这样做:

Note that there is just one type of predicate in v4: semantic predicates, { ... }?. If you need to inspect the contents of a token, for example, you can do it like this:

id_capitals_only
 : {_input.LT(1).getText().matches("[A-Z]+")}? ID
 ;

ID
 : [a-zA-Z]+
 ;

编辑

正如 Sam Harwell 在评论中提到的:

EDIT

And as Sam Harwell mentions in the comments:

语义谓词{...}?在 V4 中的工作类似于 V3 中的门控语义谓词.V3 中的非门控谓词在 ANTLR 4 中没有对应项.

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

08-20 20:30