[antlr-interest] error handling

Bharath Sundararaman Bharath.Sundararaman at starthis.com
Tue Apr 5 05:21:43 PDT 2005


I actually don't throw any exceptions; I parse the entire file, collect
all the exceptions and return it to the user. This however induces a lot
of extra work because you have to decide which errors can be cascaded
and check the values returned by various rules to avoid any nullpointer
or classcast exceptions. (Since I don't throw them, I have to remember
that some values are illegal).

Do you print the trivial errors on the console and throw exceptions for
the serious ones?

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Bryan Ewbank
Sent: Tuesday, April 05, 2005 4:06 AM
To: 'antlr-interest' Interest
Subject: Re: [antlr-interest] error handling

I use a different approach, because catching exceptions outside the
parser means your customer only get the first syntax error each time. 
I'd prefer to give them as many as possible...

I tell antlr to disable exceptions for most productions, then enable
exceptions for a few key production recovery points (statement,
expression, and a few others), then have my own reportError() method
that keeps a running count of errors seen:

   MyParser extends Parser;
   options {  defaultErrorHandler=false; }
   ...
   expression
   options {defaultErrorHandler=true;}
   :
      expr    { ## = #( #[EXPR,"EXPR"], ## ); }
   ;


The top level routine in my parser returns true/false based on the
number of errors:

     parseme
     returns [bool r = true]
     :
         realParseProduction
         { if (nerr > 0) r = false; }
     ;


More information about the antlr-interest mailing list