[antlr-interest] RewriteEmptyStreamException upon mismatch
Jim Idle
jimi at temporal-wave.com
Fri Oct 5 13:23:58 PDT 2007
The error message is correct of course, but if you still want to try and
produce a tree then what you need to do is detect that the subrule failed.
The easiest way to do this is to use:
c: X ;
b: myc=c
-> {$myc==null}? ^( ERROR ) // Or something sensible etc
-> ^( X c)
;
> -----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 1: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.
>
>
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.488 / Virus Database: 269.14.1/1050 - Release Date:
> 10/4/2007 5:03 PM
>
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.488 / Virus Database: 269.14.1/1050 - Release Date: 10/4/2007
5:03 PM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071005/263f3b59/attachment.html
More information about the antlr-interest
mailing list