-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine_supervisor.py
422 lines (395 loc) · 15.3 KB
/
machine_supervisor.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
class MachineSupervisor:
def __init__(self, lexical_analyzer):
self.supervisor = lexical_analyzer
self.source_string = lexical_analyzer.source_string
self.current_string = None
self.end = len(self.source_string) - 1
self.frontP = 0
self.backP = 0
self.found = None
self.nth = None
self.get_attribute = lexical_analyzer.attribute_table
self.machineList = [
self.ws_machine,
self.addop_machine,
self.mulop_machine,
self.id_machine,
self.longreal_machine,
self.real_machine,
self.int_machine,
self.relop_machine,
self.catchall_machine
]
self.start_machine()
def start_machine(self):
while not(self.backP == self.end):
self.run_machines()
self.backP = self.frontP
def reset(self):
self.frontP = self.backP
def run_machines(self):
nth = 0
found = False
while not found:
self.reset()
found = self.machineList[nth]()
nth += 1
def get_next_char(self):
try:
current_char = self.source_string[self.frontP]
self.frontP += 1
return current_char
except IndexError:
return ''
def write_to_token_file(self, token_type, attribute):
self.current_string = self.source_string[self.backP:self.frontP]
attribute_info = None
if token_type == 'KEYWORD':
attribute_info = self.supervisor.reserved_words[attribute]
elif token_type == 'ID':
attribute_info = self.supervisor.symbol_table[attribute]
elif token_type == 'LEXERR':
attribute_info = attribute
self.supervisor.listing_file.write("{0:<10} {1:<30} {2:<15}\n"
.format('LEXERR:', attribute+':', self.current_string))
elif attribute in self.get_attribute:
attribute_info = self.get_attribute[attribute] # use dictionary to get the attribute number
else:
attribute_info = attribute
# NEED TO IMPLEMENT ADDING TOKEN IN TOKEN TABLE
# NEED TO IMPLEMENT ADDING LEXERR IN LISTING FILE AS WELL
# self.supervisor.token_table()
self.supervisor.token_file.\
write("{0:<10} {1:<15} {2:<15} {3:<20}\n"
.format(self.supervisor.line_num, self.current_string, token_type, attribute_info))
def ws_machine(self):
curr_char = self.get_next_char()
self.whitespaces = [
'\n',
'\r',
'\t',
' '
]
if curr_char == '\n' or curr_char == '\r':
return True
elif curr_char == '\t' or curr_char == ' ':
curr_char = self.get_next_char()
while curr_char == '\t' or curr_char == ' ':
curr_char = self.get_next_char()
self.frontP -= 1
return True
else:
return False
def addop_machine(self):
curr_char = self.get_next_char()
if curr_char == '+':
self.write_to_token_file('ADDOP', 'PLUS')
return True
elif curr_char == '-':
self.write_to_token_file('ADDOP', 'MINUS')
return True
elif curr_char == 'o':
curr_char = self.get_next_char()
if curr_char == 'r'and self.get_next_char() == ' ':
self.frontP -= 1
self.write_to_token_file('ADDOP', 'OR')
return True
return False
def mulop_machine(self):
curr_char = self.get_next_char()
if curr_char == '*':
self.write_to_token_file('MULOP', 'MULT')
return True
elif curr_char == '/':
self.write_to_token_file('MULOP', 'DIVIDE')
return True
elif curr_char == 'd':
curr_char = self.get_next_char()
if curr_char == 'i':
curr_char = self.get_next_char()
if (curr_char == 'v' and
self.get_next_char() in self.whitespaces):
self.frontP -= 1
self.write_to_token_file('MULOP', 'DIV')
return True
elif curr_char == 'm':
curr_char = self.get_next_char()
if curr_char == 'o':
curr_char = self.get_next_char()
if (curr_char == 'd' and
self.get_next_char() in self.whitespaces):
self.frontP -= 1
self.write_to_token_file('MULOP', 'MOD')
return True
elif curr_char == 'a':
curr_char = self.get_next_char()
if curr_char == 'n':
curr_char = self.get_next_char()
if (curr_char == 'd' and
self.get_next_char() in self.whitespaces):
self.frontP -= 1
self.write_to_token_file('MULOP', 'AND')
return True
return False
def id_machine(self):
curr_char = self.get_next_char()
if curr_char.isalpha():
id_string = curr_char
curr_char = self.get_next_char()
while curr_char.isalnum():
id_string += curr_char
curr_char = self.get_next_char()
self.frontP -= 1 # this doesn't cover the case when it is '', frontP is one less than what it should be
if id_string in self.supervisor.reserved_words:
# write_to_token_file needs to change so it accommodates numbers as well
# self.write_to_token_file('KEYWORD', self.supervisor.reserved_words.index(id_string))
self.write_to_token_file('KEYWORD', id_string)
else:
if id_string not in self.supervisor.symbol_table:
# not sure what the symbol_table is suppose to do...
# are you suppose to add the id_string into symbol_table when it has an error
# "id too long"?
# self.supervisor.symbol_table.append(id_string)
self.supervisor.symbol_table[id_string] = id(id_string)
if len(id_string) > 10:
# error token for length
self.write_to_token_file('LEXERR', 'ID too long')
else:
# how to get a unique attribute number for the said id?
self.write_to_token_file('ID', id_string)
return True
return False
def print_error(self, attribute):
self.write_to_token_file('LEXERR', attribute)
def longreal_machine(self):
curr_char = self.get_next_char()
int_length = 0
real_length = 0
exp_length = 0
errors = []
error_type = {
0: 'LONGREAL has leading zeros',
1: 'LONGREAL xx too long',
2: 'LONGREAL yy too long',
3: 'LONGREAL has trailing zeros',
4: 'LONGREAL zz too long',
}
if curr_char.isdigit():
val = float(curr_char)
num_zeroes = self.has_leading_zeros(curr_char)
int_length += num_zeroes
# check for leading zeros
if num_zeroes > 1:
errors.append(0)
while curr_char.isdigit():
int_length += 1
val *= 10
val += int(curr_char)
curr_char = self.get_next_char()
# check for the length of int place
if int_length > 5:
errors.append(1)
if curr_char == '.':
curr_char = self.get_next_char()
while curr_char.isdigit():
real_length += 1
curr_char = float(curr_char) * (0.1 ** real_length)
val += float(curr_char)
curr_char = self.get_next_char()
# check for length of real place
if real_length > 5:
errors.append(2)
# check for trailing zeros
if self.has_trailing_zeros(real_length):
errors.append(3)
# do I need to check to see if it is capital or lower case e?
if curr_char == 'E':
curr_char = self.get_next_char()
exp_val = 0
while curr_char.isdigit():
exp_length += 1
exp_val *= 10
exp_val += int(curr_char)
curr_char = self.get_next_char()
if exp_length > 2:
errors.append(4)
val = val ** exp_val
self.frontP -= 1
if real_length == 0 or exp_length == 0:
return False
elif len(errors) > 0:
for e in errors:
self.print_error(error_type[e])
else:
self.write_to_token_file('LONGREAL', val)
return True
return False
def real_machine(self):
curr_char = self.get_next_char()
int_length = 0
real_length = 0
errors = []
error_type = {
0: 'REAL has leading zeros',
1: 'REAL has too long integers',
2: 'REAL has too long decimals',
3: 'REAL has trailing zeros',
}
if curr_char.isdigit():
val = int(curr_char)
num_zeroes = self.has_leading_zeros(curr_char)
int_length += num_zeroes
# check for leading zeros
if num_zeroes > 1:
errors.append(0)
while curr_char.isdigit():
int_length += 1
val *= 10
val += int(curr_char)
curr_char = self.get_next_char()
# check for the length of int place
if int_length > 5:
errors.append(1)
if curr_char == '.':
curr_char = self.get_next_char()
while curr_char.isdigit():
real_length += 1
curr_char = float(curr_char) * (0.1 ** real_length)
val += float(curr_char)
curr_char = self.get_next_char()
# check for length of real place
if real_length > 5:
errors.append(2)
# check for trailing zeros
if self.has_trailing_zeros(real_length):
errors.append(3)
self.frontP -= 1
if real_length == 0:
return False
elif len(errors) > 0:
for e in errors:
self.print_error(error_type[e])
else:
self.write_to_token_file('REAL', val)
return True
return False
def int_machine(self):
curr_char = self.get_next_char()
int_length = 0
errors = []
error_type = {
0: 'INT has leading zeros',
1: 'INT too long',
}
if curr_char.isdigit():
val = int(curr_char)
num_zeroes = self.has_leading_zeros(curr_char)
int_length += num_zeroes
curr_char = self.get_next_char()
while curr_char.isdigit():
int_length += 1
val *= 10
val += int(curr_char)
curr_char = self.get_next_char()
if num_zeroes > 1:
errors.append(0)
if int_length > 10:
errors.append(1)
self.frontP -= 1
# empty can be rewritten as not errors?
if len(errors) > 0:
for e in errors:
self.print_error(error_type[e])
else:
self.write_to_token_file('INT', val)
return True
return False
def has_leading_zeros(self, num):
leading_zero = 0
if num == '0':
while num == '0':
leading_zero += 1
num = self.get_next_char()
self.frontP -= 1
return leading_zero
def has_trailing_zeros(self, real_length):
# check the char at the very end
check_place = self.frontP - 2
try:
num = self.source_string[check_place]
if num == '0' and real_length > 1:
return True
return False
except IndexError:
return False
def relop_machine(self):
curr_char = self.get_next_char()
if curr_char == '=':
self.write_to_token_file('RELOP', 'EQUAL')
return True
elif curr_char == '<':
curr_char = self.get_next_char()
states = {
'>': ['RELOP', 'NOTEQUAL'],
'=': ['RELOP', 'LEQUAL'],
}
if curr_char in states:
token = states[curr_char]
token_type = token[0]
attribute = token[1]
self.write_to_token_file(token_type, attribute)
else:
self.frontP -= 1
self.write_to_token_file('RELOP', 'LESS')
return True
elif curr_char == '>':
curr_char = self.get_next_char()
if curr_char == '=':
self.write_to_token_file('RELOP', 'GEQUAL')
else:
self.frontP -= 1
self.write_to_token_file('RELOP', 'GREATER')
return True
return False
def catchall_machine(self):
curr_char = self.get_next_char()
states = {
'(': ['OPENPAREN', 61],
')': ['CLOSEDPAREN', 62],
';': ['SEMICOLON', 63],
',': ['COMMA', 64],
'[': ['OPENBRACK', 65],
']': ['CLOSEDBRACK', 66]
}
if curr_char in states:
token = states[curr_char]
token_type = token[0]
attribute = token[1]
self.write_to_token_file(token_type, attribute)
elif curr_char == '.':
token = self.dot_state()
token_type = token[0]
attribute = token[1]
self.write_to_token_file(token_type, attribute)
elif curr_char == ':':
token = self.colon_state()
token_type = token[0]
attribute = token[1]
self.write_to_token_file(token_type, attribute)
else:
self.write_to_token_file('LEXERR', 'Unrecognized Symbol')
return True
def dot_state(self):
curr_char = self.get_next_char()
if curr_char == '.':
return['DOUBLEDOT', 68]
else:
self.frontP -= 1
return ['DOT', 67]
def colon_state(self):
curr_char = self.get_next_char()
if curr_char == '=':
return ['ASSIGNOP', 60]
else:
self.frontP -= 1
return ['COLON', 69]