188 |
|
|
189 |
Reverse Polish Notation Calculator |
Reverse Polish Notation Calculator |
190 |
|
|
191 |
* Decls: Rpcalc Decls. Bison and C declarations for rpcalc. |
* Decls: Rpcalc Decls. Prologue (declarations) for rpcalc. |
192 |
* Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation. |
* Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation. |
193 |
* Lexer: Rpcalc Lexer. The lexical analyzer. |
* Lexer: Rpcalc Lexer. The lexical analyzer. |
194 |
* Main: Rpcalc Main. The controlling function. |
* Main: Rpcalc Main. The controlling function. |
220 |
|
|
221 |
Outline of a Bison Grammar |
Outline of a Bison Grammar |
222 |
|
|
223 |
* C Declarations:: Syntax and usage of the C declarations section. |
* Prologue:: Syntax and usage of the prologue (declarations section). |
224 |
* Bison Declarations:: Syntax and usage of the Bison declarations section. |
* Bison Declarations:: Syntax and usage of the Bison declarations section. |
225 |
* Grammar Rules:: Syntax and usage of the grammar rules section. |
* Grammar Rules:: Syntax and usage of the grammar rules section. |
226 |
* C Code:: Syntax and usage of the additional C code section. |
* Epilogue:: Syntax and usage of the epilogue (additional code section). |
227 |
|
|
228 |
Defining Language Semantics |
Defining Language Semantics |
229 |
|
|
736 |
|
|
737 |
@example |
@example |
738 |
%@{ |
%@{ |
739 |
@var{C declarations} |
@var{Prologue (declarations)} |
740 |
%@} |
%@} |
741 |
|
|
742 |
@var{Bison declarations} |
@var{Bison declarations} |
744 |
%% |
%% |
745 |
@var{Grammar rules} |
@var{Grammar rules} |
746 |
%% |
%% |
747 |
@var{Additional C code} |
@var{Epilogue (additional code)} |
748 |
@end example |
@end example |
749 |
|
|
750 |
@noindent |
@noindent |
751 |
The @samp{%%}, @samp{%@{} and @samp{%@}} are punctuation that appears |
The @samp{%%}, @samp{%@{} and @samp{%@}} are punctuation that appears |
752 |
in every Bison grammar file to separate the sections. |
in every Bison grammar file to separate the sections. |
753 |
|
|
754 |
The C declarations may define types and variables used in the actions. |
The prologue may define types and variables used in the actions. You can |
755 |
You can also use preprocessor commands to define macros used there, and use |
also use preprocessor commands to define macros used there, and use |
756 |
@code{#include} to include header files that do any of these things. |
@code{#include} to include header files that do any of these things. |
757 |
|
|
758 |
The Bison declarations declare the names of the terminal and nonterminal |
The Bison declarations declare the names of the terminal and nonterminal |
762 |
The grammar rules define how to construct each nonterminal symbol from its |
The grammar rules define how to construct each nonterminal symbol from its |
763 |
parts. |
parts. |
764 |
|
|
765 |
The additional C code can contain any C code you want to use. Often the |
The epilogue can contain any code you want to use. Often the definition of |
766 |
definition of the lexical analyzer @code{yylex} goes here, plus subroutines |
the lexical analyzer @code{yylex} goes here, plus subroutines called by the |
767 |
called by the actions in the grammar rules. In a simple program, all the |
actions in the grammar rules. In a simple program, all the rest of the |
768 |
rest of the program can go here. |
program can go here. |
769 |
|
|
770 |
@node Examples, Grammar File, Concepts, Top |
@node Examples, Grammar File, Concepts, Top |
771 |
@chapter Examples |
@chapter Examples |
812 |
@samp{.y} extension is a convention used for Bison input files. |
@samp{.y} extension is a convention used for Bison input files. |
813 |
|
|
814 |
@menu |
@menu |
815 |
* Decls: Rpcalc Decls. Bison and C declarations for rpcalc. |
* Decls: Rpcalc Decls. Prologue (declarations) for rpcalc. |
816 |
* Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation. |
* Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation. |
817 |
* Lexer: Rpcalc Lexer. The lexical analyzer. |
* Lexer: Rpcalc Lexer. The lexical analyzer. |
818 |
* Main: Rpcalc Main. The controlling function. |
* Main: Rpcalc Main. The controlling function. |
840 |
%% /* Grammar rules and actions follow */ |
%% /* Grammar rules and actions follow */ |
841 |
@end example |
@end example |
842 |
|
|
843 |
The C declarations section (@pxref{C Declarations, ,The C Declarations Section}) contains two |
The declarations section (@pxref{Prologue, , The prologue}) contains two |
844 |
preprocessor directives. |
preprocessor directives. |
845 |
|
|
846 |
The @code{#define} directive defines the macro @code{YYSTYPE}, thus |
The @code{#define} directive defines the macro @code{YYSTYPE}, thus |
1161 |
arrange all the source code in one or more source files. For such a |
arrange all the source code in one or more source files. For such a |
1162 |
simple example, the easiest thing is to put everything in one file. The |
simple example, the easiest thing is to put everything in one file. The |
1163 |
definitions of @code{yylex}, @code{yyerror} and @code{main} go at the |
definitions of @code{yylex}, @code{yyerror} and @code{main} go at the |
1164 |
end, in the ``additional C code'' section of the file (@pxref{Grammar |
end, in the epilogue of the file |
1165 |
Layout, ,The Overall Layout of a Bison Grammar}). |
(@pxref{Grammar Layout, ,The Overall Layout of a Bison Grammar}). |
1166 |
|
|
1167 |
For a large project, you would probably have several source files, and use |
For a large project, you would probably have several source files, and use |
1168 |
@code{make} to arrange to recompile them. |
@code{make} to arrange to recompile them. |
1779 |
|
|
1780 |
@example |
@example |
1781 |
%@{ |
%@{ |
1782 |
@var{C declarations} |
@var{Prologue} |
1783 |
%@} |
%@} |
1784 |
|
|
1785 |
@var{Bison declarations} |
@var{Bison declarations} |
1788 |
@var{Grammar rules} |
@var{Grammar rules} |
1789 |
%% |
%% |
1790 |
|
|
1791 |
@var{Additional C code} |
@var{Epilogue} |
1792 |
@end example |
@end example |
1793 |
|
|
1794 |
Comments enclosed in @samp{/* @dots{} */} may appear in any of the sections. |
Comments enclosed in @samp{/* @dots{} */} may appear in any of the sections. |
1795 |
|
|
1796 |
@menu |
@menu |
1797 |
* C Declarations:: Syntax and usage of the C declarations section. |
* Prologue:: Syntax and usage of the prologue. |
1798 |
* Bison Declarations:: Syntax and usage of the Bison declarations section. |
* Bison Declarations:: Syntax and usage of the Bison declarations section. |
1799 |
* Grammar Rules:: Syntax and usage of the grammar rules section. |
* Grammar Rules:: Syntax and usage of the grammar rules section. |
1800 |
* C Code:: Syntax and usage of the additional C code section. |
* Epilogue:: Syntax and usage of the epilogue. |
1801 |
@end menu |
@end menu |
1802 |
|
|
1803 |
@node C Declarations, Bison Declarations, , Grammar Outline |
@node Prologue, Bison Declarations, , Grammar Outline |
1804 |
@subsection The C Declarations Section |
@subsection The prologue |
1805 |
@cindex C declarations section |
@cindex declarations section |
1806 |
@cindex declarations, C |
@cindex Prologue |
1807 |
|
@cindex declarations |
1808 |
|
|
1809 |
The @var{C declarations} section contains macro definitions and |
The @var{prologue} section contains macro definitions and |
1810 |
declarations of functions and variables that are used in the actions in the |
declarations of functions and variables that are used in the actions in the |
1811 |
grammar rules. These are copied to the beginning of the parser file so |
grammar rules. These are copied to the beginning of the parser file so |
1812 |
that they precede the definition of @code{yyparse}. You can use |
that they precede the definition of @code{yyparse}. You can use |
1814 |
need any C declarations, you may omit the @samp{%@{} and @samp{%@}} |
need any C declarations, you may omit the @samp{%@{} and @samp{%@}} |
1815 |
delimiters that bracket this section. |
delimiters that bracket this section. |
1816 |
|
|
1817 |
@node Bison Declarations, Grammar Rules, C Declarations, Grammar Outline |
@node Bison Declarations, Grammar Rules, Prologue, Grammar Outline |
1818 |
@subsection The Bison Declarations Section |
@subsection The Bison Declarations Section |
1819 |
@cindex Bison declarations (introduction) |
@cindex Bison declarations (introduction) |
1820 |
@cindex declarations, Bison (introduction) |
@cindex declarations, Bison (introduction) |
1824 |
In some simple grammars you may not need any declarations. |
In some simple grammars you may not need any declarations. |
1825 |
@xref{Declarations, ,Bison Declarations}. |
@xref{Declarations, ,Bison Declarations}. |
1826 |
|
|
1827 |
@node Grammar Rules, C Code, Bison Declarations, Grammar Outline |
@node Grammar Rules, Epilogue, Bison Declarations, Grammar Outline |
1828 |
@subsection The Grammar Rules Section |
@subsection The Grammar Rules Section |
1829 |
@cindex grammar rules section |
@cindex grammar rules section |
1830 |
@cindex rules section for grammar |
@cindex rules section for grammar |
1836 |
@samp{%%} (which precedes the grammar rules) may never be omitted even |
@samp{%%} (which precedes the grammar rules) may never be omitted even |
1837 |
if it is the first thing in the file. |
if it is the first thing in the file. |
1838 |
|
|
1839 |
@node C Code, , Grammar Rules, Grammar Outline |
@node Epilogue, , Grammar Rules, Grammar Outline |
1840 |
@subsection The Additional C Code Section |
@subsection The epilogue |
1841 |
@cindex additional C code section |
@cindex additional C code section |
1842 |
|
@cindex epilogue |
1843 |
@cindex C code, section for additional |
@cindex C code, section for additional |
1844 |
|
|
1845 |
The @var{additional C code} section is copied verbatim to the end of the |
The @var{epilogue} is copied verbatim to the end of the parser file, just as |
1846 |
parser file, just as the @var{C declarations} section is copied to the |
the @var{prologue} is copied to the beginning. This is the most convenient |
1847 |
beginning. This is the most convenient place to put anything that you |
place to put anything that you want to have in the parser file but which need |
1848 |
want to have in the parser file but which need not come before the |
not come before the definition of @code{yyparse}. For example, the |
1849 |
definition of @code{yyparse}. For example, the definitions of |
definitions of @code{yylex} and @code{yyerror} often go here. |
1850 |
@code{yylex} and @code{yyerror} often go here. @xref{Interface, ,Parser |
@xref{Interface, ,Parser C-Language Interface}. |
|
C-Language Interface}. |
|
1851 |
|
|
1852 |
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 |
1853 |
from the grammar rules. |
from the grammar rules. |
1855 |
The Bison parser itself contains many static variables whose names start |
The Bison parser itself contains many static variables whose names start |
1856 |
with @samp{yy} and many macros whose names start with @samp{YY}. It is a |
with @samp{yy} and many macros whose names start with @samp{YY}. It is a |
1857 |
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 |
1858 |
manual) in the additional C code section of the grammar file. |
manual) in the epilogue of the grammar file. |
1859 |
|
|
1860 |
@node Symbols, Rules, Grammar Outline, Grammar File |
@node Symbols, Rules, Grammar Outline, Grammar File |
1861 |
@section Symbols, Terminal and Nonterminal |
@section Symbols, Terminal and Nonterminal |
2176 |
@end example |
@end example |
2177 |
|
|
2178 |
@noindent |
@noindent |
2179 |
This macro definition must go in the C declarations section of the grammar |
This macro definition must go in the prologue of the grammar file |
2180 |
file (@pxref{Grammar Outline, ,Outline of a Bison Grammar}). |
(@pxref{Grammar Outline, ,Outline of a Bison Grammar}). |
2181 |
|
|
2182 |
@node Multiple Types, Actions, Value Type, Semantics |
@node Multiple Types, Actions, Value Type, Semantics |
2183 |
@subsection More Than One Value Type |
@subsection More Than One Value Type |
3151 |
|
|
3152 |
Keep in mind that the parser uses many C identifiers starting with |
Keep in mind that the parser uses many C identifiers starting with |
3153 |
@samp{yy} and @samp{YY} for internal purposes. If you use such an |
@samp{yy} and @samp{YY} for internal purposes. If you use such an |
3154 |
identifier (aside from those in this manual) in an action or in additional |
identifier (aside from those in this manual) in an action or in epilogue |
3155 |
C code in the grammar file, you are likely to run into trouble. |
in the grammar file, you are likely to run into trouble. |
3156 |
|
|
3157 |
@menu |
@menu |
3158 |
* Parser Function:: How to call @code{yyparse} and what it returns. |
* Parser Function:: How to call @code{yyparse} and what it returns. |
4596 |
it is nonzero, all integers are parsed in hexadecimal, and tokens starting |
it is nonzero, all integers are parsed in hexadecimal, and tokens starting |
4597 |
with letters are parsed as integers if possible. |
with letters are parsed as integers if possible. |
4598 |
|
|
4599 |
The declaration of @code{hexflag} shown in the C declarations section of |
The declaration of @code{hexflag} shown in the prologue of the parser file |
4600 |
the parser file is needed to make it accessible to the actions |
is needed to make it accessible to the actions (@pxref{Prologue, ,The Prologue}). |
4601 |
(@pxref{C Declarations, ,The C Declarations Section}). You must also write the code in @code{yylex} |
You must also write the code in @code{yylex} to obey the flag. |
|
to obey the flag. |
|
4602 |
|
|
4603 |
@node Tie-in Recovery, , Lexical Tie-ins, Context Dependency |
@node Tie-in Recovery, , Lexical Tie-ins, Context Dependency |
4604 |
@section Lexical Tie-ins and Error Recovery |
@section Lexical Tie-ins and Error Recovery |
4666 |
runs, the @code{yydebug} parser-trace feature can help you figure out why. |
runs, the @code{yydebug} parser-trace feature can help you figure out why. |
4667 |
|
|
4668 |
To enable compilation of trace facilities, you must define the macro |
To enable compilation of trace facilities, you must define the macro |
4669 |
@code{YYDEBUG} when you compile the parser. You could use |
@code{YYDEBUG} when you compile the parser. You could use @samp{-DYYDEBUG=1} |
4670 |
@samp{-DYYDEBUG=1} as a compiler option or you could put @samp{#define |
as a compiler option or you could put @samp{#define YYDEBUG 1} in the prologue |
4671 |
YYDEBUG 1} in the C declarations section of the grammar file |
of the grammar file (@pxref{Prologue, , The Prologue}). Alternatively, use the |
4672 |
(@pxref{C Declarations, ,The C Declarations Section}). Alternatively, use the @samp{-t} option when |
@samp{-t} option when you run Bison (@pxref{Invocation, ,Invoking Bison}). |
4673 |
you run Bison (@pxref{Invocation, ,Invoking Bison}). We always define @code{YYDEBUG} so that |
We always define @code{YYDEBUG} so that debugging is always possible. |
|
debugging is always possible. |
|
4674 |
|
|
4675 |
The trace facility uses @code{stderr}, so you must add @w{@code{#include |
The trace facility uses @code{stderr}, so you must add |
4676 |
<stdio.h>}} to the C declarations section unless it is already there. |
@w{@code{#include <stdio.h>}} to the prologue unless it is already there. |
4677 |
|
|
4678 |
Once you have compiled the program with trace facilities, the way to |
Once you have compiled the program with trace facilities, the way to |
4679 |
request a trace is to store a nonzero value in the variable @code{yydebug}. |
request a trace is to store a nonzero value in the variable @code{yydebug}. |
5191 |
@table @samp |
@table @samp |
5192 |
@item %% |
@item %% |
5193 |
Delimiter used to separate the grammar rule section from the |
Delimiter used to separate the grammar rule section from the |
5194 |
Bison declarations section or the additional C code section. |
Bison declarations section or the epilogue. |
5195 |
@xref{Grammar Layout, ,The Overall Layout of a Bison Grammar}. |
@xref{Grammar Layout, ,The Overall Layout of a Bison Grammar}. |
5196 |
|
|
5197 |
@item %@{ %@} |
@item %@{ %@} |
5198 |
All code listed between @samp{%@{} and @samp{%@}} is copied directly to |
All code listed between @samp{%@{} and @samp{%@}} is copied directly to |
5199 |
the output file uninterpreted. Such code forms the ``C declarations'' |
the output file uninterpreted. Such code forms the prologue of the input |
5200 |
section of the input file. @xref{Grammar Outline, ,Outline of a Bison |
file. @xref{Grammar Outline, ,Outline of a Bison |
5201 |
Grammar}. |
Grammar}. |
5202 |
|
|
5203 |
@item /*@dots{}*/ |
@item /*@dots{}*/ |