I developed this small ANSI C program at the university. It is a simple formula parser, useful for many situations, like an intelligent input in dialog boxes.

The gag in this parser is the possibility to compile formulas into p-code to get a major performance improvement while calculating. This is useful for formula plotters, etc., that uses the same formula only with different values.

In Parse.h some configurations can be done:

  • ‘‘Dbl’’ is a typedef for the type of the calculation of the parser. Normally it should be ‘‘float’’ or ‘‘double’’. ‘‘TERM_COMPILER’’ activates the p-code compiler. If you use the formula only once, than you should disable it, otherwise enable it.
  • buildTerm is called to compile an ASCII expression into p-code. If the ‘‘TERM_COMPILER’’ is not active, this routine does nothing.
  • ‘‘calcTerm’’, ‘‘calcTerm1’’, ‘‘calcTerm2’’, ‘‘calcTerm3’’ calculates the current expression with no, 1, 2 or 3 parameters. The parameters are put into the variables ‘‘X’’, ‘‘Y’’ and ‘‘Z’’. Undefined variables have the value ‘‘HUGE_VAL’’.

Besides the trivial operations (‘’+‘’, ‘’-‘’, ‘’*‘’, ‘’/‘’, ‘’^‘’, with brackets) the parser also know the following functions:

Name Description
‘‘PI’’ the value PI (3.1415926…)
‘‘E’’ the value e (2.7182818)
‘‘X’’, ‘‘Y’’, ‘‘Z’’ the values X, Y, Z
‘‘acos’’, ‘‘arccos’’ the function cos–1(a)
‘‘asin’’, ‘‘arcsin’’ the function sin–1(a)
‘‘atan’’, ‘‘arctan’’ the function tan–1(a)
‘‘atan2’’, ‘‘arctan2’’ the function tan–1(a,b)
‘‘ceil’’ the function ceil(a)
‘‘cos’’ the function cos(a)
‘‘cosh’’ the function cosh(a)
‘‘exp’’ the function exp(a)
‘‘floor’’ the function floor(a)
‘‘fmod’’ the function fmod(a)
‘‘log’’ the function log(a)
‘‘log10’’ the function log10(a)
‘‘pow’’ the function pow(a,b)
‘‘sin’’ the function sin(a)
‘‘sinh’’ the function sinh(a)
‘‘sqrt’’ the function sqrt(a)
‘‘tan’’ the function tan(a)
‘‘tanh’’ the function tanh(a)

Sourcecode for the Parser