# TExpressionParser

A flexible and fast expression parser object for logical and mathematical functions.

Author: Egbert van Nes

Status: Freeware with source

Version: 1.1.4

Delphi version: tested with D4, D5 and Kylix (probably usable for D2 and D3 too)

Date: September 2002

Download with source code (36 K)

- Algorithm
- New features
- Precedence of operands
- New in version 1.1
- New in version 1.1.1
- New in version 1.1.2
- New in version 1.1.3
- New in version 1.1.4
- Contact with the author

### Algorithm

The fast evaluation algorithm ('pseudo-compiler' generating a linked list that evaluates fast) is based upon TParser - an extremely fast component for parsing and evaluating mathematical expressions ('pseudo-compiled' code is only 40-80% slower than compiled Delphi code).

See also: TParser (by Renate Schaaf , 1993; Alin Flaider , 1996; Version 9-10: Stefan Hoffmeister, 1996-1997)

I used that valuable free parser for some years but needed to add logical operands, which was more difficult for me than rewriting the parser.

TExpressionParser uses also two stages: (1) compiling creating a linked list which contains addresses of procedures and arguments ('slow' process) (2) evaluating the linked list, which is extremely fast. The parser is especially suitable for repeated evaluation of expressions that include variables.

### New features

TExpressionParser is approximately equally fast in evaluating expressions as TParser, but the compiling is made object oriented, and programmed recursively, requiring much less code and making it easier to customize the parser.

From version 1.1 on optimization is added, making repeated evaluation often even faster.

Furthermore, there are several operands added:

- comparison: > < <> = <= >= (work also on strings)
- logical: and or xor not
- factorial: !
- percentage: %
- assign to variables: :=

User defined functions can have maximal maxArg (=4) parameters set MaxArg (in unit ParseClass) to a higher value if needed.

- The required format of the expression is Pascal style (optionally C
^{++}style operands are also supported) with the following additional operands: - factorial (x!)
- power (x^y)

percentage (x%)

Implicit multiplying is not supported: e.g. (X+1)(24-3) generates a syntax error and should be replaced by (x+1)*(24-3)

Logical functions evaluate in 0 if False and 1 if True The AsString property returns True/False if the expression is logical.

The comparison functions (> <> < etc.) work also with string constants ('string') and string variables. These comparisons are not case sensitive.

### Precedence of operands

The precedence of the operands is little different from Pascal (Delphi), giving a lower precedence to logical operands, as these by default only act on Booleans (and not on integers like in Pascal). This behavior is easily adjustable.

- (highest): ! -x +x %
- ^
- * / div mod
- + -
- > >= < <= <> =
- not
- or and xor
- (lowest): :=

This precedence order is easily customizable by overriding/changing the FillExpressList method (the precedence order is defined there).

You can use user-defined variables in the expressions and also assign to variables using the := operand

The use of this object is very simple, therefore it doesn't seem necessary to make a non-visual component of it.

### New in version 1.1:

Optimization, increasing the efficiency for evaluating an expression many times (with a variable in the expression). The 'compiler' then removes constant expressions and replaces these with the evaluated result.

e.g. 4*4*x becomes 16*x

ln(5)+3*x becomes 1.609437912+3*x

limitation:

4*x+3+3+5 evaluates as 4*x+3+3+5 (due to precedence rules)

whereas:

4*x+(3+3+5) becomes 4*x+11 (use brackets to be sure that constant expressions are removed by the compiler)

### New in Version 1.1.1

- Evaluation of hexadecimal numbers (e.g. $FF, the $-sign for hexadecimals is adjustable) and show result as hex.
- Changes in class implementation

#### New in Version 1.1.2

- The variable DecimalSeparator (SysUtils) now determines the decimal separator. If the decimal separator is a comma then the function argument separator is a semicolon ';'

### New in Version 1.1.3

- Rearranged the classes, added a basic class TCustomParser for maximal flexibility.
- Multiline formula parser (a contribution kindly offered by Xavier Mor-Mur, xmormur@telepolis.com, xmormur@teleline.es)
- New example application for multiline formula's
- ++ and -- support (like C++)

### New in Version 1.1.4

- Changed the implementation of DecimalSeparator (DecimSeparator is a property now that by default equals SysUtils.DecimalSeparator).
- Several bug fixes (memory leaks).
- Speed fix for NaN evaluation.
- New example application for testing the evaluation speed of TExpressionParser.

### Contact with the author

author: Egbert van Nes

You are encouraged to send bug reports.