Manual
Manual
Manual
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 3: Bison Grammar Files 41<br />
3 Bison Grammar Files<br />
Bison takes as input a context-free grammar specification and produces a C-language function<br />
that recognizes correct instances of the grammar.<br />
The Bison grammar input file conventionally has a name ending in ‘.y’. See Chapter 9<br />
[Invoking Bison], page 97.<br />
3.1 Outline of a Bison Grammar<br />
A Bison grammar file has four main sections, shown here with the appropriate delimiters:<br />
%{<br />
Prologue<br />
%}<br />
Bison declarations<br />
%%<br />
Grammar rules<br />
%%<br />
Epilogue<br />
Comments enclosed in ‘/* ... */’ may appear in any of the sections. As a GNU extension,<br />
‘//’ introduces a comment that continues until end of line.<br />
3.1.1 The prologue<br />
The Prologue section contains macro definitions and declarations of functions and variables<br />
that are used in the actions in the grammar rules. These are copied to the beginning of the<br />
parser file so that they precede the definition of yyparse. You can use ‘#include’ to get<br />
the declarations from a header file. If you don’t need any C declarations, you may omit the<br />
‘%{’ and ‘%}’ delimiters that bracket this section.<br />
The Prologue section is terminated by the the first occurrence of ‘%}’ that is outside a<br />
comment, a string literal, or a character constant.<br />
You may have more than one Prologue section, intermixed with the Bison declarations.<br />
This allows you to have C and Bison declarations that refer to each other. For example, the<br />
%union declaration may use types defined in a header file, and you may wish to prototype<br />
functions that take arguments of type YYSTYPE. This can be done with two Prologue blocks,<br />
one before and one after the %union declaration.<br />
%{<br />
#include <br />
#include "ptypes.h"<br />
%}<br />
%union {<br />
long int n;<br />
tree t; /* tree is defined in ‘ptypes.h’. */<br />
}<br />
%{