]> git.saurik.com Git - bison.git/blobdiff - doc/bison.texinfo
* doc/bison.texinfo: Fix some typos.
[bison.git] / doc / bison.texinfo
index 84a7dee83efe73980ae52419419cf68d1e36ff42..a8cdf473e7a3adaa6cb983c7dcf934804aa68532 100644 (file)
@@ -1998,7 +1998,9 @@ type for storing locations is not needed: we will use the type provided
 by default (@pxref{Location Type, ,Data Types of Locations}), which is a
 four member structure with the following integer fields:
 @code{first_line}, @code{first_column}, @code{last_line} and
-@code{last_column}.
+@code{last_column}.  By conventions, and in accordance with the GNU
+Coding Standards and common practice, the line and column count both
+start at 1.
 
 @node Ltcalc Rules
 @subsection Grammar Rules for @code{ltcalc}
@@ -2633,7 +2635,7 @@ they precede the definition of @code{yyparse}.  You can use
 don't need any C declarations, you may omit the @samp{%@{} and
 @samp{%@}} delimiters that bracket this section.
 
-The @var{Prologue} section is terminated by the the first occurrence
+The @var{Prologue} section is terminated by the first occurrence
 of @samp{%@}} that is outside a comment, a string literal, or a
 character constant.
 
@@ -2664,6 +2666,37 @@ can be done with two @var{Prologue} blocks, one before and one after the
 @dots{}
 @end smallexample
 
+@findex %before-header
+@findex %start-header
+@findex %after-header
+If you've instructed Bison to generate a header file (@pxref{Table of Symbols,
+,%defines}), you probably want @code{#include "ptypes.h"} to appear
+in that header file as well.
+In that case, use @code{%before-header}, @code{%start-header}, and
+@code{%after-header} instead of @var{Prologue} sections
+(@pxref{Table of Symbols, ,%start-header}):
+
+@smallexample
+%before-header @{
+  #include <stdio.h>
+@}
+
+%start-header @{
+  #include "ptypes.h"
+@}
+%union @{
+  long int n;
+  tree t;  /* @r{@code{tree} is defined in @file{ptypes.h}.} */
+@}
+
+%after-header @{
+  static void print_token_value (FILE *, int, YYSTYPE);
+  #define YYPRINT(F, N, L) print_token_value (F, N, L)
+@}
+
+@dots{}
+@end smallexample
+
 @node Bison Declarations
 @subsection The Bison Declarations Section
 @cindex Bison declarations (introduction)
@@ -3059,7 +3092,8 @@ the semantic values of all language constructs.  This was true in the
 @acronym{RPN} and infix calculator examples (@pxref{RPN Calc, ,Reverse Polish
 Notation Calculator}).
 
-Bison's default is to use type @code{int} for all semantic values.  To
+Bison normally uses the type @code{int} for semantic values if your
+program uses the same data type for all language constructs.  To
 specify some other type, define @code{YYSTYPE} as a macro, like this:
 
 @example
@@ -3086,9 +3120,11 @@ requires you to do two things:
 
 @itemize @bullet
 @item
-Specify the entire collection of possible data types, with the
+Specify the entire collection of possible data types, either by using the
 @code{%union} Bison declaration (@pxref{Union Decl, ,The Collection of
-Value Types}).
+Value Types}), or by using a @code{typedef} or a @code{#define} to
+define @code{YYSTYPE} to be a union type whose member names are
+the type tags.
 
 @item
 Choose one of those types for each symbol (terminal or nonterminal) for
@@ -3312,8 +3348,8 @@ it might discard the previous semantic context @code{$<context>5} without
 restoring it.
 Thus, @code{$<context>5} needs a destructor (@pxref{Destructor Decl, , Freeing
 Discarded Symbols}).
-However, Bison currently provides no means to declare a destructor for a
-mid-rule action's semantic value.
+However, Bison currently provides no means to declare a destructor specific to
+a particular mid-rule action's semantic value.
 
 One solution is to bury the mid-rule action inside a nonterminal symbol and to
 declare a destructor for that symbol:
@@ -3467,7 +3503,7 @@ since all tokens and groupings always use the same type.
 
 You can specify the type of locations by defining a macro called
 @code{YYLTYPE}, just as you can specify the semantic value type by
-defining @code{YYSTYPE} (@pxref{Value Type}).
+defining a @code{YYSTYPE} macro (@pxref{Value Type}).
 When @code{YYLTYPE} is not defined, Bison uses a default structure type with
 four members:
 
@@ -3481,6 +3517,9 @@ typedef struct YYLTYPE
 @} YYLTYPE;
 @end example
 
