]> git.saurik.com Git - bison.git/blobdiff - doc/bison.texinfo
maint: avoid "magic number exit".
[bison.git] / doc / bison.texinfo
index 3d90277be9a997fef9f7c1d61317f7a204106c6a..7fb051553684245f3a40256a2a661468c8e96e68 100644 (file)
@@ -33,7 +33,7 @@
 This manual (@value{UPDATED}) is for GNU Bison (version
 @value{VERSION}), the GNU parser generator.
 
-Copyright @copyright{} 1988-1993, 1995, 1998-2011 Free Software
+Copyright @copyright{} 1988-1993, 1995, 1998-2012 Free Software
 Foundation, Inc.
 
 @quotation
@@ -125,7 +125,7 @@ The Concepts of Bison
                            the name of an identifier, etc.).
 * Semantic Actions::     Each rule can have an action containing C code.
 * GLR Parsers::          Writing parsers for general context-free languages.
-* Locations Overview::   Tracking Locations.
+* Locations::            Overview of location tracking.
 * Bison Parser::         What are Bison's input and output,
                            how is the output used?
 * Stages::               Stages in writing and running Bison grammars.
@@ -178,6 +178,8 @@ Multi-Function Calculator: @code{mfcalc}
 * Mfcalc Declarations::    Bison declarations for multi-function calculator.
 * Mfcalc Rules::           Grammar rules for the calculator.
 * Mfcalc Symbol Table::    Symbol table management subroutines.
+* Mfcalc Lexer::           The lexical analyzer.
+* Mfcalc Main::            The controlling function.
 
 Bison Grammar Files
 
@@ -451,7 +453,7 @@ use Bison or Yacc, we suggest you start by reading this chapter carefully.
                            the name of an identifier, etc.).
 * Semantic Actions::     Each rule can have an action containing C code.
 * GLR Parsers::          Writing parsers for general context-free languages.
-* Locations Overview::   Tracking Locations.
+* Locations::            Overview of location tracking.
 * Bison Parser::         What are Bison's input and output,
                            how is the output used?
 * Stages::               Stages in writing and running Bison grammars.
@@ -1178,8 +1180,8 @@ Another Bison feature requiring special consideration is @code{YYERROR}
 initiate error recovery.
 During deterministic GLR operation, the effect of @code{YYERROR} is
 the same as its effect in a deterministic parser.
-The effect in a deferred action is similar, but the precise point of the 
-error is undefined;  instead, the parser reverts to deterministic operation, 
+The effect in a deferred action is similar, but the precise point of the
+error is undefined;  instead, the parser reverts to deterministic operation,
 selecting an unspecified stack on which to continue with a syntax error.
 In a semantic predicate (see @ref{Semantic Predicates}) during nondeterministic
 parsing, @code{YYERROR} silently prunes
@@ -1210,12 +1212,12 @@ widget :
 @end smallexample
 
 @noindent
-is one way to allow the same parser to handle two different syntaxes for 
+is one way to allow the same parser to handle two different syntaxes for
 widgets.  The clause preceded by @code{%?} is treated like an ordinary
 action, except that its text is treated as an expression and is always
-evaluated immediately (even when in nondeterministic mode).  If the 
+evaluated immediately (even when in nondeterministic mode).  If the
 expression yields 0 (false), the clause is treated as a syntax error,
-which, in a nondeterministic parser, causes the stack in which it is reduced 
+which, in a nondeterministic parser, causes the stack in which it is reduced
 to die.  In a deterministic parser, it acts like YYERROR.
 
 As the example shows, predicates otherwise look like semantic actions, and
@@ -1226,7 +1228,7 @@ labels.
 
 There is a subtle difference between semantic predicates and ordinary
 actions in nondeterministic mode, since the latter are deferred.
-For example, we could try to rewrite the previous example as 
+For example, we could try to rewrite the previous example as
 
 @smallexample
 widget :
@@ -1240,7 +1242,7 @@ widget :
 false).  However, this
 does @emph{not} have the same effect if @code{new_args} and @code{old_args}
 have overlapping syntax.
-Since the mid-rule actions testing @code{new_syntax} are deferred, 
+Since the mid-rule actions testing @code{new_syntax} are deferred,
 a GLR parser first encounters the unresolved ambiguous reduction
 for cases where @code{new_args} and @code{old_args} recognize the same string
 @emph{before} performing the tests of @code{new_syntax}.  It therefore
@@ -1278,7 +1280,7 @@ will suffice.  Otherwise, we suggest
 %@}
 @end example
 
-@node Locations Overview
+@node Locations
 @section Locations
 @cindex location
 @cindex textual location
