-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.py
385 lines (352 loc) · 15.9 KB
/
scanner.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
# With some help from CS143 stanford course
import enum
class TokenType(enum.Enum):
EOF = -1
NUM = 0
ID = 1
KEYWORD = 2
SYMBOL = 3
COMMENT = 4
WHITESPACE = 5
class ErrorType(enum.Enum):
INVALID_INPUT = 'Invalid input'
UNCLOSED_COMMENT = 'Unclosed comment'
UNMATCHED_COMMENT = 'Unmatched comment'
INVALID_NUMBER = 'Invalid number'
class TokenState(enum.Enum):
INITIAL = 0
WHITESPACE = 1
NUM_START = 2
NUM = 3
INVALID_NUMBER = 4
START_ID_OR_KEYWORD = 5
ID_OR_KEYWORD = 6
ASTERISK = 7
UNMATCHED_COMMENT = 8
START_EQUAL = 9
EQUAL_EQUAL = 10
EQUAL = 11
SYMBOL = 12
START_SLASH = 13
START_BLOCK_COMMENT = 14
BLOCK_COMMENT_ASTERISK = 15
BLOCK_COMMENT = 16
START_LINE_COMMENT = 17
LINE_COMMENT = 18
NEWLINE = 19
INVALID_INPUT = 20
ASTERISK_SYMBOL = 21
class Scanner:
SYMBOLS = [';', ':', ',', '[', ']', '(', ')', '{', '}', '+', '-', '<']
KEYWORDS = ['if', 'else', 'void', 'int',
'repeat', 'break', 'until', 'return', 'endif']
WHITESPACES = [' ', '\r', '\t', '\v', '\f']
def __init__(self, input_file, chunk_size=8192):
self.tokens = dict()
self.errors = dict()
self.symbol_table = [{
"lexeme": "output",
"fnuc/var": "function",
"no.Args": 1,
"type": "void",
"scope": 0,
"params": ["int"]
}]
self.scope_stack = [0]
self.input_file = input_file
self.text = ''
self.file_pointer = 0
self.current_line = 1
self.unclosed_comment_size = 7
self.chunk_size = chunk_size
self.declaration_flag = False
self.error_flag = False
self.arg_list_stack = []
self.read_text()
self.init_symbol_table()
def read_text(self):
with open(self.input_file, "r", encoding="utf-8") as f:
f.seek(self.file_pointer)
chunk = f.read(self.chunk_size)
if not chunk:
raise EOFError
self.text += chunk
self.file_pointer += self.chunk_size
def init_symbol_table(self):
for keyword in self.KEYWORDS:
# self.symbol_table.append(keyword)
self.symbol_table.append({"lexeme": keyword, "scope": len(self.scope_stack) - 1})
def scan(self):
token = self.get_next_token()
if token is not None:
(token_type, lexeme) = token
while token_type != TokenType.EOF:
if token is not None:
if token_type == TokenType.KEYWORD or token_type == TokenType.ID:
if lexeme not in self.symbol_table:
# self.symbol_table.append(lexeme)
self.symbol_table.append({"lexeme": lexeme, "scope": len(self.scope_stack) - 1})
self.add_token(token_type, lexeme)
token = self.get_next_token()
(token_type, lexeme) = token
self.save_tokens()
self.save_symbol_table()
self.save_errors()
def save_tokens(self):
tokens = ''
for line_number, line_tokens in self.tokens.items():
tokens += str(line_number) + '.' + '\t'
for token in line_tokens:
(token_type, lexeme) = token
tokens += '(' + str(token_type) + ', ' + lexeme + ') '
tokens += '\n'
with open('tokens.txt', 'w', encoding="utf-8") as file:
file.write(tokens)
def save_symbol_table(self):
symbol_table = ''
for line_number, symbol in enumerate(self.symbol_table):
symbol_table += str(line_number + 1) + '.' + ' ' + symbol + '\n'
with open('symbol_table.txt', 'w', encoding="utf-8") as file:
file.write(symbol_table)
def save_errors(self):
errors = ''
if self.errors:
for line_number, line_errors in self.errors.items():
errors += str(line_number) + '.' + '\t'
for error in line_errors:
(lexeme, error_type) = error
errors += '(' + lexeme + ', ' + str(error_type) + ') '
errors += '\n'
else:
errors = 'There is no lexical error.'
with open('lexical_errors.txt', 'w', encoding="utf-8") as file:
file.write(errors)
def add_error(self, error):
(error_type, lexeme) = error
if self.current_line not in self.errors:
self.errors[self.current_line] = []
self.errors[self.current_line].append((lexeme, error_type.value))
def add_token(self, token_type, lexeme):
if self.current_line not in self.tokens:
self.tokens[self.current_line] = []
self.tokens[self.current_line].append((token_type.name, lexeme))
def add_to_symbol_table(self, token):
scope = len(self.scope_stack) - 1
if token is not None:
(token_type, lexeme) = token
if token_type == TokenType.ID:
if self.find_address(lexeme, scope) == -1:
self.symbol_table.append(
{"lexeme": lexeme, "scope": scope, "fnuc/var": "local_var"})
def get_next_token(self):
input_ended = False
state = TokenState.INITIAL
while True:
if not self.text or input_ended:
try:
self.read_text()
except EOFError:
if state == TokenState.START_BLOCK_COMMENT or state == TokenState.BLOCK_COMMENT_ASTERISK or \
state == TokenState.START_LINE_COMMENT:
comment_size = self.unclosed_comment_size
lexeme = self.text[:comment_size]
if len(self.text) > len(lexeme):
lexeme = lexeme + '...'
self.add_error((ErrorType.UNCLOSED_COMMENT, lexeme))
elif state == TokenState.NUM_START:
token = (TokenType.NUM, self.text)
self.text = ''
return token
elif state == TokenState.START_ID_OR_KEYWORD:
token_type = TokenType.KEYWORD if self.text in self.KEYWORDS else TokenType.ID
token = (token_type, self.text)
self.text = ''
return token
self.current_line += self.text.count("\n")
self.text = ''
return TokenType.EOF, '$'
tokens = []
error_occurred = False
input_ended = False
state = TokenState.INITIAL
for i in range(len(self.text) + 1):
try:
next_character = self.text[i]
except IndexError:
next_character = self.text[-1]
if state == TokenState.INITIAL: # 0
if next_character in self.WHITESPACES:
state = TokenState.WHITESPACE
elif next_character.isdigit():
state = TokenState.NUM_START
elif next_character.isalpha():
state = TokenState.START_ID_OR_KEYWORD
elif next_character == '*':
state = TokenState.ASTERISK
elif next_character == '=':
state = TokenState.START_EQUAL
elif next_character in self.SYMBOLS:
state = TokenState.SYMBOL
elif next_character == '/':
state = TokenState.START_SLASH
elif next_character == '\n':
state = TokenState.NEWLINE
else:
state = TokenState.INVALID_INPUT
elif state == TokenState.WHITESPACE: # 1
if next_character in self.WHITESPACES or next_character == '\n':
state = TokenState.WHITESPACE
else:
break
elif state == TokenState.NUM_START: # 2
if next_character in self.WHITESPACES:
state = TokenState.NUM
elif next_character.isdigit():
state = TokenState.NUM_START
elif next_character.isalpha():
state = TokenState.INVALID_NUMBER
elif next_character == '*' or next_character == '=' or next_character in self.SYMBOLS or \
next_character == '/' or next_character == '\n':
state = TokenState.NUM
else:
state = TokenState.INVALID_NUMBER
elif state == TokenState.NUM: # 3
tokens.append((TokenType.NUM, self.text[:i - 1]))
state = None
elif state == TokenState.INVALID_NUMBER: # 4
error = (ErrorType.INVALID_NUMBER, self.text[:i])
self.add_error(error)
self.text = self.text[i:]
error_occurred = True
break
elif state == TokenState.START_ID_OR_KEYWORD: # 5
if next_character in self.WHITESPACES:
state = TokenState.ID_OR_KEYWORD
elif next_character.isdigit() or next_character.isalpha():
state = TokenState.START_ID_OR_KEYWORD
elif next_character == '*' or next_character == '=' or next_character in self.SYMBOLS or \
next_character == '/' or next_character == '\n':
state = TokenState.ID_OR_KEYWORD
else:
state = TokenState.INVALID_INPUT
elif state == TokenState.ID_OR_KEYWORD: # 6
tokens.append((TokenType.ID, self.text[:i - 1]))
state = None
elif state == TokenState.ASTERISK: # 7
if next_character in self.WHITESPACES or next_character.isdigit() or next_character.isalpha() or \
next_character == '*' or next_character == '=' or \
next_character == '\n' or next_character in self.SYMBOLS:
state = TokenState.ASTERISK_SYMBOL
elif next_character == '/':
state = TokenState.UNMATCHED_COMMENT
else:
state = TokenState.INVALID_INPUT
elif state == TokenState.UNMATCHED_COMMENT: # 8
error = (ErrorType.UNMATCHED_COMMENT, self.text[:i])
self.add_error(error)
self.text = self.text[i:]
error_occurred = True
break
elif state == TokenState.START_EQUAL: # 9
if next_character in self.WHITESPACES or next_character.isdigit() or next_character.isalpha() or \
next_character == '*' or next_character == '/' or next_character == '\n' \
or next_character in self.SYMBOLS:
state = TokenState.EQUAL
elif next_character == '=':
state = TokenState.EQUAL_EQUAL
else:
state = TokenState.INVALID_INPUT
elif state == TokenState.EQUAL_EQUAL: # 10
tokens.append((TokenType.SYMBOL, self.text[:i]))
state = None
elif state == TokenState.EQUAL: # 11
tokens.append((TokenType.SYMBOL, self.text[:i - 1]))
state = None
elif state == TokenState.SYMBOL: # 12
tokens.append((TokenType.SYMBOL, self.text[:i]))
state = None
elif state == TokenState.START_SLASH: # 13
if next_character == '*':
state = TokenState.START_BLOCK_COMMENT
elif next_character == '/':
state = TokenState.START_LINE_COMMENT
else:
error = (ErrorType.INVALID_INPUT, self.text[:i])
self.add_error(error)
self.text = self.text[i:]
error_occurred = True
break
elif state == TokenState.START_BLOCK_COMMENT: # 14
if next_character == '*':
state = TokenState.BLOCK_COMMENT_ASTERISK
else:
state = TokenState.START_BLOCK_COMMENT
elif state == TokenState.BLOCK_COMMENT_ASTERISK: # 15
if next_character == '*':
state = TokenState.BLOCK_COMMENT_ASTERISK
elif next_character == '/':
state = TokenState.BLOCK_COMMENT
else:
state = TokenState.START_BLOCK_COMMENT
elif state == TokenState.BLOCK_COMMENT: # 16
tokens.append((TokenType.COMMENT, self.text[:i]))
state = None
elif state == TokenState.START_LINE_COMMENT: # 17
if next_character == '\n':
state = TokenState.LINE_COMMENT
else:
state = TokenState.START_LINE_COMMENT
elif state == TokenState.LINE_COMMENT: # 18
tokens.append((TokenType.COMMENT, self.text[:i]))
state = None
elif state == TokenState.NEWLINE: # 19
if next_character in self.WHITESPACES or next_character == '\n':
state = TokenState.NEWLINE
else:
tokens.append((TokenType.WHITESPACE, self.text[:i]))
state = None
elif state == TokenState.INVALID_INPUT: # 20
error = (ErrorType.INVALID_INPUT, self.text[:i])
self.add_error(error)
self.text = self.text[i:]
error_occurred = True
break
elif state == TokenState.ASTERISK_SYMBOL: # 21
tokens.append((TokenType.SYMBOL, self.text[:i - 1]))
state = None
if state is None:
break
elif i >= len(self.text):
input_ended = True
break
if error_occurred or input_ended:
continue
if tokens:
max_token = tokens[-1]
(token, lexeme) = max_token
self.text = self.text[len(lexeme):]
if token == TokenType.WHITESPACE or token == TokenType.COMMENT:
self.current_line += lexeme.count('\n')
continue
if token == TokenType.ID:
token = TokenType.KEYWORD if lexeme in self.KEYWORDS else TokenType.ID
return token, lexeme
else:
self.text = self.text[1:]
def find_address(self, lexeme, scope):
for i in range(len(self.symbol_table)):
if self.symbol_table[i]["lexeme"] == lexeme:
if "fnuc/var" in self.symbol_table[i]:
if self.symbol_table[i]["fnuc/var"] == "function":
if self.symbol_table[i]["scope"] <= scope:
return i
elif self.symbol_table[i]["fnuc/var"] == "global_var":
if self.symbol_table[i]["scope"] <= scope:
return i
else:
if self.symbol_table[i]["scope"] == scope:
return i
else:
if self.symbol_table[i]["scope"] == scope:
return i
return -1