Click here to Skip to main content
13,143,774 members (32,783 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 1 Oct 2004

Expression Evaluator for C# based on Expression Tree

, 1 Oct 2004
Rate this:
Please Sign up or sign in to vote.
This is an expression evaluator that evaluates any expression with the binary operators. It also works with the Symbolic expressions. It takes Infix expressions, but it's easily extensible.


The Expression Evaluator is designed to evaluate expressions with binary operators. The Expression Evaluator contains mainly four classes: ExpEvaluator, ExpParser, ExpTreeNode, and the IOperatorImp. The ExpEvaluator class has a has-a relationship with the ExpParser, ExpTreeNode and IOperatorImp.

The ExpEvaluator evaluates the expression with the help form ExpParser, and the IOperatorImp.ExpParser is the class where the parsing of the expression is being done. It derives from the interface IParser. The expression tree and stack is being used in expressing the expression in the program. The ExpParser parses the expression and returns the ExpTreeNode type of the object, which then ExpEvaluator uses to evaluate the expression. ExpTreeNode follows the composite pattern. ExpParser has been written to support the symbols in the expression.

The role of the IOperatorImp in the design is to provide the meaning of the operators used in an expression. It derives from the IOperator interface. Client is supposed to first create the parser that it wants to use to parse the expression. The parser should be derived from the IParser. If the client does not provide the IParser, the ExpEvaluator will use the default implementation of the IParser, i.e., the Infix Parser. The client also needs to provide the IOperator implantation object to the ExpEvaluator which it will use to calculate the expression. Same as Parser, if the client does not provide the implementation, the ExpEvaluator will use default IOperator implementation. For error handling, the ErrorException class has been written.

We can also set the priority of the operator with respect to the others. In case of the same priority, the left to right evaluation happens. But then that can be easily implemented. By implementing the IOperator interface, user can give their own meaning to the operators. By implementing the IParser, user can parse the postfix and prefix expressions also. This design also provides the user to register their own Tokens and the Type they want them to be recognized. Symbols are allowed in the Expression. The value of the Symbol can be as indexed property of the Evaluator. One disadvantage that I see here in this design is if the user is implementing the parser, he is restricted to return the ExpTreeNode to the Evaluator. That can also be omitted if we define the IExpEnumerator interface which provides all the function that a parsed return object to the ExpEvaluator should have. A little change in the design will solve this problem.

Usage of the Exp EVALUATOR

IParser par = new ExpParser();
ExpEvaluator eu = new ExpEvaluator(par); 
double res =eu.Evaluate();

Users can also use the Symbols here. Expressions like "x+(1/y)+z" can also be solved. User need to provide the value of the Symbols like:

eu["x"] =100;
eu["y"] =10;
eu["z"] =101;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralHello Maurya Pin
Shyam SS1-Jul-08 22:42
memberShyam SS1-Jul-08 22:42 
GeneralImplementing stubbed ReMakeExpression is easy Pin
Steve Poling13-Jun-08 9:44
memberSteve Poling13-Jun-08 9:44 
Generalbugs Pin
asaf_lahav37-Nov-04 3:36
memberasaf_lahav37-Nov-04 3:36 
GeneralBugs found Pin
asaf_lahav37-Nov-04 3:28
memberasaf_lahav37-Nov-04 3:28 
GeneralRe: Bugs found Pin
Anonymous8-Nov-04 21:00
sussAnonymous8-Nov-04 21:00 
GeneralMissing sources Pin
Marc Sommer2-Oct-04 20:00
memberMarc Sommer2-Oct-04 20:00 
GeneralRe: Missing sources Pin
Maurya,Neelesh3-Oct-04 19:12
memberMaurya,Neelesh3-Oct-04 19:12 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 1 Oct 2004
Article Copyright 2004 by Maurya,Neelesh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid