Click here to Skip to main content
Click here to Skip to main content

An expression evaluator written in VB.NET

, 14 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
An expression evaluator written in VB.NET.



There is a new version (still very simple), at the address below: 

http://www.codeproject.com/Articles/13779/The-expression-evaluator-revisited-Eval-function-i 



Introduction

My company needed a small expression evaluator to use in our .NET application. Using the .NET framework compilation capabilities seem to be the most obvious way to make an evaluator. However, in practice this technique has a nasty side effect, it looks like it creates a new DLL in memory each time you evaluate your function and it seems nearly impossible to unload the DLL. You can refer to remarks at the end of the article Evaluating Mathematical Expressions by Compiling C# Code at Runtime for more details.

This evaluator is neither using CodeDOM nor trying to compile VB source. On the contrary, it parses your expression and evaluates its value.

Compared to other projects that I have seen, this evaluator can do the following:

  • access and process string expressions.

    You can evaluate "Hello" + " " + "world"

  • access and process objects.

    You can evaluate ThisForm.Left.

  • it also offers easy extensibility.

You can add any number of custom functions without having to change the evaluator code.

Using the code

The evaluator can be run with just two lines of code:

Dim mEvaluator As New Evaluator 
Dim res As integer = CInt(mEvaluator.Eval("1+1"))

How to provide variables for the evaluator

The evaluator raises an event GetVariable when a keyword is not detected. There is no need for you to publish all the variables and then run the eval. On the contrary, you can provide an on demand function which provides only the needed variables:

 Private Sub Evaluator1_GetVariable(ByVal name As String, _
            ByRef value As Object) Handles Evaluator1.GetVariable
    Select Case name
      Case "anumber"
        value = 5.0
      Case "theForm"
        value = Me
      Case "adate"
        value = #1/1/2005#
    End Select
End Sub

How to extend the evaluator with custom functions

The member functions found in the class EvalFunctions are automatically used by the evaluator. In this example, you can see how we can make the evaluator implement the sin and now functions:

Public Class EvalFunctions 
   Function sin(ByVal v As Double) As Double
     Return Math.Sin(v)
   End Function

   Function now() As DateTime
     Return Microsoft.VisualBasic.Now
   End Function

As you can see you don't need much wrapping, the function can be written and used straightaway in this class. Note however that the evaluator does not make any distinction between the Integers and Doubles. Therefore, remember to use Doubles and not Integers for your function parameters.

How does this work?

The evaluator is made of a classic Tokenizer followed by a classic Parser. I wrote both of them in VB, without using any Lex or Bisons tools. The aim was readability over speed. Tokenizing, parsing and execution is done in one pass. This is elegant and at the same time quite efficient because the evaluator never looks ahead or back, more than one character.

The tokenizer

It reads the characters one by one and changes its state according to the characters it encounters. When it recognizes one of the recognized Token types, it returns it to the parser. If it does not recognize a character, it will raise a syntax error exception.

' Recognized token types :
Private Enum eTokenType
 none                   ' temporary state
 end_of_formula         ' when the tokenizer reach the end
 operator_plus          ' +
 operator_minus         ' -
 operator_mul           ' *
 operator_div           ' /
 operator_percent       ' %
 open_parenthesis       ' (
 comma                  ' ,
 dot                    ' .
 close_parenthesis      ' )
 operator_ne            ' <>
 operator_gt            ' <=
 operator_ge            ' >=
 operator_eq            ' =
 operator_le            ' <=
 operator_lt            ' <
 operator_and           ' AND
 operator_or            ' OR
 operator_not           ' NOT
 operator_concat        ' & 
 value_identifier       ' any word starting with a letter or _ 
 value_true             ' TRUE 
 value_false            ' FALSE 
 value_number           ' any number starting 0-9 or . 
 value_string           ' any string starting ' or " 
 open_bracket           ' [ 
 close_bracket          ' ] 
End Enum

