ANTLR 4:如何知道规则中子部分的存在 [英] ANTLR 4 : How to know the existence of subpart in rule

查看:32
本文介绍了ANTLR 4:如何知道规则中子部分的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

varDeclaration
    :   type ID ('=' expression)? ';' 
    ;

所以,并不总是('='表达式) 存在.但是,有时,我想处理这部分,但不知道它在此上下文中是否存在.我正在使用 ANTLR 4(并且经常使用 Listener),我怎么知道这一点.

So, not always ('=' expression) exist. But, sometimes, I want to process this part, but don't know it exist or not in this context. I'm using ANTLR 4 (and often using Listener), how can I know this.

谢谢:)

推荐答案

在你的监听器 (exitVarDeclaration) 或访问者 (visitVarDeclaration) 中检查是否 ctx.expression() == null.如果为 null,则 ('=' expression) 不存在.如果非空,那么它确实存在.

In your listener (exitVarDeclaration) or visitor (visitVarDeclaration) check whether ctx.expression() == null. If null, then ('=' expression) didn't exist. If non-null, then it did exist.

这篇关于ANTLR 4:如何知道规则中子部分的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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