Multi-function Calc
Previous: <Simple Error Recovery=>SimpleErro> * Next: <Exercises=>Exercises> * Up: <Examples=>Examples>

#Wrap on
{fH3}Multi-Function Calculator: {fCode}mfcalc{f}{f}

Now that the basics of Bison have been discussed, it is time to move on to
a more advanced problem.  The above calculators provided only five
functions, {fEmphasis}+{f}, {fEmphasis}-{f}, {fEmphasis}\*{f}, {fEmphasis}\/{f} and {fEmphasis}^{f}.  It would
be nice to have a calculator that provides other mathematical functions such
as {fCode}sin{f}, {fCode}cos{f}, etc.

It is easy to add new operators to the infix calculator as long as they are
only single-character literals.  The lexical analyzer {fCode}yylex{f} passes
back all non-number characters as tokens, so new grammar rules suffice for
adding a new operator.  But we want something more flexible: built-in
functions whose syntax has this form:

#Wrap off
#fCode
{fStrong}function\_name{f} ({fStrong}argument{f})
#f
#Wrap on

At the same time, we will add memory to the calculator, by allowing you
to create named variables, store values in them, and use them later.
Here is a sample session with the multi-function calculator:

#Wrap off
#fCode
% mfcalc
pi = 3.141592653589
3.1415926536
sin(pi)
0.0000000000
alpha = beta1 = 2.3
2.3000000000
alpha
2.3000000000
ln(alpha)
0.8329091229
exp(ln(beta1))
2.3000000000
%
#f
#Wrap on

Note that multiple assignment and nested function calls are permitted.

#Wrap off
<Decl=>MfcalcDecl>:      Bison declarations for multi-function calculator.
<Rules=>MfcalcRule>:    Grammar rules for the calculator.
<Symtab=>MfcalcSymt>:  Symbol table management subroutines.
#Wrap on

