[antlr-interest] Java grammar accepting junk
ron.hunter-duvar at oracle.com
Fri Aug 15 14:04:31 PDT 2008
I'm doing some Java parsing with Antlr 3.1 and the Java.g grammar from
Antlr.org. When I pass it non-Java input (e.g. shell scripts), it
complains a lot, but still acts as if the parsing succeeded. I noticed
that the grammar didn't have an EOF token to force it to go to end of
file, so I added a new top level rule:
: compilationUnit EOF
and invoked it with that new target. Seemed simple enough. But it didn't
help. The parser still happily accepts garbage:
line 1:0 no viable alternative at character '#'
line 5:0 no viable alternative at character '#'
line 5:1 no viable alternative at character '#'
line 5:2 no viable alternative at character '#'
line 1:1 no viable alternative at input '!'
The first and last line of output are from my driver code. Basically I
was expecting the parser to throw an exception, which would have counted
as a failure. Since it didn't, it counts it as a success.
Maybe I'm not understanding how error reporting works in Antlr 3.1. I've
worked quite a bit with Antlr 2.7, but I'm new to Antlr 3. I don't have
the book, and haven't found anything in the wiki that explains this.
Perhaps someone can enlighten me?
Ron Hunter-Duvar | Software Developer V | 403-272-6580
Oracle Service Engineering
Gulf Canada Square 401 - 9th Avenue S.W., Calgary, AB, Canada T2P 3C5
All opinions expressed here are mine, and do not necessarily represent
those of my employer.
More information about the antlr-interest