ANTLR 4 : How to know the existence of subpart in rule

陌路散爱 提交于 2019-12-06 09:35:45

问题


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 :)


回答1:


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.



来源:https://stackoverflow.com/questions/16392152/antlr-4-how-to-know-the-existence-of-subpart-in-rule

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!