This chapter presents an extention of the calculator described in the tutorial (see 3). This calculator has
more functions and a memory.
9.2New functions
9.2.1Trigonometric and other functions
This calculator can compute some numerical functions (sin, cos, sqrt, ...). The make_op function
(see figure 3.4) has been extended to return these functions. Tokens must also be defined
to scan function names. funct1 defines the name of unaries functions and funct2 defines
the name of binaries functions. Finally the grammar rule of the atoms has been added a
branch to parse functions. The Function non terminal symbol parser unaries and binaries
functions.
9.2.2Memories
The calculator has memories. A memory cell is identified by a name. For example, if the user types
pi = 4 * atan(1), the memory cell named pi will contain the value of and cos(pi) will return
-1.
To display the content of the whole memory, the user can type vars.
The variables are saved in a dictionnary. In fact the parser itself is a dictionnary (the parser inherits
from the dict class).
The START symbol parses a variable creation or a single expression and the Atom parses variable
names (the Var symbol parses a variable name and returns its value).
9.3Source code
9.3.1TPG grammar
The calculator source code can be a grammar for TPG. I.e. the calc.g file is translated into a calc.py
script by TPG. Just type in: