Antlr4: how to find out which alternative is chosen according to the context

Suppose there is a type rule. This is either a predefined type (specified by IDENTIFIER) or a Descriptor type.

type
:   IDENTIFIER
|   typeDescriptor
;

In my program, I have an instance of typeContext 'ctx'. How to find out if the IDENTIFIER path is selected or the Descriptor type is selected.

I recognize one way to test ctx.IDENTIFIER() == nulland ctx.typeDescriptor() == null. But it seems that this does not work very well when there are many alternatives. Is there a way to return an index to indicate which rule is selected? Thank you

+4
source share
1 answer

, (, ), , #.

type
  : IDENTIFIER     # someType
  | typeDescriptor # someOtherType
  ;

ParserRuleContext . SomeTypeContext, SomeOtherTypeContext, .

+6

All Articles