-
Notifications
You must be signed in to change notification settings - Fork 0
/
mian.py
86 lines (67 loc) · 1.95 KB
/
mian.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class Tree:
def __init__(self, symbol, left=None, right=None):
self.symbol = symbol
self.left = left
self.right = right
G = ['a', 'b', 'c']
tokens = ['|', '.', '(', ')', '*', 'end']
def prep_string(expression):
res = []
expression = expression.replace(' ', '')
for i in expression:
res.append(i)
res.append('end')
return res
def print_tree(root):
a = "("
if root.left is not None:
a += print_tree(root.left)
a += " " + root.symbol + " "
if root.right is not None:
a += print_tree(root.right)
a += ')'
return a
def get_token(token_list, expected):
if token_list[0] == expected and expected in tokens:
del token_list[0]
return True
else:
return False
def get_symbol(token_list):
if get_token(token_list, '('):
x = get_line(token_list)
get_token(token_list, ')')
return x
else:
x = token_list[0]
if x not in G:
raise Exception('Wrong Expression: symbol ' + x + ' is not in G')
token_list[0:1] = []
return Tree(x, None, None)
def get_star(token_list):
a = get_symbol(token_list)
if get_token(token_list, '*'):
return Tree('*', a)
else:
return a
def get_dot(token_list):
a = get_star(token_list)
if get_token(token_list, '.'):
b = get_dot(token_list) # this line changed
return Tree('.', a, b)
elif token_list[0] not in tokens and token_list[0] not in G:
raise Exception('Wrong Expression: symbol ' + token_list[0])
else:
return a
def get_line(token_list):
a = get_dot(token_list)
if get_token(token_list, '|'):
b = get_line(token_list)
return Tree('|', a, b)
else:
return a
tree = Tree('|', Tree('a'), Tree('.', Tree('b'), Tree('*', Tree('c'))))
print(print_tree(tree))
token_list = prep_string('a|((b.c).a)*')
tree = get_line(token_list)
print(print_tree(tree))