Re: wanted: error-correcting C parser

Terence John Parr <>
Mon, 20 Feb 1995 06:36:01 GMT

          From comp.compilers

Related articles
wanted: error-correcting C parser (1995-02-13)
Re: wanted: error-correcting C parser (Zerksis D. Umrigar) (1995-02-15)
Re: wanted: error-correcting C parser (1995-02-18)
Re: wanted: error-correcting C parser (Terence John Parr) (1995-02-20)
| List of all articles for this month |

Newsgroups: comp.compilers
From: Terence John Parr <>
Keywords: C, parse, errors
Organization: Compilers Central
References: 95-02-114
Date: Mon, 20 Feb 1995 06:36:01 GMT (Robert E. Keller) writes:
> I'm looking for a parser which understands C (preferably ANSI-C)
> (or some "reasonable" subset) and which, if it encounters a syntax
> error, delivers a set of all those tokens/symbols which are correct
> in place of the wrong symbol (not caring for what comes after the wrong
> symbol, of course);
> example:
> given
> extern short c;
> int fnc(int a, int b){return a+;}
> ..
> the described parser comes forward with like
> error: line 2, symbol #14 (";");
> correct symbols: a b c ( ' ! <.. some more unary operators etc. ..>
> correct tokens: NUMBER

I've got two things for you: (i) an exact solution and (ii)
a new way to handle parser errors called PARSER EXCEPTION HANDLING:

I just ran your input through the ANSI C front end I built
for ANTLR (the predicated-LL(k) parser generator of PCCTS) and this
is what I got:


This is what happens without me telling ANTLR anything about error
correction. The parser also recovered automatically and continued
with the parse. It is the default mode.

On the other hand, a more clear error message would be

line 2: syntax error at ";" missing { MOP AOP "expression atom" ... }

and could easily be generated by giving ANTLR an error class which
groups tokens (printed during error messages); e.g.,

#errclass "expression atom" { STRING CHARACTER ... }

However, if you want something even more powerful, you should use the really
slick PARSER EXCEPTION HANDLING. You essentially provide handlers for
different error signals such as NoViableAlt or MismatchedToken.

: "return" e:expression
catch MismatchedToken :
catch NoViableAlt :
/* do error reporting and recovery here */

which means "any time the parser gets a mismatched token or can't
find any alternative of a rule to match the current input specifically
during the parse of expression, jump all the back up and do this error

There are lots of details to make much of this happen automatically too
and there are many more features. These critters are avaiable
using either the C or the C++ interface.

> any reference to parser generators capable of generating such a parser
> is welcome, too;

My shameless plug would be for PCCTS which you can check out at

   in pub/pccts

The ANSI C grammar is in pub/pccts/contrib/ansi.tar and the
latest version is 1.31, but 1.32b3 is up on the net already.
(there has been a shuffling of IP addresses at the site where points...your name server may not
have been updated yet; it should be ok).

The documentation is complete, but spread over the set of
release notes. I'm working steadily on the book and will
have it out this Summer if it kills me (which it might).


PS If people are interested, I can outline more of the
parser exception handling. It's proven very effective
thus far--even better than hand-built stuff because ANTLR
handles lots of details for you (such as managing the
call stack).

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.