@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
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 "calc.h" /* Contains definition of `symrec'. */
int yylex (void);
void yyerror (char const *);
%@}
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 */
@group
line:
'\n'
- | exp '\n' @{ printf ("\t%.10g\n", $1); @}
- | error '\n' @{ yyerrok; @}
+ | exp '\n' @{ printf ("%.10g\n", $1); @}
+ | error '\n' @{ yyerrok; @}
;
@end group
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. */
function that initializes the symbol table. Here it is, and
@code{init_table} as well:
+@comment file: mfcalc.y
@smallexample
#include <stdio.h>
@end group
@group
+#include <math.h> /* Math functions, cos(), sin(), etc. */
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
@group
/* Put arithmetic functions in table. */
+static
void
init_table (void)
@{
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)
@{
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>
/* 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
@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