The Tokenizer is fairly simple, it accepts a loose VB/Excel syntax. The evaluator is split into two classes, one does the tokenization and the second processes the tokens. This is the standard way of doing it. This is quite flexible also. This way, if you wish you could amend it to accept a C++ syntax by changing the way the parser detects the operators eq, ne, and, or, not... Changing the Tokenizer will not force you to reprogram the rest of the evaluator.

The Parser

The Parser is a bit more complicated than a Tokenizer. It is like the Tokenizer with a sort of flow machine, a bit like a pipe. It will process the token one by one without looking ahead or back.

In this article, I speak about operators, left parts and right parts. In the expression 1 + 2, I call + the operator, 1 is the left part and 2 is the right part.

One of the complicated concepts of the Parser is priorities. For example, the expression:

1 + 2 * 3

is not treated the same way as the expression:

1 * 2 + 3

The evaluator operates using a standard set of priorities. The multiplication has more priority than addition. Therefore:

1 + 2 * 3 = 1 + 6 = 7
1 * 2 + 3 = 2 + 3 = 5

In the above cases, we need to do the multiplication first.

So how can this be done in one pass?

At any time, the parser knows what is its level of priority.

Private Enum ePriority
  none = 0
  [concat] = 1
  [or] = 2
  [and] = 3
  [not] = 4
  equality = 5
  plusminus = 6
  muldiv = 7
  percent = 8
  unaryminus = 9
End Enum

When the parser encounters an operator, it will recursively call the parser to get the right part. When the parser returns the right part, the operator can apply its operation (for example +) and the parsing continues.

The interesting part is that while calculating the right part, the Tokenizer already knows its current level of priority. Therefore, while parsing the right part, if it detects an operator with more priority, it will continue its parsing and return only the resulting value.

You said it supports object?

Yes, the evaluator supports the . operator. If you enter the expression theForm.text then the evaluator will return the title of the form. If you enter the expression theForm.left, it will return its runtime left position. This feature is only experimental and has not been tested yet. That is why I have put this code here, hoping others will find its features valuable and submit their improvements.

How does this work?

In fact the object came free. I used System.Reflection to evaluate the custom functions. And the same code is used to access the object's methods and properties. When the parser encounters an identifier that is a keyword without any meaning for it, it will try to reflect the CurrentObject to see if it can find a method or a property with the same name.

 mi = CurrentObject.GetType().GetMethod(func, _
  _Reflection.BindingFlags.IgnoreCase _ 
  Or Reflection.BindingFlags.Public _ Or Reflection.BindingFlags.Instance)

If a method or a property is found, it will feed its parameters.

valueleft = mi.Invoke(CurrentObject, _
  _ System.Reflection.BindingFlags.Default, Nothing, 
  _ DirectCast(parameters.ToArray(GetType(Object)), Object()), Nothing)

Points of Interest

This is the only formula evaluator available on CodeProject with a separate Tokenizer and Parser (I believe). The extensibility can be pushed to the maximum due to the use of System.Reflection.

History

  • 7th Feb 2005
    • First release.
  • 10th Feb 2005
    • Greatly increased the length and detail of this article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pascal Ganaye
