Mfcalc Decl
Previous: <Multi-function Calc=>Multifunct> * Next: <Mfcalc Rules=>MfcalcRulf> * Up: <Multi-function Calc=>Multifunct>

#Wrap on
{fH4}Declarations for {fCode}mfcalc{f}{f}

Here are the C and Bison declarations for the multi-function calculator.

#Wrap off
#fCode
%\{
\#include <math.h>  \/\* For math functions, cos(), sin(), etc. \*\/
\#include "calc.h"  \/\* Contains definition of `symrec'        \*\/
%\}
%union \{
double     val;  \/\* For returning numbers.                   \*\/
symrec  \*tptr;   \/\* For returning symbol-table pointers      \*\/
\}

%token <val>  NUM        \/\* Simple double precision number   \*\/
%token <tptr> VAR FNCT   \/\* Variable and Function            \*\/
%type  <val>  exp

%right '='
%left '-' '+'
%left '\*' '\/'
%left NEG     \/\* Negation--unary minus \*\/
%right '^'    \/\* Exponentiation        \*\/

\/\* Grammar follows \*\/

%%
#f
#Wrap on

The above grammar introduces only two new features of the Bison language.
These features allow semantic values to have various data types
(\*Note <Multiple Types=>MultipleTy>: More Than One Value Type).

The {fCode}%union{f} declaration specifies the entire list of possible types;
this is instead of defining {fCode}YYSTYPE{f}.  The allowable types are now
double-floats (for {fCode}exp{f} and {fCode}NUM{f}) and pointers to entries in
the symbol table.  \*Note <Union Decl=>UnionDecl>: The Collection of Value Types.

Since values can now have various types, it is necessary to associate a
type with each grammar symbol whose semantic value is used.  These symbols
are {fCode}NUM{f}, {fCode}VAR{f}, {fCode}FNCT{f}, and {fCode}exp{f}.  Their
declarations are augmented with information about their data type (placed
between angle brackets).

The Bison construct {fCode}%type{f} is used for declaring nonterminal symbols,
just as {fCode}%token{f} is used for declaring token types.  We have not used
{fCode}%type{f} before because nonterminal symbols are normally declared
implicitly by the rules that define them.  But {fCode}exp{f} must be declared
explicitly so we can specify its value type.  \*Note <Type Decl=>TypeDecl>: Nonterminal Symbols.

