[antlr-interest] Cannot understand why interpreter accepts bad input...

Bern.McCarty at bentley.com Bern.McCarty at bentley.com
Fri Dec 7 13:11:51 PST 2007


>>> Incidentally, when I tried your original grammar (without
modification) it did indeed generate an error on the console.  No tricks
required.

When you ran it in the debugger, yes, but not when merely using the
interpreter.  Whereas in my last message the first grammer provides an
indication of an exception when run in the interpreter, while the 2nd
grammar does not.  And I have no understanding of why this is.

I'm concluding that I should not prototype/modify/develop my grammar by
doing edit/interpret cycles.  It is just messing me up.  I guess I have
to use edit/debug instead.

-----Original Message-----
From: Gavin Lambert [mailto:antlr at mirality.co.nz] 
Sent: Friday, December 07, 2007 4:02 PM
To: Bern McCarty; antlr-interest at antlr.org
Subject: Re: [antlr-interest] Cannot understand why interpreter accepts
bad input...

At 03:38 8/12/2007, Bern.McCarty at bentley.com wrote:
>I'm really struggling with trying to use the AntlrWorks 
>interpreter and greatly appreciate any help I can get.  The first 
>grammer below does NOT require Gavin's trick of defining a 
>"IllegalChar: .;" rule in order to get a NoViableAltException 
>when feeding bad input like "a" into the interpreter.

'Twasn't my trick; it was G.R. who suggested it.

Incidentally, when I tried your original grammar (without 
modification) it did indeed generate an error on the console.  No 
tricks required.

Note that the error doesn't appear in the AST diagram; it appears 
in the Output window.  This is because it occurs at lexing 
time.  By adding a catch-all rule like the above you're moving the 
error to occur at parsing time instead (since your parser grammer 
doesn't accept IllegalChar anywhere), which is why the error 
appears differently.


Personally I've got mixed feelings about the auto-error-recovery 
in ANTLR.  In many cases I'd rather it just instantly aborted any 
time it didn't like what it saw, but I can see the merit in 
continuing on and doing the best it can (if nothing else, it lets 
you see several errors in a row).  In any case you can override 
the lexer's error-handling behaviour to do whatever you want 
(though note that this won't affect the interpreter).



More information about the antlr-interest mailing list