CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2012
    Posts
    58

    evaluating logical boolean expressions

    hi,

    I am looking for a library to aid me in evaluating Boolean expressions.
    For example, i have an expression like this:

    (1*(5+3)+9*65/5-(354*4565*5643+98) >= 12345) && ( 654*987+123 || (2345 > 23423 && 1 != 2)))
    (it can alos be much longer!)

    and would like to evaluate them to a true/false boolean.
    My dear gcc would kick some *** here with its static evaluation, but it`s way too complicated to use a compiler or the like for this.

    There are tons of libraries to calculate the (numerical) result of a mathematical expression, but this is not what i want to do.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: evaluating logical boolean expressions

    Perhaps you should start by defining the grammar of these boolean expressions. After that, it becomes a question of whether you want to write your own parser or use some tool to create a parser for you.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2012
    Posts
    58

    Re: evaluating logical boolean expressions

    forgot that, sorry.

    Standard math/c++ set of expressions, i would say, ie:

    <,>,>=,<=,&&,||, nested brevets,*,+,-,/, etc.

    If i was to write my own parser, would you know of any "reference" project i could learn from or base on?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: evaluating logical boolean expressions

    Quote Originally Posted by tuli View Post
    forgot that, sorry.

    Standard math/c++ set of expressions, i would say, ie:

    <,>,>=,<=,&&,||, nested brevets,*,+,-,/, etc.

    If i was to write my own parser, would you know of any "reference" project i could learn from or base on?
    Look up "grammar" and "recursive descent parsers". You need to formally define the grammar first, then from the grammar (production) rules, you create the parser.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

    Re: evaluating logical boolean expressions

    Quote Originally Posted by tuli View Post
    I am looking for a library to aid me in evaluating Boolean expressions.
    you can use the boost.Spirit library to write a BNRF based parser relatively quickly ( the "Qi" library component, more specifically; there are also a lexer and an output generator library components but you'll probably not need them ... ). Used in conjunction with boost.Phoenix you can process the expression in-place while parsing, including grammar error handling.

    Quote Originally Posted by tuli View Post
    (it can alos be much longer!)
    so, are those expressions human or computed generated ? in the latter case you don't need a parser, just store the expression in a computer-understandable representation directly.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: evaluating logical boolean expressions

    An alternative approach would be to load a linkable script language. Writing your own expression parser of "mini language" to do this will take a considerable amount of development time.

    One example is LUA (www.lua.org) which is pretty easy to integrate into a C/C++ program. But there are many others.

    Another way out would be to take the expression and form it into a VBScript or javascript and use the wscript.exe to evaluate it for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured