-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval2.py
211 lines (147 loc) · 5.43 KB
/
eval2.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'''
An interpreter which handles operator precedence
'''
EOF = 'EOF'
INTEGER = 'N'
PLUS = '+'
MINUS = '-'
TIMES = '*'
DIVIDE = '/'
LPAREN = '('
RPAREN = ')'
class Token(object):
def __init__(self, type, value):
self.type = type
self.value = value
def __str__(self):
return 'Token({type}, {value})'.format(type=self.type, value=self.value)
class Interpreter(object):
def __init__(self, text):
self.text = text
self.pos = 0
self.curr_char = self.text[self.pos]
self.curr_token = self.get_next_token()
def advance(self):
self.pos += 1
if self.pos >= len(self.text):
self.curr_char = None
else:
self.curr_char = self.text[self.pos]
def consume(self, type):
if self.curr_token.type == type:
self.curr_token = self.get_next_token()
else:
raise Exception('Syntax error... %s', self.curr_token.type)
def skip_whitespace(self):
while self.pos < len(self.text) and self.curr_char.isspace():
self.advance()
def integer(self):
result = ''
while self.pos < len(self.text) and self.curr_char.isdigit():
result += self.curr_char
self.advance()
return int(result)
def get_next_token(self):
while self.curr_char is not None:
if self.curr_char.isspace():
self.skip_whitespace()
continue
elif self.curr_char.isdigit():
self.curr_token = Token(INTEGER, self.integer())
return self.curr_token
elif self.curr_char == '+':
self.curr_token = Token(PLUS, '+')
self.advance()
return self.curr_token
elif self.curr_char == '-':
self.curr_token = Token(MINUS, '-')
self.advance()
return self.curr_token
elif self.curr_char == '*':
self.curr_token = Token(TIMES, '*')
self.advance()
return self.curr_token
elif self.curr_char == '/':
self.curr_token = Token(DIVIDE, '/')
self.advance()
return self.curr_token
elif self.curr_char == '(':
self.curr_token = Token(LPAREN, '(')
self.advance()
return self.curr_token
elif self.curr_char == ')':
self.curr_token = Token(RPAREN, ')')
self.advance()
return self.curr_token
else:
raise Exception('Error')
self.curr_token = Token(EOF, None)
return self.curr_token
# rule for grouping with parentheses
def group(self):
# group : (LPAREN group RPAREN) ((+/-/TIMES/DIVIDE) group)*
result = 0
# handles the very first group
if self.curr_token.type == LPAREN:
self.consume(LPAREN)
result = self.group()
self.consume(RPAREN)
else:
result = self.expr()
while self.curr_token.type in (PLUS, MINUS, TIMES, DIVIDE):
op = self.curr_token
if op.type == TIMES:
self.consume(TIMES)
result *= self.group()
elif op.type == DIVIDE:
self.consume(DIVIDE)
result /= self.group()
elif op.type == MINUS:
self.consume(MINUS)
result -= self.group()
elif op.type == PLUS:
self.consume(PLUS)
result += self.group()
return result
# rule for +/-
def expr(self):
# expr : term ((+/-) term)*
result = self.term()
while self.curr_token.type in (PLUS, MINUS):
op = self.curr_token
if op.type == PLUS:
self.consume(PLUS)
result += self.group()
elif op.type == MINUS:
self.consume(MINUS)
result -= self.group()
return result
# rule for TIMES/DIVIDE
def term(self):
# term : group ((TIMES/DIVIDE) group)*
result = self.factor()
while self.curr_token.type in (TIMES, DIVIDE):
op = self.curr_token
if op.type == TIMES:
self.consume(TIMES)
result *= self.group()
elif op.type == DIVIDE:
self.consume(DIVIDE)
result /= self.group()
return result
# rule for base integer
def factor(self):
curr_token = self.curr_token
self.consume(INTEGER)
return curr_token.value
def main():
try:
while True:
input_str = raw_input('calc> ')
if input_str == '': continue
result = Interpreter(input_str).group()
print result
except EOFError:
print 'Goodbye!\n'
if __name__ == '__main__':
main()