ANTLR4中的语义谓词? [英] Semantic predicates in ANTLR4?

查看:152
本文介绍了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中,不再有门控语义谓词 ,并且也不再有 syntactic谓词( ... )=>,因为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中没有对应的谓词.

The semantic predicates {...}? in V4 work like the gated semantic predicates did in V3. The ungated predicates from V3 do not have a counterpart in ANTLR 4.

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

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