X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/67a9768ed49978ce5c5d4d4636091c427ac0f3e6..148d66d8612b5e382495f43d672fbdeeee1d3802:/doc/bison.texinfo diff --git a/doc/bison.texinfo b/doc/bison.texinfo index e78f4346..38888287 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -34,7 +34,7 @@ This manual is for @acronym{GNU} Bison (version @value{VERSION}, @value{UPDATED}), the @acronym{GNU} parser generator. Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1995, 1998, -1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. +1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. @quotation Permission is granted to copy, distribute and/or modify this document @@ -2048,7 +2048,7 @@ exp : NUM @{ $$ = $1; @} @} @end group @group - | '-' exp %preg NEG @{ $$ = -$2; @} + | '-' exp %prec NEG @{ $$ = -$2; @} | exp '^' exp @{ $$ = pow ($1, $3); @} | '(' exp ')' @{ $$ = $2; @} @end group @@ -2681,14 +2681,21 @@ feature test macros can affect the behavior of Bison-generated @cindex Prologue Alternatives @findex %code -@findex %requires -@findex %provides -@findex %code-top +@findex %code requires +@findex %code provides +@findex %code top +(The prologue alternatives described here are experimental. +More user feedback will help to determine whether they should become permanent +features.) + The functionality of @var{Prologue} sections can often be subtle and inflexible. -As an alternative, Bison provides a set of more explicit directives: -@code{%code-top}, @code{%requires}, @code{%provides}, and @code{%code}. -@xref{Table of Symbols}. +As an alternative, Bison provides a %code directive with an explicit qualifier +field, which identifies the purpose of the code and thus the location(s) where +Bison should generate it. +For C/C++, the qualifier can be omitted for the default location, or it can be +@code{requires}, @code{provides}, or @code{top}. +@xref{Decl Summary,,%code}. Look again at the example of the previous section: @@ -2719,7 +2726,7 @@ For example, if you decide to override Bison's default definition for @code{YYLTYPE}, in which @var{Prologue} section should you write your new definition? You should write it in the first since Bison will insert that code into the -parser code file @emph{before} the default @code{YYLTYPE} definition. +parser source code file @emph{before} the default @code{YYLTYPE} definition. In which @var{Prologue} section should you prototype an internal function, @code{trace_token}, that accepts @code{YYLTYPE} and @code{yytokentype} as arguments? @@ -2728,23 +2735,26 @@ You should prototype it in the second since Bison will insert that code This distinction in functionality between the two @var{Prologue} sections is established by the appearance of the @code{%union} between them. -This behavior raises several questions. +This behavior raises a few questions. First, why should the position of a @code{%union} affect definitions related to @code{YYLTYPE} and @code{yytokentype}? Second, what if there is no @code{%union}? In that case, the second kind of @var{Prologue} section is not available. This behavior is not intuitive. -To avoid this subtle @code{%union} dependency, rewrite the example using -@code{%code-top} and @code{%code}. +To avoid this subtle @code{%union} dependency, rewrite the example using a +@code{%code top} and an unqualified @code{%code}. Let's go ahead and add the new @code{YYLTYPE} definition and the @code{trace_token} prototype at the same time: @smallexample -%code-top @{ +%code top @{ #define _GNU_SOURCE #include - /* The following code really belongs in a %requires; see below. */ + + /* WARNING: The following code really belongs + * in a `%code requires'; see below. */ + #include "ptypes.h" #define YYLTYPE YYLTYPE typedef struct YYLTYPE @@ -2772,33 +2782,34 @@ Let's go ahead and add the new @code{YYLTYPE} definition and the @end smallexample @noindent -In this way, @code{%code-top} and @code{%code} achieve the same functionality -as the two kinds of @var{Prologue} sections, but it's always explicit which -kind you intend. +In this way, @code{%code top} and the unqualified @code{%code} achieve the same +functionality as the two kinds of @var{Prologue} sections, but it's always +explicit which kind you intend. Moreover, both kinds are always available even in the absence of @code{%union}. -The first @var{Prologue} section above now logically contains two parts. -The first two lines need to appear in the parser code file. -The fourth line is required by @code{YYSTYPE} and thus also needs to appear in -the parser code file. +The @code{%code top} block above logically contains two parts. +The first two lines before the warning need to appear near the top of the +parser source code file. +The first line after the warning is required by @code{YYSTYPE} and thus also +needs to appear in the parser source code file. However, if you've instructed Bison to generate a parser header file -(@pxref{Table of Symbols, ,%defines}), you probably want the fourth line to -appear before the @code{YYSTYPE} definition in that header file as well. -Also, the @code{YYLTYPE} definition should appear in the parser header file to +(@pxref{Decl Summary, ,%defines}), you probably want that line to appear before +the @code{YYSTYPE} definition in that header file as well. +The @code{YYLTYPE} definition should also appear in the parser header file to override the default @code{YYLTYPE} definition there. -In other words, in the first @var{Prologue} section, all but the first two -lines are dependency code for externally exposed definitions (@code{YYSTYPE} -and @code{YYLTYPE}) required by Bison. -Thus, they belong in one or more @code{%requires}: +In other words, in the @code{%code top} block above, all but the first two +lines are dependency code required by the @code{YYSTYPE} and @code{YYLTYPE} +definitions. +Thus, they belong in one or more @code{%code requires}: @smallexample -%code-top @{ +%code top @{ #define _GNU_SOURCE #include @} -%requires @{ +%code requires @{ #include "ptypes.h" @} %union @{ @@ -2806,7 +2817,7 @@ Thus, they belong in one or more @code{%requires}: tree t; /* @r{@code{tree} is defined in @file{ptypes.h}.} */ @} -%requires @{ +%code requires @{ #define YYLTYPE YYLTYPE typedef struct YYLTYPE @{ @@ -2830,28 +2841,41 @@ Thus, they belong in one or more @code{%requires}: @noindent Now Bison will insert @code{#include "ptypes.h"} and the new @code{YYLTYPE} definition before the Bison-generated @code{YYSTYPE} and @code{YYLTYPE} -definitions in both the parser code file and the parser header file. -(By the same reasoning, @code{%requires} would also be the appropriate place to -write your own definition for @code{YYSTYPE}.) +definitions in both the parser source code file and the parser header file. +(By the same reasoning, @code{%code requires} would also be the appropriate +place to write your own definition for @code{YYSTYPE}.) + +When you are writing dependency code for @code{YYSTYPE} and @code{YYLTYPE}, you +should prefer @code{%code requires} over @code{%code top} regardless of whether +you instruct Bison to generate a parser header file. +When you are writing code that you need Bison to insert only into the parser +source code file and that has no special need to appear at the top of that +file, you should prefer the unqualified @code{%code} over @code{%code top}. +These practices will make the purpose of each block of your code explicit to +Bison and to other developers reading your grammar file. +Following these practices, we expect the unqualified @code{%code} and +@code{%code requires} to be the most important of the four @var{Prologue} +alternatives. At some point while developing your parser, you might decide to provide @code{trace_token} to modules that are external to your parser. Thus, you might wish for Bison to insert the prototype into both the parser -header file and the parser code file. -Since this function is not a dependency of any Bison-required definition (such -as @code{YYSTYPE}), it doesn't make sense to move its prototype to a -@code{%requires}. +header file and the parser source code file. +Since this function is not a dependency required by @code{YYSTYPE} or +@code{YYLTYPE}, it doesn't make sense to move its prototype to a +@code{%code requires}. More importantly, since it depends upon @code{YYLTYPE} and @code{yytokentype}, -@code{%requires} is not sufficient. -Instead, move its prototype from the @code{%code} to a @code{%provides}: +@code{%code requires} is not sufficient. +Instead, move its prototype from the unqualified @code{%code} to a +@code{%code provides}: @smallexample -%code-top @{ +%code top @{ #define _GNU_SOURCE #include @} -%requires @{ +%code requires @{ #include "ptypes.h" @} %union @{ @@ -2859,7 +2883,7 @@ Instead, move its prototype from the @code{%code} to a @code{%provides}: tree t; /* @r{@code{tree} is defined in @file{ptypes.h}.} */ @} -%requires @{ +%code requires @{ #define YYLTYPE YYLTYPE typedef struct YYLTYPE @{ @@ -2871,7 +2895,7 @@ Instead, move its prototype from the @code{%code} to a @code{%provides}: @} YYLTYPE; @} -%provides @{ +%code provides @{ void trace_token (enum yytokentype token, YYLTYPE loc); @} @@ -2885,17 +2909,18 @@ Instead, move its prototype from the @code{%code} to a @code{%provides}: @noindent Bison will insert the @code{trace_token} prototype into both the parser header -file and the parser code file after the definitions for @code{yytokentype}, -@code{YYLTYPE}, and @code{YYSTYPE}. +file and the parser source code file after the definitions for +@code{yytokentype}, @code{YYLTYPE}, and @code{YYSTYPE}. The above examples are careful to write directives in an order that reflects -the layout of the generated parser code and header files: -@code{%code-top}, @code{%requires}, @code{%provides}, and then @code{%code}. -While your grammar files will generally be easier to read if you also follow +the layout of the generated parser source code and header files: +@code{%code top}, @code{%code requires}, @code{%code provides}, and then +@code{%code}. +While your grammar files may generally be easier to read if you also follow this order, Bison does not require it. Instead, Bison lets you choose an organization that makes sense to you. -Any of these directives may be declared multiple times in the grammar file. +You may declare any of these directives multiple times in the grammar file. In that case, Bison concatenates the contained code in declaration order. This is the only way in which the position of one of these directives within the grammar file affects its functionality. @@ -2906,12 +2931,12 @@ For example, you may organize semantic-type-related directives by semantic type: @smallexample -%requires @{ #include "type1.h" @} +%code requires @{ #include "type1.h" @} %union @{ type1 field1; @} %destructor @{ type1_free ($$); @} %printer @{ type1_print ($$); @} -%requires @{ #include "type2.h" @} +%code requires @{ #include "type2.h" @} %union @{ type2 field2; @} %destructor @{ type2_free ($$); @} %printer @{ type2_print ($$); @} @@ -2926,6 +2951,16 @@ definitions section is going to adversely affect their functionality in some counter-intuitive manner just because it comes first. Such an organization is not possible using @var{Prologue} sections. +This section has been concerned with explaining the advantages of the four +@var{Prologue} alternatives over the original Yacc @var{Prologue}. +However, in most cases when using these directives, you shouldn't need to +think about all the low-level ordering issues discussed here. +Instead, you should simply use these directives to label each block of your +code according to its purpose and let Bison handle the ordering. +@code{%code} is the most generic label. +Move code to @code{%code requires}, @code{%code provides}, or @code{%code top} +as needed. + @node Bison Declarations @subsection The Bison Declarations Section @cindex Bison declarations (introduction) @@ -4215,8 +4250,8 @@ 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 - +@findex <*> +@findex <> During error recovery (@pxref{Error Recovery}), symbols already pushed on the stack and tokens coming from the rest of the file are discarded until the parser falls on its feet. If the parser runs out of memory, @@ -4244,21 +4279,29 @@ The Parser Function @code{yyparse}}). 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}. +tag 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 +grammar symbol that has that semantic type tag 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}. +Finally, you can define two different kinds of default @code{%destructor}s. +(These default forms are experimental. +More user feedback will help to determine whether they should become permanent +features.) +You can place each of @code{<*>} and @code{<>} in the @var{symbols} list of +exactly one @code{%destructor} declaration in your grammar file. +The parser will invoke the @var{code} associated with one of these whenever it +discards any user-defined grammar symbol that has no per-symbol and no per-type +@code{%destructor}. +The parser uses the @var{code} for @code{<*>} in the case of such a grammar +symbol for which you have formally declared a semantic type tag (@code{%type} +counts as such a declaration, but @code{$$} does not). +The parser uses the @var{code} for @code{<>} in the case of such a grammar +symbol that has no declared semantic type tag. @end deffn @noindent -For instance: +For example: @smallexample %union @{ char *string; @} @@ -4269,35 +4312,52 @@ For instance: %union @{ char character; @} %token CHR %type chr -%destructor @{ free ($$); @} %symbol-default -%destructor @{ free ($$); printf ("%d", @@$.first_line); @} STRING1 string1 +%token TAGLESS + %destructor @{ @} +%destructor @{ free ($$); @} <*> +%destructor @{ free ($$); printf ("%d", @@$.first_line); @} STRING1 string1 +%destructor @{ printf ("Discarding tagless symbol.\n"); @} <> @end smallexample @noindent guarantees that, when the parser discards any user-defined symbol that has a semantic type tag other than @code{}, it passes its semantic value -to @code{free}. +to @code{free} by default. 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}: +Finally, the parser merely prints a message whenever it discards any symbol, +such as @code{TAGLESS}, that has no semantic type tag. + +A Bison-generated parser invokes the default @code{%destructor}s only for +user-defined as opposed to Bison-defined symbols. +For example, the parser will not invoke either kind of default +@code{%destructor} 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 either 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 may invoke one of them for the end token (token 0) if you +redefine it from @code{$end} to, for example, @code{END}: @smallexample %token END 0 @end smallexample +@cindex actions in mid-rule +@cindex mid-rule actions +Finally, Bison will never invoke a @code{%destructor} for an unreferenced +mid-rule semantic value (@pxref{Mid-Rule Actions,,Actions in Mid-Rule}). +That is, Bison does not consider a mid-rule to have a semantic value if you do +not reference @code{$$} in the mid-rule's action or @code{$@var{n}} (where +@var{n} is the RHS symbol position of the mid-rule) in any later action in that +rule. +However, if you do reference either, the Bison-generated parser will invoke the +@code{<>} @code{%destructor} whenever it discards the mid-rule symbol. + @ignore @noindent In the future, it may be possible to redefine the @code{error} token as a @@ -4509,12 +4569,129 @@ Declare the expected number of shift-reduce conflicts In order to change the behavior of @command{bison}, use the following directives: +@deffn {Directive} %code @{@var{code}@} +@findex %code +This is the unqualified form of the @code{%code} directive. +It inserts @var{code} verbatim at the default location in the output. +That default location is determined by the selected target language and/or +parser skeleton. + +@cindex Prologue +For the current C/C++ skeletons, the default location is the parser source code +file after the usual contents of the parser header file. +Thus, @code{%code} replaces the traditional Yacc prologue, +@code{%@{@var{code}%@}}, for most purposes. +For a detailed discussion, see @ref{Prologue Alternatives}. + +@comment For Java, the default location is inside the parser class. + +(Like all the Yacc prologue alternatives, this directive is experimental. +More user feedback will help to determine whether it should become a permanent +feature.) +@end deffn + +@deffn {Directive} %code @var{qualifier} @{@var{code}@} +This is the qualified form of the @code{%code} directive. +If you need to specify location-sensitive verbatim @var{code} that does not +belong at the default location selected by the unqualified @code{%code} form, +use this form instead. + +@var{qualifier} identifies the purpose of @var{code} and thus the location(s) +where Bison should generate it. +Not all values of @var{qualifier} are available for all target languages: + +@itemize @bullet +@findex %code requires +@item requires + +@itemize @bullet +@item Language(s): C, C++ + +@item Purpose: This is the best place to write dependency code required for +@code{YYSTYPE} and @code{YYLTYPE}. +In other words, it's the best place to define types referenced in @code{%union} +directives, and it's the best place to override Bison's default @code{YYSTYPE} +and @code{YYLTYPE} definitions. + +@item Location(s): The parser header file and the parser source code file +before the Bison-generated @code{YYSTYPE} and @code{YYLTYPE} definitions. +@end itemize + +@item provides +@findex %code provides + +@itemize @bullet +@item Language(s): C, C++ + +@item Purpose: This is the best place to write additional definitions and +declarations that should be provided to other modules. + +@item Location(s): The parser header file and the parser source code file after +the Bison-generated @code{YYSTYPE}, @code{YYLTYPE}, and token definitions. +@end itemize + +@item top +@findex %code top + +@itemize @bullet +@item Language(s): C, C++ + +@item Purpose: The unqualified @code{%code} or @code{%code requires} should +usually be more appropriate than @code{%code top}. +However, occasionally it is necessary to insert code much nearer the top of the +parser source code file. +For example: + +@smallexample +%code top @{ + #define _GNU_SOURCE + #include +@} +@end smallexample + +@item Location(s): Near the top of the parser source code file. +@end itemize +@ignore +@item imports +@findex %code imports + +@itemize @bullet +@item Language(s): Java + +@item Purpose: This is the best place to write Java import directives. + +@item Location(s): The parser Java file after any Java package directive and +before any class definitions. +@end itemize +@end ignore +@end itemize + +(Like all the Yacc prologue alternatives, this directive is experimental. +More user feedback will help to determine whether it should become a permanent +feature.) + +@cindex Prologue +For a detailed discussion of how to use @code{%code} in place of the +traditional Yacc prologue for C/C++, see @ref{Prologue Alternatives}. +@end deffn + @deffn {Directive} %debug In the parser file, define the macro @code{YYDEBUG} to 1 if it is not already defined, so that the debugging facilities are compiled. @end deffn @xref{Tracing, ,Tracing Your Parser}. +@deffn {Directive} %define @var{define-variable} +@deffnx {Directive} %define @var{define-variable} @var{value} +Define a variable to adjust Bison's behavior. +The list of available variables and their meanings depends on the selected +target language and/or the parser skeleton (@pxref{Decl Summary,,%language}). +The @var{value} can be omitted for boolean variables; for +boolean variables, the skeletons will treat a @var{value} of @samp{0} +or @samp{false} as the boolean variable being false, and anything else +as true. +@end deffn + @deffn {Directive} %defines Write a header file containing macro definitions for the token type names defined in the grammar as well as a few other declarations. @@ -4548,11 +4725,15 @@ 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 %requires -@findex %provides -If you have declared @code{%requires} or @code{%provides}, the output +@findex %code requires +@findex %code provides +If you have declared @code{%code requires} or @code{%code provides}, the output header also contains their code. -@xref{Table of Symbols, ,%requires}. +@xref{Decl Summary, ,%code}. +@end deffn + +@deffn {Directive} %defines @var{defines-file} +Same as above, but save in the file @var{defines-file}. @end deffn @deffn {Directive} %destructor @@ -4560,11 +4741,17 @@ Specify how the parser should reclaim the memory associated to discarded symbols. @xref{Destructor Decl, , Freeing Discarded Symbols}. @end deffn -@deffn {Directive} %file-prefix="@var{prefix}" +@deffn {Directive} %file-prefix "@var{prefix}" Specify a prefix to use for all Bison output file names. The names are chosen as if the input file were named @file{@var{prefix}.y}. @end deffn +@deffn {Directive} %language "@var{language}" +Specify the programming language for the generated parser. Currently +supported languages include C and C++. +@var{language} is case-insensitive. +@end deffn + @deffn {Directive} %locations Generate the code processing the locations (@pxref{Action Features, ,Special Features for Use in Actions}). This mode is enabled as soon as @@ -4573,14 +4760,14 @@ grammar does not use it, using @samp{%locations} allows for more accurate syntax error messages. @end deffn -@deffn {Directive} %name-prefix="@var{prefix}" +@deffn {Directive} %name-prefix "@var{prefix}" Rename the external symbols used in the parser so that they start with @var{prefix} instead of @samp{yy}. The precise list of symbols renamed in C parsers is @code{yyparse}, @code{yylex}, @code{yyerror}, @code{yynerrs}, @code{yylval}, @code{yychar}, @code{yydebug}, and (if locations are used) @code{yylloc}. For example, if you use -@samp{%name-prefix="c_"}, the names become @code{c_parse}, @code{c_lex}, +@samp{%name-prefix "c_"}, the names become @code{c_parse}, @code{c_lex}, and so on. In C++ parsers, it is only the surrounding namespace which is named @var{prefix} instead of @samp{yy}. @xref{Multiple Parsers, ,Multiple Parsers in the Same Program}. @@ -4613,7 +4800,7 @@ associate errors with the parser file, treating it an independent source file in its own right. @end deffn -@deffn {Directive} %output="@var{file}" +@deffn {Directive} %output "@var{file}" Specify @var{file} for the parser file. @end deffn @@ -4627,6 +4814,21 @@ Require version @var{version} or higher of Bison. @xref{Require Decl, , Require a Version of Bison}. @end deffn +@deffn {Directive} %skeleton "@var{file}" +Specify the skeleton to use. + +You probably don't need this option unless you are developing Bison. +You should use @code{%language} if you want to specify the skeleton for a +different language, because it is clearer and because it will always choose the +correct skeleton for non-deterministic or push parsers. + +If @var{file} does not contain a @code{/}, @var{file} is the name of a skeleton +file in the Bison installation directory. +If it does, @var{file} is an absolute file name or a file name relative to the +directory of the grammar file. +This is similar to how most shells resolve commands. +@end deffn + @deffn {Directive} %token-table Generate an array of token names in the parser file. The name of the array is @code{yytname}; @code{yytname[@var{i}]} is the name of the @@ -7206,23 +7408,25 @@ this option is specified. Tuning the parser: @table @option -@item -S @var{file} -@itemx --skeleton=@var{file} -Specify the skeleton to use. You probably don't need this option unless -you are developing Bison. - @item -t @itemx --debug In the parser file, define the macro @code{YYDEBUG} to 1 if it is not already defined, so that the debugging facilities are compiled. @xref{Tracing, ,Tracing Your Parser}. +@item -L @var{language} +@itemx --language=@var{language} +Specify the programming language for the generated parser, as if +@code{%language} was specified (@pxref{Decl Summary, , Bison Declaration +Summary}). Currently supported languages include C and C++. +@var{language} is case-insensitive. + @item --locations Pretend that @code{%locations} was specified. @xref{Decl Summary}. @item -p @var{prefix} @itemx --name-prefix=@var{prefix} -Pretend that @code{%name-prefix="@var{prefix}"} was specified. +Pretend that @code{%name-prefix "@var{prefix}"} was specified. @xref{Decl Summary}. @item -l @@ -7237,6 +7441,22 @@ parser file, treating it as an independent source file in its own right. @itemx --no-parser Pretend that @code{%no-parser} was specified. @xref{Decl Summary}. +@item -S @var{file} +@itemx --skeleton=@var{file} +Specify the skeleton to use, similar to @code{%skeleton} +(@pxref{Decl Summary, , Bison Declaration Summary}). + +You probably don't need this option unless you are developing Bison. +You should use @option{--language} if you want to specify the skeleton for a +different language, because it is clearer and because it will always +choose the correct skeleton for non-deterministic or push parsers. + +If @var{file} does not contain a @code{/}, @var{file} is the name of a skeleton +file in the Bison installation directory. +If it does, @var{file} is an absolute file name or a file name relative to the +current working directory. +This is similar to how most shells resolve commands. + @item -k @itemx --token-table Pretend that @code{%token-table} was specified. @xref{Decl Summary}. @@ -7379,14 +7599,16 @@ int yyparse (void); @node C++ Bison Interface @subsection C++ Bison Interface -@c - %skeleton "lalr1.cc" +@c - %language "C++" @c - Always pure @c - initial action -The C++ parser @acronym{LALR}(1) skeleton is named @file{lalr1.cc}. To -select it, you may either pass the option @option{--skeleton=lalr1.cc} -to Bison, or include the directive @samp{%skeleton "lalr1.cc"} in the -grammar preamble. When run, @command{bison} will create several +The C++ @acronym{LALR}(1) parser is selected using the language directive, +@samp{%language "C++"}, or the synonymous command-line option +@option{--language=c++}. +@xref{Decl Summary}. + +When run, @command{bison} will create several entities in the @samp{yy} namespace. Use the @samp{%name-prefix} directive to change the namespace name, see @ref{Decl Summary}. The various classes are generated in the following files: @@ -7418,7 +7640,7 @@ for a complete and accurate documentation. @node C++ Semantic Values @subsection C++ Semantic Values @c - No objects in unions -@c - YSTYPE +@c - YYSTYPE @c - Printer and destructor The @code{%union} directive works as for C, see @ref{Union Decl, ,The @@ -7448,7 +7670,7 @@ Symbols}. @c - %locations @c - class Position @c - class Location -@c - %define "filename_type" "const symbol::Symbol" +@c - %define filename_type "const symbol::Symbol" When the directive @code{%locations} is used, the C++ parser supports location tracking, see @ref{Locations, , Locations Overview}. Two @@ -7460,7 +7682,7 @@ and a @code{location}, a range composed of a pair of The name of the file. It will always be handled as a pointer, the parser will never duplicate nor deallocate it. As an experimental feature you may change it to @samp{@var{type}*} using @samp{%define -"filename_type" "@var{type}"}. +filename_type "@var{type}"}. @end deftypemethod @deftypemethod {position} {unsigned int} line @@ -7524,7 +7746,7 @@ Move @code{begin} onto @code{end}. The output files @file{@var{output}.hh} and @file{@var{output}.cc} declare and define the parser class in the namespace @code{yy}. The class name defaults to @code{parser}, but may be changed using -@samp{%define "parser_class_name" "@var{name}"}. The interface of +@samp{%define parser_class_name "@var{name}"}. The interface of this class is detailed below. It can be extended using the @code{%parse-param} feature: its semantics is slightly changed since it describes an additional member of the parser class, and an @@ -7697,8 +7919,8 @@ Similarly for the parser itself. @comment file: calc++-driver.hh @example - // Handling the parser. - void parse (const std::string& f); + // Run the parser. Return 0 on success. + int parse (const std::string& f); std::string file; bool trace_parsing; @end example @@ -7739,15 +7961,16 @@ calcxx_driver::~calcxx_driver () @{ @} -void +int calcxx_driver::parse (const std::string &f) @{ file = f; scan_begin (); yy::calcxx_parser parser (*this); parser.set_debug_level (trace_parsing); - parser.parse (); + int res = parser.parse (); scan_end (); + return res; @} void @@ -7774,25 +7997,25 @@ the grammar for. @comment file: calc++-parser.yy @example -%skeleton "lalr1.cc" /* -*- C++ -*- */ -%require "2.1a" +%language "C++" /* -*- C++ -*- */ +%require "@value{VERSION}" %defines -%define "parser_class_name" "calcxx_parser" +%define parser_class_name "calcxx_parser" @end example @noindent -@findex %requires +@findex %code 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, ,%requires}. +@xref{Decl Summary, ,%code}. @comment file: calc++-parser.yy @example -%requires @{ +%code requires @{ # include class calcxx_driver; @} @@ -7876,7 +8099,7 @@ avoid name clashes. %token ASSIGN ":=" %token IDENTIFIER "identifier" %token NUMBER "number" -%type exp "expression" +%type exp @end example @noindent @@ -7889,7 +8112,7 @@ To enable memory deallocation during error recovery, use %printer @{ debug_stream () << *$$; @} "identifier" %destructor @{ delete $$; @} "identifier" -%printer @{ debug_stream () << $$; @} "number" "expression" +%printer @{ debug_stream () << $$; @} @end example @noindent @@ -8043,8 +8266,13 @@ void calcxx_driver::scan_begin () @{ yy_flex_debug = trace_scanning; - if (!(yyin = fopen (file.c_str (), "r"))) - error (std::string ("cannot open ") + file); + if (file == "-") + yyin = stdin; + else if (!(yyin = fopen (file.c_str (), "r"))) + @{ + error (std::string ("cannot open ") + file); + exit (1); + @} @} void @@ -8073,11 +8301,8 @@ main (int argc, char *argv[]) driver.trace_parsing = true; else if (*argv == std::string ("-s")) driver.trace_scanning = true; - else - @{ - driver.parse (*argv); - std::cout << driver.result << std::endl; - @} + else if (!driver.parse (*argv)) + std::cout << driver.result << std::endl; @} @end example @@ -8523,58 +8748,38 @@ Separates alternate rules for the same result nonterminal. @xref{Rules, ,Syntax of Grammar Rules}. @end deffn -@deffn {Symbol} $accept -The predefined nonterminal whose only rule is @samp{$accept: @var{start} -$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} <*> +Used to define a default tagged @code{%destructor} or default tagged +@code{%printer}. -@deffn {Directive} %code @{@var{code}@} -Other than semantic actions, this is probably the most common place you should -write verbatim code for the parser implementation. -For C/C++, it replaces the traditional Yacc prologue, -@code{%@{@var{code}%@}}, for most purposes. -For Java, it inserts code into the parser class. +This feature is experimental. +More user feedback will help to determine whether it should become a permanent +feature. -@cindex Prologue -@findex %union -Compare with @code{%@{@var{code}%@}} (@pxref{Prologue, ,The Prologue}) -appearing after the first @code{%union @{@var{code}@}} in a C/C++ based grammar -file. -While Bison will continue to support @code{%@{@var{code}%@}} for backward -compatibility, @code{%code @{@var{code}@}} is cleaner as its functionality does -not depend on its position in the grammar file relative to any -@code{%union @{@var{code}@}}. -Specifically, @code{%code @{@var{code}@}} always inserts your @var{code} into -the parser code file after the usual contents of the parser header file. - -@xref{Prologue Alternatives}. +@xref{Destructor Decl, , Freeing Discarded Symbols}. @end deffn -@deffn {Directive} %code-top @{@var{code}@} -Occasionally for C/C++ it is desirable to insert code near the top of the -parser code file. -For example: +@deffn {Directive} <> +Used to define a default tagless @code{%destructor} or default tagless +@code{%printer}. -@smallexample -%code-top @{ - #define _GNU_SOURCE - #include -@} -@end smallexample +This feature is experimental. +More user feedback will help to determine whether it should become a permanent +feature. -@noindent -For Java, @code{%code-top @{@var{code}@}} is currently unused. +@xref{Destructor Decl, , Freeing Discarded Symbols}. +@end deffn -@cindex Prologue -@findex %union -Compare with @code{%@{@var{code}%@}} appearing before the first -@code{%union @{@var{code}@}} in a C/C++ based grammar file. -@code{%code-top @{@var{code}@}} is cleaner as its functionality does not depend -on its position in the grammar file relative to any -@code{%union @{@var{code}@}}. +@deffn {Symbol} $accept +The predefined nonterminal whose only rule is @samp{$accept: @var{start} +$end}, where @var{start} is the start symbol. @xref{Start Decl, , The +Start-Symbol}. It cannot be used in the grammar. +@end deffn -@xref{Prologue Alternatives}. +@deffn {Directive} %code @{@var{code}@} +@deffnx {Directive} %code @var{qualifier} @{@var{code}@} +Insert @var{code} verbatim into output parser source. +@xref{Decl Summary,,%code}. @end deffn @deffn {Directive} %debug @@ -8593,11 +8798,22 @@ Precedence}. @end deffn @end ifset +@deffn {Directive} %define @var{define-variable} +@deffnx {Directive} %define @var{define-variable} @var{value} +Define a variable to adjust Bison's behavior. +@xref{Decl Summary,,%define}. +@end deffn + @deffn {Directive} %defines Bison declaration to create a header file meant for the scanner. @xref{Decl Summary}. @end deffn +@deffn {Directive} %defines @var{defines-file} +Same as above, but save in the file @var{defines-file}. +@xref{Decl Summary}. +@end deffn + @deffn {Directive} %destructor Specify how the parser should reclaim the memory associated to discarded symbols. @xref{Destructor Decl, , Freeing Discarded Symbols}. @@ -8630,7 +8846,7 @@ Bison declaration to request verbose, specific error message strings when @code{yyerror} is called. @end deffn -@deffn {Directive} %file-prefix="@var{prefix}" +@deffn {Directive} %file-prefix "@var{prefix}" Bison declaration to set the prefix of the output files. @xref{Decl Summary}. @end deffn @@ -8644,6 +8860,11 @@ Parsers, ,Writing @acronym{GLR} Parsers}. Run user code before parsing. @xref{Initial Action Decl, , Performing Actions before Parsing}. @end deffn +@deffn {Directive} %language +Specify the programming language for the generated parser. +@xref{Decl Summary}. +@end deffn + @deffn {Directive} %left Bison declaration to assign left associativity to token(s). @xref{Precedence Decl, ,Operator Precedence}. @@ -8662,7 +8883,7 @@ function is applied to the two semantic values to get a single result. @xref{GLR Parsers, ,Writing @acronym{GLR} Parsers}. @end deffn -@deffn {Directive} %name-prefix="@var{prefix}" +@deffn {Directive} %name-prefix "@var{prefix}" Bison declaration to rename the external symbols. @xref{Decl Summary}. @end deffn @@ -8684,7 +8905,7 @@ Bison declaration to assign nonassociativity to token(s). @xref{Precedence Decl, ,Operator Precedence}. @end deffn -@deffn {Directive} %output="@var{file}" +@deffn {Directive} %output "@var{file}" Bison declaration to set the name of the parser file. @xref{Decl Summary}. @end deffn @@ -8700,18 +8921,6 @@ Bison declaration to assign a precedence to a specific rule. @xref{Contextual Precedence, ,Context-Dependent Precedence}. @end deffn -@deffn {Directive} %provides @{@var{code}@} -This is the right place to write additional definitions you would like Bison to -expose externally. -For C/C++, this directive inserts your @var{code} both into the parser header -file (if generated; @pxref{Table of Symbols, ,%defines}) and into the parser -code file after Bison's required definitions. -For Java, it inserts your @var{code} into the parser java file after the parser -class. - -@xref{Prologue Alternatives}. -@end deffn - @deffn {Directive} %pure-parser Bison declaration to request a pure (reentrant) parser. @xref{Pure Decl, ,A Pure (Reentrant) Parser}. @@ -8722,44 +8931,21 @@ Require version @var{version} or higher of Bison. @xref{Require Decl, , Require a Version of Bison}. @end deffn -@deffn {Directive} %requires @{@var{code}@} -This is the right place to write dependency code for externally exposed -definitions required by Bison. -For C/C++, such exposed definitions are those usually appearing in the parser -header file. -Thus, this is the right place to define types referenced in -@code{%union @{@var{code}@}} directives, and it is the right place to override -Bison's default @code{YYSTYPE} and @code{YYLTYPE} definitions. -For Java, this is the right place to write import directives. - -@cindex Prologue -@findex %union -Compare with @code{%@{@var{code}%@}} (@pxref{Prologue, ,The Prologue}) -appearing before the first @code{%union @{@var{code}@}} in a C/C++ based -grammar file. -Unlike @code{%@{@var{code}%@}}, @code{%requires @{@var{code}@}} inserts your -@var{code} both into the parser code file and into the parser header file (if -generated; @pxref{Table of Symbols, ,%defines}) since Bison's required -definitions should depend on it in both places. - -@xref{Prologue Alternatives}. -@end deffn - @deffn {Directive} %right Bison declaration to assign right associativity to token(s). @xref{Precedence Decl, ,Operator Precedence}. @end deffn +@deffn {Directive} %skeleton +Specify the skeleton to use; usually for development. +@xref{Decl Summary}. +@end deffn + @deffn {Directive} %start 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}. @@ -9148,12 +9334,12 @@ grammatically indivisible. The piece of text it represents is a token. @c LocalWords: pre STDC GNUC endif yy YY alloca lf stddef stdlib YYDEBUG @c LocalWords: NUM exp subsubsection kbd Ctrl ctype EOF getchar isdigit @c LocalWords: ungetc stdin scanf sc calc ulator ls lm cc NEG prec yyerrok -@c LocalWords: longjmp fprintf stderr preg yylloc YYLTYPE cos ln +@c LocalWords: longjmp fprintf stderr yylloc YYLTYPE cos ln @c LocalWords: smallexample symrec val tptr FNCT fnctptr func struct sym @c LocalWords: fnct putsym getsym fname arith fncts atan ptr malloc sizeof @c LocalWords: strlen strcpy fctn strcmp isalpha symbuf realloc isalnum @c LocalWords: ptypes itype YYPRINT trigraphs yytname expseq vindex dtype -@c LocalWords: Rhs YYRHSLOC LE nonassoc op deffn typeless typefull yynerrs +@c LocalWords: Rhs YYRHSLOC LE nonassoc op deffn typeless yynerrs @c LocalWords: yychar yydebug msg YYNTOKENS YYNNTS YYNRULES YYNSTATES @c LocalWords: cparse clex deftypefun NE defmac YYACCEPT YYABORT param @c LocalWords: strncmp intval tindex lvalp locp llocp typealt YYBACKUP @@ -9163,5 +9349,5 @@ grammatically indivisible. The piece of text it represents is a token. @c LocalWords: hexflag STR exdent itemset asis DYYDEBUG YYFPRINTF args @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: nbar yytext fst snd osplit ntwo strdup AST @c LocalWords: YYSTACK DVI fdl printindex