]> git.saurik.com Git - bison.git/blobdiff - doc/bison.texinfo
djgpp/config.sed: Adjust config.sed for the use of autoconf 2.60.
[bison.git] / doc / bison.texinfo
index a8cdf473e7a3adaa6cb983c7dcf934804aa68532..bcff2ea5a0732a4a4a16b319006b54ac66077bf7 100644 (file)
@@ -2649,6 +2649,7 @@ can be done with two @var{Prologue} blocks, one before and one after the
 
 @smallexample
 %@{
+  #define _GNU_SOURCE
   #include <stdio.h>
   #include "ptypes.h"
 %@}
@@ -2666,22 +2667,24 @@ 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
+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 @code{_GNU_SOURCE} or
+@code{_POSIX_C_SOURCE} should appear before all Bison declarations, as
+feature test macros can affect the behavior of Bison-generated
+@code{#include} directives.
+
+@findex %requires
+@findex %code
 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}):
+In that case, use @code{%requires}, @code{%provides}, and
+@code{%code} instead of @var{Prologue} sections
+(@pxref{Table of Symbols, ,%requires}):
 
 @smallexample
-%before-header @{
-  #include <stdio.h>
-@}
-
-%start-header @{
+%requires @{
   #include "ptypes.h"
 @}
 %union @{
@@ -2689,7 +2692,9 @@ In that case, use @code{%before-header}, @code{%start-header}, and
   tree t;  /* @r{@code{tree} is defined in @file{ptypes.h}.} */
 @}
 
-%after-header @{
+%code @{
+  #include <stdio.h>
+
   static void print_token_value (FILE *, int, YYSTYPE);
   #define YYPRINT(F, N, L) print_token_value (F, N, L)
 @}
@@ -3986,6 +3991,7 @@ For instance, if your locations use a file name, you may use
 @subsection Freeing Discarded Symbols
 @cindex freeing discarded symbols
 @findex %destructor
+@findex %symbol-default
 
 During error recovery (@pxref{Error Recovery}), symbols already pushed
 on the stack and tokens coming from the rest of the file are discarded
@@ -4010,18 +4016,24 @@ Within @var{code}, @code{$$} designates the semantic value associated
 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.
+When a symbol is listed among @var{symbols}, its @code{%destructor} is called a
+per-symbol @code{%destructor}.
+You may also define a per-type @code{%destructor} by listing a semantic type
+among @var{symbols}.
+In that case, the parser will invoke this @var{code} whenever it discards any
+grammar symbol that has that semantic type unless that symbol has its own
+per-symbol @code{%destructor}.
+
+Finally, you may define a default @code{%destructor} by placing
+@code{%symbol-default} in the @var{symbols} list of exactly one
+@code{%destructor} declaration in your grammar file.
+In that case, the parser will invoke the associated @var{code} whenever it
+discards any user-defined grammar symbol for which there is no per-type or
+per-symbol @code{%destructor}.
 @end deffn
 
+@noindent
 For instance:
 
 @smallexample
@@ -4030,18 +4042,45 @@ For instance:
 %token <string> STRING2
 %type  <string> string1
 %type  <string> string2
-%destructor @{ free ($$); @}
+%union @{ char character; @}
+%token <character> CHR
+%type  <character> chr
+%destructor @{ free ($$); @} %symbol-default
 %destructor @{ free ($$); printf ("%d", @@$.first_line); @} STRING1 string1
+%destructor @{ @} <character>
 @end smallexample
 
 @noindent
-guarantees that, when the parser discards any user-declared symbol, it passes
-its semantic value to @code{free}.
+guarantees that, when the parser discards any user-defined symbol that has a
+semantic type tag other than @code{<character>}, 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.
 
+Notice that a Bison-generated parser invokes the default @code{%destructor}
+only for user-defined as opposed to Bison-defined symbols.
+For example, the parser will not invoke it for the special Bison-defined
+symbols @code{$accept}, @code{$undefined}, or @code{$end} (@pxref{Table of
+Symbols, ,Bison Symbols}), none of which you can reference in your grammar.
+It also will not invoke it for the @code{error} token (@pxref{Table of Symbols,
+,error}), which is always defined by Bison regardless of whether you reference
+it in your grammar.
+However, it will invoke it for the end token (token 0) if you redefine it from
+@code{$end} to, for example, @code{END}:
+
+@smallexample
+%token END 0
+@end smallexample
+
+@ignore
+@noindent
+In the future, it may be possible to redefine the @code{error} token as a
+nonterminal that captures the discarded symbols.
+In that case, the parser will invoke the default destructor for it as well.
+@end ignore
+
 @sp 1
 
 @cindex discarded symbols
@@ -4285,11 +4324,11 @@ 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
+@findex %requires
+@findex %provides
+If you have declared @code{%requires} or @code{%provides}, the output
 header also contains their code.
-@xref{Table of Symbols, ,%start-header}.
+@xref{Table of Symbols, ,%requires}.
 @end deffn
 
 @deffn {Directive} %destructor
@@ -6332,8 +6371,7 @@ As documented elsewhere (@pxref{Algorithm, ,The Bison Parser Algorithm})
 Bison parsers are @dfn{shift/reduce automata}.  In some cases (much more
 frequent than one would hope), looking at this automaton is required to
 tune or simply fix a parser.  Bison provides two different
-representation of it, either textually or graphically (as a @acronym{VCG}
-file).
+representation of it, either textually or graphically (as a DOT file).
 
 The textual file is generated when the options @option{--report} or
 @option{--verbose} are specified, see @xref{Invocation, , Invoking
@@ -7031,10 +7069,11 @@ The other output files' names are constructed from @var{file} as
 described under the @samp{-v} and @samp{-d} options.
 
 @item -g
-Output a @acronym{VCG} definition of the @acronym{LALR}(1) grammar
-automaton computed by Bison.  If the grammar file is @file{foo.y}, the
-@acronym{VCG} output file will
-be @file{foo.vcg}.
+Output a graphical representation of the @acronym{LALR}(1) grammar
+automaton computed by Bison, in @uref{http://www.graphviz.org/, Graphviz}
+@uref{http://www.graphviz.org/doc/info/lang.html, @acronym{DOT}} format.
+If the grammar file is @file{foo.y}, the output file will
+be @file{foo.dot}.
 
 @item --graph=@var{graph-file}
 The behavior of @var{--graph} is the same than @samp{-g}.  The only
@@ -7389,8 +7428,8 @@ factor both as follows.
 
 @comment file: calc++-driver.hh
 @example
-// Announce to Flex the prototype we want for lexing function, ...
-# define YY_DECL                                       \
+// Tell Flex the lexer's prototype ...
+# define YY_DECL                                        \
   yy::calcxx_parser::token_type                         \
   yylex (yy::calcxx_parser::semantic_type* yylval,      \
          yy::calcxx_parser::location_type* yylloc,      \
@@ -7518,18 +7557,18 @@ the grammar for.
 @end example
 
 @noindent
-@findex %start-header
+@findex %requires
 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}.
+@xref{Table of Symbols, ,%requires}.
 
 @comment file: calc++-parser.yy
 @example
-%start-header @{
+%requires @{
 # include <string>
 class calcxx_driver;
 @}
@@ -7588,13 +7627,13 @@ them.
 @end example
 
 @noindent
-@findex %after-header
-The code between @samp{%after-header @{} and @samp{@}} is output in the
+@findex %code
+The code between @samp{%code @{} and @samp{@}} is output in the
 @file{*.cc} file; it needs detailed knowledge about the driver.
 
 @comment file: calc++-parser.yy
 @example
-%after-header @{
+%code @{
 # include "calc++-driver.hh"
 @}
 @end example
@@ -7641,7 +7680,9 @@ unit: assignments exp  @{ driver.result = $2; @};
 assignments: assignments assignment @{@}
            | /* Nothing.  */        @{@};
 
-assignment: "identifier" ":=" exp @{ driver.variables[*$1] = $3; @};
+assignment:
+     "identifier" ":=" exp
+       @{ driver.variables[*$1] = $3; delete $1; @};
 
 %left '+' '-';
 %left '*' '/';
@@ -7649,7 +7690,7 @@ exp: exp '+' exp   @{ $$ = $1 + $3; @}
    | exp '-' exp   @{ $$ = $1 - $3; @}
    | exp '*' exp   @{ $$ = $1 * $3; @}
    | exp '/' exp   @{ $$ = $1 / $3; @}
-   | "identifier"  @{ $$ = driver.variables[*$1]; @}
+   | "identifier"  @{ $$ = driver.variables[*$1]; delete $1; @}
    | "number"      @{ $$ = $1; @};
 %%
 @end example
@@ -7810,8 +7851,8 @@ main (int argc, char *argv[])
       driver.trace_scanning = true;
     else
       @{
-       driver.parse (*argv);
-       std::cout << driver.result << std::endl;
+        driver.parse (*argv);
+        std::cout << driver.result << std::endl;
       @}
 @}
 @end example
@@ -8264,49 +8305,35 @@ $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}@}
+@deffn {Directive} %code @{@var{code}@}
 Specifies code to be inserted into the code file after the contents of the
 header file.
-@xref{Table of Symbols, ,%start-header}.
+@xref{Table of Symbols, ,%requires}.
 @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}@}
+@deffn {Directive} %provides @{@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}.
+@xref{Table of Symbols, ,%requires}.
 @end deffn
 
-@deffn {Directive} %start-header @{@var{code}@}
+@deffn {Directive} %requires @{@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
+@findex %provides
+@findex %code
 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 @{
+%requires @{
   /* 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
@@ -8315,17 +8342,17 @@ convenient for you:
 @}
 %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.  */
+   * for %requires, %provides or %code blocks is not affected by their
+   * declaration position relative to any %union in the grammar file.  */
 @}
-%end-header @{
+%provides @{
   /* 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 @{
+%code @{
   /* 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
@@ -8483,6 +8510,11 @@ Bison declaration to specify the start symbol.  @xref{Start Decl, ,The
 Start-Symbol}.
 @end deffn
 
+@deffn {Directive} %symbol-default
+Used to declare a default @code{%destructor} or default @code{%printer}.
+@xref{Destructor Decl, , Freeing Discarded Symbols}.
+@end deffn
+
 @deffn {Directive} %token
 Bison declaration to declare token(s) without specifying precedence.
 @xref{Token Decl, ,Token Type Names}.
@@ -8882,9 +8914,9 @@ grammatically indivisible.  The piece of text it represents is a token.
 @c LocalWords: strncmp intval tindex lvalp locp llocp typealt YYBACKUP
 @c LocalWords: YYEMPTY YYEOF YYRECOVERING yyclearin GE def UMINUS maybeword
 @c LocalWords: Johnstone Shamsa Sadaf Hussain Tomita TR uref YYMAXDEPTH
-@c LocalWords: YYINITDEPTH stmnts ref stmnt initdcl maybeasm VCG notype
+@c LocalWords: YYINITDEPTH stmnts ref stmnt initdcl maybeasm notype
 @c LocalWords: hexflag STR exdent itemset asis DYYDEBUG YYFPRINTF args
-@c LocalWords: infile ypp yxx outfile itemx vcg tex leaderfill
+@c LocalWords: infile ypp yxx outfile itemx 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