Click here to Skip to main content
15,396,032 members
Articles / Programming Languages / Visual Basic
Posted 7 Feb 2005


82 bookmarked

An expression evaluator written in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (56 votes)
14 Oct 2012CPOL5 min read
An expression evaluator written in VB.NET.

<hr /><hr /><p><code>There is a new version (still very simple), at the address below: 

Image 1


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.


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


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


About the Author

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

Comments and Discussions

GeneralExcellent job Pin
Dao Viet Cuong1-Feb-16 0:05
MemberDao Viet Cuong1-Feb-16 0:05 
Question5/5 Excellent - have just one question Pin
Member 83787697-Dec-15 9:18
MemberMember 83787697-Dec-15 9:18 
GeneralMy vote of 5 Pin
Sergey Sementsov23-Oct-12 1:23
MemberSergey Sementsov23-Oct-12 1:23 
GeneralMy vote of 5 Pin
opisana27-Sep-12 2:56
Memberopisana27-Sep-12 2:56 
GeneralMy vote of 5 Pin
hareshdgr826-Jul-12 21:52
Memberhareshdgr826-Jul-12 21:52 
GeneralShort crrcuiting iif Pin
oscargs12-Jun-11 9:46
Memberoscargs12-Jun-11 9:46 
GeneralRe: Short crrcuiting iif Pin
Pascal Ganaye12-Jun-11 10:54
MemberPascal Ganaye12-Jun-11 10:54 
GeneralRe: Short crrcuiting iif Pin
fastal20-Jul-11 10:24
Memberfastal20-Jul-11 10:24 
GeneralMy vote of 5 Pin
LLLLGGGG10-Dec-10 7:15
MemberLLLLGGGG10-Dec-10 7:15 
GeneralVery nice, but 1 question Pin
PieterSO24-Nov-10 1:19
MemberPieterSO24-Nov-10 1:19 
GeneralGreat Code Pin
wldrumstcs17-Aug-10 11:03
Memberwldrumstcs17-Aug-10 11:03 
QuestionFound an issue. .1+.1 fails. Pin
WantToLearn.NET15-Jan-10 2:29
MemberWantToLearn.NET15-Jan-10 2:29 
AnswerRe: Found an issue. .1+.1 fails. Pin
Anthony Daly22-Jul-10 23:49
MemberAnthony Daly22-Jul-10 23:49 
GeneralRe: Found an issue. .1+.1 fails. Pin
Gilus'8-Apr-14 22:53
MemberGilus'8-Apr-14 22:53 
GeneraltheForm.Text.Substring(0,1) Pin
Armoghan Asif29-Dec-09 10:29
MemberArmoghan Asif29-Dec-09 10:29 
GeneralThis is a great expression Pin
b3707-Sep-08 8:46
Memberb3707-Sep-08 8:46 
GeneralUse with Excel VBA Pin
rashul17-Aug-08 23:33
Memberrashul17-Aug-08 23:33 
GeneralThanks again - another satisfied consumer Pin
tim.yost30-Nov-07 5:03
Membertim.yost30-Nov-07 5:03 
GeneralRe: Thanks again - another satisfied consumer Pin
Pascal Ganaye21-Jul-09 0:22
MemberPascal Ganaye21-Jul-09 0:22 
GeneralNew version available Pin
Pascal Ganaye3-Sep-07 3:11
MemberPascal Ganaye3-Sep-07 3:11 
GeneralRe: New version available Pin
Anthony Daly22-Jul-10 23:51
MemberAnthony Daly22-Jul-10 23:51 
GeneralExcellent! Pin
davidcoop19-Apr-07 17:18
Memberdavidcoop19-Apr-07 17:18 
GeneralExcellent! Pin
Jeff_Man5-Mar-07 10:39
MemberJeff_Man5-Mar-07 10:39 
Questioncompound expressions Pin
daveko11-Jan-07 14:21
Memberdaveko11-Jan-07 14:21 
AnswerRe: compound expressions Pin
ahunkins26-Jan-07 11:33
Memberahunkins26-Jan-07 11:33 

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

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