[antlr-interest] RewriteEmptyStreamException upon mismatch

Bruce Pierson bpierson at theglobal.net
Fri Oct 5 13:09:45 PDT 2007


Alan:

Just hazarding a guess, but try { channel=HIDDEN; } rather than { skip(); }
on your WS declaration.

I think that's the new way to do it in 3.0...

--Bruce

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Alan Rooks
Sent: Friday, October 05, 2007 2:02 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] RewriteEmptyStreamException upon mismatch

I'm getting a RewriteEmptyStreamException from ANTLR (3.0.1) at runtime,
which appears to be due to a combination of using rewrite rules to build the
AST, and exception handling due to mismatched input.

I think this must be a bug; does anyone have any ideas for a workaround that
will still allow me to use rewrite rules and handle syntax errors?

Here's a very simple grammar that demonstrates the problem:

===============================
grammar Test;

options {
    output = AST;
}

WS: (' '|'\t'|'\r'|'\n')+ { skip(); } ;

X: 'x' ;
PLUS: '+' ;

c: X ;
b: c -> ^( X c ) ;
a: b ( PLUS^ b )? ;
===============================

This should accept "x" or "x + x".  If I run TestParser.a() and give it just
"x +", then I get:

   BR.recoverFromMismatchedToken
   line 0:-1 mismatched input '<EOF>' expecting X
   Exception in thread "main" \
           org.antlr.runtime.tree.RewriteEmptyStreamException: rule c
        at org.antlr.runtime.tree.RewriteRuleElementStream._next( \
                                    RewriteRuleElementStream.java:158)
        at org.antlr.runtime.tree.RewriteRuleElementStream.next( \
                                    RewriteRuleElementStream.java:145)
        at TestParser.b(TestParser.java:128)
        at TestParser.a(TestParser.java:204)
        at RunTest.main(RunTest.java:10)

If I change the rewrite rule to other forms, such as "b: c -> ^( c ) ;" or
"b: c -> c ;", I still get the same error.  However, if I remove the rewrite
rule and just leave "b: c ;" - which I think is supposed to actually have
the same effect as the simpler rewrite rules above - then I don't get the
exception... ANTLR just prints the first two lines above, about the
mismatched input.

Can anyone confirm that getting the RewriteEmptyStreamException indicates a
bug?  Or tell me what I'm doing wrong?  I really need the generality of the
rewrite rules in my actual grammar, so ideas for workarounds would be
greatly appreciated.

Thanks in advance,

Alan

-------------------------
Alan Rooks
AMI Semiconductor Canada
http://www.amis.com/
-------------------------
AMI Semiconductor - "Silicon Solutions for the Real World"
NOTICE: 
This electronic message contains information that may be confidential or
privileged. The information is intended for the use of the individual or
entity named above. If you are not the intended recipient, please be aware
that any disclosure, copying, distribution or use of the contents of this
information is prohibited. If you received this electronic message in error,
please notify the sender and delete the copy you received.




More information about the antlr-interest mailing list