[antlr-interest] mild simplification and tree grammars

Mark Wright markwright at internode.on.net
Tue Apr 20 06:46:30 PDT 2010


On Tue, Apr 20, 2010 at 03:21:00PM +0200, Giampaolo Tomassoni wrote:
> > Oops, I see placed the { after the $ earlier, where it should
> > be outside the $paramater, like {$parameter}, as in:
> > 
> > protected
> > conditionalExpression[CommonTree TheActualASTRoot]
> >     :   QMARK t=expression COMMA f=expression
> >             -> ^(ITE {$TheActualASTRoot} $t $f)
> >     |
> >     ;
> 
> Ah, that's it!
> 
> Right, it works. Thank you.
> 
> I'm a bit astonished with that, then: why a {$var} notation is needed here?
> What's the difference with $t and $f, in example?
> 
> Giampaolo

There are subtle differences in syntax depending on the context
of the variable, and in this case whether it is a parameter or not.

Another extension of the example shows another ANTLR feature,
with associated subtle syntax.  Say the conditionalExpression
was being called with TheActualASTRoot being optional, then
ANTLR supports this with semantic predicates on the tree generation
options:

protected
conditionalExpression[CommonTree TheActualASTRoot]
    :   QMARK t=expression COMMA f=expression
            -> {(TheActualASTRoot != null)}? ^(ITE {$TheActualASTRoot} $t $f)
            -> {(TheActualASTRoot == null)}? ^(ITE $t $f)
            ->
    |
    ;



More information about the antlr-interest mailing list