antlr - ANTLR4 - Access token group in a sequence using context -


i have grammar includes rule:

expr:       unaryexpr '(' (stat | expr | constant) ')'                                        #labelunaryexpr     | binaryexpr '(' (stat | expr | constant) ',' (stat | expr | constant) ')'          #labelbinaryexpr     | multipleexpr '(' (stat | expr | constant) (',' (stat | expr | constant))+ ')'     #labelmultipleexpr     ;        

for expr, can access value of unaryexpr calling ctx.unarystat(). how can access (stat | expr | constant) similarly? there solution doesn't require modifying grammar adding rule group?

since you've labelled alternatives, can access (stat | expr | constant) in respective listener/visitor method:

@override public void enterlabelunaryexpr(@notnull exprparser.labelunaryexprcontext ctx) {    // 1 of these return other null   system.out.println(ctx.stat());   system.out.println(ctx.expr());   system.out.println(ctx.constant()); } 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -