Click here to Skip to main content
15,881,841 members
Articles / Programming Languages / C#
Article

Expression Evaluator for C# based on Expression Tree

Rate me:
Please Sign up or sign in to vote.
2.91/5 (10 votes)
1 Oct 20042 min read 69.6K   1.6K   22   8
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

C#
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:

C#
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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLicense Pin
rlandfair16-Feb-24 9:36
rlandfair16-Feb-24 9:36 
GeneralHello Maurya Pin
Shyam SS1-Jul-08 22:42
Shyam SS1-Jul-08 22:42 
GeneralImplementing stubbed ReMakeExpression is easy Pin
StevePoling13-Jun-08 9:44
StevePoling13-Jun-08 9:44 
Generalbugs Pin
asaf_lahav37-Nov-04 3:36
asaf_lahav37-Nov-04 3:36 
GeneralBugs found Pin
asaf_lahav37-Nov-04 3:28
asaf_lahav37-Nov-04 3:28 
GeneralRe: Bugs found Pin
Anonymous8-Nov-04 21:00
Anonymous8-Nov-04 21:00 
GeneralMissing sources Pin
Marc Sommer2-Oct-04 20:00
Marc Sommer2-Oct-04 20:00 
GeneralRe: Missing sources Pin
Maurya,Neelesh3-Oct-04 19:12
Maurya,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.