[antlr-interest] how to collect multiple AST under single root

Bryan Ewbank ewbank at gmail.com
Tue Apr 12 05:35:42 PDT 2005


The problem is that "tmp" refers to the last rule2 recognized, not the
list of same.  The default generated AST is a chain, so you need to
add a new root above it:

   rule1: ( rule2 )* { #rule1 = #( [RULE, "root"], ## ); };

Here, "##" means that tree already constructed.  Note that you can
also write this in a more general way

   rule1: ( rule2 )* { ## = #( [RULE, "root"], ## ); };

Hope this helps,
- B

On Apr 11, 2005 6:09 PM, Anton Arhipov <tonyfox24 at hot.ee> wrote:
> hi! could anyone suggest please how could I collect multiple ASTs under
> a single root :
> 
> rule1 :
>       ( tmp:rule2 )*
>       {
>           #rule1 = #([RULE, "root"],tmp);
>        }
>     ;
> 
> rule2 :
>  ...............
>    ;
> 
> this creates only a root with a single child.
> 
> thx.
> A.
>


More information about the antlr-interest mailing list