[antlr-interest] error handling

Bryan Ewbank ewbank at gmail.com
Tue Apr 5 11:05:30 PDT 2005


I print most messages, but each tree parser does one thing (syntax,
type inference, semantics, etc), and I terminate /between/ passes if
there were any errors reported.

I don't worry too much about cascade errors because I've suppressed
exception handling on all but a few key tree node types (STMT, EXPR). 
This means when there's an unrecoverable error anywhere in an
expression, the tree for that expression is abandoned.  However, the
only unrecoverable errors are internal errors - for other errors, only
the first one or two are printed, then the rest are suppressed until I
pass through the STMT or EXPR node.

On Apr 5, 2005 8:21 AM, Bharath Sundararaman
<Bharath.Sundararaman at starthis.com> wrote:
> 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