[antlr-interest] ANTLR - Detecting if parsing was successful
Ron Hunter-Duvar
ron.hunter-duvar at oracle.com
Fri Apr 23 13:20:54 PDT 2010
Hi Kunal,
There's a getNumberOfSyntaxErrors() method available on the parser
object. If there were any errors this will return a number greater than
zero (though I don't believe it includes lexer errors, you have to check
separately for them). You can test this when the parsing finishes.
To detect at the end of individual rules, you can test the state.failed
field. But just looking at the generated code, I don't think testing it
in the @after will work, because a match failure will result in an early
return, so the @after won't get executed. I don't know if there's a way
to tell Antlr to put your code in the finally block to guarantee it
always gets executed (and if so, you'd probably want to check
state.backtracking to see if this is a real failure or a failure during
backtracking). Otherwise, I think you'd need to check in an action in
the calling rule (but then that action won't be executed until the
calling rule succeeds). I'm not sure if there's a way to make this work.
Turning off recovery is a separate issue. I don't know if there's any
other way than overriding the recover method and possibly some related
methods in BaseRecognizer.
Ron
Kunal Sawlani wrote:
> Hi,
> I am a new to ANTLR and have been trying to detect if the parsing was
> successful or not. As of now, I was using the @after block, which gets
> executed
> only if the parsing was successful. But this block gets executed, even for
> incorrect inputs in the language. I think I am missing something, which I
> must do
> to deactivate the error recovery mechanism, to avoid the after block from
> being executed. Can anyone please guide me to some material on this issue.
>
> Any help would be greatly appreciated.
> Thanks
>
--
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