[antlr-interest] AntLR C# target exceptions problem

Jan Newger Jan.Newger at rwth-aachen.de
Mon Apr 13 09:28:14 PDT 2009


Jan Newger wrote:
> Sam Harwell wrote:
>> It's a good idea to override ReportError, and this will almost surely
>> provide you with the results you want. You can also put a breakpoint in
>> NoViableAltException's constructor. 
>>
> 
> So you are proposing that I override ReportError and re-throw the
> exception? I basically want this code to work as expected (taken from
> http://www.antlr.org/wiki/display/ANTLR3/Five+minute+introduction+to+ANTLR+3):
> 
> try {
>             parser.expr();
>         } catch (RecognitionException e)  {
>             Console.Error.WriteLine(e.StackTrace);
>         }
> 
> Since the RecognitionException is swallowed within the parser itself and
> the default implementation of ReportError doesn't rethrow it by itself,
> this code doesn't work as expected, i.e. the catch block can never be
> executed. Correct?
> 

I poked a little bit through the source code of the C# runtime. IMHO the
code shown above, which was taken from the antlr website, is wrong,
because it implies that the RecognitionException might reach the catch
block.
As outlined previously, the parser will never throw this exception
because each rule swallows the exception and the error condition is
signlaed via the ReportError method. On the other hand, the lexer also
never seems to throw this kind of exception. It seems to me that the
lexer throws e.g. MismatchedTokenException, but it never throws
NoViableAltException and therefore no RecognitionException.
Please correct me if I'm wrong.

Thanks
Jan


More information about the antlr-interest mailing list