Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only use AND, OR , NOT #83

Open
shezankazi opened this issue Oct 15, 2018 · 1 comment
Open

only use AND, OR , NOT #83

shezankazi opened this issue Oct 15, 2018 · 1 comment

Comments

@shezankazi
Copy link

Is there any possibility parse only AND, OR and NOT operators? For example I cannot parse a query C OR C++ because of the ++ in the string.

@tomas789
Copy link
Contributor

tomas789 commented Dec 5, 2022

The built-in parser is somehow limited and is not intended to be a fully general parser. So following script will fail

import boolean

algebra = boolean.BooleanAlgebra()
TRUE, FALSE, NOT, AND, OR, symbol = algebra.definition()

formula = algebra.parse("C|C++")
print(formula)

Traceback:

Traceback (most recent call last):
  File "/Users/tomaskrejci/Developer/quine-mccluskey/test.py", line 9, in <module>
    formula = algebra.parse("C|C++")
              ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomaskrejci/Developer/quine-mccluskey/boolean.py/boolean/boolean.py", line 252, in parse
    raise ParseError(
boolean.boolean.ParseError: Invalid operator sequence without symbols such as AND OR or OR OR for token: "+" at position: 4

I see two options to go about this.

First, you can create the formula directly using Python primitives. If you need parsing then you'll need to build your own parser that can build the tree for you.

Here is an example that will work:

import boolean

algebra = boolean.BooleanAlgebra()
TRUE, FALSE, NOT, AND, OR, symbol = algebra.definition()

formula = OR(symbol("C"), symbol("C++"))
print(formula)

The output is simply C|C++. Note that the formula cannot be parsed using algebra.parse for the reason discussed above.

Second, you can rename the variables to something that you can parse. For example C becomes x0 and C++ becomes x1. You can then work with those names and convert them back to your original names when you are finished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants