-
Notifications
You must be signed in to change notification settings - Fork 1
/
expression_tree.py
59 lines (48 loc) · 1.55 KB
/
expression_tree.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Symbol:
def __init__(self):
self.value = None
self.left = None
self.right = None
def parse_string(self,string):
if len(string) is 1:
self.value = string
return
if string[0] is '(' and string[-1] is ')':
string = string[1:-1]
depth = 0
for i in range(len(string)):
c = string[i]
if c is "(":
depth += 1
elif c is ")":
depth -=1
if self.isoperator(c) and depth == 0:
self.value = c
if len(string[:i]) is not 0:
self.left = Symbol()
self.left.parse_string(string[:i])
if len(string[i+1:]) is not 0:
self.right = Symbol()
self.right.parse_string(string[i+1:])
def visit(self):
if self.left is not None:
self.left.visit()
print(self.value)
if self.right is not None:
self.right.visit()
@staticmethod
def isoperator(char):
if char in ['⊃','⇔','∧','∨','¬']:
return True
return False
class Expression:
def __init__(self):
self.root = Symbol()
def from_string(self,string):
self.root.parse_string(string)
def traverse(self):
self.root.visit()
if __name__ == '__main__':
e = Expression()
e.from_string('((Q∧S)⊃((S∨R)∨R))')
e.traverse()