[antlr-interest] Java grammar accepting junk

Ron Hunter-Duvar ron.hunter-duvar at oracle.com
Fri Aug 15 15:13:50 PDT 2008


So that would be by creating a RecognizerSharedState object, passing it 
to the parser constructor, then checking state.syntaxErrors > 0 at the 
end, correct?

Is there a way to know whether the parser succeeded in recovering and 
parsing to the end of the file or gave up part way through?

Thanks,
Ron


Terence Parr wrote:
> 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.
>>
>
>

-- 
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