[antlr-interest] how to concatenate string nodes into one node when create AST?

O.E. Dragon dragonoe at mcmaster.ca
Tue May 9 13:58:06 PDT 2006


On Tue, 9 May 2006 16:50:18 -0400 
 Charlie Yang <Cyang at infobal.com> wrote:
> if I do like below in the grammar, 
> .....
> argument ! :
>       	an:argument_name  at:argument_type
> 	{ #argument = #([FUNCTION_ARGU, "argu"], #an, #at  ); }
> .....
> 
> the output AST sub tree would be:
> 	("argu",        argument_name, argument_type ).   "argu" is root,
> and two others are children.
>       
> 
> How can I combine string type child nodes into single one child node
> like
> that ?:
> 	("argu",          argument_name + argument_type ) 
>          or    ("argu",        argument_name + SPACE + argument_type
> ) 


Something like this?


  { #argument = #([FUNCTION_ARGU, "argu"], [SOMETYPE, #an.getText() +
#at.getType()] ); }

If you're looking for the _name_ of the type instead of the integer,
you'll have to do a reverse lookup with tokenNames (one of the parser
object's fields. getTokenNames() gets it for you I think). Of course
this would be simpler if token types were in an enum (Java 1.5
codegen... maybe for v3).

-Olivier


More information about the antlr-interest mailing list