+At the beginning of the parsing, Bison initializes all these fields to 1
+for @code{yylloc}.
+
 @node Actions and Locations
 @subsection Actions and Locations
 @cindex location actions
@@ -3859,6 +3898,35 @@ only the first @code{%union} declaration can specify a tag.
 Note that, unlike making a @code{union} declaration in C, you need not write
 a semicolon after the closing brace.
 
+Instead of @code{%union}, you can define and use your own union type
+@code{YYSTYPE} if your grammar contains at least one
+@samp{<@var{type}>} tag.  For example, you can put the following into
+a header file @file{parser.h}:
+
+@example
+@group
+union YYSTYPE @{
+  double val;
+  symrec *tptr;
+@};
+typedef union YYSTYPE YYSTYPE;
+@end group
+@end example
+
+@noindent
+and then your grammar can use the following
+instead of @code{%union}:
+
+@example
+@group
+%@{
+#include "parser.h"
+%@}
+%type <val> expr
+%token <tptr> ID
+@end group
+@end example
+
 @node Type Decl
 @subsection Nonterminal Symbols
 @cindex declaring value types, nonterminals
@@ -3939,26 +4007,40 @@ symbol is automatically discarded.
 Invoke the braced @var{code} whenever the parser discards one of the
 @var{symbols}.
 Within @var{code}, @code{$$} designates the semantic value associated
-with the discarded symbol.  The additional parser parameters are also
-available (@pxref{Parser Function, , The Parser Function
-@code{yyparse}}).
+with the discarded symbol, and @code{@@$} designates its location.
+The additional parser parameters are also available (@pxref{Parser Function, ,
+The Parser Function @code{yyparse}}).
+@end deffn
+
+@deffn {Directive} %destructor @{ @var{code} @}
+@cindex default %destructor
+Invoke the braced @var{code} whenever the parser discards any user-declared
+grammar symbol for which the user has not specifically declared any
+@code{%destructor}.
+This is known as the default @code{%destructor}.
+As in the previous form, @code{$$}, @code{@@$}, and the additional parser
+parameters are available.
 @end deffn
 
 For instance:
 
 @smallexample
-%union
-@{
-  char *string;
-@}
-%token <string> STRING
-%type  <string> string
-%destructor @{ free ($$); @} STRING string
+%union @{ char *string; @}
+%token <string> STRING1
+%token <string> STRING2
+%type  <string> string1
+%type  <string> string2
+%destructor @{ free ($$); @}
+%destructor @{ free ($$); printf ("%d", @@$.first_line); @} STRING1 string1
 @end smallexample
 
 @noindent
-guarantees that when a @code{STRING} or a @code{string} is discarded,
-its associated memory will be freed.
+guarantees that, when the parser discards any user-declared symbol, it passes
+its semantic value to @code{free}.
+However, when the parser discards a @code{STRING1} or a @code{string1}, it also
+prints its line number to @code{stdout}.
+It performs only the second @code{%destructor} in this case, so it invokes
+@code{free} only once.
 
 @sp 1
 
@@ -4176,10 +4258,13 @@ names defined in the grammar as well as a few other declarations.
 If the parser output file is named @file{@var{name}.c} then this file
 is named @file{@var{name}.h}.
 
-Unless @code{YYSTYPE} is already defined as a macro, the output header
-declares @code{YYSTYPE}.  Therefore, if you are using a @code{%union}
+For C parsers, the output header declares @code{YYSTYPE} unless
+@code{YYSTYPE} is already defined as a macro or you have used a
+@code{<@var{type}>} tag without using @code{%union}.
+Therefore, if you are using a @code{%union}
 (@pxref{Multiple Types, ,More Than One Value Type}) with components that
 require other definitions, or if you have defined a @code{YYSTYPE} macro
+or type definition
 (@pxref{Value Type, ,Data Types of Semantic Values}), you need to
 arrange for these definitions to be propagated to all modules, e.g., by
 putting them in a prerequisite header that is included both by your
@@ -4191,7 +4276,7 @@ Parser}.
 
 If you have also used locations, the output header declares
 @code{YYLTYPE} and @code{yylloc} using a protocol similar to that of
