-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserAppII.py
188 lines (171 loc) · 4.23 KB
/
ParserAppII.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
class Node:
def __init__(self, symbol, left=None, right=None):
self.symbol = symbol
self.leftChild = left
self.rightChild = right
def __str__(self):
return self.symbol
import sys
error = False
next_token = '%'
input_stream = []
def lex():
global next_token
while input_stream and input_stream[0] in ' \n\t':
input_stream.pop(0)
if input_stream:
next_token = input_stream.pop(0)
else:
next_token = '$'
def printTree(tree):
if tree:
printTree(tree.leftChild)
printTree(tree.rightChild)
print(tree.symbol, end=' ')
def evaluate(tree):
if tree is None:
return 0
if tree.symbol.isdigit():
return int(tree.symbol)
elif tree.symbol == '+':
return evaluate(tree.leftChild) + evaluate(tree.rightChild)
elif tree.symbol == '-':
return evaluate(tree.leftChild) - evaluate(tree.rightChild)
elif tree.symbol == '*':
return evaluate(tree.leftChild) * evaluate(tree.rightChild)
elif tree.symbol == '/':
try:
return evaluate(tree.leftChild) / evaluate(tree.rightChild)
except ZeroDivisionError:
print("Error: Division by zero")
return None
elif tree.symbol == 'a':
return 10
elif tree.symbol == 'b':
return 20
elif tree.symbol == 'c':
return 30
elif tree.symbol == 'd':
return 40
def unconsumed_input():
return ''.join(input_stream)
def G():
global error
lex()
print("G -> E")
tree = E()
if next_token == '$' and not error:
print("success")
return tree
else:
print("failure: unconsumed input=", unconsumed_input())
return None
def E():
global error
if error:
return None
print("E -> T R")
temp = T()
return R(temp)
def R(tree):
global error
if error:
return None
if next_token == '+':
print("R -> + T R")
lex()
temp1 = T()
temp2 = R(temp1)
return Node('+', tree, temp2)
elif next_token == '-':
print("R -> - T R")
lex()
temp1 = T()
temp2 = R(temp1)
return Node('-', tree, temp2)
else:
print("R -> e")
return tree
def T():
global error
if error:
return None
print("T -> F S")
temp = F()
return S(temp)
def S(tree):
global error
if error:
return None
if next_token == '*':
print("S -> * F S")
lex()
temp1 = F()
temp2 = S(temp1)
return Node('*', tree, temp2)
elif next_token == '/':
print("S -> / F S")
lex()
temp1 = F()
temp2 = S(temp1)
return Node('/', tree, temp2)
else:
print("S -> e")
return tree
def F():
global error
if error:
return None
if next_token == '(':
print("F -> ( E )")
lex()
temp = E()
if next_token == ')':
lex()
return temp
else:
error = True
print("error: unexpected token", next_token)
print("unconsumed input", unconsumed_input())
return None
elif next_token in 'abcd':
return M()
elif next_token in '0123456789':
return N()
else:
error = True
print("error: unexpected token", next_token)
print("unconsumed input", unconsumed_input())
return None
def M():
global error
if error:
return None
symbol = next_token
lex()
return Node(symbol)
def N():
global error
if error:
return None
symbol = next_token
lex()
return Node(symbol)
def main():
global error
global input_stream
with open('expression.txt', 'r') as file:
for line in file:
print("\nEvaluating: " + line.strip())
input_stream = list(line.strip())
theTree = G()
if not error:
print("Postfix notation of the tree:")
printTree(theTree)
print("\nThe evaluated value is:", evaluate(theTree))
else:
print("Input not parsed correctly")
input_stream = []
error = False
if __name__ == "__main__":
main()