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".
- #define YYSTYPE const char*
+ #include <stdio.h>
+ #define YYSTYPE char const *
+ int yylex (void);
+ void yyerror (char const *);
and define the @code{stmtMerge} function as:
@example
and define the @code{stmtMerge} function as:
@example
-static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1)
+static YYSTYPE
+stmtMerge (YYSTYPE x0, YYSTYPE x1)
@{
printf ("<OR> ");
return "";
@{
printf ("<OR> ");
return "";
- #define YYSTYPE const char*
+ #define YYSTYPE char const *
static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);
%@}
@end example
static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);
%@}
@end example
@cindex @code{incline}
@cindex @acronym{GLR} parsers and @code{inline}
@cindex @code{incline}
@cindex @acronym{GLR} parsers and @code{inline}
-Note that the @acronym{GLR} parsers require an ISO C89 compiler. In
-addition, they use the @code{inline} keyword, which is not C89, but a
-common extension. It is up to the user of these parsers to handle
+The @acronym{GLR} parsers require a compiler for @acronym{ISO} C89 or
+later. In addition, they use the @code{inline} keyword, which is not
+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
macro @code{AC_C_INLINE}, a mere
@example
%@{
portability issues. For instance, if using Autoconf and the Autoconf
macro @code{AC_C_INLINE}, a mere
@example
%@{
-#if ! defined __GNUC__ && ! defined inline
-# define inline
-#endif
+ #if __STDC_VERSION__ < 199901 && ! defined __GNUC__ && ! defined inline
+ #define inline
+ #endif
The prologue may define types and variables used in the actions. You can
also use preprocessor commands to define macros used there, and use
@code{#include} to include header files that do any of these things.
The prologue may define types and variables used in the actions. You can
also use preprocessor commands to define macros used there, and use
@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
symbols, and may also describe operator precedence and the data types of
The Bison declarations declare the names of the terminal and nonterminal
symbols, and may also describe operator precedence and the data types of
The grammar rules define how to construct each nonterminal symbol from its
parts.
The grammar rules define how to construct each nonterminal symbol from its
parts.
-The epilogue can contain any code you want to use. Often the definition of
-the lexical analyzer @code{yylex} goes here, plus subroutines called by the
-actions in the grammar rules. In a simple program, all the rest of the
-program can go here.
+The epilogue can contain any code you want to use. Often the
+definitions of functions declared in the prologue go here. In a
+simple program, all the rest of the program can go here.
@node Examples
@chapter Examples
@node Examples
@chapter Examples
/* Reverse polish notation calculator. */
%@{
/* Reverse polish notation calculator. */
%@{
-#define YYSTYPE double
-#include <math.h>
+ #define YYSTYPE double
+ #include <math.h>
+ int yylex (void);
+ void yyerror (char const *);
@end example
The declarations section (@pxref{Prologue, , The prologue}) contains two
@end example
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
specifying the C data type for semantic values of both tokens and
The @code{#define} directive defines the macro @code{YYSTYPE}, thus
specifying the C data type for semantic values of both tokens and
The @code{#include} directive is used to declare the exponentiation
function @code{pow}.
The @code{#include} directive is used to declare the exponentiation
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
about the token types (@pxref{Bison Declarations, ,The Bison
Declarations Section}). Each terminal symbol that is not a
The second section, Bison declarations, provides information to Bison
about the token types (@pxref{Bison Declarations, ,The Bison
Declarations Section}). Each terminal symbol that is not a
@group
#include <stdio.h>
@group
#include <stdio.h>
+/* Called by yyparse on error. */
-yyerror (const char *s) /* Called by yyparse on error. */
@{
printf ("%s\n", s);
@}
@{
printf ("%s\n", s);
@}
@file{calc.y}, an infix desk-top calculator.
@example
@file{calc.y}, an infix desk-top calculator.
@example
-/* Infix notation calculator--calc */
+/* Infix notation calculator. */
-#define YYSTYPE double
-#include <math.h>
+ #define YYSTYPE double
+ #include <math.h>
+ #include <stdio.h>
+ int yylex (void);
+ void yyerror (char const *);
-/* Bison Declarations */
+/* Bison declarations. */
%token NUM
%left '-' '+'
%left '*' '/'
%left NEG /* negation--unary minus */
%token NUM
%left '-' '+'
%left '*' '/'
%left NEG /* negation--unary minus */
-%right '^' /* exponentiation */
+%right '^' /* exponentiation */
-/* Grammar follows */
-%%
-input: /* empty string */
+%% /* The grammar follows. */
+input: /* empty */
/* Location tracking calculator. */
%@{
/* Location tracking calculator. */
%@{
-#define YYSTYPE int
-#include <math.h>
+ #define YYSTYPE int
+ #include <math.h>
+ int yylex (void);
+ void yyerror (char const *);
%@}
/* Bison declarations. */
%@}
/* Bison declarations. */
-%% /* Grammar follows */
+%% /* The grammar follows. */
-#include <math.h> /* For math functions, cos(), sin(), etc. */
-#include "calc.h" /* Contains definition of `symrec' */
+ #include <math.h> /* For math functions, cos(), sin(), etc. */
+ #include "calc.h" /* Contains definition of `symrec'. */
+ int yylex (void);
+ void yyerror (char const *);
%@}
@end group
@group
%union @{
%@}
@end group
@group
%union @{
- double val; /* For returning numbers. */
- symrec *tptr; /* For returning symbol-table pointers. */
+ double val; /* For returning numbers. */
+ symrec *tptr; /* For returning symbol-table pointers. */
-%token <val> NUM /* Simple double precision number. */
-%token <tptr> VAR FNCT /* Variable and Function. */
+%token <val> NUM /* Simple double precision number. */
+%token <tptr> VAR FNCT /* Variable and Function. */
%type <val> exp
@group
%right '='
%left '-' '+'
%left '*' '/'
%type <val> exp
@group
%right '='
%left '-' '+'
%left '*' '/'
-%left NEG /* Negation--unary minus */
-%right '^' /* Exponentiation */
+%left NEG /* negation--unary minus */
+%right '^' /* exponentiation */
-/* Grammar follows */
-%%
+%% /* The grammar follows. */
@end smallexample
The above grammar introduces only two new features of the Bison language.
@end smallexample
The above grammar introduces only two new features of the Bison language.
| '(' exp ')' @{ $$ = $2; @}
;
@end group
| '(' exp ')' @{ $$ = $2; @}
;
@end group
typedef double (*func_t) (double);
@end group
@group
typedef double (*func_t) (double);
@end group
@group
-/* Data type for links in the chain of symbols. */
+/* Data type for links in the chain of symbols. */
- char *name; /* name of symbol */
+ char *name; /* name of symbol */
int type; /* type of symbol: either VAR or FNCT */
union
@{
int type; /* type of symbol: either VAR or FNCT */
union
@{
- double var; /* value of a VAR */
- func_t fnctptr; /* value of a FNCT */
+ double var; /* value of a VAR */
+ func_t fnctptr; /* value of a FNCT */
- struct symrec *next; /* link field */
+ struct symrec *next; /* link field */
@};
@end group
@group
typedef struct symrec symrec;
@};
@end group
@group
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 *getsym (const char *);
+symrec *putsym (char const *, func_t);
+symrec *getsym (char const *);
@end group
@end smallexample
@end group
@end smallexample
#include <stdio.h>
@group
#include <stdio.h>
@group
-int
-main (void)
-@{
- init_table ();
- return yyparse ();
-@}
-@end group
-
-@group
+/* Called by yyparse on error. */
-yyerror (const char *s) /* Called by yyparse on error. */
@{
printf ("%s\n", s);
@}
@{
printf ("%s\n", s);
@}
- char *fname;
- double (*fnct)(double);
+ char const *fname;
+ double (*fnct) (double);
-struct init arith_fncts[] =
+struct init const arith_fncts[] =
@{
"sin", sin,
"cos", cos,
@{
"sin", sin,
"cos", cos,
@group
/* The symbol table: a chain of `struct symrec'. */
@group
/* The symbol table: a chain of `struct symrec'. */
-symrec *sym_table = (symrec *) 0;
+
+@group
+int
+main (void)
+@{
+ init_table ();
+ return yyparse ();
+@}
+@end group
@end smallexample
By simply editing the initialization list and adding the necessary include
@end smallexample
By simply editing the initialization list and adding the necessary include
-putsym (char *sym_name, int sym_type)
+putsym (char const *sym_name, int sym_type)
@{
symrec *ptr;
ptr = (symrec *) malloc (sizeof (symrec));
@{
symrec *ptr;
ptr = (symrec *) malloc (sizeof (symrec));
-getsym (const char *sym_name)
+getsym (char const *sym_name)
@{
symrec *ptr;
for (ptr = sym_table; ptr != (symrec *) 0;
@{
symrec *ptr;
for (ptr = sym_table; ptr != (symrec *) 0;
%@}
@var{Bison declarations}
%@}
@var{Bison declarations}
* Epilogue:: Syntax and usage of the epilogue.
@end menu
* Epilogue:: Syntax and usage of the epilogue.
@end menu
-@node Prologue, Bison Declarations, , Grammar Outline
@subsection The prologue
@cindex declarations section
@cindex Prologue
@subsection The prologue
@cindex declarations section
@cindex Prologue
-#include <stdio.h>
-#include "ptypes.h"
+ #include <stdio.h>
+ #include "ptypes.h"
-static void print_token_value (FILE *, int, YYSTYPE);
-#define YYPRINT(F, N, L) print_token_value (F, N, L)
+ static void print_token_value (FILE *, int, YYSTYPE);
+ #define YYPRINT(F, N, L) print_token_value (F, N, L)
@samp{%%} (which precedes the grammar rules) may never be omitted even
if it is the first thing in the file.
@samp{%%} (which precedes the grammar rules) may never be omitted even
if it is the first thing in the file.
-@node Epilogue, , Grammar Rules, Grammar Outline
@subsection The epilogue
@cindex additional C code section
@cindex epilogue
@subsection The epilogue
@cindex additional C code section
@cindex epilogue
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
not come before the definition of @code{yyparse}. For example, the
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
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}.
If the last section is empty, you may omit the @samp{%%} that separates it
from the grammar rules.
@xref{Interface, ,Parser C-Language Interface}.
If the last section is empty, you may omit the @samp{%%} that separates it
from the grammar rules.
-The Bison parser itself contains many static variables whose names start
-with @samp{yy} and many macros whose names start with @samp{YY}. It is a
+The Bison parser itself contains many macros and identifiers whose
+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
manual) in the epilogue of the grammar file.
good idea to avoid using any such names (except those documented in this
manual) in the epilogue of the grammar file.
@code{yyerror} are:
@example
@code{yyerror} are:
@example
-void yyerror (const char *msg); /* Yacc parsers. */
-void yyerror (YYLTYPE *locp, const char *msg); /* GLR parsers. */
+void yyerror (char const *msg); /* Yacc parsers. */
+void yyerror (YYLTYPE *locp, char const *msg); /* GLR parsers. */
@end example
If @samp{%parse-param @{int *nastiness@}} is used, then:
@example
@end example
If @samp{%parse-param @{int *nastiness@}} is used, then:
@example
-void yyerror (int *randomness, const char *msg); /* Yacc parsers. */
-void yyerror (int *randomness, const char *msg); /* GLR parsers. */
+void yyerror (int *randomness, char const *msg); /* Yacc parsers. */
+void yyerror (int *randomness, char const *msg); /* GLR parsers. */
@end example
Finally, GLR and Yacc parsers share the same @code{yyerror} calling
@end example
Finally, GLR and Yacc parsers share the same @code{yyerror} calling
int yyparse (int *nastiness, int *randomness);
void yyerror (YYLTYPE *locp,
int *nastiness, int *randomness,
int yyparse (int *nastiness, int *randomness);
void yyerror (YYLTYPE *locp,
int *nastiness, int *randomness,
-Please, note that the prototypes are only indications of how the code
-produced by Bison will use @code{yyerror}; you still have freedom on the
-exit value, and even on making @code{yyerror} a variadic function. It
-is precisely to enable this that the message is always passed last.
+The prototypes are only indications of how the code produced by Bison
+uses @code{yyerror}. Bison-generated code always ignores the returned
+value, so @code{yyerror} can return any type, including @code{void}.
+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
The variable @code{yynerrs} contains the number of syntax errors
@vindex yynerrs
The variable @code{yynerrs} contains the number of syntax errors
+ int hexflag;
+ int yylex (void);
+ void yyerror (char const *);
calculator (@pxref{Mfcalc Decl, ,Declarations for @code{mfcalc}}):
@smallexample
calculator (@pxref{Mfcalc Decl, ,Declarations for @code{mfcalc}}):
@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
print_token_value (FILE *file, int type, YYSTYPE value)
static void
print_token_value (FILE *file, int type, YYSTYPE value)
@end deffn
@deffn {Function} yyerror
@end deffn
@deffn {Function} yyerror
-User-supplied function to be called by @code{yyparse} on error. The
-function receives one argument, a pointer to a character string
-containing an error message. @xref{Error Reporting, ,The Error
+User-supplied function to be called by @code{yyparse} on error.
+@xref{Error Reporting, ,The Error
Reporting Function @code{yyerror}}.
@end deffn
Reporting Function @code{yyerror}}.
@end deffn