[antlr-interest] Problem catching exceptions in c# generated code

Ed Sykes ed.sykes at gmail.com
Thu Feb 21 08:50:18 PST 2008


You're right, i should have mentioned that the exception is being thrown
from the lexer, which is being called by the parser.

The try catch was around just the entry point on the parser, i've put the
try catch now around the whole setup / parse and still have the same
problem:

try
{
    MetricellKPIExpressionLexer lex = new MetricellKPIExpressionLexer(new
ANTLRStringStream(_expression));
    CommonTokenStream tokens = new CommonTokenStream(lex);
    MetricellKPIExpressionParser parser = new
MetricellKPIExpressionParser(tokens);

** MetricellKPIExpressionParser.expr_return result = parser.expr();
    CommonTree parsedTree = (CommonTree)(result.Tree);
    isValid = hasValidMeasures(parsedTree);
}
catch (MismatchedTokenException mmt)
{
    isValid = false;
}
catch (Exception ex)
{
    isValid = false;
}

In either case when the exception is thrown it should find the catch block
in the execution path. very puzzling. I've put the call stack below:

MetricellWebApplication.DLL!MetricellKPIExpressionLexer.mAND()
MetricellWebApplication.DLL!MetricellKPIExpressionLexer.mTokens()
[External Code]
MetricellWebApplication.DLL!MetricellKPIExpressionParser.expr()
MetricellWebApplication.DLL!MetriCell.MetricellKPIExpression.isValid()

At least i know that the exception can be caught from
MetricellKPIExpressionLexer.mAND() and that it can't be caught from
MetricellKPIExpression.isValid(). I can move the exception handling around
and see what the highest point in the call stack that works is. I suspect
that it's going to be
MetricellWebApplication.DLL!MetricellKPIExpressionLexer.mTokens(), but let's
see.

Not wanting to edit the generated code is exactly why i'm trying to fix
this!

thanks, ed

On Thu, Feb 21, 2008 at 3:16 PM, Johannes Luber <jaluber at gmx.de> wrote:

> Ed Sykes schrieb:
> >
> > I've just tried moving the try catch into the lexer around the call to
> > Antlr.Runtime.Match() and this seems to catch the exception. I'm
> > confused as to why putting the code in the driver doesn't work, but
> > moving the try catch to the lexer does.
> >
> > at least i have a workaround if i can't figure out how to get it working
> > in the driver.
>
> Now I believe I know what's your problem actually is. You are getting
> exceptions in the lexer, but you aren't catching them because the catch
> clause doesn't include the lexer. That would make sense as the lexing is
> done separately from the parsing. There only one problem with the
> theory: FillBuffer() is called by NextToken() is called by the parser.
> So the exception should be on the call chain. Strange. Nonetheless test
> it, because changing the generate code is always a bad idea, if there is
> an other way.
>
> And to your other question in the other email: ANTLR has a special
> syntax for exceptions in its grammar, but that doesn't work yet.
>
> Johannes
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080221/f85e67e7/attachment.html 


More information about the antlr-interest mailing list