简体   繁体   English

如何检查ANTLR4规则是否为空?

[英]How to check whether ANTLR4 rule is null?

I'm trying this (it's just an example): 我正在尝试(只是一个例子):

foo:
  bar?
  { if ($bar == null) { ... } }
  ;

ANTLR4 compiler says: ANTLR4编译器说:

Message{errorType=ISOLATED_RULE_REF, args=[bar, $bar] ...

What is the workaround? 解决方法是什么?

Ideally the action you have written above would be placed in the exitFoo method of a listener which you use after the parse is complete. 理想情况下,您上面编写的操作将放置在解析完成后使用的侦听器的exitFoo方法中。

However, if you do leave the action in the grammar, to reference the context object itself, use the ctx property. 但是,如果确实将操作留在语法中, ctx引用上下文对象本身,请使用ctx属性。 For example, $bar.ctx is the expression you are trying to use here, and $ctx evaluates to the context object for the enclosing rule. 例如, $bar.ctx是您要在此处使用的表达式,而$ctx求出上下文对象的封闭规则。

Have you seen the FAQ How do I test if an optional rule was matched? 您是否看到过常见问题解答我如何测试是否匹配了可选规则? ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM