[comp.compilers] YACC error handling

heirich@cs.ucsd.edu (Alan Heirich) (07/30/89)

A correspondent asked about automatic error diagnostics in YACC-generated
parsers.  I've modified the sources to an old version of YACC to provide two
things:

  -- automatic description of states, shifts, reductions, and error
     resynchronization during execution
  -- a function to produce a descriptive string of all of the lookahead
     tokens or goto nonterminals from a given state, during execution

The first modification allows you to debug your grammar.  It provides
much more information than the normal "yydebug=1" features because it
describes states as item sets, as shown in the grammar description
produced by YACC.  The second modification allows your compiler to
produce the sorts of useful error messages requested by the
correspondent, under your control.  (In order to produce a message you
only need to know a state number; the YACC-generated parser calls
yyerror while it is in the state at which the error was seen, so if
you modify the call to yyerror to pass in yystate your yyerror function
can store the state for later use during your recovery action).

The modifications involved changing youtpt (the routine which
writes out the grammar description) to add arrays of strings
to your parser source file giving the names of terminals and 
nonterminals, and giving the lookahead and goto sets encoded 
as shorts to index into those string arrays.

Please note: I am *not* offering to distribute the source code to
these changes, unless someone can suggest an extremely easy way
for this to happen (e.g. someone else distributes it).  But I would
be happy to post more information about how the modifications were
made if people are interested.

-------------------------
Alan Heirich     Comp. Sci. & Eng., Cognitive Science
C-014 University of California, San Diego 92093

heirich@cs.ucsd.edu aheirich@ucsd.bitnet
-- 
Send compilers articles to compilers@ima.isc.com or, perhaps, Levine@YALE.EDU
{ decvax | harvard | yale | bbn }!ima.  Meta-mail to ima!compilers-request.
Please send responses to the author of the message, not the poster.