| Refresh | Home EGTry.com

create node by target language runtime


antlr grammar with embedded java code to create node

grammar TreeConstruct;
options 
{
  output=AST;
}

tokens
{
PLUS;
MINUS;
}

prog 
: first=DIGIT  other+=DIGIT*  -> ^( 
  {
    "1".equals($first.text)?
      new CommonTree(new CommonToken(PLUS, "plus")) 
    : 
      new CommonTree(new CommonToken(MINUS, "minus"))
    
    } 

  $other*)
;

DIGIT: '0' .. '9' ;


WS: (' '|'\r'|'\n') {$channel=HIDDEN;};



input 1

1 2 3


tree output

(plus 2 3)



input 2

2 2 3


tree output

(minus 2 3)