[antlr-interest] mild simplification and tree grammars
    Giampaolo Tomassoni 
    Giampaolo at Tomassoni.biz
       
    Wed Apr 21 03:51:33 PDT 2010
    
    
  
> 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)
>             ->
>     |
>     ;
Nice.
I didn't get why you were telling me that, but now I see: this way it is
easier to avoid non-LLR notations.
Just, I can't turn on my shc (SomethingHasChanged) flag anymore: a rule
like:
condExpr
	:    QMARK c=orExpression t=condExpr f=condExpr
		-> {($c.tree.getType()==TRUE)}?  {shc=true;} $t
		-> {($c.tree.getType()==FALSE)}? {shc=true;} $f
		->
	;
Produces a java source with errors, because {shc=true;} is now interpreted
like a tree node reference, not like a java statement to be passed to the
compiler verbatim.
I had to use a function's side-effect to keep track of tree modifications:
@members {
    private boolean shc = false;
    private boolean sshc(boolean cond) {
    	shc |= cond;
    	return(cond); 
    }
}
...
protected
condExpr
    :	QMARK c=orExpression t=condExpr f=condExpr
		-> {sshc($c.tree.getType()==TRUE)}?		$t
		-> {sshc($c.tree.getType()==FALSE)}?	$f
		-> QMARK $c $t $f
    |	additiveExpression
    ;
Also note the last '->' term is non-empty, because I'm not rewriting (the
ANTLR compiler says rewrite mode implies backtrack=true).
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
    
    
More information about the antlr-interest
mailing list