Next: Action Features, Previous: Lexical, Up: Interface [Contents][Index]
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 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 ‘%define parse.error verbose’ in the Bison declarations
section (see The Bison Declarations Section), then
Bison provides a more verbose and specific error message string instead of
just plain "syntax error"
. However, that message sometimes
contains incorrect information if LAC is not enabled (see LAC).
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 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 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. With %define api.pure
, this is
indeed the case for the GLR parsers, but not for the Yacc parser, for
historical reasons, and this is the why %define api.pure full
should be
prefered over %define api.pure
.
When %locations %define api.pure full
is used, yyerror
has the
following signature:
void yyerror (YYLTYPE *locp, 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 A Pure (Reentrant) Parser)
then it is a local variable which only the actions can access.
Next: Action Features, Previous: Lexical, Up: Interface [Contents][Index]