Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

antlr4

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

like image 222
hqt Avatar asked Dec 07 '25 13:12

hqt


1 Answers

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.

like image 182
javic Avatar answered Dec 10 '25 20:12

javic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!