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
Post a Comment