@@ -2300,17 +2302,17 @@ Here is a sample session with the multi-function calculator:
 @example
 $ @kbd{mfcalc}
 @kbd{pi = 3.141592653589}
-3.1415926536
+@result{} 3.1415926536
 @kbd{sin(pi)}
-0.0000000000
+@result{} 0.0000000000
 @kbd{alpha = beta1 = 2.3}
-2.3000000000
+@result{} 2.3000000000
 @kbd{alpha}
-2.3000000000
+@result{} 2.3000000000
 @kbd{ln(alpha)}
-0.8329091229
+@result{} 0.8329091229
 @kbd{exp(ln(beta1))}
-2.3000000000
+@result{} 2.3000000000
 $
 @end example
 
@@ -2320,6 +2322,8 @@ Note that multiple assignment and nested function calls are permitted.
 * Mfcalc Declarations::    Bison declarations for multi-function calculator.
 * Mfcalc Rules::           Grammar rules for the calculator.
 * Mfcalc Symbol Table::    Symbol table management subroutines.
+* Mfcalc Lexer::           The lexical analyzer.
+* Mfcalc Main::            The controlling function.
 @end menu
 
 @node Mfcalc Declarations
@@ -2327,11 +2331,13 @@ Note that multiple assignment and nested function calls are permitted.
 
 Here are the C and Bison declarations for the multi-function calculator.
 
+@comment file: mfcalc.y
 @smallexample
 @group
 %@{
-  #include <math.h>  /* For math functions, cos(), sin(), etc.  */
-  #include "calc.h"  /* Contains definition of `symrec'.  */
+  #include <stdio.h>  /* For printf, etc. */
+  #include <math.h>   /* For pow, used in the grammar.  */
+  #include "calc.h"   /* Contains definition of `symrec'.  */
   int yylex (void);
   void yyerror (char const *);
 %@}
@@ -2385,6 +2391,7 @@ Here are the grammar rules for the multi-function calculator.
 Most of them are copied directly from @code{calc}; three rules,
 those which mention @code{VAR} or @code{FNCT}, are new.
 
+@comment file: mfcalc.y
 @smallexample
 @group
 input:   /* empty */
@@ -2395,8 +2402,8 @@ input:   /* empty */
 @group
 line:
           '\n'
-        | exp '\n'   @{ printf ("\t%.10g\n", $1); @}
-        | error '\n' @{ yyerrok;                  @}
+        | exp '\n'   @{ printf ("%.10g\n", $1); @}
+        | error '\n' @{ yyerrok;                @}
 ;
 @end group
 
@@ -2431,6 +2438,7 @@ The symbol table itself consists of a linked list of records.  Its
 definition, which is kept in the header @file{calc.h}, is as follows.  It
 provides for either functions or variables to be placed in the table.
 
+@comment file: calc.h
 @smallexample
 @group
 /* Function type.  */
@@ -2463,22 +2471,11 @@ symrec *getsym (char const *);
 @end group
 @end smallexample
 
-The new version of @code{main} includes a call to @code{init_table}, a
-function that initializes the symbol table.  Here it is, and
-@code{init_table} as well:
+The new version of @code{main} will call @code{init_table} to initialize
+the symbol table:
 
+@comment file: mfcalc.y
 @smallexample
-#include <stdio.h>
-
-@group
-/* Called by yyparse on error.  */
-void
-yyerror (char const *s)
-@{
-  printf ("%s\n", s);
-@}
-@end group
-
 @group
 struct init
 @{
@@ -2490,13 +2487,13 @@ struct init
 @group
 struct init const arith_fncts[] =
 @{
-  "sin",  sin,
-  "cos",  cos,
-  "atan", atan,
-  "ln",   log,
-  "exp",  exp,
-  "sqrt", sqrt,
-  0, 0
+  @{ "atan", atan @},
+  @{ "cos",  cos  @},
+  @{ "exp",  exp  @},
+  @{ "ln",   log  @},
+  @{ "sin",  sin  @},
+  @{ "sqrt", sqrt @},
+  @{ 0, 0 @},
 @};
 @end group
 
@@ -2507,6 +2504,7 @@ symrec *sym_table;
 
 @group
 /* Put arithmetic functions in table.  */
+static
 void
 init_table (void)
 @{
@@ -2519,15 +2517,6 @@ init_table (void)
     @}
 @}
 @end group
-
-@group
-int
-main (void)
-@{
-  init_table ();
-  return yyparse ();
-@}
-@end group
 @end smallexample
 
 By simply editing the initialization list and adding the necessary include
