[antlr-interest] ANTLR - Detecting if parsing was successful

Kunal Sawlani kunalsawlani at gmail.com
Mon Apr 26 13:33:19 PDT 2010


Hi Ron,
Thanks for the reply. It helped me get few of my doubts cleared as to what I
have to do. It turns out, that the errors that I am trying to catch are the
lexer errors, and not the parser errors. I will look into that, and once I
have the solution I would post back here. If anyone has already dealt with
this issue, any help would be greatly appreciated.
Thanks


On Fri, Apr 23, 2010 at 4:20 PM, Ron Hunter-Duvar <
ron.hunter-duvar at oracle.com> wrote:

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


-- 
Kunal Sawlani


More information about the antlr-interest mailing list