[antlr-interest] C++ exceptions escape from libantlr3c

Christopher L Conway cconway at cs.nyu.edu
Thu Apr 8 10:08:16 PDT 2010


We have had a problem with our application crashing when a parser
exception is thrown. We override the default error handler as follows:

   /* init code... */
   pFooParser fooParser = NewFooParser(tokenStream);
   fooParser->pParser->rec->reportError = &reportError;

where reportError is defined inside my project:

   void reportError(pANTLR3_BASE_RECOGNIZER recognizer) {
       /* Collect information... */
       throw new ParserException(msg,filename,line,column);
   }

The main method is set up to catch ParserException, but on some (not
all) systems we get a crash with the message:

   terminate called after throwing an instance of 'ParserException'

The culprit seems to be libantlr3c routines
(e.g., recoverFromMismatchedToken) on the stack when the exception is
thrown. Since the library doesn't support exception handling, the
exception escapes and causes a crash.

A fairly simple fix for this problem is to add -fexceptions to the
command line when you compile the library. Unfortunately, the
libantlr3c configure script doesn't directly support user-supplied
CFLAGS. I'm attaching a patch that adds a --enable-cplusplus argument
to the configure script. (You need to rerun autoconf after applying
the patch for the changes to take effect.)

Regards,
Chris


More information about the antlr-interest mailing list