-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntax.py
697 lines (557 loc) · 19.1 KB
/
Syntax.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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# -----------------------------------------------------|GLOBALS|--------------------------------------------------------
source = ""
start_index = 0
line_index = 1 # Will be used for errors
char_index_of_line = 1 # Will be used for errors
# ------------------------ DEFINE STATES PART ------------------------
state0 = 0 # This is the starting state
state1 = 1 # Lex is reading IDs
state2 = 2 # Lex is reading constants
state3 = 3 # Lex read * symbol
state4 = 4 # Lex read / symbol
# These states have to do with comments
state5 = 5 # Long Comment state
state6 = 6 # Lex read * while in long comment state
state7 = 7 # Lex read / while in long comment state
state8 = 8 # Short Comment State
state9 = 9 # Lex read / while in short comment state
state10 = 10 # Lex has read * symbol while in short comment
# These states have to do with double char ops
state11 = 11 # Lex has read < symbol
state12 = 12 # Lex has read > symbol
state13 = 13 # Lex has read : symbol
specials1 = {state3, state4} # The states where depending on the next char return or not
specials2 = {state10, state11} # The states where depending on the next char they add it to the word
# ------------------------ DEFINE GENERAL TOKENS PART ------------------------
EOFTK = 14
IDTK = 15
CONSTANTTK = 16
ADDTK = 17
MULTIPLYTK = 18
BRACKETTK = 19
SEPARATORTK = 20
COMPARATORTK = 21
DECLARATORTK = 22
stateR = {EOFTK, IDTK, CONSTANTTK, ADDTK,
MULTIPLYTK, BRACKETTK, SEPARATORTK, COMPARATORTK, DECLARATORTK}
singles = {ADDTK, BRACKETTK, SEPARATORTK}
longs = {state1, state2}
# ------------------------ DEFINE SPECIAL TOKENS PART ------------------------
# All of them were originally IDTKs, we now come to specify them
PROGRAMTK = 23
DECLARETK = 24
IFTK = 25
ELSETK = 26
WHILETK = 27
DOUBLEWHILETK = 28
LOOPTK = 29
EXITTK = 30
FORCASETK = 31
INCASETK = 32
WHENTK = 33
DEFAULTTK = 34
NOTTK = 35
ANDTK = 36
ORTK = 37
FUNCTIONTK = 38
PROCEDURETK = 39
CALLTK = 40
RETURNTK = 41
INTK = 42
INOUTTK = 43
INPUTTK = 44
PRINTTK = 45
taken_words = ["program", "declare", "if", "else", "while", "doublewhile", "loop", "exit", "forcase", "incase",
"when", "default", "not", "and", "or", "function", "procedure", "call", "return", "in",
"inout", "input", "print"]
double_ops = ["<>", ">=", "<=", ":="]
# ----------------------- LEX ERRORS -----------------------
Error1 = 46 # Char after Digit
Error2 = 47 # Closed long comment without opening one
Error3 = 48 # Opened second comment inside another one
Error4 = 49 # EOF while in long comment
Error5 = 50 # Invalid symbol
stateE = {Error1, Error2, Error3, Error4, Error5}
# ----------------------- LEX PART -----------------------
states = [
# state0
[state1, state2, ADDTK, state3, state4, state11, state12,
COMPARATORTK, state13, SEPARATORTK, SEPARATORTK, BRACKETTK, BRACKETTK, BRACKETTK,
BRACKETTK, BRACKETTK, BRACKETTK, state0, state0, EOFTK, Error5],
# state1
[state1, state1, IDTK, IDTK, IDTK, IDTK, IDTK,
IDTK, IDTK, IDTK, IDTK, IDTK, IDTK, IDTK,
IDTK, IDTK, IDTK, IDTK, IDTK, IDTK, IDTK],
# state2
[Error1, state2, CONSTANTTK, CONSTANTTK, CONSTANTTK, CONSTANTTK, CONSTANTTK,
CONSTANTTK, CONSTANTTK, CONSTANTTK, CONSTANTTK, CONSTANTTK, CONSTANTTK, CONSTANTTK,
CONSTANTTK, CONSTANTTK, CONSTANTTK, CONSTANTTK, CONSTANTTK, CONSTANTTK, CONSTANTTK],
# state3
[MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, Error2, MULTIPLYTK, MULTIPLYTK,
MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK,
MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK],
# state4
[MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, state5, state8, MULTIPLYTK, MULTIPLYTK,
MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK,
MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK, MULTIPLYTK],
# state5
[state5, state5, state5, state6, state7, state5, state5,
state5, state5, state5, state5, state5, state5, state5,
state5, state5, state5, state5, state5, Error4, state5],
# state6
[state5, state5, state5, state5, state0, state5, state5,
state5, state5, state5, state5, state5, state5, state5,
state5, state5, state5, state5, state5, Error4, state5],
# state7
[state5, state5, state5, Error3, Error3, state5, state5,
state5, state5, state5, state5, state5, state5, state5,
state5, state5, state5, state5, state5, Error4, state5],
# state8
[state8, state8, state8, state10, state9, state8, state8,
state8, state8, state8, state8, state8, state8, state8,
state8, state8, state8, state0, state8, EOFTK, state8],
# state9
[state8, state8, state8, Error3, Error3, state8, state8,
state8, state8, state8, state8, state8, state8, state8,
state8, state8, state8, state0, state8, EOFTK, state8],
# state10
[state8, state8, state8, state8, Error2, state8, state8,
state8, state8, state8, state8, state8, state8, state8,
state8, state8, state8, state0, state8, EOFTK, state8],
# state11
[COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK,
COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK,
COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK],
# state12
[COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK,
COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK,
COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK, COMPARATORTK],
# state13
[SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK,
DECLARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK,
SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK, SEPARATORTK],
]
def error(error_type):
print("There was a Lex Error in Line:", line_index, "Character:", char_index_of_line)
print("More specifically:")
if error_type == Error1:
print("\tError", error_type, ": Digits can not be followed by characters")
elif error_type == Error2:
print("\tError", error_type, ": Closed long comment without opening one")
elif error_type == Error3:
print("\tError", error_type, ": Opened second comment inside another one")
elif error_type == Error4:
print("\tError", error_type, ": EOF while in long comment")
elif error_type == Error5:
print("\tError", error_type, ": Invalid symbol")
else:
print("\tUndefined error")
exit()
def eof(current_char):
total = start_index + current_char
if total > len(source)-1:
return True
return False
def find_symbol(symbol):
global line_index, char_index_of_line
if symbol in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
return 0
elif symbol in "0123456789":
return 1
elif symbol in "+-*/":
if symbol in "+-":
return 2
elif symbol == "*":
return 3
else:
return 4
elif symbol in "<>=":
if symbol == "<":
return 5
elif symbol == ">":
return 6
else:
return 7
elif symbol in ":;,":
if symbol == ":":
return 8
elif symbol == ";":
return 9
else:
return 10
elif symbol in "()[]{}":
if symbol == "(":
return 11
elif symbol == ")":
return 12
elif symbol == "[":
return 13
elif symbol == "]":
return 14
elif symbol == "{":
return 15
else:
return 16
elif symbol in "\n \t":
if symbol == "\n":
line_index += 1
char_index_of_line = 0
return 17
else:
return 18
else:
return 20
def update_state(state, char):
column = find_symbol(char)
state = states[state][column]
return state
def identify(token, word):
for i in range(0, len(taken_words)):
if word == taken_words[i]:
token = i + 23
return token
def handle_return(token, word, temp, symbol, index):
global start_index
if token == IDTK:
token = identify(token, word)
start_index += index - 1
return token, word
elif token == CONSTANTTK:
start_index += index - 1
return token, word
elif token in singles:
if temp == ":":
start_index += index - 1
return token, temp
else:
start_index += index
return token, symbol
elif token == MULTIPLYTK:
start_index += index - 1
return token, temp
elif token == COMPARATORTK:
opp = temp + symbol
if opp in double_ops:
start_index += index
return token, opp
elif symbol == "=":
start_index += index
return token, symbol
else:
start_index += index - 1
return token, temp
elif token == DECLARATORTK:
start_index += index
return token, ":="
elif token == EOFTK:
start_index += index
return token, ""
def lex():
current_char = 0
state = state0
word = "" # Word must be a buffer[30]
temp = ""
symbol = ""
while state not in stateR and state not in stateE:
if eof(current_char):
state = states[state][19] # All of them are in stateR so it exits immediately
current_char +=1
else:
symbol = source[start_index + current_char]
state = update_state(state, symbol)
if state in longs: # State1 or State2 create IDs or Constants
word += symbol
elif state == state3 or state == state4:
temp = symbol
elif state == state5 or state == state8:
temp = ""
elif state == state11 or state == state12 or state == state13:
temp = symbol
# Any other state does not affect the word
current_char += 1
# start_index += current_char
if state in stateE:
error(state)
else:
token = state
return handle_return(token, word, temp, symbol, current_char)
def load_source(filename):
global source
source = open(filename).read()
return
def main():
load_source("source.min")
for i in range(0, 4):
print(lex())
main()
#------------------------------------------------------------------- SYNTAX -------------------------------------------------------------------------------------------#
def error(s):
print(s);
def program():
if token == PROGRAMTΚ:
token = lex()[0];
if token == IDTK:
token = lex()[0];
block();
else: error("The keyword 'id' was expected");
else: error("The keyword 'program' was expected");
def block():
declarations();
subprograms();
statements();
def declarations():
while token == DECLARETK:
token = lex()[0];
varlist();
if token == SEPARATORTK:
token = lex()[0];
def varlist():
if token == IDTK:
token = lex()[0];
while token == SEPARATORTK:
token = lex()[0];
def subprograms():
while (token == FUNCTIONTK or token == PROCEDURETK):
subprograms();
def subprogram():
if token == FUNCTIONTK:
token = lex()[0];
if token == IDTK:
token = lex()[0];
funcbody();
else: error ("The keyword 'id' was expected");
elif token == PROCEDURETK:
token = lex()[0];
if (token == IDTK):
token = lex()[0];
funcbody();
else: error ("The keyword 'id' was expected");
def funcbody():
formalpars();
if token == BRACKETTK:
token = lex()[0];
block();
if token == BRACKETTK:
token = lex()[0];
else: error ("Right curly brackett expected");
def formalpars():
if token == BRACKETTK:
token = lex()[0];
formalparlist();
if token == BRACKETTK:
token = lex()[0];
else: error ("right bracket expected");
def formalparitem():
if token == INTTK:
token = lex()[0];
if token == IDTK:
token = lex()[0];
else: error ("The keyword 'id' expected");
elif token == INOUTTK:
token = lex()[0];
if (token == idtk):
token = lex()[0];
else: error ("The keyword 'id' expected");
def statements():
statement();
if token == BRACKETTK:
token = lex()[0];
statement();
while (token == SEPARATORTK):
statement();
if token == BRACKETTK:
token = lex()[0];
else: error ("Right curly bracket expected");
def statement():
assignmentstat();
ifstat();
whilestat();
doublewhilestat();
loopstat();
exitstat();
forcasestat();
incase();
callstat();
returnstat();
inputstat();
priststat();
def assignmentstat():
if token == IDKT:
token = lex()[0];
if token == SEPARATORTK:
token = lex()[0];
if token == COMPARATORTK:
token = lex()[0];
expression();
else: error("The keyword 'id' was expected");
def ifstat():
if token == IFTK:
token = lex()[0];
if token == BRACKETTK:
token = lex()[0];
condition();
if token == BRACKETTK:
token = lex()[0];
else: error ("Right brackett expected");
if token == THENTK:
token = lex()[0];
statements();
elsepart();
else: error ("the keyword ‘then’ was expected");
def elsepart():
if token == THENTK:
token = lex()[0];
statements();
def whilestat():
if token == WHILETK:
token = lex()[0];
if token == BRACKETTK:
token = lex()[0];
condition();
if token == BRACKETTK:
token = lex()[0];
else: error ("Right brackett expected");
statements();
else: error ("the keyword 'while' was expected");
def doublewhilestat():
if token == DOUBLEWHILETK:
token = lex()[0];
if token == BRACKETTK:
token = lex()[0];
condition();
if token == BRACKETTK:
token = lex()[0];
else: error ("Right brackett expected");
statements();
if token == ELSETK:
token = lex()[0];
statements();
else: error ("The keyword 'doublewhile' was expected");
def loopstat():
if token == LOOPTK:
token = lex()[0];
statements();
else: error ("The keyword 'loop' was expected");
def exitstat():
if token == EXITTK:
token = lex()[0];
else: error("The keyword 'exit' was expected");
def forcasestat():
if token == FORCASETK:
while(token == WHENTK):
condition();
if token == SEPARATORTK:
statements();
else: error("The keyword 'forcase' was expected");
def incasestat():
if token == INCASETK:
while (token == WHENTK):
condition();
if token == SEPARATORTK:
statements();
else: error("The keyword 'incase' was expected");
def returnstat():
if token == RETURNTK:
token = lex()[0];
expression();
else: error ("The keyword 'return' was expected");
def callstat():
if token == CALLTK:
token = lex()[0];
if token == IDTK:
token = lex()[0];
actualpars();
else: error ("The keyword 'id' was expected");
else: error ("The keyword 'call' was expected");
def printstat():
if token == PRINTTK:
token = lex()[0];
if token == BRACKETTK:
token = lex()[0];
expression();
if token == BRACKETTK:
token = lex()[0];
else: error ("right bracket expected");
else: error ("The keyword 'print' was expected");
def inputstat():
if token == INPUTTK:
token = lex()[0];
if token == BRACKETTK:
token == lex();
if token == IDTK:
token = lex()[0];
else: error ("The keyword 'id' was expected");
if token == BRACKETTK:
token = lex()[0];
else: error ("Right bracket expected");
else: error ("The keyword 'input' was expected");
def actualpars():
if token == BRACKETTK:
token = lex()[0];
actualparlist();
if token == BRACKETTK:
token = lex()[0];
else: error ("Right bracket expected");
def actualparitem():
if token == INTTK:
token = lex()[0];
expression();
elif token == INOUTTK:
token = lex()[0];
if token == IDTK:
token = lex()[0];
else: error ("The keyword 'id' was expected");
def condition():
boolterm();
while (token == ORTK):
boolterm();
def boolterm():
boolfactor();
while (token == ANDTK):
boolfactor();
def boolfactor():
if token == NOTTK:
token = lex()[0];
if token == BRACKETTK:
token = lex()[0];
condition();
if token == BRACKETTK:
token = lex()[0];
else: error("right bracket expected");
elif token == BRACKETTK:
token = lex()[0];
condition ();
if token == BRACKETTK:
token = lex()[0];
else: error("right bracket expected");
else:
expression();
relOper();
expression();
def expression():
optionalsign();
term();
while (token == ADDTK):
addoper();
term();
def term():
factor();
while(token == MULTIPLYTK):
mulOper();
def factor():
if token == CONSTANTTK:
token = lex()[0];
elif token == BRACKETTK:
token = lex()[0];
condition();
if token == BRACKETTK:
token = lex()[0];
else: error("Right bracket expected");
else:
if token == IDTK:
token = lex()[0];
idtail();
def idtail():
actualpars();
def optionalsign():
addoper();