[antlr-interest] Rewrite Rule Inconsistent Behavior (Tree Grammar Problem)

Aditya Damodaran adityad at gmail.com
Thu Feb 3 16:21:33 PST 2011


Hi,
I'm trying to rewrite a simple tree where ^(operator op1 op2) -> ^(FUNC
operator op1 op2)
I am having fairly inconsistent behavior when I attempt to do this.

tree grammar MExpr;


options {

  ....

  filter=true;

  rewrite=true;

}


bottomup: mathExpr

 term

: ID

| BOOLEAN | INT | FLOAT | STRING

|   ^(FUNC .);


mathExpr

: ^(m=MINUS x=term) -> ^(FUNC NEGATION $x)

|   ^(t=operator t1=term t2=term) -> ^(FUNC $t $t1 $t2);


operator: PLUS | MINUS | MULTOP;


When defined above I get the following StackTrace:

Exception in thread "main"
org.antlr.runtime.tree.RewriteEmptyStreamException: rule t

at org.antlr.runtime.tree.RewriteRuleElementStream._next(
RewriteRuleElementStream.java:157)

at org.antlr.runtime.tree.RewriteRuleElementStream.nextTree(
RewriteRuleElementStream.java:144)....


I have isolated the reason (I think) to be the code that the "operator" rule
produces...



*            **if** ( (**input**.LA(1)>=**PLUS** && **input**.LA(1)<=**
MULTOP**) ) {*

*                **input**.consume();*


As a result the operator_return has a start at the correct token but a tree
of null which causes this error.

If I switch the mathExpr rule to


|^(PLUS t1=term t2=term) -> ^(FUNC PLUS $t1 $t2);


then everything works fine. Obviously this means I have to specify the
rewrite rule for EVERY operator.

I'm sure I'm missing something basic but I'm unable to figure it out. Any
help is much appreciated.


Another interesting variation is that if I just have


operator: PLUS;


then the toStringTree of the return value goes into an infinite recursion
and blows up.


Thanks,

Adi


More information about the antlr-interest mailing list