-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax_analyzer.py
More file actions
817 lines (750 loc) · 24.4 KB
/
Copy pathsyntax_analyzer.py
File metadata and controls
817 lines (750 loc) · 24.4 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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
class SyntaxAnalyzer:
def __init__(self, lex_symbol_table):
self.symbol_table = lex_symbol_table
self.attribute_dict = {}
self.name_symbol_dict = {}
self.token_list = []
self.parse_table = []
self.nonterminal_list = []
self.follows_list = []
# *, /, div, mod, and
self.mulops = [
'MULT',
'DIVIDE',
'DIV',
'MOD',
'AND',
]
# <, <>, <=, >, >=, =
self.relops = [
'LESS',
'NOTEQUAL',
'LEQUAL',
'GREATER',
'GEQUAL',
'EQUAL'
]
# +, -, or
self.addops = [
'PLUS',
'MINUS',
'OR'
]
# :=
self.assignops = [
':='
]
self.tok = None
self.index = 0
self.set_attribute_dict()
self.read_name_to_symbol()
self.generate_parsetable()
def set_attribute_dict(self):
# attribute values of keywords
keywords_file = open('Input/Keywords.txt', 'r')
for line in keywords_file:
line_split = line.split()
attribute_name = line_split[0]
attribute_value = int(line_split[1])
self.attribute_dict.update({attribute_name : attribute_value})
# attribute values of non-keyword(RELOP, ADDOP, etc.) tokens
token_info_file = open('Input/AttributeList.txt', 'r')
for line in token_info_file:
line_split = line.split()
attribute_name = line_split[0]
attribute_value = int(line_split[1])
self.attribute_dict.update({attribute_name : attribute_value})
# ASSIGNOP and other misc reserved characters
self.attribute_dict.update({'ASSIGNOP' : 60})
self.attribute_dict.update({'OPENPAREN' : 61})
self.attribute_dict.update({'CLOSEDPAREN' : 62})
self.attribute_dict.update({'SEMICOLON' : 63})
self.attribute_dict.update({'COMMA' : 64})
self.attribute_dict.update({'OPENBRACK' : 65})
self.attribute_dict.update({'CLOSEDBRACK' : 66})
self.attribute_dict.update({'DOT' : 67})
self.attribute_dict.update({'DOUBLEDOT' : 68})
self.attribute_dict.update({'COLON' : 69})
def generate_parsetable(self):
self.read_nonterminals()
self.read_firsts()
self.read_follows()
def read_nonterminals(self):
nonterminals = open('Input/Nonterminals.txt', 'r')
for line in nonterminals:
self.nonterminal_list.append(line[:-1])
self.parse_table.append({'nonterminal': line[:-1], 'first': [], 'follows': []})
def read_firsts(self):
firsts = open('Input/Firsts.txt', 'r')
count = 0
for line in firsts:
line = line.replace(' ', '')
first_list = line[:-1].split('|')
self.parse_table[count]['first'] = first_list
count += 1
def read_follows(self):
follows = open('Input/Follows.txt', 'r')
count = 0
for line in follows:
line = line.replace(' ', '')
f_list = line[:-1].split('|')
self.follows_list.append(f_list)
self.parse_table[count]['follows'] = f_list
count += 1
def create_expected_list(self, ind, epsilon):
expected_list = self.parse_table[ind]['first']
if epsilon:
expected_list.append(self.parse_table[ind]['follows'])
return expected_list
def get_token(self):
self.tok = self.token_list[self.index]
self.index += 1
def get_token_lexeme(self):
return self.tok.keys()[0]
def get_token_type(self):
return self.tok.values()[0][0]
def get_token_attribute(self):
return self.tok.values()[0][1]
def check_token(self, expected):
# checks multiple expected items
checked = False
if 'num' in expected:
if self.get_token_type() == 'INT':
return True
elif self.get_token_type() == 'REAL':
return True
for item in expected:
checked = (self.get_token_attribute() == self.attribute_dict[item])
if checked: # short-circuited
return checked
return checked # only time it is returned here, checked should be False
def read_token_table(self):
token_file = open('Output/token.txt', 'r')
token_file.readline()
for line in token_file:
line_split = line.split()
self.token_list.append({line_split[1] : [line_split[2], line_split[3]]})
def read_name_to_symbol(self):
txt_file = open('Input/NameToSymbol.txt', 'r')
for line in txt_file:
line_split = line.split()
self.name_symbol_dict[line_split[0]] = line_split[1]
def syntax_error(self, nonterminal, epsilon=False, symbols_expected=[]):
expected_string = ''
if nonterminal in ['match_err']:
# sent from match error where the symbols_expected already provided
expected_string = ', '.join(symbols_expected)
else:
# sent from nonterminal where the symbols_expected not provided
ind = self.nonterminal_list.index(nonterminal)
expected_list = self.create_expected_list(ind, epsilon)
expected_string = ', '.join(expected_list)
err_msg = 'SYNERR: Expected {0} got {1}'.format(
expected_string, self.get_token_lexeme())
# sync token
sync_set = self.parse_table[ind]['follows']
sync_set.append('$')
if self.tok not in sync_set: # TODO: need to ascertain that syncing is
# done in both match error as well as pass in grammar
while self.tok not in sync_set:
self.get_token()
self.index -= 1
def parse(self):
self.get_token()
self.program()
self.match('$')
def match(self, expected):
symbols_expected = []
for name in expected:
symbol = self.name_symbol_dict[name]
symbols_expected.append(symbol)
if (self.get_token_lexeme() in symbols_expected and
'$' in symbols_expected):
return print '!!! Syntax Analyzer Completed !!!'
elif '$' not in symbols_expected:
if 'num' in symbols_expected:
if self.get_token_type in ['INT', 'REAL']: # longreal?
self.get_token()
return print '---Getting next token---'
else:
return self.syntax_error(
'match_err',
False,
['INT', 'REAL']
)
# elif expected == 'ADDOP':
# if self.get_token_type in self.addops:
# self.get_token()
# else:
# self.syntax_error('misc', False, self.addops)
# elif expected == 'MULOP':
# if self.get_token_type in self.mulops:
# self.get_token()
# else:
# self.syntax_error('misc', False, self.mulops)
# elif expected == 'RELOP':
# if self.get_token_type in self.relops:
# self.get_token()
# else:
# self.syntax_error('misc', False, self.relops)
# elif expected == 'ASSIGNOP':
# if self.get_token_type in self.relops:
# self.get_token()
# else:
# self.syntax_error('misc', False, self.assignops)
elif self.get_token_lexeme() in symbols_expected:
# not expecting EOF is obvious if inside this clause
self.get_token()
return print '---Getting next token---'
self.syntax_error('match_err', False, symbols_expected)
# getting next token happens in syntax_error where syncing is done
def program(self):
# 1.1.1
if self.check_token(['program']):
self.match('program')
self.match('id')
self.match('(')
self.identifier_list()
self.match(')')
self.match(';')
self.program1()
else:
self.syntax_error('program')
def program1(self):
# 1.2.1
if self.check_token(['var']):
self.match('var')
self.declarations()
self.program2()
# 1.2.2
elif self.check_token(['procedure']):
self.match('procedure')
self.subprogram_declarations
self.compound_statement
self.match('.')
# 1.2.3
elif self.check_token(['begin']):
self.match('begin')
self.compound_statement
self.match('.')
else:
self.syntax_error('program1')
def program2(self):
# 1.3.1
if self.check_token(['procedure']):
self.match('procedure')
self.subprogram_declarations()
self.compound_statement
self.match('.')
# 1.3.2
elif self.check_token(['begin']):
self.match('begin')
self.compound_statement()
self.match('.')
else:
self.syntax_error('program2')
def identifier_list(self):
# 2.1.1
if self.check_token(['id']):
self.match('id')
self.identifier_list1()
else:
self.syntax_error('identifier_list')
def identifier_list1(self):
# 2.2.1
if self.check_token(['COMMA']):
self.match(',')
self.identifier_list1()
# 2.2.2
elif self.check_token(['CLOSEDPAREN']):
pass
# for epsilon you don't match because the match should happen
# when checking the next nonterminal
else:
self.syntax_error('identifier_list1', True)
# has epsilon so must check follows as well in expected
def declarations(self):
# 3.1.1
if self.check_token(['var']):
self.match('var')
self.match('id')
self.match(':')
self.type()
self.match(';')
self.declarations1()
else:
self.syntax_error('declarations')
def declarations1(self):
# 3.2.1
if self.check_token(['var']):
self.match('var')
self.match('id')
self.match(':')
self.type()
self.match(';')
self.declarations1()
# 3.2.2
elif self.check_token(['begin', 'procedure']):
pass
else:
self.syntax_error('declarations1', True)
def type(self):
# 4.1.1
if self.check_token(['integer', 'real']):
self.standard_type()
# also no matching here since it will be matched at
# the call of standard_type
# 4.1.2
elif self.check_token(['array']):
self.match('array')
self.match('[')
self.num() # ??? it can be int or real?
self.match(']')
self.match('of')
self.standard_type()
else:
self.syntax_error('type')
def standard_type(self):
# 5.1.1
if self.check_token(['integer']):
self.match('integer')
# 5.1.2
elif self.check_token(['real']):
self.match('real')
else:
self.syntax_error('standard_type')
def subprogram_declarations(self):
# 6.1.1
if self.check_token(['procedure']):
self.subprogram_declaration()
self.match(';')
self.subprogram_declarations1()
else:
self.syntax_error('subprogram_declarations')
def subprogram_declarations1(self):
# 6.2.1
if self.check_token(['procedure']):
self.subprogram_declaration()
self.match(';')
self.subprogram_declarations1()
# 6.2.2
elif self.check_token(['begin']):
pass
else:
self.syntax_error('subprogram_declarations1', True)
def subprogram_declaration(self):
# 7.1.1
if self.check_token(['procedure']):
self.subprogram_head()
self.subprogram_declaration1()
else:
self.syntax_error('subprogram_declaration')
def subprogram_declaration1(self):
# 7.2.1
if self.check_token(['var']):
self.declarations()
self.subprogram_declaration2()
# 7.2.2
elif self.check_token(['procedure']):
self.subprogram_declarations()
self.compound_statement()
# 7.2.3
elif self.check_token(['begin']):
self.compound_statement()
else:
self.syntax_error('subprogram_declaration1')
def subprogram_declaration2(self):
# 7.3.1
if self.check_token(['procedure']):
self.subprogram_declarations()
self.compound_statement()
# 7.3.2
elif self.check_token(['begin']):
self.compound_statement()
else:
self.syntax_error('subprogram_declaration2')
def subprogram_head(self):
# 8.1.1
if self.check_token(['procedure']):
self.match('procedure')
self.match('id')
self.subprogram_head1()
else:
self.syntax_error('subprogram_head')
def subprogram_head1(self):
# 8.2.1
if self.check_token(['(']):
self.arguments()
self.match(';')
# 8.2.2
elif self.check_token([';']):
self.match(';')
else:
self.syntax_error('subprogram_head1')
def arguments(self):
# 9.1.1
if self.check_token(['(']):
self.match('(')
self.parameter_list()
self.match(')')
else:
self.syntax_error('arguments')
def paramter_list(self):
# 10.1.1
if self.check_token(['id']):
self.match('id')
self.match(':')
self.type()
self.paramter_list1()
else:
self.syntax_error('parameter_list')
def parameter_list1(self):
# 10.2.1
if self.check_token([';']):
self.match(';')
self.match('id')
self.match(':')
self.type()
self.paramter_list1()
# 10.2.2
elif self.check_token([')']):
pass
else:
self.syntax_error('parameter_list1', True)
def compound_statement(self):
# 11.1.1
if self.check_token(['begin']):
self.match('begin')
self.compound_statement1()
else:
self.syntax_error('compound_statement')
def compound_statement1(self):
# 11.2.1
check_firsts = [
'id',
'call',
'begin',
'if',
'while',
]
if self.check_token(check_firsts):
self.optional_statements()
self.match('end')
# 11.2.2
elif self.check_token(['end']):
self.match('end')
else:
self.syntax_error('compound_statement1')
def optional_statements(self):
# 12.1.1
check_firsts = [
'id',
'call',
'begin',
'if',
'while',
]
if self.check_token(check_firsts):
self.statement_list()
else:
self.syntax_error('optional_statements')
def statement_list(self):
# 13.1.1
check_firsts = [
'id',
'call',
'begin',
'if',
'while',
]
if self.check_token(check_firsts):
self.statement()
self.statement_list1()
else:
self.syntax_error('statement_list')
def statement_list1(self):
# 13.2.1
if self.check_token(['SEMICOLON']):
self.match(';')
self.statement()
self.statement_list1()
# 13.2.2
elif self.check_token(['end']):
pass
else:
self.syntax_error('statement_list1', True)
def statement(self):
# 14.1.1
if self.check_token(['id']):
self.match('id')
self.variable()
self.match('ASSIGNOP')
# it can be the different stuff that is assignop like :=
self.expression()
# 14.1.2
elif self.check_token(['call']):
self.match('call')
self.procedure_statement()
# 14.1.3
elif self.check_token(['begin']):
self.match('begin')
self.compound_statement()
# 14.1.4
elif self.check_token(['if']):
self.match('if')
self.expression()
self.match('then')
self.statement()
self.statement1()
# 14.1.5
elif self.check_token(['while']):
self.match('while')
self.expression()
self.match('do')
self.statement()
else:
self.syntax_error('statement')
def statement1(self):
# 14.2.1
if self.check_token(['else']):
self.match('else')
self.statement()
# 14.2.2
elif self.check_token(['end', 'else', 'SEMICOLON']):
pass
else:
self.syntax_error('statement1', True)
def variable(self):
# 15.1.1
if self.check_token(['id']):
self.match('id')
self.variable1()
else:
self.syntax_error('variable')
def variable1(self):
# 15.2.1
if self.check_token(['OPENBRACK']):
self.match('[')
self.expression()
self.match(']')
# 15.2.2
elif self.check_token(['ASSIGNOP']):
pass
else:
self.syntax_error('variable1', True)
def procedure_statement(self):
# 16.1.1
if self.check_token(['call']):
self.match('call')
self.match('id')
self.procedure_statement1()
else:
self.syntax_error('procedure_statement')
def procedure_statement1(self):
# 16.2.1
if self.check_token(['OPENPAREN']):
self.match('(')
self.expression_list()
self.match(')')
# 16.2.2
elif self.check_token(['end', 'else', 'SEMICOLON']):
pass
else:
self.syntax_error('procedure_statement1', True)
def expression_list(self):
# 17.1.1
check_firsts = [
'id',
'num',
'OPENPAREN',
'not',
'PLUS',
'MINUS'
]
if self.check_token(check_firsts):
self.match(check_firsts)
self.expression()
self.expression_list1()
else:
self.syntax_error('expression_list')
def expression_list1(self):
# 17.2.1
if self.check_token(['COMMA']):
self.match(',')
self.expression()
self.expression_list1()
# 17.2.2
elif self.check_token(['CLOSEDPAREN']):
pass
else:
self.syntax_error('expression_list1', True)
def expression(self):
# 18.1.1
check_firsts = [
'id',
'num',
'OPENPAREN',
'not',
'PLUS',
'MINUS'
]
if self.check_token(check_firsts):
self.simple_expression()
self.expression1()
else:
self.syntax_error('expression')
def expression1(self):
check_follows = [
'CLOSEDPAREN',
'then',
'do',
'CLOSEDBRACK',
'end',
'else',
'SEMICOLON',
'COMMA'
]
# 18.2.1
if self.check_token(self.relops):
self.match('RELOP')
self.simple_expression
# 18.2.2
elif self.check_token(check_follows):
pass
else:
self.syntax_error('expression1', True)
def is_sign(self):
if self.check_token(['PLUS']):
self.match('+')
return True
elif self.check_token(['MINUS']):
self.match('-')
return True
return False
def simple_expression(self):
# 19.1.1
check_firsts = [
'id',
'num',
'OPENPAREN',
'not'
]
if self.check_token(check_firsts):
self.term()
self.simple_expression1()
# 19.1.2
elif self.is_sign():
self.term()
self.simple_expression1()
else:
self.syntax_error('simple_expression')
def simple_expression1(self):
check_follows = [
'OPENPAREN',
'then',
'do',
'OPENBRACK',
'end',
'else',
'SEMICOLON',
'COMMA'
]
check_follows.extend(self.relop)
# 19.2.1
if self.check_token(self.addops):
self.match('ADDOP')
self.term()
self.simple_expression1()
# 19.2.2
elif self.check_token(check_follows):
pass
else:
self.syntax_error('simple_expression1', True)
def term(self):
# 20.1.1
check_firsts = [
'id',
'num',
'OPENPAREN',
'not'
]
if self.check_token(check_firsts):
self.factor
self.term1
else:
self.syntax_error('term')
def term1(self):
check_follows = [
'CLOSEDPAREN',
'then',
'do',
'CLOSEDBRACK',
'end',
'else',
'SEMICOLON',
'COMMA'
]
check_follows.extend(self.relops)
check_follows.extend(self.addops)
# 20.2.1
if self.check_token(self.mulops):
self.match('MULOPS')
self.factor()
self.term1()
# 20.2.2
elif self.check_token(check_follows):
pass
else:
self.syntax_error('term1', True)
def factor(self):
# 21.1.1
if self.check_token(['id']):
self.match('id')
self.factor1()
# 21.1.2
elif self.check_token('num'): #TODO: check is num
self.match('num')
# 21.1.3
elif self.check_token(['OPENPAREN']):
self.match('(')
self.expression()
self.match(')')
# 21.1.4
elif self.check_token(['not']):
self.match('not')
self.factor()
else:
self.syntax_error('factor')
def factor1(self):
check_follows = [
'CLOSEDPAREN',
'then',
'do',
'CLOSEDBRACK',
'end',
'else',
'SEMICOLON',
'COMMA'
]
check_follows.extend(self.mulops)
check_follows.extend(self.addops)
check_follows.extend(self.relops)
# 21.2.1
if self.check_token(['OPENBRACK']):
self.match('[')
self.expression()
self.match(']')
# 21.2.2
elif self.check_token(check_follows):
pass
else:
self.syntax_error('factor1', True)
def sign(self):
# 22.1.1
if self.check_token('PLUS'):
self.match('+')
# 22.1.2
elif self.check_token('MINUS'):
self.match('-')
else:
self.syntax_error('sign')