[antlr-interest] Rewriting Rule Problem
Gavin Lambert
antlr at mirality.co.nz
Mon Nov 3 12:11:14 PST 2008
At 07:40 4/11/2008, Ralf Hund wrote:
> exprOr : l=exprXor (o=opGroupOr r=exprXor -> ^(Expression $l
$o
>$r))*;
>
>However, this always results in a RewriteEmptyStreamException.
This means that one of the expressions you're trying to insert
into the AST is an empty tree, which suggests that there's
something optional in one of the subrules, or they're not
producing the tree you think they are.
>However there's no way to access $l in exprOrRight as it's in a
>different rule(at least i'm not aware of any way to do that). Is
>there any simpler way to solve the problem? What am I doing
wrong?
It's possible to pass $l into the subrule as a parameter and use
it that way, but I doubt that's what you really want to do. Your
original rule above will only work properly for zero or one
matches of the parenthetic block -- as soon as you hit two or
more, you'll lose some of the input. It's better to write it like
this:
exprOr
: (exprXor -> exprXor)
(o=opGroupOr r=exprXor -> ^(Expression $exprOr $o $r))*
;
If your operator was a single token instead of a subrule
invokation (and you wanted the operator to be the root of the
tree, which is fairly common), you could alternatively write it
like this:
exprOr
: exprXor (OR^ exprXor)*
;
Or:
exprOr
: exprXor ((OR1^ | OR2^) exprXor)*
;
More information about the antlr-interest
mailing list