www.delorie.com/gnu/docs/bison/bison_76.html   search  
Buy the book!

Bison 1.875

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.3 The Error Reporting Function yyerror

The Bison parser detects a syntax error or parse error whenever it reads a token which cannot satisfy any syntax rule. An action in the grammar can also explicitly proclaim an error, using the macro YYERROR (see section Special Features for Use in Actions).

The Bison parser expects to report the error by calling an error reporting function named yyerror, which you must supply. It is called by yyparse whenever a syntax error is found, and it receives one argument. For a syntax error, the string is normally "syntax error".

If you invoke the directive %error-verbose in the Bison declarations section (see section The Bison Declarations Section), then Bison provides a more verbose and specific error message string instead of just plain "syntax error".

The parser can detect one other kind of error: stack overflow. This happens when the input contains constructions that are very deeply nested. It isn't likely you will encounter this, since the Bison parser extends its stack automatically up to a very large limit. But if overflow happens, yyparse calls yyerror in the usual fashion, except that the argument string is "parser stack overflow".

The following definition suffices in simple programs:

yyerror (char const *s)
  fprintf (stderr, "%s\n", s);

After yyerror returns to yyparse, the latter will attempt error recovery if you have written suitable error recovery grammar rules (see section 6. Error Recovery). If recovery is impossible, yyparse will immediately return 1.

Obviously, in location tracking pure parsers, yyerror should have an access to the current location. This is indeed the case for the GLR parsers, but not for the Yacc parser, for historical reasons. I.e., if `%locations %pure-parser' is passed then the prototypes for yyerror are:

void yyerror (char const *msg);                 /* Yacc parsers.  */
void yyerror (YYLTYPE *locp, char const *msg);  /* GLR parsers.   */

The prototypes are only indications of how the code produced by Bison uses yyerror. Bison-generated code always ignores the returned value, so yyerror can return any type, including void. Also, yyerror can be a variadic function; that is why the message is always passed last.

Traditionally yyerror returns an int that is always ignored, but this is purely for historical reasons, and void is preferable since it more accurately describes the return type for yyerror.

The variable yynerrs contains the number of syntax errors encountered so far. Normally this variable is global; but if you request a pure parser (see section A Pure (Reentrant) Parser) then it is a local variable which only the actions can access.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003