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

Anton Arhipov tonyfox24 at hot.ee
Tue Apr 12 12:58:50 PDT 2005


Thx!

Actually this one worked for me:

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

Bryan Ewbank wrote:

>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