-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression_parser.py
95 lines (77 loc) · 2.44 KB
/
expression_parser.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
92
93
94
95
import operator
import re
import argparse
operators = {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv, "^": operator.pow}
operators_priority = {"+": 0, "-": 0, "*": 1, "/": 1, "^": 2}
def main():
parser = create_parser()
namespace = parser.parse_args()
if namespace.file:
with open(namespace.file, "r") as file:
exp = file.readline()
elif namespace.expression:
exp = namespace.expression
else:
exp = input("Enter expression: ")
rev_polish_note = reverse_polish_notation(exp)
print("Answer: ", execute(rev_polish_note))
def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument("expression", nargs="?")
parser.add_argument('-f', "--file")
return parser
def check_expression(expression):
input_string = str(expression)
input_string.replace(" ", "")
if "," in input_string:
raise ValueError()
def execute(reverse_polish):
stack = []
for lexeme in reverse_polish:
if is_number(lexeme):
stack.append(lexeme)
else:
stack.append(operators[lexeme](float(stack.pop(-2)), float(stack.pop())))
return stack.pop()
def reverse_polish_notation(expression):
lexemes = get_lexemes(expression)
output = []
stack = []
for lexeme in lexemes:
if is_number(lexeme):
output.append(lexeme)
elif lexeme == "(":
stack.append(lexeme)
elif lexeme == ")":
current_lexeme = stack.pop()
while current_lexeme != "(":
output.append(current_lexeme)
current_lexeme = stack.pop()
elif lexeme in operators_priority:
while stack and stack[-1] in operators_priority and \
operators_priority[stack[-1]] >= operators_priority[lexeme]:
output.append(stack.pop())
stack.append(lexeme)
while stack:
output.append(stack.pop())
return output
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def get_lexemes(expression):
lexemes = []
i = 0
while i < len(expression):
number = re.match(r"[0-9]+\.*[0-9]+", expression[i:])
if number:
lexemes.append(number.group())
i += number.end() - 1
else:
lexemes.append(expression[i])
i += 1
return lexemes
if __name__ == "__main__":
main()