-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
555 lines (508 loc) · 23.1 KB
/
Copy pathmain.py
File metadata and controls
555 lines (508 loc) · 23.1 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import sys
# Character classes
LETTER = 0
DIGIT = 1
UNKNOWN = 99
# token codes
reserved_word = 10 # reserved word : call, variable, print_ari
ident = 11 # identifier -> letter, digit, underbar(_)...starting with letter or underbar(_)!
semi_colon = 12 # ;
comma = 13 # ,
left_paren = 14 # {
right_paren = 15 # }
func_name = 16 # function name
eof = 28 # $ eof
# global declarations
charClass = 0 # Is character digit/letter
token_string = "" # save lexeme
tokenized_text = [] # tokenized input
token_index = 0 # input string's index
nextChar = "" # get next character in input string
next_token = 0 # save token type
# error declarations
syntax_error = False
syntax_error_msg = "Syntax Error.\n"
syntax_ok_msg = "Syntax O.K.\n"
# ======================================================
# for getting inputs from txt file
f = open(sys.argv[1], 'r')
txt = f.readlines()
input = ""
for statement in txt:
input += statement.strip()
input += "$"
# ======================================================
# functions for grammar
def start(): # <start> -> <functions>
#print("start checking grammar")
getChar()
lexical()
functions()
if syntax_error == False:
print(syntax_ok_msg)
else: print(syntax_error_msg)
def functions(): # <functions> -> <function> | <function> <functions>
function()
if next_token != eof:
lexical()
functions()
else:
pass
def function(): # <function> -> <identifier> { <function_body> }
global syntax_error, tokenized_text
if next_token == ident:
lexical()
if next_token == left_paren:
lexical()
function_body()
if next_token == right_paren:
lexical()
else :
syntax_error = True
#print("오른쪽 괄호가 없음 function()")
def function_body(): # <function_body> -> <var_definitions> <statements> | <statements>
if next_token == reserved_word and token_string == "variable":
var_definitions()
lexical()
statements()
else:
lexical()
statements()
def var_definitions(): # <var_definitions> -> <var_definition> | <var_definition> <var_definitions>
var_definition()
lexical()
if next_token == reserved_word and token_string == "variable":
var_definitions()
else:
pass
def var_definition(): # <var_definition> -> variable <var_list> ;
global syntax_error
if next_token == reserved_word and token_string == "variable":
lexical()
var_list()
if next_token == semi_colon:
pass
else:
syntax_error = True
#print("var_definition() 세미콜론 없음")
else:
syntax_error = True
#print("var_definition() variable로 시작 안함")
def var_list(): # <var_list> -> <identifier> | <identifier>, <var_list>
if next_token == ident:
lexical()
if next_token == comma:
lexical()
var_list()
else:
pass
def statements(): # <statements> -> <statement> | <statement> <statements>
statement()
if next_token != right_paren:
lexical()
statements()
else:
pass
def statement(): # <statement> -> call <identifier>; | print_ari; | <identifier> ;
global syntax_error
if next_token == reserved_word and token_string == "call":
lexical()
if next_token == ident:
lexical()
if next_token == semi_colon:
pass
else:
syntax_error =True
#print("statement() call ident 뒤에 세미콜론 없음")
else:
syntax_error = True
#print("staement() call 뒤에 ident 없음")
elif next_token == reserved_word and token_string == "print_ari":
lexical()
if next_token == semi_colon:
pass
else:
syntax_error = True
#print("statement() print_ari뒤에 세미콜론 없음")
elif next_token == ident:
lexical()
if next_token == semi_colon:
pass
else:
syntax_error = True
#print("statement() ident 뒤에 세미콜론 없음")
else:
pass
# ======================================================
# functions for lexical()
def lookup(ch):
global next_token, token_string
if ch == '{':
addChar()
next_token = left_paren
#print(token_string)
tokenized_text.append([token_string, next_token])
elif ch == '}':
addChar()
next_token = right_paren
#print(token_string)
tokenized_text.append([token_string, next_token])
elif ch == ',':
addChar()
next_token = comma
#print(token_string)
tokenized_text.append([token_string, next_token])
elif ch == ';':
addChar()
next_token = semi_colon
#print(token_string)
tokenized_text.append([token_string, next_token])
elif ch == '$':
addChar()
next_token = eof
tokenized_text.append([token_string, next_token])
def addChar():
global token_string, nextChar
token_string += nextChar
def getChar():
global nextChar, token_index, charClass
nextChar = input[token_index]
token_index += 1
if nextChar != '$':
if nextChar.isalpha() or nextChar == '_': # In c identifier rule, identifier can have letter, digit , or underbar(_)
charClass = LETTER # let letter and underbar(_) be letter charClass
elif nextChar.isdigit():
charClass = DIGIT
else:
charClass = UNKNOWN
else:
charClass = eof
def getNonBlank(): # skip the blanks
while nextChar.isspace():
getChar()
def lexical():
global charClass, next_token, token_string
token_string = ""
getNonBlank()
if charClass == LETTER: # identifier must start with letter or underbar(_), not digit
addChar()
getChar()
while charClass == LETTER or charClass == DIGIT:
addChar()
getChar()
if token_string == "call":
next_token = reserved_word
#print(token_string)
tokenized_text.append([token_string,next_token])
elif token_string == "print_ari":
next_token = reserved_word
#print(token_string)
tokenized_text.append([token_string,next_token])
elif token_string == "variable":
next_token = reserved_word
#print(token_string)
tokenized_text.append([token_string,next_token])
else:
next_token = ident
#print(token_string)
tokenized_text.append([token_string,next_token])
elif charClass == UNKNOWN:
lookup(nextChar)
getChar()
elif charClass == eof:
next_token = eof
tokenized_text.append([token_string, next_token])
if next_token == eof:
pass
# =========start parsing and check the grammar=========
start()
# =======execute the program with main function========
activation_record_instance = {} # sample ari
ari = [] # ari
main_list = [] # save the temp ari of main
func_list = [0,0] # save the temp ari of other sub function
dynamic_link = ["main",] # save the dynamic call
function_line = 0 # execute statement count
function_idx = [] # position of functions in tokenized_text
function_name = [] # save function name declared in tokenized_text
ep = 0 # save EP point
main = False # flag that tells us the function is main or not
if not syntax_error:
# find the function name position
for i in range(len(tokenized_text)):
if tokenized_text[i][0] == '{':
function_name.append(tokenized_text[i-1][0])
function_idx.append(i-1)
# if main function is not declared, then print error msg and exit
if 'main' in function_name:
main = True
else:
print("“No starting function.”")
# making sample ari
for i in range(len(tokenized_text)):
if tokenized_text[i][0] == '{':
j = i
while 1:
if tokenized_text[j+1][0] == '}':
break
j += 1
if tokenized_text[j][0] == "variable":
while 1:
j += 1
if tokenized_text[j][0] != ';':
if tokenized_text[j][1] == 11:
if tokenized_text[i-1][0] == "main":
main_list.append(tokenized_text[j][0])
else:
func_list.append(tokenized_text[j][0])
else:
break
if tokenized_text[i-1][0] == "main":
activation_record_instance[tokenized_text[i-1][0]]=main_list
main_list = []
else :
activation_record_instance[tokenized_text[i-1][0]]=func_list
func_list = [0, 0]
i = j
# if there is main function, then execute the program!
if main:
caller_func=''
ari.append(['main', activation_record_instance['main']])
i = function_idx[function_name.index('main')]
i += 1 # '{'
while 1:
i += 1
if tokenized_text[i][0] == '}': # if main ends, exit the program
break
elif tokenized_text[i][0] == 'variable':
function_line -= 1
elif tokenized_text[i][0] == ';':
function_line += 1
elif tokenized_text[i][0] == "print_ari": # if there is print_ari, print ari
#print("hi ari~", ari)
for ari_idx in range(len(ari) - 1, -1, -1):
for stack_idx in range(len(ari[ari_idx][1]) - 1, -1, -1):
string_s = ""
for blank in range(len(ari[ari_idx][0])+1):
string_s += " "
if ari[ari_idx][0] == 'main':
if stack_idx == len(ari[ari_idx][1]) - 1:
local_string = str(ari[ari_idx][0]) + ":Local variable: " + str(
ari[ari_idx][1][stack_idx])
print(local_string)
else:
local_string = string_s+"Local variable: " + str(ari[ari_idx][1][stack_idx])
print(local_string)
else:
if stack_idx == len(ari[ari_idx][1]) - 1:
local_string = str(ari[ari_idx][0]) + ":Local variable: " + str(
ari[ari_idx][1][stack_idx])
print(local_string)
elif stack_idx == 0: # ret
ret_string = string_s+"Return Address: " + str(ari[ari_idx][1][stack_idx][0]) + ":" + str(
ari[ari_idx][1][stack_idx][1]) + "\n"
print(ret_string)
elif stack_idx == 1: # dynamic link
dynamic_string = string_s+"Dynamic Link: " + str(ari[ari_idx][1][stack_idx])
print(dynamic_string)
else:
local_string = string_s+"Local variable: " + str(ari[ari_idx][1][stack_idx])
print(local_string)
if ari[ari_idx][0] == 'main':
print("")
elif (tokenized_text[i - 1][0] == '{' and tokenized_text[i][1] == 11) or (
tokenized_text[i - 1][0] == ';' and tokenized_text[i][1] == 11):
link_count = 0
local_offset = 0
found = False
sample_idx = function_name.index('main') + 1
while 1:
if sample_idx < 0:
break
while 1:
if ari[sample_idx][1][local_offset] == tokenized_text[i][0]:
found = True
break
else:
local_offset += 1
if local_offset >= len(ari[sample_idx][1]):
local_offset = 0
break
if found:
sample_string = "main" + ":" + str(tokenized_text[i][0]) + " => " + str(
link_count) + ", " + str(local_offset) +"\n"
print(sample_string)
link_count = 0
local_offset = 0
break
link_count += 1
sample_idx -= 1
local_offset = 0
elif tokenized_text[i][0] == 'call':
func_list = activation_record_instance[tokenized_text[i+1][0]]
func_list[0] = ['main', function_line + 1]
func_list[1] = ep
ep += 2
ari.append([tokenized_text[i+1][0], func_list])
func = tokenized_text[i+1][0]
caller_func = func
dynamic_link.append(func)
# 메인에서 호출한 first 함수로 넘어감
j = function_idx[function_name.index(func)]
j += 1 # '{'
while 1:
j += 1
function_line1 = 0
if tokenized_text[j][0] == '}': # first sub function 끝나면 종료
ari.pop()
#print("after finishing first and pop first ", ari)
break
elif tokenized_text[j][0] == 'variable':
function_line1 -= 1
elif tokenized_text[j][0] == ';':
function_line1 += 1
elif tokenized_text[j][0] == "print_ari": # print_ari 나오면 ari 출력해주기
#print("hi ari~", ari)
for ari_idx in range(len(ari) - 1, -1, -1):
for stack_idx in range(len(ari[ari_idx][1]) - 1, -1, -1):
string_s = ""
for blank in range(len(ari[ari_idx][0]) + 1):
string_s += " "
if ari[ari_idx][0] == 'main':
if stack_idx == len(ari[ari_idx][1]) - 1:
local_string = str(ari[ari_idx][0]) + ":Local variable: " + str(
ari[ari_idx][1][stack_idx])
print(local_string)
else:
local_string = string_s+"Local variable: " + str(ari[ari_idx][1][stack_idx])
print(local_string)
else:
if stack_idx == len(ari[ari_idx][1]) - 1:
local_string = str(ari[ari_idx][0]) + ":Local variable: " + str(
ari[ari_idx][1][stack_idx])
print(local_string)
elif stack_idx == 0: # ret
ret_string = string_s+"Return Address: " + str(
ari[ari_idx][1][stack_idx][0]) + ":" + str(
ari[ari_idx][1][stack_idx][1]) + "\n"
print(ret_string)
elif stack_idx == 1: # dynamic link
dynamic_string = string_s+"Dynamic Link: " + str(ari[ari_idx][1][stack_idx])
print(dynamic_string)
else:
local_string = string_s+"Local variable: " + str(ari[ari_idx][1][stack_idx])
print(local_string)
if ari[ari_idx][0] == 'main':
print("")
elif (tokenized_text[j - 1][0] == '{' and tokenized_text[j][1] == 11) or (
tokenized_text[j - 1][0] == ';' and tokenized_text[j][1] == 11):
link_count = 0
local_offset = 0
found = False
sample_idx = function_name.index(caller_func) + 1
while 1:
if sample_idx < 0:
break
while 1:
if ari[sample_idx][1][local_offset] == tokenized_text[j][0]:
found = True
break
else:
local_offset += 1
if local_offset >= len(ari[sample_idx][1]):
local_offset = 0
break
if found:
sample_string = caller_func+":"+str(tokenized_text[j][0])+" => "+str(link_count)+", "+str(local_offset) +"\n"
print(sample_string)
link_count = 0
local_offset = 0
break
link_count += 1
sample_idx -= 1
local_offset = 0
elif tokenized_text[j][0] == 'call':
func_list = activation_record_instance[tokenized_text[j + 1][0]]
func_list[0] = [caller_func, function_line1 + 1]
func_list[1] = ep
ep += len(func_list)
ari.append([tokenized_text[j + 1][0], func_list])
func = tokenized_text[j + 1][0]
dynamic_link.append(func)
# first에서 호출한 second 함수로 넘어감
k = function_idx[function_name.index(func)]
k += 1 # '{'
caller_func = func
while 1:
k += 1
function_line2 = 0
if tokenized_text[k][0] == '}': # second sub function 끝나면 종료
dynamic_link.pop()
caller_func = dynamic_link.pop()
ari.pop()
#print("after finishing second and pop second ", ari)
break
elif tokenized_text[k][0] == "print_ari": # print_ari 나오면 ari 출력해주기
#print("hi ari!~", ari)
for ari_idx in range(len(ari)-1,-1,-1):
for stack_idx in range(len(ari[ari_idx][1])-1,-1,-1):
string_s = ""
for blank in range(len(ari[ari_idx][0]) + 1):
string_s += " "
if ari[ari_idx][0] == 'main':
if stack_idx == len(ari[ari_idx][1])-1:
local_string = str(ari[ari_idx][0])+":Local variable: "+str(ari[ari_idx][1][stack_idx])
print(local_string)
else:
local_string = string_s+"Local variable: "+str(ari[ari_idx][1][stack_idx])
print(local_string)
else:
if stack_idx == len(ari[ari_idx][1])-1:
local_string = str(ari[ari_idx][0])+":Local variable: "+str(ari[ari_idx][1][stack_idx])
print(local_string)
elif stack_idx == 0: # ret
ret_string = string_s+"Return Address: "+str(ari[ari_idx][1][stack_idx][0])+":"+str(ari[ari_idx][1][stack_idx][1])+"\n"
print(ret_string)
elif stack_idx == 1: # dynamic link
dynamic_string = string_s+"Dynamic Link: "+str(ari[ari_idx][1][stack_idx])
print(dynamic_string)
else:
local_string = string_s+"Local variable: "+str(ari[ari_idx][1][stack_idx])
print(local_string)
if ari[ari_idx][0] == 'main':
print("")
elif (tokenized_text[k-1][0] == '{' and tokenized_text[k][1] == 11) or (tokenized_text[k-1][0] == ';' and tokenized_text[k][1] == 11):
link_count = 0
local_offset = 0
found = False
sample_idx = function_name.index(caller_func)+1
while 1:
if sample_idx < 0:
break
while 1:
if ari[sample_idx][1][local_offset] == tokenized_text[k][0]:
found = True
break
else:
local_offset += 1
if local_offset >= len(ari[sample_idx][1]):
local_offset = 0
break
if found:
sample_string = caller_func + ":" + str(tokenized_text[k][0]) + " => " + str(
link_count) + ", " + str(local_offset)+"\n"
print(sample_string)
link_count = 0
local_offset = 0
break
link_count += 1
sample_idx -= 1
local_offset = 0
func_list = [0,0]
k += 1
func_list = [0,0]
j += 1
func_list = [0,0]
i += 1
#print(tokenized_text)
#print(activation_record_instance)