@@ -2540,7 +2529,11 @@ linked to the front of the list, and a pointer to the object is returned.
 The function @code{getsym} is passed the name of the symbol to look up.  If
 found, a pointer to that symbol is returned; otherwise zero is returned.
 
+@comment file: mfcalc.y
 @smallexample
+#include <stdlib.h> /* malloc. */
+#include <string.h> /* strlen. */
+
 symrec *
 putsym (char const *sym_name, int sym_type)
 @{
@@ -2567,6 +2560,9 @@ getsym (char const *sym_name)
 @}
 @end smallexample
 
+@node Mfcalc Lexer
+@subsection The @code{mfcalc} Lexer
+
 The function @code{yylex} must now recognize variables, numeric values, and
 the single-character arithmetic operators.  Strings of alphanumeric
 characters with a leading letter are recognized as either variables or
@@ -2582,6 +2578,7 @@ returned to @code{yyparse}.
 No change is needed in the handling of numeric values and arithmetic
 operators in @code{yylex}.
 
+@comment file: mfcalc.y
 @smallexample
 @group
 #include <ctype.h>
@@ -2624,7 +2621,10 @@ yylex (void)
       /* Initially make the buffer long enough
          for a 40-character symbol name.  */
       if (length == 0)
-        length = 40, symbuf = (char *)malloc (length + 1);
+        @{
+          length = 40;
+          symbuf = (char *) malloc (length + 1);
+        @}
 
       i = 0;
       do
@@ -2664,6 +2664,34 @@ yylex (void)
 @end group
 @end smallexample
 
+@node Mfcalc Main
+@subsection The @code{mfcalc} Main
+
+The error reporting function is unchanged, and the new version of
+@code{main} includes a call to @code{init_table}:
+
+@comment file: mfcalc.y
+@smallexample
+
+@group
+@group
+/* Called by yyparse on error.  */
+void
+yyerror (char const *s)
+@{
+  fprintf (stderr, "%s\n", s);
+@}
+@end group
+
+int
+main (int argc, char const* argv[])
+@{
+  init_table ();
+  return yyparse ();
+@}
+@end group
+@end smallexample
+
 This program is both powerful and flexible.  You may easily add new
 functions, and it is a simple job to modify this code to install
 predefined variables such as @code{pi} or @code{e} as well.
@@ -7745,7 +7773,7 @@ that allows variable-length arrays.  The default is 200.
 Do not allow @code{YYINITDEPTH} to be greater than @code{YYMAXDEPTH}.
 
 You can generate a deterministic parser containing C++ user code from
-the default (C) skeleton, as well as from the C++ skeleton 
+the default (C) skeleton, as well as from the C++ skeleton
 (@pxref{C++ Parsers}).  However, if you do use the default skeleton
 and want to allow the parsing stack to grow,
 be careful not to use semantic types or location types that require
@@ -9270,7 +9298,8 @@ scanner should use @code{yy::parser::token::FOO}.  The scanner can use
 
 @defcv {Type} {parser} {syntax_error}
 This class derives from @code{std::runtime_error}.  Throw instances of it
-from user actions to raise parse errors.  This is equivalent with first
+from the scanner or from the user actions to raise parse errors.  This is
+equivalent with first
 invoking @code{error} to report the location and message of the syntax
 error, and then to invoke @code{YYERROR} to enter the error-recovery mode.
 But contrary to @code{YYERROR} which can only be invoked from user actions
@@ -9940,7 +9969,7 @@ calcxx_driver::scan_begin ()
   else if (!(yyin = fopen (file.c_str (), "r")))
     @{
       error (std::string ("cannot open ") + file + ": " + strerror(errno));
-      exit (1);
+      exit (EXIT_FAILURE);
     @}
 @}
 
@@ -10674,11 +10703,17 @@ yyparse (char const *file)
 {
   yyin = fopen (file, "r");
   if (!yyin)
-    exit (2);
+  {
+    perror ("fopen");
+    exit (EXIT_FAILURE);
+  }
   /* One token only.  */
   yylex ();
   if (fclose (yyin) != 0)
-    exit (3);
+  {
+    perror ("fclose");
+    exit (EXIT_FAILURE);
+  }
   return 0;
 }
 
@@ -10937,7 +10972,7 @@ transcript of the build session, starting with the invocation of
 send additional files as well (such as `config.h' or `config.cache').
 
 Patches are most welcome, but not required.  That is, do not hesitate to
-send a bug report just because you can not provide a fix.
+send a bug report just because you cannot provide a fix.
 
 Send bug reports to @email{bug-bison@@gnu.org}.