mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-27 05:03:07 +00:00
Emphasize that yylex and yyerror must be declared before being used.
E.g., one should typically declare them in the prologue. Use GNU coding style in examples. Put "const" consistently after the type it modifies. Mention that C99 supports "inline". Mention that yyerror traditionally returns "int".
This commit is contained in:
@@ -685,7 +685,10 @@ Let's consider an example, vastly simplified from a C++ grammar.
|
|||||||
|
|
||||||
@example
|
@example
|
||||||
%@{
|
%@{
|
||||||
#define YYSTYPE const char*
|
#include <stdio.h>
|
||||||
|
#define YYSTYPE char const *
|
||||||
|
int yylex (void);
|
||||||
|
void yyerror (char const *);
|
||||||
%@}
|
%@}
|
||||||
|
|
||||||
%token TYPENAME ID
|
%token TYPENAME ID
|
||||||
@@ -783,7 +786,8 @@ stmt : expr ';' %merge <stmtMerge>
|
|||||||
and define the @code{stmtMerge} function as:
|
and define the @code{stmtMerge} function as:
|
||||||
|
|
||||||
@example
|
@example
|
||||||
static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1)
|
static YYSTYPE
|
||||||
|
stmtMerge (YYSTYPE x0, YYSTYPE x1)
|
||||||
@{
|
@{
|
||||||
printf ("<OR> ");
|
printf ("<OR> ");
|
||||||
return "";
|
return "";
|
||||||
@@ -796,7 +800,7 @@ in the C declarations at the beginning of the file:
|
|||||||
|
|
||||||
@example
|
@example
|
||||||
%@{
|
%@{
|
||||||
#define YYSTYPE const char*
|
#define YYSTYPE char const *
|
||||||
static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);
|
static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);
|
||||||
%@}
|
%@}
|
||||||
@end example
|
@end example
|
||||||
@@ -813,15 +817,16 @@ as both an @code{expr} and a @code{decl}, and print
|
|||||||
|
|
||||||
@cindex @code{incline}
|
@cindex @code{incline}
|
||||||
@cindex @acronym{GLR} parsers and @code{inline}
|
@cindex @acronym{GLR} parsers and @code{inline}
|
||||||
Note that the @acronym{GLR} parsers require an ISO C89 compiler. In
|
The @acronym{GLR} parsers require a compiler for @acronym{ISO} C89 or
|
||||||
addition, they use the @code{inline} keyword, which is not C89, but a
|
later. In addition, they use the @code{inline} keyword, which is not
|
||||||
common extension. It is up to the user of these parsers to handle
|
C89, but is C99 and is a common extension in pre-C99 compilers. It is
|
||||||
|
up to the user of these parsers to handle
|
||||||
portability issues. For instance, if using Autoconf and the Autoconf
|
portability issues. For instance, if using Autoconf and the Autoconf
|
||||||
macro @code{AC_C_INLINE}, a mere
|
macro @code{AC_C_INLINE}, a mere
|
||||||
|
|
||||||
@example
|
@example
|
||||||
%@{
|
%@{
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
%@}
|
%@}
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
@@ -830,9 +835,9 @@ will suffice. Otherwise, we suggest
|
|||||||
|
|
||||||
@example
|
@example
|
||||||
%@{
|
%@{
|
||||||
#if ! defined __GNUC__ && ! defined inline
|
#if __STDC_VERSION__ < 199901 && ! defined __GNUC__ && ! defined inline
|
||||||
# define inline
|
#define inline
|
||||||
#endif
|
#endif
|
||||||
%@}
|
%@}
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
@@ -992,6 +997,9 @@ in every Bison grammar file to separate the sections.
|
|||||||
The prologue may define types and variables used in the actions. You can
|
The prologue may define types and variables used in the actions. You can
|
||||||
also use preprocessor commands to define macros used there, and use
|
also use preprocessor commands to define macros used there, and use
|
||||||
@code{#include} to include header files that do any of these things.
|
@code{#include} to include header files that do any of these things.
|
||||||
|
You need to declare the lexical analyzer @code{yylex} and the error
|
||||||
|
printer @code{yyerror} here, along with any other global identifiers
|
||||||
|
used by the actions in the grammar rules.
|
||||||
|
|
||||||
The Bison declarations declare the names of the terminal and nonterminal
|
The Bison declarations declare the names of the terminal and nonterminal
|
||||||
symbols, and may also describe operator precedence and the data types of
|
symbols, and may also describe operator precedence and the data types of
|
||||||
@@ -1000,10 +1008,9 @@ semantic values of various symbols.
|
|||||||
The grammar rules define how to construct each nonterminal symbol from its
|
The grammar rules define how to construct each nonterminal symbol from its
|
||||||
parts.
|
parts.
|
||||||
|
|
||||||
The epilogue can contain any code you want to use. Often the definition of
|
The epilogue can contain any code you want to use. Often the
|
||||||
the lexical analyzer @code{yylex} goes here, plus subroutines called by the
|
definitions of functions declared in the prologue go here. In a
|
||||||
actions in the grammar rules. In a simple program, all the rest of the
|
simple program, all the rest of the program can go here.
|
||||||
program can go here.
|
|
||||||
|
|
||||||
@node Examples
|
@node Examples
|
||||||
@chapter Examples
|
@chapter Examples
|
||||||
@@ -1070,8 +1077,10 @@ calculator. As in C, comments are placed between @samp{/*@dots{}*/}.
|
|||||||
/* Reverse polish notation calculator. */
|
/* Reverse polish notation calculator. */
|
||||||
|
|
||||||
%@{
|
%@{
|
||||||
#define YYSTYPE double
|
#define YYSTYPE double
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
int yylex (void);
|
||||||
|
void yyerror (char const *);
|
||||||
%@}
|
%@}
|
||||||
|
|
||||||
%token NUM
|
%token NUM
|
||||||
@@ -1080,7 +1089,7 @@ calculator. As in C, comments are placed between @samp{/*@dots{}*/}.
|
|||||||
@end example
|
@end example
|
||||||
|
|
||||||
The declarations section (@pxref{Prologue, , The prologue}) contains two
|
The declarations section (@pxref{Prologue, , The prologue}) contains two
|
||||||
preprocessor directives.
|
preprocessor directives and two forward declarations.
|
||||||
|
|
||||||
The @code{#define} directive defines the macro @code{YYSTYPE}, thus
|
The @code{#define} directive defines the macro @code{YYSTYPE}, thus
|
||||||
specifying the C data type for semantic values of both tokens and
|
specifying the C data type for semantic values of both tokens and
|
||||||
@@ -1093,6 +1102,12 @@ which is a floating point number.
|
|||||||
The @code{#include} directive is used to declare the exponentiation
|
The @code{#include} directive is used to declare the exponentiation
|
||||||
function @code{pow}.
|
function @code{pow}.
|
||||||
|
|
||||||
|
The forward declarations for @code{yylex} and @code{yyerror} are
|
||||||
|
needed because the C language requires that functions be declared
|
||||||
|
before they are used. These functions will be defined in the
|
||||||
|
epilogue, but the parser calls them so they must be declared in the
|
||||||
|
prologue.
|
||||||
|
|
||||||
The second section, Bison declarations, provides information to Bison
|
The second section, Bison declarations, provides information to Bison
|
||||||
about the token types (@pxref{Bison Declarations, ,The Bison
|
about the token types (@pxref{Bison Declarations, ,The Bison
|
||||||
Declarations Section}). Each terminal symbol that is not a
|
Declarations Section}). Each terminal symbol that is not a
|
||||||
@@ -1381,8 +1396,9 @@ here is the definition we will use:
|
|||||||
@group
|
@group
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* Called by yyparse on error. */
|
||||||
void
|
void
|
||||||
yyerror (const char *s) /* Called by yyparse on error. */
|
yyerror (char const *s)
|
||||||
@{
|
@{
|
||||||
printf ("%s\n", s);
|
printf ("%s\n", s);
|
||||||
@}
|
@}
|
||||||
@@ -1482,23 +1498,25 @@ parentheses nested to arbitrary depth. Here is the Bison code for
|
|||||||
@file{calc.y}, an infix desk-top calculator.
|
@file{calc.y}, an infix desk-top calculator.
|
||||||
|
|
||||||
@example
|
@example
|
||||||
/* Infix notation calculator--calc */
|
/* Infix notation calculator. */
|
||||||
|
|
||||||
%@{
|
%@{
|
||||||
#define YYSTYPE double
|
#define YYSTYPE double
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
int yylex (void);
|
||||||
|
void yyerror (char const *);
|
||||||
%@}
|
%@}
|
||||||
|
|
||||||
/* Bison Declarations */
|
/* Bison declarations. */
|
||||||
%token NUM
|
%token NUM
|
||||||
%left '-' '+'
|
%left '-' '+'
|
||||||
%left '*' '/'
|
%left '*' '/'
|
||||||
%left NEG /* negation--unary minus */
|
%left NEG /* negation--unary minus */
|
||||||
%right '^' /* exponentiation */
|
%right '^' /* exponentiation */
|
||||||
|
|
||||||
/* Grammar follows */
|
%% /* The grammar follows. */
|
||||||
%%
|
input: /* empty */
|
||||||
input: /* empty string */
|
|
||||||
| input line
|
| input line
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -1628,8 +1646,10 @@ the same as the declarations for the infix notation calculator.
|
|||||||
/* Location tracking calculator. */
|
/* Location tracking calculator. */
|
||||||
|
|
||||||
%@{
|
%@{
|
||||||
#define YYSTYPE int
|
#define YYSTYPE int
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
int yylex (void);
|
||||||
|
void yyerror (char const *);
|
||||||
%@}
|
%@}
|
||||||
|
|
||||||
/* Bison declarations. */
|
/* Bison declarations. */
|
||||||
@@ -1640,7 +1660,7 @@ the same as the declarations for the infix notation calculator.
|
|||||||
%left NEG
|
%left NEG
|
||||||
%right '^'
|
%right '^'
|
||||||
|
|
||||||
%% /* Grammar follows */
|
%% /* The grammar follows. */
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
@noindent
|
@noindent
|
||||||
@@ -1863,29 +1883,30 @@ Here are the C and Bison declarations for the multi-function calculator.
|
|||||||
@smallexample
|
@smallexample
|
||||||
@group
|
@group
|
||||||
%@{
|
%@{
|
||||||
#include <math.h> /* For math functions, cos(), sin(), etc. */
|
#include <math.h> /* For math functions, cos(), sin(), etc. */
|
||||||
#include "calc.h" /* Contains definition of `symrec' */
|
#include "calc.h" /* Contains definition of `symrec'. */
|
||||||
|
int yylex (void);
|
||||||
|
void yyerror (char const *);
|
||||||
%@}
|
%@}
|
||||||
@end group
|
@end group
|
||||||
@group
|
@group
|
||||||
%union @{
|
%union @{
|
||||||
double val; /* For returning numbers. */
|
double val; /* For returning numbers. */
|
||||||
symrec *tptr; /* For returning symbol-table pointers. */
|
symrec *tptr; /* For returning symbol-table pointers. */
|
||||||
@}
|
@}
|
||||||
@end group
|
@end group
|
||||||
%token <val> NUM /* Simple double precision number. */
|
%token <val> NUM /* Simple double precision number. */
|
||||||
%token <tptr> VAR FNCT /* Variable and Function. */
|
%token <tptr> VAR FNCT /* Variable and Function. */
|
||||||
%type <val> exp
|
%type <val> exp
|
||||||
|
|
||||||
@group
|
@group
|
||||||
%right '='
|
%right '='
|
||||||
%left '-' '+'
|
%left '-' '+'
|
||||||
%left '*' '/'
|
%left '*' '/'
|
||||||
%left NEG /* Negation--unary minus */
|
%left NEG /* negation--unary minus */
|
||||||
%right '^' /* Exponentiation */
|
%right '^' /* exponentiation */
|
||||||
@end group
|
@end group
|
||||||
/* Grammar follows */
|
%% /* The grammar follows. */
|
||||||
%%
|
|
||||||
@end smallexample
|
@end smallexample
|
||||||
|
|
||||||
The above grammar introduces only two new features of the Bison language.
|
The above grammar introduces only two new features of the Bison language.
|
||||||
@@ -1946,7 +1967,7 @@ exp: NUM @{ $$ = $1; @}
|
|||||||
| '(' exp ')' @{ $$ = $2; @}
|
| '(' exp ')' @{ $$ = $2; @}
|
||||||
;
|
;
|
||||||
@end group
|
@end group
|
||||||
/* End of grammar */
|
/* End of grammar. */
|
||||||
%%
|
%%
|
||||||
@end smallexample
|
@end smallexample
|
||||||
|
|
||||||
@@ -1965,33 +1986,33 @@ provides for either functions or variables to be placed in the table.
|
|||||||
|
|
||||||
@smallexample
|
@smallexample
|
||||||
@group
|
@group
|
||||||
/* Function type. */
|
/* Function type. */
|
||||||
typedef double (*func_t) (double);
|
typedef double (*func_t) (double);
|
||||||
@end group
|
@end group
|
||||||
|
|
||||||
@group
|
@group
|
||||||
/* Data type for links in the chain of symbols. */
|
/* Data type for links in the chain of symbols. */
|
||||||
struct symrec
|
struct symrec
|
||||||
@{
|
@{
|
||||||
char *name; /* name of symbol */
|
char *name; /* name of symbol */
|
||||||
int type; /* type of symbol: either VAR or FNCT */
|
int type; /* type of symbol: either VAR or FNCT */
|
||||||
union
|
union
|
||||||
@{
|
@{
|
||||||
double var; /* value of a VAR */
|
double var; /* value of a VAR */
|
||||||
func_t fnctptr; /* value of a FNCT */
|
func_t fnctptr; /* value of a FNCT */
|
||||||
@} value;
|
@} value;
|
||||||
struct symrec *next; /* link field */
|
struct symrec *next; /* link field */
|
||||||
@};
|
@};
|
||||||
@end group
|
@end group
|
||||||
|
|
||||||
@group
|
@group
|
||||||
typedef struct symrec symrec;
|
typedef struct symrec symrec;
|
||||||
|
|
||||||
/* The symbol table: a chain of `struct symrec'. */
|
/* The symbol table: a chain of `struct symrec'. */
|
||||||
extern symrec *sym_table;
|
extern symrec *sym_table;
|
||||||
|
|
||||||
symrec *putsym (const char *, func_t);
|
symrec *putsym (char const *, func_t);
|
||||||
symrec *getsym (const char *);
|
symrec *getsym (char const *);
|
||||||
@end group
|
@end group
|
||||||
@end smallexample
|
@end smallexample
|
||||||
|
|
||||||
@@ -2003,17 +2024,9 @@ function that initializes the symbol table. Here it is, and
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@group
|
@group
|
||||||
int
|
/* Called by yyparse on error. */
|
||||||
main (void)
|
|
||||||
@{
|
|
||||||
init_table ();
|
|
||||||
return yyparse ();
|
|
||||||
@}
|
|
||||||
@end group
|
|
||||||
|
|
||||||
@group
|
|
||||||
void
|
void
|
||||||
yyerror (const char *s) /* Called by yyparse on error. */
|
yyerror (char const *s)
|
||||||
@{
|
@{
|
||||||
printf ("%s\n", s);
|
printf ("%s\n", s);
|
||||||
@}
|
@}
|
||||||
@@ -2022,13 +2035,13 @@ yyerror (const char *s) /* Called by yyparse on error. */
|
|||||||
@group
|
@group
|
||||||
struct init
|
struct init
|
||||||
@{
|
@{
|
||||||
char *fname;
|
char const *fname;
|
||||||
double (*fnct)(double);
|
double (*fnct) (double);
|
||||||
@};
|
@};
|
||||||
@end group
|
@end group
|
||||||
|
|
||||||
@group
|
@group
|
||||||
struct init arith_fncts[] =
|
struct init const arith_fncts[] =
|
||||||
@{
|
@{
|
||||||
"sin", sin,
|
"sin", sin,
|
||||||
"cos", cos,
|
"cos", cos,
|
||||||
@@ -2042,7 +2055,7 @@ struct init arith_fncts[] =
|
|||||||
|
|
||||||
@group
|
@group
|
||||||
/* The symbol table: a chain of `struct symrec'. */
|
/* The symbol table: a chain of `struct symrec'. */
|
||||||
symrec *sym_table = (symrec *) 0;
|
symrec *sym_table;
|
||||||
@end group
|
@end group
|
||||||
|
|
||||||
@group
|
@group
|
||||||
@@ -2059,6 +2072,15 @@ init_table (void)
|
|||||||
@}
|
@}
|
||||||
@}
|
@}
|
||||||
@end group
|
@end group
|
||||||
|
|
||||||
|
@group
|
||||||
|
int
|
||||||
|
main (void)
|
||||||
|
@{
|
||||||
|
init_table ();
|
||||||
|
return yyparse ();
|
||||||
|
@}
|
||||||
|
@end group
|
||||||
@end smallexample
|
@end smallexample
|
||||||
|
|
||||||
By simply editing the initialization list and adding the necessary include
|
By simply editing the initialization list and adding the necessary include
|
||||||
@@ -2073,7 +2095,7 @@ found, a pointer to that symbol is returned; otherwise zero is returned.
|
|||||||
|
|
||||||
@smallexample
|
@smallexample
|
||||||
symrec *
|
symrec *
|
||||||
putsym (char *sym_name, int sym_type)
|
putsym (char const *sym_name, int sym_type)
|
||||||
@{
|
@{
|
||||||
symrec *ptr;
|
symrec *ptr;
|
||||||
ptr = (symrec *) malloc (sizeof (symrec));
|
ptr = (symrec *) malloc (sizeof (symrec));
|
||||||
@@ -2087,7 +2109,7 @@ putsym (char *sym_name, int sym_type)
|
|||||||
@}
|
@}
|
||||||
|
|
||||||
symrec *
|
symrec *
|
||||||
getsym (const char *sym_name)
|
getsym (char const *sym_name)
|
||||||
@{
|
@{
|
||||||
symrec *ptr;
|
symrec *ptr;
|
||||||
for (ptr = sym_table; ptr != (symrec *) 0;
|
for (ptr = sym_table; ptr != (symrec *) 0;
|
||||||
@@ -2245,7 +2267,7 @@ appropriate delimiters:
|
|||||||
|
|
||||||
@example
|
@example
|
||||||
%@{
|
%@{
|
||||||
@var{Prologue}
|
@var{Prologue}
|
||||||
%@}
|
%@}
|
||||||
|
|
||||||
@var{Bison declarations}
|
@var{Bison declarations}
|
||||||
@@ -2268,7 +2290,7 @@ continues until end of line.
|
|||||||
* Epilogue:: Syntax and usage of the epilogue.
|
* Epilogue:: Syntax and usage of the epilogue.
|
||||||
@end menu
|
@end menu
|
||||||
|
|
||||||
@node Prologue, Bison Declarations, , Grammar Outline
|
@node Prologue
|
||||||
@subsection The prologue
|
@subsection The prologue
|
||||||
@cindex declarations section
|
@cindex declarations section
|
||||||
@cindex Prologue
|
@cindex Prologue
|
||||||
@@ -2292,8 +2314,8 @@ can be done with two @var{Prologue} blocks, one before and one after the
|
|||||||
|
|
||||||
@smallexample
|
@smallexample
|
||||||
%@{
|
%@{
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "ptypes.h"
|
#include "ptypes.h"
|
||||||
%@}
|
%@}
|
||||||
|
|
||||||
%union @{
|
%union @{
|
||||||
@@ -2302,8 +2324,8 @@ can be done with two @var{Prologue} blocks, one before and one after the
|
|||||||
@}
|
@}
|
||||||
|
|
||||||
%@{
|
%@{
|
||||||
static void print_token_value (FILE *, int, YYSTYPE);
|
static void print_token_value (FILE *, int, YYSTYPE);
|
||||||
#define YYPRINT(F, N, L) print_token_value (F, N, L)
|
#define YYPRINT(F, N, L) print_token_value (F, N, L)
|
||||||
%@}
|
%@}
|
||||||
|
|
||||||
@dots{}
|
@dots{}
|
||||||
@@ -2331,7 +2353,7 @@ There must always be at least one grammar rule, and the first
|
|||||||
@samp{%%} (which precedes the grammar rules) may never be omitted even
|
@samp{%%} (which precedes the grammar rules) may never be omitted even
|
||||||
if it is the first thing in the file.
|
if it is the first thing in the file.
|
||||||
|
|
||||||
@node Epilogue, , Grammar Rules, Grammar Outline
|
@node Epilogue
|
||||||
@subsection The epilogue
|
@subsection The epilogue
|
||||||
@cindex additional C code section
|
@cindex additional C code section
|
||||||
@cindex epilogue
|
@cindex epilogue
|
||||||
@@ -2341,14 +2363,17 @@ The @var{Epilogue} is copied verbatim to the end of the parser file, just as
|
|||||||
the @var{Prologue} is copied to the beginning. This is the most convenient
|
the @var{Prologue} is copied to the beginning. This is the most convenient
|
||||||
place to put anything that you want to have in the parser file but which need
|
place to put anything that you want to have in the parser file but which need
|
||||||
not come before the definition of @code{yyparse}. For example, the
|
not come before the definition of @code{yyparse}. For example, the
|
||||||
definitions of @code{yylex} and @code{yyerror} often go here.
|
definitions of @code{yylex} and @code{yyerror} often go here. Because
|
||||||
|
C requires functions to be declared before being used, you often need
|
||||||
|
to declare functions like @code{yylex} and @code{yyerror} in the Prologue,
|
||||||
|
even if you define them int he Epilogue.
|
||||||
@xref{Interface, ,Parser C-Language Interface}.
|
@xref{Interface, ,Parser C-Language Interface}.
|
||||||
|
|
||||||
If the last section is empty, you may omit the @samp{%%} that separates it
|
If the last section is empty, you may omit the @samp{%%} that separates it
|
||||||
from the grammar rules.
|
from the grammar rules.
|
||||||
|
|
||||||
The Bison parser itself contains many static variables whose names start
|
The Bison parser itself contains many macros and identifiers whose
|
||||||
with @samp{yy} and many macros whose names start with @samp{YY}. It is a
|
names start with @samp{yy} or @samp{YY}, so it is a
|
||||||
good idea to avoid using any such names (except those documented in this
|
good idea to avoid using any such names (except those documented in this
|
||||||
manual) in the epilogue of the grammar file.
|
manual) in the epilogue of the grammar file.
|
||||||
|
|
||||||
@@ -4166,7 +4191,7 @@ The following definition suffices in simple programs:
|
|||||||
@example
|
@example
|
||||||
@group
|
@group
|
||||||
void
|
void
|
||||||
yyerror (const char *s)
|
yyerror (char const *s)
|
||||||
@{
|
@{
|
||||||
@end group
|
@end group
|
||||||
@group
|
@group
|
||||||
@@ -4187,15 +4212,15 @@ parsers, but not for the Yacc parser, for historical reasons. I.e., if
|
|||||||
@code{yyerror} are:
|
@code{yyerror} are:
|
||||||
|
|
||||||
@example
|
@example
|
||||||
void yyerror (const char *msg); /* Yacc parsers. */
|
void yyerror (char const *msg); /* Yacc parsers. */
|
||||||
void yyerror (YYLTYPE *locp, const char *msg); /* GLR parsers. */
|
void yyerror (YYLTYPE *locp, char const *msg); /* GLR parsers. */
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
If @samp{%parse-param @{int *nastiness@}} is used, then:
|
If @samp{%parse-param @{int *nastiness@}} is used, then:
|
||||||
|
|
||||||
@example
|
@example
|
||||||
void yyerror (int *randomness, const char *msg); /* Yacc parsers. */
|
void yyerror (int *randomness, char const *msg); /* Yacc parsers. */
|
||||||
void yyerror (int *randomness, const char *msg); /* GLR parsers. */
|
void yyerror (int *randomness, char const *msg); /* GLR parsers. */
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
Finally, GLR and Yacc parsers share the same @code{yyerror} calling
|
Finally, GLR and Yacc parsers share the same @code{yyerror} calling
|
||||||
@@ -4222,14 +4247,20 @@ int yylex (YYSTYPE *lvalp, YYLTYPE *llocp, int *nastiness);
|
|||||||
int yyparse (int *nastiness, int *randomness);
|
int yyparse (int *nastiness, int *randomness);
|
||||||
void yyerror (YYLTYPE *locp,
|
void yyerror (YYLTYPE *locp,
|
||||||
int *nastiness, int *randomness,
|
int *nastiness, int *randomness,
|
||||||
const char *msg);
|
char const *msg);
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
@noindent
|
@noindent
|
||||||
Please, note that the prototypes are only indications of how the code
|
The prototypes are only indications of how the code produced by Bison
|
||||||
produced by Bison will use @code{yyerror}; you still have freedom on the
|
uses @code{yyerror}. Bison-generated code always ignores the returned
|
||||||
exit value, and even on making @code{yyerror} a variadic function. It
|
value, so @code{yyerror} can return any type, including @code{void}.
|
||||||
is precisely to enable this that the message is always passed last.
|
Also, @code{yyerror} can be a variadic function; that is why the
|
||||||
|
message is always passed last.
|
||||||
|
|
||||||
|
Traditionally @code{yyerror} returns an @code{int} that is always
|
||||||
|
ignored, but this is purely for historical reasons, and @code{void} is
|
||||||
|
preferable since it more accurately describes the return type for
|
||||||
|
@code{yyerror}.
|
||||||
|
|
||||||
@vindex yynerrs
|
@vindex yynerrs
|
||||||
The variable @code{yynerrs} contains the number of syntax errors
|
The variable @code{yynerrs} contains the number of syntax errors
|
||||||
@@ -5383,7 +5414,9 @@ as an identifier if it appears in that context. Here is how you can do it:
|
|||||||
@example
|
@example
|
||||||
@group
|
@group
|
||||||
%@{
|
%@{
|
||||||
int hexflag;
|
int hexflag;
|
||||||
|
int yylex (void);
|
||||||
|
void yyerror (char const *);
|
||||||
%@}
|
%@}
|
||||||
%%
|
%%
|
||||||
@dots{}
|
@dots{}
|
||||||
@@ -5981,7 +6014,12 @@ Here is an example of @code{YYPRINT} suitable for the multi-function
|
|||||||
calculator (@pxref{Mfcalc Decl, ,Declarations for @code{mfcalc}}):
|
calculator (@pxref{Mfcalc Decl, ,Declarations for @code{mfcalc}}):
|
||||||
|
|
||||||
@smallexample
|
@smallexample
|
||||||
#define YYPRINT(file, type, value) print_token_value (file, type, value)
|
%@{
|
||||||
|
static void print_token_value (FILE *, int, YYSTYPE);
|
||||||
|
#define YYPRINT(file, type, value) print_token_value (file, type, value)
|
||||||
|
%@}
|
||||||
|
|
||||||
|
@dots{} %% @dots{} %% @dots{}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
print_token_value (FILE *file, int type, YYSTYPE value)
|
print_token_value (FILE *file, int type, YYSTYPE value)
|
||||||
@@ -6420,9 +6458,8 @@ after a syntax error. @xref{Error Recovery}.
|
|||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Function} yyerror
|
@deffn {Function} yyerror
|
||||||
User-supplied function to be called by @code{yyparse} on error. The
|
User-supplied function to be called by @code{yyparse} on error.
|
||||||
function receives one argument, a pointer to a character string
|
@xref{Error Reporting, ,The Error
|
||||||
containing an error message. @xref{Error Reporting, ,The Error
|
|
||||||
Reporting Function @code{yyerror}}.
|
Reporting Function @code{yyerror}}.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user