home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
285_01
/
bisoninf.2
< prev
next >
Wrap
Text File
|
1990-07-10
|
52KB
|
1,398 lines
Info file bison.info, produced by Makeinfo, -*- Text -*- from input
file bison.texinfo.
This file documents the Bison parser generator.
Copyright (C) 1988 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the sections entitled ``Bison General Public License'' and
``Conditions for Using Bison'' are included exactly as in the
original, and provided that the entire resulting derived work is
distributed under the terms of a permission notice identical to this
one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the text of the translations of the sections
entitled ``Bison General Public License'' and ``Conditions for Using
Bison'' must be approved for accuracy by the Foundation.
File: bison.info, Node: Mfcalc Decl, Next: Mfcalc Rules, Prev: Multi-function Calc, Up: Multi-function Calc
Declarations for `mfcalc'
-------------------------
Here are the C and Bison declarations for the multi-function
calculator.
%{
#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 */
%%
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::.).
The `%union' declaration specifies the entire list of possible types;
this is instead of defining `YYSTYPE'. The allowable types are now
double-floats (for `exp' and `NUM') and pointers to entries in the
symbol table. *Note Union Decl::.
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 `NUM', `VAR', `FNCT', and `exp'. Their declarations are
augmented with information about their data type (placed between
angle brackets).
The Bison construct `%type' is used for declaring nonterminal
symbols, just as `%token' is used for declaring token types. We have
not used `%type' before because nonterminal symbols are normally
declared implicitly by the rules that define them. But `exp' must be
declared explicitly so we can specify its value type. *Note Type
Decl::.
File: bison.info, Node: Mfcalc Rules, Next: Mfcalc Symtab, Prev: Mfcalc Decl, Up: Multi-function Calc
Grammar Rules for `mfcalc'
--------------------------
Here are the grammar rules for the multi-function calculator. Most
of them are copied directly from `calc'; three rules, those which
mention `VAR' or `FNCT', are new.
input: /* empty */
| input line
;
line:
'\n'
| exp '\n' { printf ("\t%.10g\n", $1); }
| error '\n' { yyerrok; }
;
exp: NUM { $$ = $1; }
| VAR { $$ = $1->value.var; }
| VAR '=' exp { $$ = $3; $1->value.var = $3; }
| FNCT '(' exp ')' { $$ = (*($1->value.fnctptr))($3); }
| exp '+' exp { $$ = $1 + $3; }
| exp '-' exp { $$ = $1 - $3; }
| exp '*' exp { $$ = $1 * $3; }
| exp '/' exp { $$ = $1 / $3; }
| '-' exp %prec NEG { $$ = -$2; }
| exp '^' exp { $$ = pow ($1, $3); }
| '(' exp ')' { $$ = $2; }
;
/* End of grammar */
%%
File: bison.info, Node: Mfcalc Symtab, Prev: Mfcalc Rules, Up: Multi-function Calc
Managing the Symbol Table for `mfcalc'
--------------------------------------
The multi-function calculator requires a symbol table to keep track
of the names and meanings of variables and functions. This doesn't
affect the grammar rules (except for the actions) or the Bison
declarations, but it requires some additional C functions for support.
The symbol table itself consists of a linked list of records. Its
definition, which is kept in the header `calc.h', is as follows. It
provides for either functions or variables to be placed in the table.
/* Data type for links in the chain of symbols. */
struct symrec
{
char *name; /* name of symbol */
int type; /* type of symbol: either VAR or FNCT */
union {
double var; /* value of a VAR */
double (*fnctptr)(); /* value of a FNCT */
} value;
struct symrec *next; /* link field */
};
typedef struct symrec symrec;
/* The symbol table: a chain of `struct symrec'. */
extern symrec *sym_table;
symrec *putsym ();
symrec *getsym ();
The new version of `main' includes a call to `init_table', a function
that initializes the symbol table. Here it is, and `init_table' as
well:
#include <stdio.h>
main()
{
init_table ();
yyparse ();
}
yyerror (s) /* Called by yyparse on error */
char *s;
{
printf ("%s\n", s);
}
struct init
{
char *fname;
double (*fnct)();
};
struct init arith_fncts[]
= {
"sin", sin,
"cos", cos,
"atan", atan,
"ln", log,
"exp", exp,
"sqrt", sqrt,
0, 0
};
/* The symbol table: a chain of `struct symrec'. */
symrec *sym_table = (symrec *)0;
init_table () /* puts arithmetic functions in table. */
{
int i;
symrec *ptr;
for (i = 0; arith_fncts[i].fname != 0; i++)
{
ptr = putsym (arith_fncts[i].fname, FNCT);
ptr->value.fnctptr = arith_fncts[i].fnct;
}
}
By simply editing the initialization list and adding the necessary
include files, you can add additional functions to the calculator.
Two important functions allow look-up and installation of symbols in
the symbol table. The function `putsym' is passed a name and the
type (`VAR' or `FNCT') of the object to be installed. The object is
linked to the front of the list, and a pointer to the object is
returned. The function `getsym' is passed the name of the symbol to
look up. If found, a pointer to that symbol is returned; otherwise
zero is returned.
symrec *
putsym (sym_name,sym_type)
char *sym_name;
int sym_type;
{
symrec *ptr;
ptr = (symrec *) malloc (sizeof(symrec));
ptr->name = (char *) malloc (strlen(sym_name)+1);
strcpy (ptr->name,sym_name);
ptr->type = sym_type;
ptr->value.var = 0; /* set value to 0 even if fctn. */
ptr->next = (struct symrec *)sym_table;
sym_table = ptr;
return ptr;
}
symrec *
getsym (sym_name)
char *sym_name;
{
symrec *ptr;
for (ptr = sym_table; ptr != (symrec *) 0;
ptr = (symrec *)ptr->next)
if (strcmp (ptr->name,sym_name) == 0)