[antlr-interest] mild simplification and tree grammars

Giampaolo Tomassoni Giampaolo at Tomassoni.biz
Wed Apr 21 04:27:02 PDT 2010


> > Do you think is it fine this way or there is some workaround I can
> implement
> > to avoid the need of a side-effect function?
> >
> > Giampaolo
> 
> Predicates should not have observable* side effects.
> 
> The solution seems simple: the actions should be called before the
> tree rewrites, like:
> 
> condExpr
> 	:    QMARK c=orExpression t=condExpr f=condExpr
>                 {shc=true;}
> 		-> {($c.tree.getType()==TRUE)}?   $t
> 		-> {($c.tree.getType()==FALSE)}?  $f
> 		->
> 	;
> 
> Regards, Mark

Well, I attempted that, but then the shc var is set true even when there are
no rewrites (the empty rewrite case in your example). This is probably
because the disambiguating predicates are on the right side of the rewrite
operator, while the {shc=true;} seems an action to me. So, it is of course
to its left.

I don't see a way to put an action to the right side of the rewrite
operator, since a {...} notation would be interpreted like a tree reference,
if I understand it right.

Maybe all this means there is space for an enhancement in ANTLR? ;)

Giampaolo


> PS * since disambiguating semantic predicates may be called tens or
> hundreds of times at the same input file position in complex
> predicated parsers by antlr generated code, for performance
> reasons it may be necessary for complex semantic predicates to
> calculate the
> result once at each input file position, and cache the result, to
> avoid re-computing it over and over again.



More information about the antlr-interest mailing list