-@code{YYSTYPE} and @code{yylval}.  @xref{Locations, ,Tracking
+the @code{YYSTYPE} macro and @code{yylval}.  @xref{Locations, ,Tracking
 Locations}.
 
 This output file is normally essential if you wish to put the definition
@@ -4199,6 +4284,12 @@ of @code{yylex} in a separate source file, because @code{yylex}
 typically needs to be able to refer to the above-mentioned declarations
 and to the token type codes.  @xref{Token Values, ,Semantic Values of
 Tokens}.
+
+@findex %start-header
+@findex %end-header
+If you have declared @code{%start-header} or @code{%end-header}, the output
+header also contains their code.
+@xref{Table of Symbols, ,%start-header}.
 @end deffn
 
 @deffn {Directive} %destructor
@@ -6673,7 +6764,7 @@ The trace facility outputs messages with macro calls of the form
 @var{format} and @var{args} are the usual @code{printf} format and
 arguments.  If you define @code{YYDEBUG} to a nonzero value but do not
 define @code{YYFPRINTF}, @code{<stdio.h>} is automatically included
-and @code{YYPRINTF} is defined to @code{fprintf}.
+and @code{YYFPRINTF} is defined to @code{fprintf}.
 
 Once you have compiled the program with trace facilities, the way to
 request a trace is to store a nonzero value in the variable @code{yydebug}.
@@ -6830,9 +6921,12 @@ different diagnostics to be generated, and may change behavior in
 other minor ways.  Most importantly, imitate Yacc's output
 file name conventions, so that the parser output file is called
 @file{y.tab.c}, and the other outputs are called @file{y.output} and
-@file{y.tab.h}.  Thus, the following shell script can substitute
-for Yacc, and the Bison distribution contains such a script for
-compatibility with @acronym{POSIX}:
+@file{y.tab.h}.
+Also, if generating an @acronym{LALR}(1) parser in C, generate @code{#define}
+statements in addition to an @code{enum} to associate token numbers with token
+names.
+Thus, the following shell script can substitute for Yacc, and the Bison
+distribution contains such a script for compatibility with @acronym{POSIX}:
 
 @example
 #! /bin/sh
@@ -6901,7 +6995,7 @@ Same as above, but save in the file @var{defines-file}.
 
 @item -b @var{file-prefix}
 @itemx --file-prefix=@var{prefix}
-Pretend that @code{%file-prefix} was specified, i.e, specify prefix to use
+Pretend that @code{%file-prefix} was specified, i.e., specify prefix to use
 for all Bison output file names.  @xref{Decl Summary}.
 
 @item -r @var{things}
@@ -6925,7 +7019,7 @@ the full set of items for each state, instead of its core only.
 
 @item -v
 @itemx --verbose
-Pretend that @code{%verbose} was specified, i.e, write an extra output
+Pretend that @code{%verbose} was specified, i.e., write an extra output
 file containing verbose descriptions of the grammar and
 parser.  @xref{Decl Summary}.
 
@@ -7326,7 +7420,6 @@ public:
 @noindent
 To encapsulate the coordination with the Flex scanner, it is useful to
 have two members function to open and close the scanning phase.
-members.
 
 @comment file: calc++-driver.hh
 @example
@@ -7425,19 +7518,21 @@ the grammar for.
 @end example
 
 @noindent
+@findex %start-header
 Then come the declarations/inclusions needed to define the
 @code{%union}.  Because the parser uses the parsing driver and
 reciprocally, both cannot include the header of the other.  Because the
 driver's header needs detailed knowledge about the parser class (in
 particular its inner types), it is the parser's header which will simply
 use a forward declaration of the driver.
+@xref{Table of Symbols, ,%start-header}.
 
 @comment file: calc++-parser.yy
 @example
-%@{
+%start-header @{
 # include <string>
 class calcxx_driver;
-%@}
+@}
 @end example
 
 @noindent
@@ -7493,15 +7588,15 @@ them.
 @end example
 
 @noindent
-The code between @samp{%@{} and @samp{%@}} after the introduction of the
-@samp{%union} is output in the @file{*.cc} file; it needs detailed
-knowledge about the driver.
+@findex %after-header
+The code between @samp{%after-header @{} and @samp{@}} is output in the
+@file{*.cc} file; it needs detailed knowledge about the driver.
 
 @comment file: calc++-parser.yy
 @example
-%@{
+%after-header @{
 # include "calc++-driver.hh"
-%@}
+@}
 @end example
 
 
@@ -8169,6 +8264,86 @@ $end}, where @var{start} is the start symbol.  @xref{Start Decl, , The
 Start-Symbol}.  It cannot be used in the grammar.
 @end deffn
 
