[antlr-interest] C#: Unhandled NoViableAltexception from lexer

Johannes Luber jaluber at gmx.de
Mon Sep 1 15:25:56 PDT 2008


Shawn Poulson schrieb:
> Hello all,
> 
> I’m struggling with what appears to be an unexpected behavior in my C#
> code.  When I attempt to parse a string with my grammar, it works as
> long as the input is valid to my parser rules.  When the parser bawks at
> the input I get an exception derived from RecognitionException and my
> code can handle invalid input.  When the lexer bawks, I usually get a
> NoViableAltException thrown that I’m also able to handle.
> 
> Except, in a specific case my code is unable to handle a
> NoViableAltException within a try/catch block.  For example, my grammar
> does not utilize semicolons anywhere.  So, if I feed it an input with a
> semi anywhere in the input, the lexer will choke and throw a
> NoViableAltException, as expected.  For some reason, even if my code
> surrounds the ANTLR parser call with a try/catch(Exception) block, the
> program execution halts with an unhandled exception.
> 
> I experienced this with ANTLR 3.0.1, in which case the exception was
> thrown from the lexer’s mTokens() method.  I noticed ANTLR 3.1 was
> released recently, so I gave that a try and used the new CSharp2
> target.  I get the same exact symptom, with the exception being thrown
> from Antlr3.Runtime.DFA.Predict().  In both revisions, I’ve browsed the
> code using .NET Reflector and I don’t see any funny business going on. 
> I don’t see any reason why a try/catch block cannot trap this exception!
> 
> I’m using VS 2008.  I’ve attempted with .NET 2.0 and .NET 3.5 framework
> targets and got the same results.  If necessary, I can send a project
> that replicates this issue.

I'd like to have the files to test this behaviour myself.
> 
> Has anyone experienced this before?  Thanks.

Someone did experience it before, but I don't know if the issue was
solved. Can you try instead

catch (Exception)

only

catch

? That should get any possibly exception. Or it could be a bug in the
.NET runtime. testing on Mono may shed light on this iisue.

Johannes
> 
>  
> 
> Shawn Poulson
> 
> spoulson at explodingcoder.com
> 
> 
> ------------------------------------------------------------------------
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 



More information about the antlr-interest mailing list