Fortran 95 function parser

This compact function parser is intended for applications where a set of mathematical expressions is specified at runtime as function strings which are then evaluated for a large number of variable values. This is done by compiling the set of function strings into byte code sequences, which can be interpreted very efficiently for the various variable values. No restrictions apply with respect to the number of simultaneously evaluated function expressions and variables.

The function parser is designed as a well documented Fortran 95 module, with extensive error checking during parsing and interpreting of the function expressions. It is easily implemented into existing Fortran 95 programs - test programs and Makefile are included in the package.

Using fparser in your Fortran 90/95 code

1. Module Import

In all program units where you want to use the function parser procedures and variables you must import the module by: USE fparser This command imports only 5 public names: initf, parsef, evalf, EvalErrMsg and EvalErrType, which are explained in the following. The remainder of the module is hidden to the calling program.

2. Initialization

The parser module has to be initialized for the simultaneous evaluation of n functions by calling the module subroutine initp one time in your Fortran code: CALL initf (n) This allocates i=1,...,n internal data structures used by the byte-compiler and subsequently by the bytecode-interpreter.

3. Function parsing

The i-th function string FuncStr is parsed (checked and compiled) into the i-th bytecode by calling the module subroutine parsef: CALL parsef (i, FuncStr, Var) The variable names as they appear in the string FuncStr have to be passed in the one-dimensional string array Var (zero size of Var is acceptable). The number of variables is implicitly passed by the dimension of this array. For some notes on the syntax of the function string see below.

4. Function evaluation

The i-th function value is evaluated for a specific set of variable values by calling the module function evalf: a = evalf (i, Val) The variable values are passed in the one-dimensional array Val which must have the same dimension as array Var.

Error handling

An error in the function parsing step leads to a detailed error message (Type and position of error) and program termination. An error during function evaluation returns a function value of 0.0 and sets the error flag EvalErrType (also imported by the USE statement) to a value > 0 (EvalErrType=0 indicates no error). An error message from the bytecode-interpreter can be obtained by calling the character function EvalErrMsg () without any argument.

Function string syntax

Although they have to be passed as array elements of the same declared length (Fortran 95 restriction), the variable names can be of arbitrary actual length for the parser. Parsing for variables is case sensitive. The syntax of the function string is similar to the Fortran convention. Mathematical Operators recognized are +, -, *, /, ** or alternatively ^, whereas symbols for brackets must be (). The function parser recognizes the (single argument) Fortran 95 intrinsic functions abs, exp, log10, log, sqrt, sinh, cosh, tanh, sin, cos, tan, asin, acos, atan. Parsing for intrinsic functions is case INsensitive. Operations are evaluated in the correct order: () expressions in brackets first -A unary minus (or plus) A**B A^B exponentiation (A raised to the power B) A*B A/B multiplication and division A+B A-B addition and subtraction The function string can contain integer or real constants. To be recognized as explicit constants these must conform to the format [+|-][nnn][.nnn][e|E|d|D[+|-]nnn] where nnn means any number of digits. The mantissa must contain at least one digit before or following an optional decimal point. Valid exponent identifiers are 'e', 'E', 'd' or 'D'. If they appear they must be followed by a valid exponent! Logo fparser project page on sourceforge