X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/f9c75dd016198f9b8c255f1bb139360eef3f071f..108df813c466663f80ed3cd5c74ec114369863d7:/doc/bison.texinfo?ds=sidebyside diff --git a/doc/bison.texinfo b/doc/bison.texinfo index 8391e714..cd687b90 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -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 @@ -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 @@ -2332,6 +2336,7 @@ Here are the C and Bison declarations for the multi-function calculator. @group %@{ #include /* For printf, etc. */ + #include /* For pow, used in the grammar. */ #include "calc.h" /* Contains definition of `symrec'. */ int yylex (void); void yyerror (char const *); @@ -2466,23 +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 - -@group -/* Called by yyparse on error. */ -void -yyerror (char const *s) -@{ - printf ("%s\n", s); -@} -@end group - @group struct init @{ @@ -2492,7 +2485,6 @@ struct init @end group @group -#include /* Math functions, cos(), sin(), etc. */ struct init const arith_fncts[] = @{ @{ "atan", atan @}, @@ -2525,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 @@ -2577,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 @@ -2678,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.