By either using the inline operator !
that discards nodes from the tree:
expression
: NAME '='! Identifier
;
or by using a rewrite operator:
expression
: NAME '=' Identifier -> NAME Identifier
;
Related Q&A that explains these operators: How to output the AST built using ANTLR?
Note that there is no ASSIGNMENT
token in your grammar. If you want to inject such a token, put it in your tokens{ ... }
block and insert it in a rewrite rule:
grammar T;
options {
// ...
}
tokens {
ASSIGNMENT;
}
statement
: assignment ';' -> assignment
;
expression
: NAME '=' Identifier -> ^(ASSIGNMENT NAME Identifier)
;