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

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

问题描述

我有此代码:

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

因此,并非总是('=' expression)存在.但是,有时候,我想处理这部分,但是不知道在这种情况下该部分是否存在.我正在使用ANTLR 4(并且经常使用监听器),我怎么知道这一点.

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)不存在.如果非null,则表示确实存在.

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天全站免登陆