Click here to Skip to main content
6,630,901 members and growing! (19,756 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Regular Expressions     Intermediate

Expression Evaluator for C# based on Expression Tree

By Maurya,Neelesh

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.
C#, VC7.NET 1.1, WinXPVS.NET2003, Dev
Posted:1 Oct 2004
Views:38,134
Bookmarked:20 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 2.91 Rating: 2.91 out of 5
4 votes, 40.0%
1

2

3
3 votes, 30.0%
4
3 votes, 30.0%
5

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

About the Author

Maurya,Neelesh


Member

Occupation: Web Developer
Location: United States United States

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralHello Maurya PinmemberShyam SS23:42 1 Jul '08  
GeneralImplementing stubbed ReMakeExpression is easy PinmemberSteve Poling10:44 13 Jun '08  
Generalbugs Pinmemberasaf_lahav34:36 7 Nov '04  
GeneralBugs found Pinmemberasaf_lahav34:28 7 Nov '04  
GeneralRe: Bugs found PinsussAnonymous22:00 8 Nov '04  
GeneralMissing sources PinmemberMarc Sommer21:00 2 Oct '04  
GeneralRe: Missing sources PinmemberMaurya,Neelesh20:12 3 Oct '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Oct 2004
Editor: Smitha Vijayan
Copyright 2004 by Maurya,Neelesh
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project