Bison Debug Syntax Error
Contents |
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 bison syntax error unexpected identifier yyerror, which you must supply. It is called by yyparse whenever a syntax error bison syntax error unexpected $end is found, and it receives one argument. For a syntax error, the string is normally "syntaxerror". If you invoke ‘%define parse.error verbose’ bison debug shift reduce conflicts 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 "syntaxerror". However, that message sometimes contains incorrect information if LAC is
Bison Yyerror
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 "memoryexhausted". In some cases bison error handling diagnostics like "syntaxerror" 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 y
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 bison error token report the error by calling an error reporting function named yyerror, which you
Bison Error Recovery Example
must supply. It is called by yyparse whenever a syntax error is found, and it receives one argument.
%define Parse.error Verbose
For a syntax error, the string is normally "syntaxerror". If you invoke ‘%define parse.error verbose’ in the Bison declarations section (see The Bison Declarations Section), then Bison provides a more verbose https://www.gnu.org/s/bison/manual/html_node/Error-Reporting.html and specific error message string instead of just plain "syntaxerror". 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 https://www.gnu.org/s/bison/manual/html_node/Error-Reporting.html 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 "memoryexhausted". In some cases diagnostics like "syntaxerror" 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 a
compilation of trace facilities, you must define the macro YYDEBUG when you compile the parser. You could use `-DYYDEBUG=1' as a compiler option or you could put `#define YYDEBUG 1' in the C declarations http://dinosaur.compilertools.net/bison/bison_11.html section of the grammar file (see section The C Declarations Section). Alternatively, use the `-t' http://www.delorie.com/gnu/docs/bison/bison_76.html option when you run Bison (see section Invoking Bison). We always define YYDEBUG so that debugging is always possible. The trace facility uses stderr, so you must add #include
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: 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 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