repl.it
@FellowHashbrown/

Logic Parser

Python

A parser that evaluates boolean expressions and gives you truth tables and values for.

fork
loading
Files
  • main.py
  • errors.py
  • logic_constants.py
  • logic_node.py
  • logic_tree.py
  • logic_var.py
  • quine_mccluskey.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from logic_tree import LogicTree
from logic_var import LogicVar
from logic_node import LogicNode
from quine_mccluskey import QM

expressions = {
    "a ^ b": "This is the most basic expression",
    "~(a v b)": "Here is an example of an expression with wrapped entirely in parentheses",
    "(a ^ b) ^ c": "This is one expression that has parentheses at the front.",
    "a ^ b ^ c": "This is an expression with no parentheses but you'll notice that the parentheses are automatically added.",
    "a ^ (b ^ c)": "Here is another expression with explicit parentheses placed.",
    "~(a v b) ^ c": "This is an expression with a ~ (NOT) operator placed for JUST the left side.",
    "~(a v b) ^ ~(a v c)": "This expression has ~ (NOT) operators on each side but not the whole expression.",
    "~(~(a v b) ^ ~(a v c))": "Remember that has_not thing in the tutorial? This is what would happen if we ignored it.",
    "a v b v c v d": "This just shows you that the parentheses continue to stack."
}

"""
for exp in expressions:
    tree = LogicTree(exp)
    print("{} - {}".format(
        str(tree),
        expressions[exp]
    ))
    print("\n".join(tree.make_table()))
    print()
"""

while True:
    a = input("Enter a logical expression: ")
    if a == "quit":
        break

    tree = LogicTree(a)

    print(tree.simplify())
    print("\n".join(tree.make_table()))
Fetching token
?