Next: Prologue Alternatives, Up: Outline of a Bison Grammar [Contents][Index]
The Prologue section contains macro definitions and declarations of
functions and variables that are used in the actions in the grammar rules.
These are copied to the beginning of the parser implementation file so that
they precede the definition of yyparse
. You can use ‘#include’
to get the declarations from a header file. If you don’t need any C
declarations, you may omit the ‘%{’ and ‘%}’ delimiters that
bracket this section.
The Prologue section is terminated by the first occurrence of ‘%}’ that is outside a comment, a string literal, or a character constant.
You may have more than one Prologue section, intermixed with the
Bison declarations. This allows you to have C and Bison declarations
that refer to each other. For example, the %union
declaration may
use types defined in a header file, and you may wish to prototype functions
that take arguments of type YYSTYPE
. This can be done with two
Prologue blocks, one before and one after the %union
declaration.
%{ #define _GNU_SOURCE #include <stdio.h> #include "ptypes.h" %}
%union {
long n;
tree t; /* tree
is defined in ptypes.h. */
}
%{ static void print_token (yytoken_kind_t token, YYSTYPE val); %}
…
When in doubt, it is usually safer to put prologue code before all Bison
declarations, rather than after. For example, any definitions of feature
test macros like _GNU_SOURCE
or _POSIX_C_SOURCE
should appear
before all Bison declarations, as feature test macros can affect the
behavior of Bison-generated #include
directives.