-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw1Work.py
336 lines (308 loc) · 11 KB
/
hw1Work.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import pdb
import lib601.sm as sm
import string
import operator
class BinaryOp:
def __init__(self, left, right):
self.left = left
self.right = right
def __str__(self):
return self.opStr + '(' + \
str(self.left) + ', ' +\
str(self.right) + ')'
__repr__ = __str__
class Sum(BinaryOp):
opStr = 'Sum'
def eval(self, env):
# Evaluating the left and right side.
left = self.left.eval(env)
right = self.right.eval(env)
# Returning the sum of both.
return operator.add(left, right)
class Prod(BinaryOp):
opStr = 'Prod'
def eval(self, env):
left = self.left.eval(env)
right = self.right.eval(env)
return operator.mul(left, right)
class Quot(BinaryOp):
opStr = 'Quot'
def eval(self, env):
left = self.left.eval(env)
right = self.right.eval(env)
return operator.div(left, right)
class Diff(BinaryOp):
opStr = 'Diff'
def eval(self, env):
left = self.left.eval(env)
right = self.right.eval(env)
return operator.sub(left, right)
class Assign(BinaryOp):
opStr = 'Assign'
def eval(self, env):
# Evaluate the right (result).
right = self.right.eval(env)
# Assign the result to the name of the variable of the left side.
env[self.left.name] = right
class Number:
def __init__(self, val):
self.value = val
def __str__(self):
return 'Num('+str(self.value)+')'
__repr__ = __str__
# Just return the value of the Number.
def eval(self, env):
return self.value
class Variable:
def __init__(self, name):
self.name = name
def __str__(self):
return 'Var('+self.name+')'
__repr__ = __str__
# Return the value associated with the variable.
def eval(self, env):
return env[self.name]
# characters that are single-character tokens
seps = ['(', ')', '+', '-', '*', '/', '=']
# Convert strings into a list of tokens (strings)
# If they are especial (seps) they stay separated in the list.
# If they aren't, they stay together, separeted only if there is a space.
def tokenize(string):
sup_str = ''
# Concatenating the string in other string(sup_string).
for char in string:
# But if the char is a special one, I plub a space after and before the special char.
if char in seps:
sup_str = sup_str + ' ' + char + ' '
else:
sup_str = sup_str + char
# By this way when I split up, I will get the result I want.
list_str = sup_str.split()
return list_str
# tokens is a list of tokens
# returns a syntax tree: an instance of {\tt Number}, {\tt Variable},
# or one of the subclasses of {\tt BinaryOp}
def parse(tokens):
def parseExp(index):
# If the token at this index is a number.
if numberTok(tokens[index]):
# Return the token instantiated as a Num and the next index.
return (Number(float(tokens[index])), index+1)
# IF the token is a variable.
elif variableTok(tokens[index]):
# Return the token instantiated as a Var and the next index.
return (Variable(tokens[index]), index+1)
# Otherwise, It is a parentheses '('.
else:
# For example (3 + 5)
# '(' --> index / '3' --> index + 1 / '+' --> op(index+2) /'5' --> op + 1/ ')' --> nextIndex
(leftTree, op) = parseExp(index + 1)
# Parse the expression and call RightTree then return it and the next index beyond the expression.
(rightTree, nextIndex) = parseExp(op + 1)
# Analyzi op to decide what kind of Instatiate to take.
if tokens[op] == '+':
return (Sum(leftTree, rightTree), nextIndex + 1)
elif tokens[op] == '*':
return (Prod(leftTree, rightTree), nextIndex + 1)
elif tokens[op] == '/':
return (Quot(leftTree, rightTree), nextIndex + 1)
elif tokens[op] == '-':
return (Diff(leftTree, rightTree), nextIndex + 1)
elif tokens[op] == '=':
return (Assign(leftTree, rightTree), nextIndex + 1)
(parsedExp, nextIndex) = parseExp(0)
return parsedExp
# token is a string
# returns True if contains only digits
def numberTok(token):
for char in token:
if not char in string.digits: return False
return True
# token is a string
# returns True its first character is a letter
def variableTok(token):
for char in token:
if char in string.letters: return True
return False
# thing is any Python entity
# returns True if it is a number
def isNum(thing):
return type(thing) == int or type(thing) == float
# Run calculator interactively
def calc():
env = {}
while True:
e = raw_input('%') # prints %, returns user input
print '%', e, '\n', parse(tokenize(e)).eval(env)# your expression here
print ' env =', env
# exprs is a list of strings
# runs calculator on those strings, in sequence, using the same environment
def calcTest(exprs):
env = {}
for e in exprs:
print '%', e # e is the experession
# Transforms string into a token, parse token and evaluate. All of this with the previous methods
# we already created.
print parse(tokenize(e)).eval(env) # your expression here
print ' env =', env
# Simple tokenizer tests
'''Answers are:
['fred']
['777']
['777', 'hi', '33']
['*', '*', '-', ')', '(']
['(', 'hi', '*', 'ho', ')']
['(', 'fred', '+', 'george', ')']
['(', 'hi', '*', 'ho', ')']
['(', 'fred', '+', 'george', ')']
'''
def testTokenize():
print tokenize('fred ')
print tokenize('777 ')
print tokenize('777 hi 33 ')
print tokenize('**-)(')
print tokenize('( hi * ho )')
print tokenize('(fred + george)')
print tokenize('(hi*ho)')
print tokenize('( fred+george )')
# print testTokenize()
# THIS IS PROPERLY WORKING.
# Simple parsing tests from the handout
'''Answers are:
Var(a)
Num(888.0)
Sum(Var(fred), Var(george))
Quot(Prod(Var(a), Var(b)), Diff(Var(cee), Var(doh)))
Quot(Prod(Var(a), Var(b)), Diff(Var(cee), Var(doh)))
Assign(Var(a), Prod(Num(3.0), Num(5.0)))
'''
def testParse():
print parse(['a'])
print parse(['888'])
print parse(['(', 'fred', '+', 'george', ')'])
print parse(['(', '(', 'a', '*', 'b', ')', '/', '(', 'cee', '-', 'doh', ')' ,')'])
print parse(tokenize('((a * b) / (cee - doh))'))
print parse(tokenize('(a = (3 * 5))'))
# print testParse()
# THIS IS PROPERLY WORKING
####################################################################
# Test cases for EAGER evaluator
####################################################################
def testEval():
env = {}
Assign(Variable('a'), Number(5.0)).eval(env)
print Variable('a').eval(env)
env['b'] = 2.0
print Variable('b').eval(env)
env['c'] = 4.0
print Variable('c').eval(env)
print Sum(Variable('a'), Variable('b')).eval(env)
print Sum(Diff(Variable('a'), Variable('c')), Variable('b')).eval(env)
Assign(Variable('a'), Sum(Variable('a'), Variable('b'))).eval(env)
print Variable('a').eval(env)
print env
# print testEval()
# THIS IS PROPERLY WORKING
# Basic calculator test cases (see handout)
testExprs = ['(2 + 5)',
'(z = 6)',
'z',
'(w = (z + 1))',
'w'
]
calcTest(testExprs)
####################################################################
# Test cases for LAZY evaluator
####################################################################
# Simple lazy eval test cases from handout
'''Answers are:
Sum(Var(b), Var(c))
Sum(2.0, Var(c))
6.0
'''
def testLazyEval():
env = {}
Assign(Variable('a'), Sum(Variable('b'), Variable('c'))).eval(env)
print Variable('a').eval(env)
env['b'] = Number(2.0)
print Variable('a').eval(env)
env['c'] = Number(4.0)
print Variable('a').eval(env)
# Lazy partial eval test cases (see handout)
lazyTestExprs = ['(a = (b + c))',
'(b = ((d * e) / 2))',
'a',
'(d = 6)',
'(e = 5)',
'a',
'(c = 9)',
'a',
'(d = 2)',
'a']
# calcTest(lazyTestExprs)
## More test cases (see handout)
partialTestExprs = ['(z = (y + w))',
'z',
'(y = 2)',
'z',
'(w = 4)',
'z',
'(w = 100)',
'z']
# calcTest(partialTestExprs)
# Tokenizing by State Machines.
class Tokenizer(sm.SM):
startState = ''
def getNextValues(self, state, inp): # inp is a single character.
if inp in seps:
# Token is a special character.
# The nextState will be the ready token (special character) and the output will be the actual state.
return (inp, state)
# It is a space.
elif inp == ' ':
# Token is ready.
# Return the token and make the nextstate empty.
return ('', state)
# Another character.
else:
# Token is not ready yet.
# Two conditions:
# The state is a special character.
if state in seps:
# I output this because it is ready.
output = state
# Then I make the state empty again to start to prepare the next token.
state = ''
return (state+inp, output)
# The state is not a special character.
else:
# Keep preparing it storing the values into the next state and outputing an empty string.
return (state+inp, '')
# TestCases
print Tokenizer().transduce('fred ')
# ['', '', '', '', 'fred']
print Tokenizer().transduce('777 ')
# ['', '', '', '777']
print Tokenizer().transduce('777 hi 33 ')
# ['', '', '', '777', '', '', 'hi', '', '', '33']
print Tokenizer().transduce('**-)( ')
# ['', '*', '*', '-', ')', '(']
print Tokenizer().transduce('(hi*ho) ')
# ['', '(', '', 'hi', '*', '', 'ho', ')']
print Tokenizer().transduce('(fred + george) ')
# ['', '(', '', '', '', 'fred', '', '+', '', '', '', '', '', '', 'george', ')']
# input = a string of characteres. output = a list of tokens.
def tokenize(inputString):
# Transforming into a list of desorganized tokens(because they have empty strings) by the Tokenizer State Machine's subclass.
tokens_des = Tokenizer().transduce(inputString+' ')
# Removing the empty strings.
# The list of organized tokens will begin empty and will receive only the characters which are not empty strings.
tokens_org = []
for token in tokens_des:
if token != '':
tokens_org.append(token)
return tokens_org
# del does not work here, because when I delete the element of the list, when I go throught the list again with the index I want
# it will not really be the place I realy want, because the length of the list decrased by one.
# TestCases.
print tokenize('(fred + george) ')