> 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


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to