[antlr-interest] Re: Exiting on parser errors

Ric Klaren ric.klaren at gmail.com
Thu Jun 9 04:53:10 PDT 2005


On 6/7/05, Greg Bedwell <gregbedwell at gmail.com> wrote:
> >>In simple terms, I just want to call exit(1) as soon as the first
> >>syntax error is detected by ANTLR and the error message is displayed.
> >>
> >>
> >Try putting "defaultErrorHandler=false;" in the >options block of your
> >grammar file. This will prevent ANTLR from catching the
> >ReconitionExceptions and other Exceptions itself and just allow the
> >exception to propagate back up to the method that is actually calling
> >the parsers.
> 
> I hate sounding thick here.  This is what I read in the documentation
> and it does exit on errors which is what I wanted but I just get an
> "Aborted" message rather than the lovely "error on line 14:5
> (expecting '{')" messages that I was getting before.

Aborted usually means that an exception was not caught.

> I'm calling my antlr generated files from here:
> 
> try
> {
>     CodegenLexer lexer(ss1);
>     CodegenParser parser(lexer);
> 
>     parser.program();
> }
> catch(exception& e)
> {
>     std::cerr << "exception: " << e.what() << std::endl;
>     exit(1);
> }

Add this one:

catch(ANTLRException& e)
{
    std::cerr << "exception: " << e.getMessage() << std::endl;
    exit(1);
}

ANTLR's exceptions do not derive from std::exception so stuff gets
thrown but is not caught. (this behaviour changed somewhere in the
past years. It might be that you ran into a doc mistake) (I don't have
sources/docs handy so I may have messed up some names here and there)

Cheers,

Ric


More information about the antlr-interest mailing list