mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-21 10:13:03 +00:00
doc: mfcalc: send errors to stderr.
* doc/bison.texinfo (Mfcalc Lexer): New. (Mfcalc Main): Move the definition of main and yyerror here, for clarity. Let yyerror report on stderr.
This commit is contained in:
@@ -178,6 +178,8 @@ Multi-Function Calculator: @code{mfcalc}
|
|||||||
* Mfcalc Declarations:: Bison declarations for multi-function calculator.
|
* Mfcalc Declarations:: Bison declarations for multi-function calculator.
|
||||||
* Mfcalc Rules:: Grammar rules for the calculator.
|
* Mfcalc Rules:: Grammar rules for the calculator.
|
||||||
* Mfcalc Symbol Table:: Symbol table management subroutines.
|
* Mfcalc Symbol Table:: Symbol table management subroutines.
|
||||||
|
* Mfcalc Lexer:: The lexical analyzer.
|
||||||
|
* Mfcalc Main:: The controlling function.
|
||||||
|
|
||||||
Bison Grammar Files
|
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 Declarations:: Bison declarations for multi-function calculator.
|
||||||
* Mfcalc Rules:: Grammar rules for the calculator.
|
* Mfcalc Rules:: Grammar rules for the calculator.
|
||||||
* Mfcalc Symbol Table:: Symbol table management subroutines.
|
* Mfcalc Symbol Table:: Symbol table management subroutines.
|
||||||
|
* Mfcalc Lexer:: The lexical analyzer.
|
||||||
|
* Mfcalc Main:: The controlling function.
|
||||||
@end menu
|
@end menu
|
||||||
|
|
||||||
@node Mfcalc Declarations
|
@node Mfcalc Declarations
|
||||||
@@ -2466,23 +2470,11 @@ symrec *getsym (char const *);
|
|||||||
@end group
|
@end group
|
||||||
@end smallexample
|
@end smallexample
|
||||||
|
|
||||||
The new version of @code{main} includes a call to @code{init_table}, a
|
The new version of @code{main} will call @code{init_table} to initialize
|
||||||
function that initializes the symbol table. Here it is, and
|
the symbol table:
|
||||||
@code{init_table} as well:
|
|
||||||
|
|
||||||
@comment file: mfcalc.y
|
@comment file: mfcalc.y
|
||||||
@smallexample
|
@smallexample
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
@group
|
|
||||||
/* Called by yyparse on error. */
|
|
||||||
void
|
|
||||||
yyerror (char const *s)
|
|
||||||
@{
|
|
||||||
printf ("%s\n", s);
|
|
||||||
@}
|
|
||||||
@end group
|
|
||||||
|
|
||||||
@group
|
@group
|
||||||
struct init
|
struct init
|
||||||
@{
|
@{
|
||||||
@@ -2525,15 +2517,6 @@ init_table (void)
|
|||||||
@}
|
@}
|
||||||
@}
|
@}
|
||||||
@end group
|
@end group
|
||||||
|
|
||||||
@group
|
|
||||||
int
|
|
||||||
main (void)
|
|
||||||
@{
|
|
||||||
init_table ();
|
|
||||||
return yyparse ();
|
|
||||||
@}
|
|
||||||
@end group
|
|
||||||
@end smallexample
|
@end smallexample
|
||||||
|
|
||||||
By simply editing the initialization list and adding the necessary include
|
By simply editing the initialization list and adding the necessary include
|
||||||
@@ -2577,6 +2560,9 @@ getsym (char const *sym_name)
|
|||||||
@}
|
@}
|
||||||
@end smallexample
|
@end smallexample
|
||||||
|
|
||||||
|
@node Mfcalc Lexer
|
||||||
|
@subsection The @code{mfcalc} Lexer
|
||||||
|
|
||||||
The function @code{yylex} must now recognize variables, numeric values, and
|
The function @code{yylex} must now recognize variables, numeric values, and
|
||||||
the single-character arithmetic operators. Strings of alphanumeric
|
the single-character arithmetic operators. Strings of alphanumeric
|
||||||
characters with a leading letter are recognized as either variables or
|
characters with a leading letter are recognized as either variables or
|
||||||
@@ -2678,6 +2664,34 @@ yylex (void)
|
|||||||
@end group
|
@end group
|
||||||
@end smallexample
|
@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
|
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
|
functions, and it is a simple job to modify this code to install
|
||||||
predefined variables such as @code{pi} or @code{e} as well.
|
predefined variables such as @code{pi} or @code{e} as well.
|
||||||
|
|||||||
Reference in New Issue
Block a user