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 :)
In your listener (
exitVarDeclaration
) or visitor (visitVarDeclaration
) check whetherctx.expression() == null
. If null, then('=' expression)
didn't exist. If non-null, then it did exist.