Software Developer (Senior)
United Kingdom United Kingdom
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberSSementsov23-Oct-12 2:23 
GeneralMy vote of 5 Pinmemberopisana27-Sep-12 3:56 
GeneralMy vote of 5 PinmemberhareshPrj26-Jul-12 22:52 
GeneralShort crrcuiting iif Pinmemberoscargs12-Jun-11 10:46 
GeneralRe: Short crrcuiting iif PinmemberPascal Ganaye12-Jun-11 11:54 
GeneralRe: Short crrcuiting iif Pinmemberfastal20-Jul-11 11:24 
GeneralMy vote of 5 PinmemberJymmy09710-Dec-10 8:15 
GeneralVery nice, but 1 question PinmemberPieterSO24-Nov-10 2:19 
GeneralGreat Code Pinmemberwldrumstcs17-Aug-10 12:03 
QuestionFound an issue. .1+.1 fails. PinmemberWantToLearn.NET15-Jan-10 3:29 
AnswerRe: Found an issue. .1+.1 fails. PinmemberAnt210023-Jul-10 0:49 
GeneralRe: Found an issue. .1+.1 fails. Pinmemberfuerer8-Apr-14 23:53 
GeneraltheForm.Text.Substring(0,1) PinmemberArmoghan Asif29-Dec-09 11:29 
GeneralThis is a great expression Pinmemberb3707-Sep-08 9:46 
GeneralThanks again - another satisfied consumer Pinmembertim.yost30-Nov-07 6:03 
GeneralRe: Thanks again - another satisfied consumer PinmemberPascal Ganaye21-Jul-09 1:22 
GeneralNew version available PinmemberPascal Ganaye3-Sep-07 4:11 
GeneralRe: New version available PinmemberAnt210023-Jul-10 0:51 
GeneralExcellent! Pinmemberdavidcoop19-Apr-07 18:18 
GeneralExcellent! PinmemberJeff_Man5-Mar-07 11:39 
Questioncompound expressions Pinmemberdaveko11-Jan-07 15:21 
AnswerRe: compound expressions Pinmemberahunkins26-Jan-07 12:33 
GeneralQuooting text Pinmemberadgerrits10-Jul-06 2:36 
GeneralOne time tokenizing for Reuse in subsequent .. PinmemberShunya15-Mar-06 1:07 
GeneralRe: One time tokenizing for Reuse in subsequent .. PinmemberPascal Ganaye14-Apr-06 16:38 
Generalspeeding up ... PinmemberShunya20-Feb-06 3:55 
GeneralRe: speeding up ... PinmemberPascal Ganaye20-Feb-06 10:53 
GeneralRe: speeding up ... PinmemberShunya21-Feb-06 4:00 
GeneralRe: speeding up ... PinmemberPascal Ganaye21-Feb-06 8:54 
GeneralRe: speeding up ... PinmemberShunya22-Feb-06 2:48 
GeneralRe: speeding up ... PinmemberPascal Ganaye25-Feb-06 7:45 
GeneralRe: speeding up ... PinmemberShunya25-Feb-06 21:58 
GeneralPascal rulez! Pinmemberadgerrits19-Jan-06 22:59 
GeneralThanks for your code Pinmemberlfarrlive27-Dec-05 9:04 
GeneralRe: Thanks for your code PinmemberPascal Ganaye31-Dec-05 0:48 
GeneralOption Compare Pinmemberdon.pratt28-Jul-05 12:53 
GeneralRe: Option Compare Pinmemberpascal ganaye28-Jul-05 15:20 
GeneralRe: Option Compare Pinmemberdon.pratt29-Jul-05 6:36 
GeneralFound another Bug PinmemberStephan Pilz25-Jul-05 22:36 
GeneralRe: Found another Bug Pinmemberpascal ganaye26-Jul-05 3:47 
GeneralFound Bug !?! PinmemberStephan Pilz19-Jul-05 4:38 
GeneralRe: Found Bug !?! Pinmemberpascal ganaye19-Jul-05 13:31 
GeneralRe: Found Bug !?! PinmemberStephan Pilz20-Jul-05 0:50 
GeneralRe: Found Bug !?! Pinmemberpascal ganaye21-Jul-05 8:52 
QuestionWhat are the [brackets] for? Pinmembermdelp4-Jul-05 5:00 
AnswerRe: What are the [brackets] for? Pinmemberpascal ganaye4-Jul-05 7:59 
GeneralRe: What are the [brackets] for? Pinmembermdelp4-Jul-05 9:10 
GeneralRe: What are the [brackets] for? PinmemberPascal Ganaye14-Apr-06 16:37 
QuestionWhat is this project about ? Pinmemberpascal ganaye1-Jul-05 7:21 
GeneralVery Nice PinmemberStewBob1-Jul-05 6:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150331.1 | Last Updated 14 Oct 2012
Article Copyright 2005 by Pascal Ganaye
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid