[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
4.7 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: memory exhaustion. This
can happen when the input contains constructions that are very deeply
nested. It isn't likely you will encounter this, since the Bison
parser normally extends its stack automatically up to a very large limit. But
if memory is exhausted, yyparse
calls yyerror
in the usual
fashion, except that the argument string is "memory exhausted"
.
In some cases diagnostics like "syntax error"
are
translated automatically from English to some other language before
they are passed to yyerror
. See section Parser Internationalization.
The following definition suffices in simple programs:
void 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 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 %define api.pure’ is passed then the prototypes for
yyerror
are:
void yyerror (char const *msg); /* Yacc parsers. */ void yyerror (YYLTYPE *locp, char const *msg); /* GLR parsers. */ |
If ‘%parse-param {int *nastiness}’ is used, then:
void yyerror (int *nastiness, char const *msg); /* Yacc parsers. */ void yyerror (int *nastiness, char const *msg); /* GLR parsers. */ |
Finally, GLR and Yacc parsers share the same yyerror
calling
convention for absolutely pure parsers, i.e., when the calling
convention of yylex
and the calling convention of
%define api.pure
are pure.
I.e.:
/* Location tracking. */ %locations /* Pure yylex. */ %define api.pure %lex-param {int *nastiness} /* Pure yyparse. */ %parse-param {int *nastiness} %parse-param {int *randomness} |
results in the following signatures for all the parser kinds:
int yylex (YYSTYPE *lvalp, YYLTYPE *llocp, int *nastiness); int yyparse (int *nastiness, int *randomness); void yyerror (YYLTYPE *locp, int *nastiness, int *randomness, char const *msg); |
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
reported 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] | [ ? ] |