[antlr-interest] AST loosing leaves in 3.3 vs. 3.2

Jim Idle jimi at temporal-wave.com
Tue Jan 4 15:54:30 PST 2011


Your grammar is erroneous and just 'happened' to work in 3.2. The root of
a node can only be the result of a sib rule if the subrule contains a
single node, otherwise the results are undefined.

You would need:

callTarget
  : call
     -> ^(FUNCTION call PACK)
  ;

call
   : ID ID ID
   ;

Which is easily constructed in all situations, even if you add an extra
root node:


callTarget
  : call
     -> ^(CALL call PACK)
  ;

call
  : one=ID two=ID three=ID
    -> ^(FUNCTION $one $two $three)
  ;


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Joseph Cottam
> Sent: Tuesday, January 04, 2011 3:48 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] AST loosing leaves in 3.3 vs. 3.2
>
> Upgrading from v3.2 to v3.3, I started loosing the leaves in some of my
> rules.  All affected rule-pairs have the same structure: The results of
> called rule becomes the root of the AST returned by the rule doing the
> calling AND the calling rule adds more nodes to the AST (not just
> returning the results of the called rule).  In the example below, the
> result of matching the "call" rule is truncated to just its root when
> it is referred to in the construction of the AST for "callTarget."  I
> feel like I am missing something simple, but I can't figure out what
> part of the v3.3 release notes covers this circumstance.
>
> 3.2 output: (FUNCTION first second third PACK)
> 3.3 output: (FUNCTION PACK)
>
> -Joseph A. Cottam
>
>
> input------------------------
> first second third
>
> grammar-------------------
> grammar Error;
>
> options {
>   language = Java;
>   output=AST;
> }
>
> tokens {FUNCTION; PACK;}
>
> callTarget
>  : call
>     -> ^(call PACK)
>  ;
>
> call
>   : one=ID two=ID three=ID
>     -> ^(FUNCTION $one $two $three)
>   ;
>
> ID    : ('a'..'z' | 'A'..'Z' | '_') ('.'? ('a'..'z' | 'A'..'Z' | '_' |
> '0'..'9'))*;
> WS  : (' '|'\r'|'\t'|'\u000C'|'\n')+ {skip();};
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list