boolean expression evaluation
-
Is there any .net class I can use to obtain a boolean value given a string that represents an expression (a boolean expression)?????
Your looking to get an expression evaluated, contained in a String? There is nothing in the .NET BCL that does this. You'll have to come up with either a 3rd party component to do this for you, or you'll have to write an expression evaluation library yourself. There is an example of this on CodeProject, but since there is a link problem going to CodeProject's site, I can't search for it in a reasonable amount of time. Just search the articles for "expression parse". RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Your looking to get an expression evaluated, contained in a String? There is nothing in the .NET BCL that does this. You'll have to come up with either a 3rd party component to do this for you, or you'll have to write an expression evaluation library yourself. There is an example of this on CodeProject, but since there is a link problem going to CodeProject's site, I can't search for it in a reasonable amount of time. Just search the articles for "expression parse". RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Is there any .net class I can use to obtain a boolean value given a string that represents an expression (a boolean expression)?????
Like all primitives,
Boolean
has aParse
method for that very reason. You can also useConvert.ToBoolean
(which usesParse
, but provides a little more abstract access to passing any primitive type (and a few other value types) toToBoolean
(though, in that case, few actually can be parsed into abool
). [EDIT] Oops, I didn't read that quite right. Nevermind; though this is still handy information if you didn't know it already. [/EDIT]Microsoft MVP, Visual C# My Articles
-
Is there any .net class I can use to obtain a boolean value given a string that represents an expression (a boolean expression)?????
There are a couple ways you could do this, including one way in the BCL (though not easy). The first is to reference the Microsoft.JScript.dll assembly and use the
Eval
class. In JScript .NET, this would be equivalent of theeval
method (also in Javascript and JScript, though those aren't compiled languages nor do they target the CLR). I believe I saw an article on this some time back on MSDN. I could be wrong, but you could try searching for it or just give it a try. The other way is to use Reflection Emit or the CodeDOM. Basically, you create a class that contains those variables and contains a method which evaluates those variables and returns abool
. Read Emitting Dynamic Assemblies[^] in the .NET Framework SDK for more information about Reflection Emit and Generating and Compiling Source Code Dynamically in Multiple Languages[^] for more information on CodeDOM.Microsoft MVP, Visual C# My Articles
-
There are a couple ways you could do this, including one way in the BCL (though not easy). The first is to reference the Microsoft.JScript.dll assembly and use the
Eval
class. In JScript .NET, this would be equivalent of theeval
method (also in Javascript and JScript, though those aren't compiled languages nor do they target the CLR). I believe I saw an article on this some time back on MSDN. I could be wrong, but you could try searching for it or just give it a try. The other way is to use Reflection Emit or the CodeDOM. Basically, you create a class that contains those variables and contains a method which evaluates those variables and returns abool
. Read Emitting Dynamic Assemblies[^] in the .NET Framework SDK for more information about Reflection Emit and Generating and Compiling Source Code Dynamically in Multiple Languages[^] for more information on CodeDOM.Microsoft MVP, Visual C# My Articles
Heath Stewart wrote: The other way is to use Reflection Emit or the CodeDOM. Basically, you create a class that contains those variables and contains a method which evaluates those variables and returns a bool. :eek: All that for "?1+1=2". Seems kind of like having a nuclear power plant to power your USB notebook fan. I'd rather smash my head into a board of nails than go through that! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Is there any .net class I can use to obtain a boolean value given a string that represents an expression (a boolean expression)?????
narada108, There is no class in .net to do what you would like. However, depending on the format of your Boolean expression this may not be a difficult task. This task can be broken down into two easier problems. Convert the expression to a postfix expression, and then evaluate the postfix expression. Typically a Boolean expression looks like this "1 and 1". This is called infix notation since the operator is between the operands. The postfix equivalent to this expression would be "1 1 and". In postfix notation the operator comes after the operands. A more complicated example follows: infix: "1 and 1 and not 0 or 0" postfix: "1 1 0 not and and 0 or" It looks difficult but you get used to it really fast. The main advantage of writing the expressions this way is that they are easy to evaluate. How to evaluate a postfix expression is covered in many introductory computer science classes. The following code will evaluate an expression in this format.
private bool EvaluatePostfixExpression(string expression) { string[] pieces = expression.Split(new char[]{' '},100); System.Collections.Stack stack = new Stack(); foreach(string cur in pieces) { if (cur == "0") stack.Push(false); else if (cur == "1") stack.Push(true); else if (cur == "and") { bool second = (bool)stack.Pop(); bool first = (bool)stack.Pop(); stack.Push(first && second); } else if (cur == "or") { bool second = (bool)stack.Pop(); bool first = (bool)stack.Pop(); stack.Push(first || second); } else if (cur == "not") { stack.Push(!(bool)stack.Pop()); } } return (bool)stack.Pop(); }
For example EvaluatePostfixExpression("1 1 0 not and and 0 or") will return true; I would recommend trying to get your Boolean expressions in postfix notation. If you can't then another simple stack based algorithm can be used to convert infix notation to postfix notation. I don't feel like writing that algorithm at the moment, so let me know if you go this way and need help with it. You can probably find these algorithms in a book on data structures. Karl Baum CEO of KGB Technologies Specializing in custom software development. -
narada108, There is no class in .net to do what you would like. However, depending on the format of your Boolean expression this may not be a difficult task. This task can be broken down into two easier problems. Convert the expression to a postfix expression, and then evaluate the postfix expression. Typically a Boolean expression looks like this "1 and 1". This is called infix notation since the operator is between the operands. The postfix equivalent to this expression would be "1 1 and". In postfix notation the operator comes after the operands. A more complicated example follows: infix: "1 and 1 and not 0 or 0" postfix: "1 1 0 not and and 0 or" It looks difficult but you get used to it really fast. The main advantage of writing the expressions this way is that they are easy to evaluate. How to evaluate a postfix expression is covered in many introductory computer science classes. The following code will evaluate an expression in this format.
private bool EvaluatePostfixExpression(string expression) { string[] pieces = expression.Split(new char[]{' '},100); System.Collections.Stack stack = new Stack(); foreach(string cur in pieces) { if (cur == "0") stack.Push(false); else if (cur == "1") stack.Push(true); else if (cur == "and") { bool second = (bool)stack.Pop(); bool first = (bool)stack.Pop(); stack.Push(first && second); } else if (cur == "or") { bool second = (bool)stack.Pop(); bool first = (bool)stack.Pop(); stack.Push(first || second); } else if (cur == "not") { stack.Push(!(bool)stack.Pop()); } } return (bool)stack.Pop(); }
For example EvaluatePostfixExpression("1 1 0 not and and 0 or") will return true; I would recommend trying to get your Boolean expressions in postfix notation. If you can't then another simple stack based algorithm can be used to convert infix notation to postfix notation. I don't feel like writing that algorithm at the moment, so let me know if you go this way and need help with it. You can probably find these algorithms in a book on data structures. Karl Baum CEO of KGB Technologies Specializing in custom software development.