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.
@smallexample
%@{
+ #define _GNU_SOURCE
#include <stdio.h>
#include "ptypes.h"
%@}
@dots{}
@end smallexample
+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 %before-header
@findex %start-header
@findex %after-header
@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
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
%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
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
@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}.
@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}
@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}.
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
@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, \
@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
assignments: assignments assignment @{@}
| /* Nothing. */ @{@};
-assignment: "identifier" ":=" exp @{ driver.variables[*$1] = $3; @};
+assignment:
+ "identifier" ":=" exp
+ @{ driver.variables[*$1] = $3; delete $1; @};
%left '+' '-';
%left '*' '/';
| 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
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
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}.
@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: YYPRINTF 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