]> git.saurik.com Git - bison.git/blobdiff - doc/bison.texinfo
* data/glr.cc, data/lalr1.cc: Using %defines is mandatory.
[bison.git] / doc / bison.texinfo
index 022510b8d11c9c9d1dbec3cdc622991c257c7967..763320bddba0c4b3ed82199f1ed1dc7384aeb44c 100644 (file)
@@ -311,6 +311,7 @@ Frequently Asked Questions
 * How Can I Reset the Parser:: @code{yyparse} Keeps some State
 * Strings are Destroyed::      @code{yylval} Loses Track of Strings
 * Implementing Gotos/Loops::   Control Flow in the Calculator
+* Multiple start-symbols::     Factoring closely related grammars
 * Secure?  Conform?::          Is Bison @acronym{POSIX} safe?
 * I can't build Bison::        Troubleshooting
 * Where can I find help?::     Troubleshouting
@@ -355,9 +356,10 @@ This edition corresponds to version @value{VERSION} of Bison.
 @node Conditions
 @unnumbered Conditions for Using Bison
 
-As of Bison version 1.24, we have changed the distribution terms for
-@code{yyparse} to permit using Bison's output in nonfree programs when
-Bison is generating C code for @acronym{LALR}(1) parsers.  Formerly, these
+The distribution terms for Bison-generated parsers permit using the
+parsers in nonfree programs.  Before Bison version 2.2, these extra
+permissions applied only when Bison was generating @acronym{LALR}(1)
+parsers in C@.  And before Bison version 1.24, Bison-generated
 parsers could be used only in programs that were free software.
 
 The other @acronym{GNU} programming tools, such as the @acronym{GNU} C
@@ -369,10 +371,10 @@ License to all of the Bison source code.
 
 The output of the Bison utility---the Bison parser file---contains a
 verbatim copy of a sizable piece of Bison, which is the code for the
-@code{yyparse} function.  (The actions from your grammar are inserted
-into this function at one point, but the rest of the function is not
-changed.)  When we applied the @acronym{GPL} terms to the code for
-@code{yyparse},
+parser's implementation.  (The actions from your grammar are inserted
+into this implementation at one point, but most of the rest of the
+implementation is not changed.)  When we applied the @acronym{GPL}
+terms to the skeleton code for the parser's implementation,
 the effect was to restrict the use of Bison output to free software.
 
 We didn't change the terms because of sympathy for people who want to
@@ -382,13 +384,11 @@ encourage people to make other software free.  So we decided to make the
 practical conditions for using Bison match the practical conditions for
 using the other @acronym{GNU} tools.
 
-This exception applies only when Bison is generating C code for an
-@acronym{LALR}(1) parser; otherwise, the @acronym{GPL} terms operate
-as usual.  You can
-tell whether the exception applies to your @samp{.c} output file by
-inspecting it to see whether it says ``As a special exception, when
-this file is copied by Bison into a Bison output file, you may use
-that output file without restriction.''
+This exception applies when Bison is generating code for a parser.
+You can tell whether the exception applies to a Bison output file by
+inspecting the file for text beginning with ``As a special
+exception@dots{}''.  The text spells out the exact terms of the
+exception.
 
 @include gpl.texi
 
@@ -4220,8 +4220,8 @@ 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{yylloc}, @code{yychar}, @code{yydebug}, and
-possible @code{yylloc}.  For example, if you use
+@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},
 and so on.  In C++ parsers, it is only the surrounding namespace which is
 named @var{prefix} instead of @samp{yy}.
@@ -7583,10 +7583,14 @@ parser's to get the set of defined tokens.
 # include <string>
 # include "calc++-driver.hh"
 # include "calc++-parser.hh"
-/* Work around a bug in flex 2.5.31.  See Debian bug 333231
-  <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.  */
+
+/* Work around an incompatibility in flex (at least versions
+   2.5.31 through 2.5.33): it generates code that does
+   not conform to C89.  See Debian bug 333231
+   <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.  */
 # undef yywrap
 # define yywrap() 1
+
 /* By default yylex returns int, we use token_type.
    Unfortunately yyterminate by default returns 0, which is
    not of token_type.  */
@@ -7727,6 +7731,7 @@ are addressed.
 * How Can I Reset the Parser::  @code{yyparse} Keeps some State
 * Strings are Destroyed::       @code{yylval} Loses Track of Strings
 * Implementing Gotos/Loops::    Control Flow in the Calculator
+* Multiple start-symbols::      Factoring closely related grammars
 * Secure?  Conform?::           Is Bison @acronym{POSIX} safe?
 * I can't build Bison::         Troubleshooting
 * Where can I find help?::      Troubleshouting
@@ -7927,6 +7932,55 @@ This topic is way beyond the scope of this manual, and the reader is
 invited to consult the dedicated literature.
 
 
+@node Multiple start-symbols
+@section Multiple start-symbols
+
+@display
+I have several closely related grammars, and I would like to share their
+implementations.  In fact, I could use a single grammar but with
+multiple entry points.
+@end display
+
+Bison does not support multiple start-symbols, but there is a very
+simple means to simulate them.  If @code{foo} and @code{bar} are the two
+pseudo start-symbols, then introduce two new tokens, say
+@code{START_FOO} and @code{START_BAR}, and use them as switches from the
+real start-symbol:
+
+@example
+%token START_FOO START_BAR;
+%start start;
+start: START_FOO foo
+     | START_BAR bar;
+@end example
+
+These tokens prevents the introduction of new conflicts.  As far as the
+parser goes, that is all that is needed.
+
+Now the difficult part is ensuring that the scanner will send these
+tokens first.  If your scanner is hand-written, that should be
+straightforward.  If your scanner is generated by Lex, them there is
+simple means to do it: recall that anything between @samp{%@{ ... %@}}
+after the first @code{%%} is copied verbatim in the top of the generated
+@code{yylex} function.  Make sure a variable @code{start_token} is
+available in the scanner (e.g., a global variable or using
+@code{%lex-param} etc.), and use the following:
+
+@example
+  /* @r{Prologue.}  */
+%%
+%@{
+  if (start_token)
+    @{
+      int t = start_token;
+      start_token = 0;
+      return t;
+    @}
+%@}
+  /* @r{The rules.}  */
+@end example
+
+
 @node Secure?  Conform?
 @section Secure?  Conform?