Parse logical expression string in qt
-
In my application, my application needs to take some action based on result of expression string provided by the application user
the expression may include
- some variables already used in my application
- Boolean and numeric constants
- logical and arithmetic(+-*/) operators
e.g. expression string might be !((var1&var2)|Var3)
in which var1, var2 and var3 are the variables already declared in my applicationif any standard scripting language is supported then i can use it and syntax of expression can be changed as per requirements.
I need some guidance or tutorial which can help me to get started
-
ECMAScript is supported by Qt (JavaScript).
Have a look at http://qt-project.org/doc/qt-4.8/scripting.htmlHowever this of course might be much too powerful than necessary (or even desirable) in your situation. A parser for such simple expressions can be written from scratch quite quickly. Recurse bracket-wise and decompose into operand-operator-operand blocks, which finally create a tree structure. Then just execute the logic of the tree, the value of the root is the result.