This project is read-only.


This is a library designed to understand basic mathematical strings and parse them into an object which can be evaluated at run time.
It is designed to allow for variables to be evaluated by a delegate.

Sample Code Creating a Parsed Component and Evaluating

IComponent[] rawComponents;
IComponent parsed = null;
if (Parser.GetComponents(txt, out rawComponents))
parsed = Parser.GetComponentTree(rawComponents);

The first first command (Parser.GetComponets) attempts to parse the string and if successful returns true.

The rawComponents array contains the different sections of the string, however at this point it has not applied any order of operation to it.

The second command Parser.GetComponentTree applies the order of operation to form a single component which can then be used to evaluate the string:

object newVal = parsed.Evaluate(getVariable);

The getVariable is a delegate to a function with a single string input returning the value of that variable.

By default, Parser.GetComponentTree pre-evaluates and constant parts of the ComponentTree. This can be stopped by calling it like:

parsed = Parser.GetComponentTree(rawComponents, false);


When evaluating the Component, functions are looked up in the Parser object. This is done using a case-insensitive match in a dictionary.

You can register most of the function in System.Math by calling:


This will register any public static function in System.Math which takes only double parameters. As Round takes a double and an int, it is wrapped as a Round(double, double). It also registers and If function.

If you wish to register a custom function,


It currently makes assumption that:

  • . is the decimal point
  • , separate terms in functions
  • , cannot currently be used in constants

Supported Operators


+ Addition
Unary Plus
+a (should support multiple)
- Subtraction
Unary Minus
-a (should support multiple)
/ Division a/b
* Multiplication a*b
^ Power a^b
% Modulo
Percentage Value (divide by 100)
Per Mille Value (divide by 1000) a‰


Equals a=b
Does Not Equal a!=b
< less than a<b
<= less than or equal to a<=b
> greater than a>b
>= greater than or equal to a>=b

Boolean Logic

! Not !a
| Exclusive Or a|b
|| Or a||b
&& And a&&b

Text Operators

& Concatenate a&b

Assignment / Equals

  • Want to be able to assign a value to a variable inside the expression
  • This would be a switch in the library in which case = would be assignment and == would Equals operator


Any numerical constant can be entered.

Commas cannot be used in constants.

In addition some keywords are recognised as constants:

  • true
  • false
  • pi (Math.PI)
  • e (Math.E)


  • 5
  • 5%
  • 1.23
  • -12.4


Numerical Characters

0 1 2 3 4 5 6 7 8 9 .

Operator Characters

+ – / * ^ % ‰ = ! < > | &

Text Constants

Must start and end with a “

Follows the Excel rules – i.e. “” for a “ in constant

Date Constants

Must start and end with a #

Formats supported: yyyy-MM-dd, dd-MMM-yy, dd-MMM-yyyy


( ) [ ]

Must be paired or will result in parser error

Variables/Functions Names

Variable and function names can consist of a-z, A-Z, _, 0-9 but cannot start with a number.

White Space

Space, Tab, Return characters will be ignored

Any Other Character

Will cause an unable to parse error

Last edited Jun 3, 2012 at 11:08 PM by jdunkerley, version 12


No comments yet.