[antlr-interest] Rewriting to Flat Tree Nodes for Associative Multiary Operators

Randall R Schulz rschulz at sonic.net
Thu Oct 2 15:01:28 PDT 2008


On Thursday 02 October 2008 14:29, Jim Idle wrote:
> On Thu, 2008-10-02 at 14:23 -0700, Randall R Schulz wrote:
> > Hi,
> >
> > Next question.
> >
> > (I've been spoiled by working mostly with strictly prefix languages
> > in the past...)
> >
> >
> > How might I alter a rule such as this one:
> >
> > op790Formula
> >
> >     :   (op780Formula -> op780Formula)
> >
> >         (
> >             Or rDisjunct = op790Formula
> >         ->  ^(Or $op790Formula $rDisjunct)
> >         ) ?
> >     ;
>
> Have you tried:
>
>
> o : v1=nextrule
>           (
>               (Or v2+=nextrule )+ ->^(OR $v1 $v2+)
>
>             | // No OR's, default tree
>
>           )
>     ;
>
>
> Or something similar?

Well, it looks to me like it's very similar to what I did try, and this, 
too, yields the org.antlr.runtime.tree.RewriteEmptyStreamException.

Whether you include an empty alternative (as you showed) or make the 
entire sub-rule optional, as I'd tried earlier:

op790FormulaFlat
    :   (op780FormulaFlat -> op780FormulaFlat)
        ( Or rDisjuncts = op790FormulaFlat) + )
    ->  ^(Or $op790FormulaFlat $rDisjuncts+)
    ) ?
    ;

the result is the same: RewriteEmptyStreamException.


> Jim


Randall Schulz


More information about the antlr-interest mailing list