X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/71b52b1342c65a5497f6b3780481ff2deb932a56..aaaa2aaef40eed197ebbd0bd45d8a66606cdb19c:/doc/bison.texinfo diff --git a/doc/bison.texinfo b/doc/bison.texinfo index 79dd3311..26639b52 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -1274,8 +1274,9 @@ will suffice. Otherwise, we suggest @example %@{ - #if __STDC_VERSION__ < 199901 && ! defined __GNUC__ && ! defined inline - #define inline + #if (__STDC_VERSION__ < 199901 && ! defined __GNUC__ \ + && ! defined inline) + # define inline #endif %@} @end example @@ -1464,11 +1465,11 @@ simple program, all the rest of the program can go here. @cindex simple examples @cindex examples, simple -Now we show and explain three sample programs written using Bison: a +Now we show and explain several sample programs written using Bison: a reverse polish notation calculator, an algebraic (infix) notation -calculator, and a multi-function calculator. All three have been tested -under BSD Unix 4.3; each produces a usable, though limited, interactive -desk-top calculator. +calculator --- later extended to track ``locations'' --- +and a multi-function calculator. All +produce usable, though limited, interactive desk-top calculators. These examples are simple, but Bison grammars for real programming languages are written the same way. You can copy these examples into a @@ -1570,24 +1571,28 @@ Here are the grammar rules for the reverse polish notation calculator. @comment file: rpcalc.y @example +@group input: /* empty */ | input line ; +@end group +@group line: '\n' | exp '\n' @{ printf ("%.10g\n", $1); @} ; +@end group +@group exp: NUM @{ $$ = $1; @} | exp exp '+' @{ $$ = $1 + $2; @} | exp exp '-' @{ $$ = $1 - $2; @} | exp exp '*' @{ $$ = $1 * $2; @} | exp exp '/' @{ $$ = $1 / $2; @} - /* Exponentiation */ - | exp exp '^' @{ $$ = pow ($1, $2); @} - /* Unary minus */ - | exp 'n' @{ $$ = -$1; @} + | exp exp '^' @{ $$ = pow ($1, $2); @} /* Exponentiation */ + | exp 'n' @{ $$ = -$1; @} /* Unary minus */ ; +@end group %% @end example @@ -1846,7 +1851,9 @@ here is the definition we will use: @example @group #include +@end group +@group /* Called by yyparse on error. */ void yyerror (char const *s) @@ -1952,6 +1959,7 @@ parentheses nested to arbitrary depth. Here is the Bison code for @example /* Infix notation calculator. */ +@group %@{ #define YYSTYPE double #include @@ -1959,32 +1967,41 @@ parentheses nested to arbitrary depth. Here is the Bison code for int yylex (void); void yyerror (char const *); %@} +@end group +@group /* Bison declarations. */ %token NUM %left '-' '+' %left '*' '/' %precedence NEG /* negation--unary minus */ %right '^' /* exponentiation */ +@end group %% /* The grammar follows. */ +@group input: /* empty */ | input line ; +@end group +@group line: '\n' | exp '\n' @{ printf ("\t%.10g\n", $1); @} ; +@end group -exp: NUM @{ $$ = $1; @} - | exp '+' exp @{ $$ = $1 + $3; @} - | exp '-' exp @{ $$ = $1 - $3; @} - | exp '*' exp @{ $$ = $1 * $3; @} - | exp '/' exp @{ $$ = $1 / $3; @} - | '-' exp %prec NEG @{ $$ = -$2; @} +@group +exp: NUM @{ $$ = $1; @} + | exp '+' exp @{ $$ = $1 + $3; @} + | exp '-' exp @{ $$ = $1 - $3; @} + | exp '*' exp @{ $$ = $1 * $3; @} + | exp '/' exp @{ $$ = $1 / $3; @} + | '-' exp %prec NEG @{ $$ = -$2; @} | exp '^' exp @{ $$ = pow ($1, $3); @} - | '(' exp ')' @{ $$ = $2; @} + | '(' exp ')' @{ $$ = $2; @} ; +@end group %% @end example @@ -2521,10 +2538,9 @@ void init_table (void) @{ int i; - symrec *ptr; for (i = 0; arith_fncts[i].fname != 0; i++) @{ - ptr = putsym (arith_fncts[i].fname, FNCT); + symrec *ptr = putsym (arith_fncts[i].fname, FNCT); ptr->value.fnctptr = arith_fncts[i].fnct; @} @} @@ -2550,8 +2566,7 @@ found, a pointer to that symbol is returned; otherwise zero is returned. symrec * putsym (char const *sym_name, int sym_type) @{ - symrec *ptr; - ptr = (symrec *) malloc (sizeof (symrec)); + symrec *ptr = (symrec *) malloc (sizeof (symrec)); ptr->name = (char *) malloc (strlen (sym_name) + 1); strcpy (ptr->name,sym_name); ptr->type = sym_type; @@ -2569,7 +2584,7 @@ getsym (char const *sym_name) symrec *ptr; for (ptr = sym_table; ptr != (symrec *) 0; ptr = (symrec *)ptr->next) - if (strcmp (ptr->name,sym_name) == 0) + if (strcmp (ptr->name, sym_name) == 0) return ptr; return 0; @} @@ -2628,24 +2643,18 @@ yylex (void) /* Char starts an identifier => read the name. */ if (isalpha (c)) @{ - symrec *s; + /* Initially make the buffer long enough + for a 40-character symbol name. */ + static size_t length = 40; static char *symbuf = 0; - static int length = 0; + symrec *s; int i; @end group - -@group - /* Initially make the buffer long enough - for a 40-character symbol name. */ - if (length == 0) - @{ - length = 40; - symbuf = (char *) malloc (length + 1); - @} + if (!symbuf) + symbuf = (char *) malloc (length + 1); i = 0; do -@end group @group @{ /* If buffer is full, make it bigger. */ @@ -2689,8 +2698,6 @@ The error reporting function is unchanged, and the new version of @comment file: mfcalc.y @smallexample - -@group @group /* Called by yyparse on error. */ void @@ -2700,6 +2707,7 @@ yyerror (char const *s) @} @end group +@group int main (int argc, char const* argv[]) @{ @@ -4136,6 +4144,7 @@ By default, @code{YYLLOC_DEFAULT} is defined this way: @end group @end smallexample +@noindent where @code{YYRHSLOC (rhs, k)} is the location of the @var{k}th symbol in @var{rhs} when @var{k} is positive, and the location of the symbol just before the reduction when @var{k} and @var{n} are both zero. @@ -8326,9 +8335,9 @@ exp (9) @cindex pointed rule @cindex rule, pointed Bison then proceeds onto the automaton itself, describing each state -with it set of @dfn{items}, also known as @dfn{pointed rules}. Each -item is a production rule together with a point (marked by @samp{.}) -that the input cursor. +with its set of @dfn{items}, also known as @dfn{pointed rules}. Each +item is a production rule together with a point (@samp{.}) marking +the location of the input cursor. @example state 0 @@ -8345,7 +8354,7 @@ beginning of the parsing, in the initial rule, right before the start symbol (here, @code{exp}). When the parser returns to this state right after having reduced a rule that produced an @code{exp}, the control flow jumps to state 2. If there is no such transition on a nonterminal -symbol, and the lookahead is a @code{NUM}, then this token is shifted on +symbol, and the lookahead is a @code{NUM}, then this token is shifted onto the parse stack, and the control flow jumps to state 1. Any other lookahead triggers a syntax error.'' @@ -8358,8 +8367,7 @@ report lists @code{NUM} as a lookahead token because @code{NUM} can be at the beginning of any rule deriving an @code{exp}. By default Bison reports the so-called @dfn{core} or @dfn{kernel} of the item set, but if you want to see more detail you can invoke @command{bison} with -@option{--report=itemset} to list all the items, include those that can -be derived: +@option{--report=itemset} to list the derived items as well: @example state 0 @@ -8411,11 +8419,11 @@ state 2 @noindent In state 2, the automaton can only shift a symbol. For instance, -because of the item @samp{exp -> exp . '+' exp}, if the lookahead if -@samp{+}, it will be shifted on the parse stack, and the automaton -control will jump to state 4, corresponding to the item @samp{exp -> exp -'+' . exp}. Since there is no default action, any other token than -those listed above will trigger a syntax error. +because of the item @samp{exp -> exp . '+' exp}, if the lookahead is +@samp{+} it is shifted onto the parse stack, and the automaton +jumps to state 4, corresponding to the item @samp{exp -> exp '+' . exp}. +Since there is no default action, any lookahead not listed triggers a syntax +error. @cindex accepting state The state 3 is named the @dfn{final state}, or the @dfn{accepting @@ -9358,9 +9366,9 @@ The types for semantic values and locations (if enabled). @end defcv @defcv {Type} {parser} {token} -A structure that contains (only) the definition of the tokens as the -@code{yytokentype} enumeration. To refer to the token @code{FOO}, the -scanner should use @code{yy::parser::token::FOO}. The scanner can use +A structure that contains (only) the @code{yytokentype} enumeration, which +defines the tokens. To refer to the token @code{FOO}, +use @code{yy::parser::token::FOO}. The scanner can use @samp{typedef yy::parser::token token;} to ``import'' the token enumeration (@pxref{Calc++ Scanner}). @end defcv @@ -10044,7 +10052,7 @@ calcxx_driver::scan_begin () yyin = stdin; else if (!(yyin = fopen (file.c_str (), "r"))) @{ - error (std::string ("cannot open ") + file + ": " + strerror(errno)); + error ("cannot open " + file + ": " + strerror(errno)); exit (EXIT_FAILURE); @} @}