| Refresh | Home EGTry.com

tree grammar to transform one tree to another


before node: has to specify a root

input example

 7 + x 


parser grammar

grammar P1;
options {
 output=AST;
 }


prog: INT '+' ID  -> ^('+' INT ID)
 ;


INT: ('0'..'9')+ ;
ID: ('a' .. 'z')+;
WS  : (' '|'\t'|'\r'|'\n')+ {skip();} ;



output tree

(+ 7 x)



tree grammar to transform tree

tree grammar T2;

options 
{
 tokenVocab=P1;
 ASTLabelType=CommonTree;
 output=AST;
 rewrite=true;
}

prog:
 ^('+' INT ID) -> ^(ID '+' INT)
;


new tree output

(x + 7)