+@deffn {Directive} %after-header @{@var{code}@}
+Specifies code to be inserted into the code file after the contents of the
+header file.
+@xref{Table of Symbols, ,%start-header}.
+@end deffn
+
+@deffn {Directive} %before-header @{@var{code}@}
+Specifies code to be inserted into the code file before the contents of the
+header file.
+@xref{Table of Symbols, ,%start-header}.
+@end deffn
+
+@deffn {Directive} %end-header @{@var{code}@}
+Specifies code to be inserted both into the header file (if generated;
+@pxref{Table of Symbols, ,%defines}) and into the code file after any
+Bison-generated definitions.
+@xref{Table of Symbols, ,%start-header}.
+@end deffn
+
+@deffn {Directive} %start-header @{@var{code}@}
+Specifies code to be inserted both into the header file (if generated;
+@pxref{Table of Symbols, ,%defines}) and into the code file before any
+Bison-generated definitions.
+
+@cindex Prologue
+@findex %before-header
+@findex %union
+@findex %end-header
+@findex %after-header
+For example, the following declaration order in the grammar file reflects the
+order in which Bison will output these code blocks.  However, you are free to
+declare these code blocks in your grammar file in whatever order is most
+convenient for you:
+
+@smallexample
+%before-header @{
+  /* Bison treats this block like a pre-prologue block: it inserts it
+   * into the code file before the contents of the header file.  It
+   * does *not* insert it into the header file.  This is a good place
+   * to put #include's that you want at the top of your code file.  A
+   * common example is `#include "system.h"'.  */
+@}
+%start-header @{
+  /* Bison inserts this block into both the header file and the code
+   * file.  In both files, the point of insertion is before any
+   * Bison-generated token, semantic type, location type, and class
+   * definitions.  This is a good place to define %union
+   * dependencies, for example.  */
+@}
+%union @{
+  /* Unlike the traditional Yacc prologue blocks, the output order
+   * for the %*-header blocks is not affected by their declaration
+   * position relative to any %union in the grammar file.  */
+@}
+%end-header @{
+  /* Bison inserts this block into both the header file and the code
+   * file.  In both files, the point of insertion is after the
+   * Bison-generated definitions.  This is a good place to declare or
+   * define public functions or data structures that depend on the
+   * Bison-generated definitions.  */
+@}
+%after-header @{
+  /* Bison treats this block like a post-prologue block: it inserts
+   * it into the code file after the contents of the header file.  It
+   * does *not* insert it into the header file.  This is a good place
+   * to declare or define internal functions or data structures that
+   * depend on the Bison-generated definitions.  */
+@}
+@end smallexample
+
+If you have multiple occurrences of any one of the above declarations, Bison
+will concatenate the contents in declaration order.
+
+@xref{Prologue, ,The Prologue}.
+@end deffn
+
+@deffn {Directive} %debug
+Equip the parser for debugging.  @xref{Decl Summary}.
+@end deffn
+
 @deffn {Directive} %debug
 Equip the parser for debugging.  @xref{Decl Summary}.
 @end deffn
@@ -8709,7 +8884,7 @@ grammatically indivisible.  The piece of text it represents is a token.
 @c LocalWords: Johnstone Shamsa Sadaf Hussain Tomita TR uref YYMAXDEPTH
 @c LocalWords: YYINITDEPTH stmnts ref stmnt initdcl maybeasm VCG notype
 @c LocalWords: hexflag STR exdent itemset asis DYYDEBUG YYFPRINTF args
-@c LocalWords: YYPRINTF infile ypp yxx outfile itemx vcg tex leaderfill
+@c LocalWords: infile ypp yxx outfile itemx vcg tex leaderfill
 @c LocalWords: hbox hss hfill tt ly yyin fopen fclose ofirst gcc ll
 @c LocalWords: yyrestart nbar yytext fst snd osplit ntwo strdup AST
 @c LocalWords: YYSTACK DVI fdl printindex