-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfix4tokens.py
91 lines (77 loc) · 3.03 KB
/
postfix4tokens.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
87
88
89
90
91
from exp_tokenization import tokenize#, tokenize
from avaliable_math_logic_operators import OPERATORs_DICT_func_1, OPERATORs_PRECEDENCE_DICT, \
OPERATORs_DICT_binary_between, OPERATORs_DICT_math_U, OPERATORs_DICT_func_2
from avaliable_math_logic_operators import before_unar_arithmetic
def tokensPostfixing(tokens: list, brackets=None):
"""
new vars
:param tokens:
:param brackets: brackets[0] - open bracket, brackets[1] - close bracket
:return:
"""
if brackets is None:
brackets = ['(', ')', '[', ']']
stack = []
result = []
start_exp = True
for t in tokens:
try:
t = float(t)
result.append(t)
except:
if t in OPERATORs_DICT_func_1 or t in OPERATORs_DICT_func_2:#sin, int, pow
stack.append(t)
elif t ==',':
while stack[-1] != brackets[0]:
result.append(stack.pop(-1))
if len(stack) == 0:
print('wrong expression: "{}" is missing'.format(brackets[0]))
return None
elif t in OPERATORs_DICT_binary_between:
t = '_' if (t == '-' and start_exp) else t
while len(stack) > 0 and (stack[-1] in OPERATORs_DICT_binary_between or stack[-1] in OPERATORs_DICT_math_U) and OPERATORs_PRECEDENCE_DICT[stack[-1]] >= OPERATORs_PRECEDENCE_DICT[t]:
result.append(stack.pop(-1))
if not(t == '+' and start_exp):
stack.append(t)
elif t == brackets[0]:
stack.append(t)
elif t == brackets[1]:
while len(stack) > 0 and stack[-1] != brackets[0]:
result.append(stack.pop(-1))
if len(stack) == 0 or stack[-1] != brackets[0]:
return None
stack.pop(-1)
if len(stack) > 0 and (stack[-1] in OPERATORs_DICT_func_1 or stack[-1] in OPERATORs_DICT_func_2):
result.append(stack.pop(-1))
else:
result.append(t)
if t in before_unar_arithmetic:
start_exp = True
else:
start_exp = False
while len(stack) > 0:
if stack[-1] == brackets[0]:
print('Not enough brackets')
return None
result.append(stack.pop(-1))
result = tuple(result)
return result
def string2postfix_tuple(infix:str, brackets):#, vars:dict
print(f'string2postfix_tuple : ', str)
infix_list = tokenize(infix, brackets)
print('infix_list = ', infix_list)
res = tokensPostfixing(infix_list, brackets)#,vars
print('string2postfix_tuple res = ', res)
return res
def test_postfix(infix:str):
infix_list = tokenize(infix)
print('infix list = ', infix_list)
res = tokensPostfixing(infix_list)
print('res = ', res)
one_line = ' '.join(map(str, res))
print('one line', one_line)
return res
if __name__ == "__main__":
nya = '(sin(6) + 30)+17 -(-pow(3)+1)'
nya = '5 + 1==6'
test_postfix(nya)