[antlr-interest] Cannot understand why interpreter accepts bad input...
Gavin Lambert
antlr at mirality.co.nz
Fri Dec 7 13:02:21 PST 2007
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