[antlr-interest] How to split int a,b;

Benjamin Niemann pink at odahoda.de
Tue Oct 2 13:38:24 PDT 2007


Hi,

Christian Andersson wrote:
> I have this type declaration below:
> 
> int a,b;
> 
> 
> My simple grammar looks like the following:
> 
> vardecl
> :type declarator_list ';' ->^(DECL type declarator_list)
> ;
> 
> declarator_list
> : ID (',' ID)* ->^(ID)+
> ;
> 
> My problem is that I want to split up int a,b; into two
> separate declarations in my AST without moving the declarator_list rule
> up to the vardecl rule.
> 
> 
> I want this tree:
> 
> DECL      DECL
> |   \     |   \
> int  'a'  int 'b'
> 
> instead of:
> 
> DECL
> |   \
> int  List
>      |  \
>     'a' 'b'

Try:

vardecl
 : type ID (',' ID)* ';' -> ^(DECL type ID)+
 ;

DECL and type will be replicated for each ID.


HTH




More information about the antlr-interest mailing list