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

Christian Andersson dt05ca4 at student.lth.se
Wed Oct 3 02:20:37 PDT 2007


> ---------------------------- Original Message ----------------------------
> Subject: How to split  int a,b;
> From:    "Christian Andersson" <dt05ca4 at student.lth.se>
> Date:    Mon, October 1, 2007 11:38
> To:      antlr-interest at antlr.org
> --------------------------------------------------------------------------
>
> Hi all,
>
> 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'
>
>
> Thanks for any help
> BR Christian
>
>


Thanks for your help.

I Guess the only way to solve this problem is to move the contents of the
declarator_list rule up to the vardecl rule.

Well this works fine when I use a single type like int a,b,c
however when I try many types for example:
unsigned long int a,b,c I get the following tree,


DECL
|          \     \     \
unsigned   long   int  'a'

DECL
|          \     \     \     \
unsigned   long   int  'a'   'b'

DECL
|          \     \     \     \
unsigned   long   int  'a'   'c'

This is obvioulsy not the tree I want!
my grammar now look like the following:

vardecl
 :types declarator (',' declarator)* ';' ->^(DECL types declarator)+
 ;

the types rule has no AST rewrite instruction.

BR Christian



More information about the antlr-interest mailing list