ANTLR

Listeners

Listener Events Using Labels

Labeling the alternatives inside a rule starting with the # operator tells ANTLR to generate listener methods for each label corresponding to the alternative.

By specifying a label for each alternative in the following rule:

// Rule
type : int     #typeInt
     | short   #typeShort
     | long    #typeLong
     | string  #typeString
     ;

// Tokens
int : 'int' ;
short : 'short' ;
long : 'long' ;
string : 'string' ;

Will generate the following methods in the generated interface that extends ParseTreeListener:

public void enterTypeInt(TypeShortContext ctx);
public void enterTypeShort(TypeIntContext ctx);
public void enterTypeLong(TypeLongContext ctx);
public void enterTypeString(TypeStringContext ctx);

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow