Expression Evaluation Design Considerations

I am modeling a system for evaluating expressions. Now the operands in these expressions can be one of several types, including some primitive .NET types. When I define my Expression class, I want some degree of type safety and, therefore, I donโ€™t want to use an โ€œobjectโ€ for the type of the operand object, so I am considering the definition of the abstract base class Operand, in which there is nothing, and subclassing each type of Operand. What do you think about it?

In addition, only some types of operands make sense with others. And finally, only some operators make sense with certain operands. I can't think of a way to implement these rules at compile time, so I think I will have to perform these checks at runtime.

Any ideas on how I could make this better?

+4
source share
4 answers

How about Expression in 3.5? I recently wrote a parser / compiler expression using this.

+1
source

I'm not sure C languages โ€‹โ€‹have this, however Java has a few packages that really make sense for this.

The JavaCC compiler or java compiler allows you to define a language (for example, your expressions), and they build the appropriate java classes. Somewhat more convenient, if not more experimental and academic DemeterJ package - this allows you to very easily specify the language of expression and comes with a library for defining visitors and strategies for working on the created class structure. If you could afford to switch to Java, I can try this. Otherwise, I would be looking for the C # -lon of one of these technologies.

Another thing to consider if you go down this route is that once you have formed your class structure in some reasonable approximation of the final result, you can subclass all generated classes and build all your application-specific input into the system subclasses. Thus, if you really need to restore a new model for the expression language, your logic will be relatively independent of your class hierarchy.

Update: it actually looks like some of these materials have been ported to .NET technology, although I haven't used it, so I'm not sure what form it might be:

http://www.ccs.neu.edu/home/lieber/inside-impl.html

Good luck

+2
source

I recently built a dynamic expression analyzer. What I found effective is to create, as you said, BaseOperand with meaningful derived classes (NumericOperand, StringOperand, DateOperand, etc.). Depending on your implementation, generators may make sense (Operand).

As a result of the implementation of the Visitor template, you can perform any validations that you like.

I had a very specific need for rolls of my own solution, but there are many options already available for processing expressions. You can take a look at some of them for inspiration or avoid reusing the wheel.

+1
source

I found a good approach for handling object types using the EXPRESSIONOASIS structure. They use a custom data structure to transfer object types. Therefore, after parsing the operand with regular expressions and given expressions, they determine the type and save this type as a property of the general class, which can be used at any time to obtain the type.

http://code.google.com/p/expressionoasis/

0
source

All Articles