How do I parse boolean logic?

Posted by d03boy on Stack Overflow See other posts from Stack Overflow or by d03boy
Published on 2010-05-19T17:31:33Z Indexed on 2010/05/19 18:00 UTC
Read the original article Hit count: 414

Filed under:
|
|
|

I need to write a boolean logic parser which will translate the boolean logic language to a SQL WHERE clause.

The order of the operands will always be in the correct order (with value on the right).

Here is a relatively simple example. There could be nested parentheses and the use of NOT operators, etc.

(CACOUNT=01 OR CACOUNT=02 OR CACOUNT=03 OR CACOUNT=05
 OR CACOUNT=07 OR CACOUNT=09 OR  CACOUNT=12 OR CACOUNT=13 OR CACOUNT=18)
 AND Q4=1 AND NAME=TIMOTHY

Here is what the WHERE clause would resemble.

WHERE (
  EXISTS (
    SELECT 1 FROM MyVerticalTable b
    WHERE b.Key=a.Key AND b.Key='CACOUNT' AND b.Value='01'
  )
)

© Stack Overflow or respective owner

Related posts about boolean-logic

Related posts about parsing