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

Shawn Poulson spoulson at explodingcoder.com
Sun Aug 31 20:28:00 PDT 2008


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.

 

Has anyone experienced this before?  Thanks.

 

Shawn Poulson

spoulson at explodingcoder.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080831/9c789002/attachment.html 


More information about the antlr-interest mailing list