ANTLR 4 : How to know the existence of subpart in

2019-04-14 07:15发布

I have this code :

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

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.

Thanks :)

标签: antlr4
1条回答
神经病院院长
2楼-- · 2019-04-14 07:47

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.

查看更多
登录 后发表回答