Click here to Skip to main content
11,493,046 members (56,075 online)
Click here to Skip to main content

Expression Evaluator for C# based on Expression Tree

, 1 Oct 2004 57.7K 1.4K 22
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.

Introduction

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); 
eu.SetExpression("2-(1+1/2)");
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;

License

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

Share

About the Author

Maurya,Neelesh
Web Developer
United States United States
No Biography provided

Comments and Discussions

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

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
Web03 | 2.8.150520.1 | Last Updated 1 Oct 2004
Article Copyright 2004 by Maurya,Neelesh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid