Nominate our 2022 Qt Champions!

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

    1. some variables already used in my application
    2. Boolean and numeric constants
    3. 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 application

    if 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

    However 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.

Log in to reply