[antlr-interest] backtrack=false and AST generation

Giampaolo Tomassoni Giampaolo at Tomassoni.biz
Sun Apr 18 07:30:06 PDT 2010


> You can pass trees from one rule to another as a parameter(s),
> maybe that might help, something like (probably requires more
> work to obtain the tree you are after):
> 
> protected
> expression
>     :	e=conditionalOrExpression conditionalExpression[$e.tree]
>     ;
> 
> protected
> conditionalExpression[CommonTree TheActualASTRoot]
>     :	QMARK t=expression COMMA f=expression
>             -> ^(ITE ${TheActualASTRoot} $t $f)
>     |
>     ;
> 
> Regards, Mark

Thank you Mark, for your clever reply.

May I ask if there is any way to do somthing like this:

protected
expression
    :	e=conditionalOrExpression c=conditionalExpression[$e] -> $c
    ;

protected
conditionalExpression[conditionalOrExpression e]
    :	QMARK t=expression COMMA f=expression	-> ^(ITE $e $t $f)
    |						-> $e
    ;

It seems cleaner to me, since this way subrules are not going to "play
dirty" with the root of their parents.

Unfortunately, this notation causes a "reference to undefined label in
rewrite rule: $e" error. The point may be "e" is not of
conditionalOrExpression type or, well, I don't really know why. Something
like that works with non-AST generating combined parsers (with more
classical actions, then). But it seems not to work with rewrite actions.

Thank you again,

Giampaolo

PS: I forgot to thank everybody for this really great open-source piece.



More information about the antlr-interest mailing list