[antlr-interest] Java grammar accepting junk

Terence Parr parrt at cs.usfca.edu
Fri Aug 15 14:23:38 PDT 2008


Well, take "succeeded" not very seriously...it just prints

                         System.out.println("finished parsing OK");

  if there was no exception... all of the recognition exceptions are  
caught inside the parser.  You could check the parser for the number  
of errors.

Ter
On Aug 15, 2008, at 2:04 PM, Ron Hunter-Duvar wrote:

> Hi,
>
> 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:
>
> sourceFile
> : compilationUnit EOF
> ;
>
> and invoked it with that new target. Seemed simple enough. But it  
> didn't help. The parser still happily accepts garbage:
>
> Parsing: test.sh
> 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 '!'
>   Succeeded
>
> 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?
>
> Thanks,
> Ron
>
> -- 
> 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 mailing list