-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.py
926 lines (713 loc) · 29.1 KB
/
parser.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
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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
# Copyright 2012 Madhusudan C.S.
#
# This file parser.py is part of PL241-MCS compiler.
#
# PL241-MCS compiler is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PL241-MCS compiler is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PL241-MCS compiler. If not, see <http://www.gnu.org/licenses/>.
"""Implements the parser for the PL241 compiler.
EBNF of the PL241 grammar is as follows:
letter = "a".."z";
digit = "0".."9";
relOp = "==" | "!=" | "<" | "<=" | ">" | ">=";
ident = letter {letter | digit};
number = digit {digit};
designator = ident{ "[" expression "]" };
factor = designator | number | "(" expression ")" | funcCall;
term = factor { ("*" | "/") factor};
expression = term {("+" | "-") term};
relation = expression relOp expression;
assignment = "let" designator "<-" expression;
funcCall = "call" ident [ "(" [expression { "," expression } ] ")" ];
ifStatement = "if" relation "then" statSequence [ "else" statSequence ] "fi";
whileStatement = "while" relation "do" statSequence "od";
returnStatement = "return" [ expression ];
statement = assignment | funcCall | ifStatement | whileStatement | returnStatement;
statSequence = statement { ";" statement };
typeDecl = "var" | "array" "[" number "]" { "[" number "]" };
varDecl = typeDecl ident { "," ident } ";";
funcDecl = ("function" | "procedure") ident [formalParam] ";" funcBody ";";
formalParam = "(" [ident { "," ident }] ")";
funcBody = { varDecl } "{" [ statSequence ] "}";
computation = "main" { varDecl } { funcDecl } "{" statSequence "}" ".";
"""
import logging
import re
import sys
from argparse import ArgumentParser
from datastructures import Node
# Module level logger object
LOGGER = logging.getLogger(__name__)
# Regular expression patterns used for parsing.
IDENT_PATTERN = r'[a-zA-Z][a-zA-Z0-9]*'
NUMBER_PATTERN = r'\d+'
IDENT_RE = re.compile(IDENT_PATTERN)
NUMBER_RE = re.compile(NUMBER_PATTERN)
TOKEN_RE = re.compile(
r'(%s|%s|\/\/|<-|==|!=|<=|>=|\+|\-|\*|\/|[\n]|[^\t\r +])' % (
NUMBER_PATTERN, IDENT_PATTERN))
GLOBAL_SCOPE_NAME = 'global'
class ParserBaseException(Exception):
def __init__(self, msg):
self._msg = msg
def __str__(self):
return '%s: %s' % (self.__class__.__name__, self._msg)
class LanguageSyntaxError(ParserBaseException):
def __str__(self):
return 'SyntaxError:%s' % self._msg
class EndControlException(ParserBaseException):
def __init__(self, begin, end, msg=None):
msg = msg if msg else ('%s encountered without a matching %s.' % (
begin, end))
super(EndControlException, self).__init__(msg)
class ElseFoundException(EndControlException):
def __init__(self, msg=None):
super(ElseFoundException, self).__init__('if', 'else', msg)
class FiFoundException(EndControlException):
def __init__(self, msg=None):
super(FiFoundException, self).__init__('if', 'fi', msg)
class OdFoundException(EndControlException):
def __init__(self, msg=None):
super(OdFoundException, self).__init__('while', 'do', msg)
class RightBracketFoundException(EndControlException):
def __init__(self, msg=None):
super(RightBracketFoundException, self).__init__('[', ']', msg)
class RightBraceFoundException(EndControlException):
def __init__(self, msg=None):
super(RightBraceFoundException, self).__init__('{', '}', msg)
class RightParenthesisFoundException(EndControlException):
def __init__(self, msg=None):
super(RightParenthesisFoundException, self).__init__('(', ')', msg)
class EndOfStatementFoundException(EndControlException):
def __init__(self, msg=None):
super(EndOfStatementFoundException, self).__init__('statement', ';', msg)
class TokenStream(object):
"""Handles the token stream of the source.
"""
def __init__(self, src):
"""Initializes the token stream from the source.
"""
self.src = src
# The stream pointers that keep track of where in the source code our
# parser currently is.
self.__stream_pointer = None
self.__tokens = None
self.__line_track = None
self.__tokenize()
def __tokenize(self):
"""Splits the entire source code stream into tokens using regular expression.
"""
initial_tokens = TOKEN_RE.findall(self.src)
line_num = 1
self.__tokens = []
self.__line_track = []
comment = False
for i, token in enumerate(initial_tokens):
if token == '\n':
comment = False
line_num += 1
continue
elif token in ['//', '#']:
comment = True
if comment:
continue
self.__tokens.append(token)
self.__line_track.append(line_num)
LOGGER.debug('Parsed tokens: %s' % self.__tokens)
# Initializes the stream to the beginning of the tokens list.
self.__stream_pointer = 0
def linenum(self):
"""Returns the current line number in the source program as seen by stream.
"""
return self.__line_track[self.__stream_pointer]
def fastforward(self, token):
"""Fast forward the stream upto the point we find the given token.
"""
try:
self.__stream_pointer = self.__tokens.index(token)
except ValueError:
raise LanguageSyntaxError('"%s" not found' % (token))
def look_ahead(self):
"""Return the next token in the stream.
If the end of stream has already been reached, the IndexError returned by
list is communicated, without handling. This should be handled outside
the class. This is a way to indicate that there is nothing more in the
stream to look ahead.
"""
return self.__tokens[self.__stream_pointer]
def __iter__(self):
"""Setup the iterator protocol.
"""
return self
def next(self):
"""Get the next item in the token stream.
"""
if self.__stream_pointer == None:
self.__tokenize()
try:
next_token = self.__tokens[self.__stream_pointer]
self.__stream_pointer += 1
except IndexError:
self.__stream_pointer = None
raise StopIteration
return next_token
def debug(self):
"""Logs all the debug information about the TokenStream.
"""
LOGGER.debug('Stream Pointer Value: %s' % (self.__stream_pointer))
LOGGER.debug('Stream Ahead: %s' % (self.__tokens[self.__stream_pointer:]))
class Parser(object):
"""Abstracts the entire grammar parser along with building the parse tree.
This parser is implemented as a home-brewn recursive descent parser with
some help from regular expression library only for tokenizing.
"""
KEYWORDS = [
'array', 'call', 'do', 'else', 'fi', 'function', 'if', 'let', 'main',
'od', 'procedure', 'return', 'then', 'var', 'while']
CONTROL_CHARACTERS_MAP = {
'.': 'period',
',': 'comma',
';': 'semicolon',
'[': 'leftbracket',
']': 'rightbracket',
'{': 'leftbrace',
'}': 'rightbrace',
'(': 'leftparen',
')': 'rightparen',
'<-': 'assignment_operator',
}
RELATIONAL_OPERATORS = ['==', '!=', '<', '<=', '>', '>=']
TERM_OPERATORS = ['*', '/']
EXPRESSION_OPERATORS = ['+', '-']
COMMENT_CHARACTERS = ['//', '#']
def __init__(self, program_file):
"""Initializes by reading the program file and constructing the parse tree.
Args:
program_file: the file object that contains the source code to compile.
"""
program_file_obj = open(program_file, 'r')
# Read the entire source in the supplied program file.
self.src = program_file_obj.read()
# Close the program file, we do not need that anymore since we have read
# entire source in the program file.
program_file_obj.close()
# The current scope of the program as the parser, parses through the source.
self.__current_scope = None
# The program symbol table, stored as dictionary. The keys of this
# dictionary represents the scope of the symbol whose value is another
# dictionary whose keys are the symbols and values are the latest value
# of the symbol.
# Initialize the symbol table with the functions provided by the language.
self.symbol_table = {
GLOBAL_SCOPE_NAME: {
'InputNum': {},
'OutputNum': {},
'OutputNewLine': {},
}
}
# This will contain two trees, the global tree with all the global
# declarations and the functions tree that contain all the function
# defintions including main.
self.trees = self.__parse()
def __update_scope(self, scope):
"""Update the current scope, add it to the symbol table if doesn't exist.
"""
self.__current_scope = scope
if self.__current_scope not in self.symbol_table:
self.symbol_table[self.__current_scope] = {}
def __parse(self):
"""Parses tokens by delegating to appropriate functions and builds tree.
"""
self.__token_stream = TokenStream(self.src)
return self.__parse_main()
def __parse_main(self):
# We fast forward to "main" in the source code because the grammar defines
# the program entry point as main.
self.__token_stream.fastforward('main')
global_token = self.__token_stream.next()
global_node = Node('global', 'global_var_declarations')
# Since main is getting defined here, update the scope to indicate it.
self.__update_scope(GLOBAL_SCOPE_NAME)
while True:
try:
self.__parse_abstract_var_decl(global_node)
except EndOfStatementFoundException:
continue
except LanguageSyntaxError, e:
if str(e).startswith('SyntaxError:%d: Expected "var" or "array" but "'
% (self.__token_stream.linenum())):
break
else:
raise
functions_node = Node('functions', 'function_definitions')
while True:
try:
self.__parse_abstract_func_decl(functions_node)
except LanguageSyntaxError, e:
if str(e).startswith(
'SyntaxError:%d: Expected "function" or "procedure" but "' % (
self.__token_stream.linenum())):
break
else:
raise
# Although we won't have any more variable declarations, to be on the
# safer side set the scope back to main since we are done with all the
# function declarations.
self.__update_scope(GLOBAL_SCOPE_NAME)
# The following three nodes are added to make main function definition
# consistent with all other function defintions
main_node = Node('keyword', 'function', functions_node)
Node('ident', 'main', main_node)
node = Node('abstract', 'formalParam', main_node)
main_body = Node('abstract', 'funcBody', main_node)
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token != '{':
raise LanguageSyntaxError('%d: Expected "{" but "%s" was found.' % (
self.__token_stream.linenum(), look_ahead_token))
self.__token_stream.next()
try:
self.__parse_abstract_stat_sequence(main_body)
if self.__token_stream.look_ahead() == '}':
self.__token_stream.next()
self.__parse_rightbrace(main_body)
except RightBraceFoundException:
pass
# The last token, which is essentially the end of the stream must be
# a period token, otherwise there is a syntax error in the program
# according to the grammar.
if self.__token_stream.next() != '.':
raise LanguageSyntaxError('%d: Program does not end with a "."' %
self.__token_stream.linenum())
return {
'globals': global_node,
'function_definitions': functions_node
}
def __parse_abstract_ident(self, parent, add=False,
ident_type='integer', dimensions=None):
"""Parses the ident type of elements in the grammar.
Args:
parent: The parent node to which this ident should be added.
add: True if new item should be created else False. If add is False
and the symbol doesn't exist in the symbol table an exception
is raised.
ident_type: The type of the identifier, can be 'array', 'integer' or
'function_name'
dimensions: The dimensions of the identifier if it is an array.
"""
look_ahead_token = self.__token_stream.look_ahead()
if IDENT_RE.match(look_ahead_token):
next_token = self.__token_stream.next()
Node('ident', next_token, parent)
# Symbol table should be updated at this point since we found a new name.
if add:
self.symbol_table[self.__current_scope][next_token] = {
'type': ident_type,
'dimensions': dimensions,
}
else:
if (next_token not in self.symbol_table[self.__current_scope] and
next_token not in self.symbol_table[GLOBAL_SCOPE_NAME]):
raise LanguageSyntaxError('%d: Symbol "%s" not declared.' % (
self.__token_stream.linenum(), next_token))
return next_token
self.__token_stream.debug()
raise LanguageSyntaxError('%d: Expected identifier but "%s" found.' % (
self.__token_stream.linenum(), look_ahead_token))
def __parse_abstract_number(self, parent, negate=False):
look_ahead_token = self.__token_stream.look_ahead()
if NUMBER_RE.match(look_ahead_token):
next_token = int(self.__token_stream.next())
if negate:
next_token = -next_token
Node('number', next_token, parent)
return next_token
raise LanguageSyntaxError('%d: Expected number but "%s" found.' % (
self.__token_stream.linenum(), look_ahead_token))
def __parse_abstract_designator(self, parent, negate=False):
node = Node('abstract', 'designator', parent)
if negate:
node = Node('operator', '-', node)
self.__parse_abstract_ident(node)
while self.__token_stream.look_ahead() == '[':
try:
self.__token_stream.next()
self.__parse_abstract_expression(node)
if self.__token_stream.look_ahead() == ']':
self.__token_stream.next()
self.__parse_rightbracket(node)
except RightBracketFoundException:
continue
def __parse_abstract_factor(self, parent):
node = Node('abstract', 'factor', parent)
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token == '(':
try:
self.__token_stream.next()
self.__parse_abstract_expression(node)
if self.__token_stream.look_ahead() == ')':
self.__token_stream.next()
self.__parse_rightparen(node)
except RightParenthesisFoundException:
pass
elif look_ahead_token == 'call':
self.__token_stream.next()
self.__parse_call(node)
else:
try:
negate = False
if look_ahead_token == '-':
negate = True
self.__token_stream.next()
look_ahead_token = self.__token_stream.look_ahead()
self.__parse_abstract_number(node, negate)
except LanguageSyntaxError:
try:
self.__parse_abstract_designator(node, negate)
except LanguageSyntaxError:
look_ahead_token = self.__token_stream.look_ahead()
if self.is_control_character(look_ahead_token):
next_token = self.__token_stream.next()
parser_method_name = '_Parser__parse_%s' % (
self.CONTROL_CHARACTERS_MAP[next_token])
parser_method = getattr(self, parser_method_name)
parser_method(node)
else:
# Re-raise the exception back if it is not a control character.
raise
def __parse_abstract_term(self, parent):
node = Node('abstract', 'term', parent)
self.__parse_abstract_factor(node)
while self.is_term_operator(self.__token_stream.look_ahead()):
self.__parse_generic_operator(node, self.__token_stream.next())
self.__parse_abstract_factor(node)
def __parse_abstract_expression(self, parent):
node = Node('abstract', 'expression', parent)
self.__parse_abstract_term(node)
while self.is_expression_operator(self.__token_stream.look_ahead()):
self.__parse_generic_operator(node, self.__token_stream.next())
self.__parse_abstract_term(node)
def __parse_abstract_relation(self, parent):
node = Node('abstract', 'relation', parent)
self.__parse_abstract_expression(node)
look_ahead_token = self.__token_stream.look_ahead()
if self.__token_stream.look_ahead() not in self.RELATIONAL_OPERATORS:
raise LanguageSyntaxError(
'%d: Relational operator expected but "%s" was found'
% (self.__token_stream.linenum(), look_ahead_token))
next_token = self.__token_stream.next()
self.__parse_generic_operator(node, next_token)
self.__parse_abstract_expression(node)
def __parse_let(self, parent):
node = Node('keyword', 'let', parent)
self.__parse_abstract_designator(node)
next_token = self.__token_stream.next()
if next_token == '<-':
self.__parse_assignment_operator(node)
else:
raise LanguageSyntaxError(
'%d: <- operator was expected but "%s" was found' % (
self.__token_stream.linenum(), next_token))
self.__parse_abstract_expression(node)
def __parse_call(self, parent):
node = Node('keyword', 'call', parent)
self.__parse_abstract_ident(node)
if self.__token_stream.look_ahead() != '(':
return
self.__token_stream.next()
try:
if self.__token_stream.look_ahead() == ')':
self.__token_stream.next()
self.__parse_rightparen(node)
self.__parse_abstract_expression(node)
while self.__token_stream.look_ahead() == ',':
self.__token_stream.next()
self.__parse_abstract_expression(node)
if self.__token_stream.look_ahead() == ')':
self.__token_stream.next()
self.__parse_rightparen(node)
except RightParenthesisFoundException:
return
def __parse_if(self, parent):
node = Node('keyword', 'if', parent)
self.__parse_abstract_relation(node)
next_token = self.__token_stream.next()
if next_token != 'then':
raise LanguageSyntaxError('%d: Expected "then" but "%s" was found.' % (
self.__token_stream.linenum(), next_token))
then_node = Node('keyword', 'then', node)
try:
self.__parse_abstract_stat_sequence(then_node)
if self.__token_stream.look_ahead() == 'else':
self.__token_stream.next()
self.__parse_else(node)
elif self.__token_stream.look_ahead() == 'fi':
self.__token_stream.next()
self.__parse_fi(node)
except ElseFoundException:
try:
else_node = Node('keyword', 'else', node)
self.__parse_abstract_stat_sequence(else_node)
if self.__token_stream.look_ahead() == 'fi':
self.__token_stream.next()
self.__parse_fi(node)
except FiFoundException:
return
except FiFoundException:
return
def __parse_else(self, parent):
raise ElseFoundException()
def __parse_fi(self, parent):
raise FiFoundException()
def __parse_while(self, parent):
node = Node('keyword', 'while', parent)
self.__parse_abstract_relation(node)
next_token = self.__token_stream.next()
if next_token != 'do':
raise LanguageSyntaxError('%d: Expected "do" but "%s" was found.' % (
self.__token_stream.linenum(), next_token))
do_node = Node('keyword', 'do', node)
try:
self.__parse_abstract_stat_sequence(do_node)
if self.__token_stream.look_ahead() == 'od':
self.__token_stream.next()
self.__parse_od(node)
except OdFoundException:
return
def __parse_od(self, parent):
raise OdFoundException()
def __parse_return(self, parent):
node = Node('keyword', 'return', parent)
look_ahead_token = self.__token_stream.look_ahead()
if self.is_keyword(look_ahead_token) and look_ahead_token != 'call':
return
self.__parse_abstract_expression(node)
def __parse_var(self, parent):
node = Node('keyword', 'var', parent)
def __parse_array(self, parent):
node = Node('keyword', 'array', parent)
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token != '[':
raise LanguageSyntaxError('%d: "[" missing from array declaration.' % (
self.__token_stream.linenum()))
dimensions = []
while self.__token_stream.look_ahead() == '[':
next_token = self.__token_stream.next()
number = self.__parse_abstract_number(node)
dimensions.append(int(number))
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token == ']':
next_token = self.__token_stream.next()
continue
else:
raise LanguageSyntaxError('%d: Expected "]" but "%s" was found."' % (
self.__token_stream.linenum(), look_ahead_token))
return dimensions
def __parse_function(self, parent):
node = Node('keyword', 'function', parent)
self.__parse_abstract_function_procedure(node)
def __parse_procedure(self, parent):
node = Node('keyword', 'procedure', parent)
self.__parse_abstract_function_procedure(node)
def __parse_abstract_function_procedure(self, parent):
self.__update_scope(GLOBAL_SCOPE_NAME)
func_name = self.__parse_abstract_ident(parent, add=True,
ident_type='function_name')
self.symbol_table[GLOBAL_SCOPE_NAME][func_name]['type'] = 'function_name'
# This function's name is still in the previous scope so that it can be
# called from the function outside this own function. Once we are done
# with it update the scope for this function
self.__update_scope(func_name)
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token == '(':
next_token = self.__token_stream.next()
try:
self.__parse_abstract_formal_param(parent)
except RightParenthesisFoundException:
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token == ')':
self.__token_stream.next()
look_ahead_token = self.__token_stream.look_ahead()
else:
node = Node('abstract', 'formalParam', parent)
if look_ahead_token != ';':
raise LanguageSyntaxError('%d: Expected ";" but "%s" was found.' % (
self.__token_stream.linenum(), look_ahead_token))
next_token = self.__token_stream.next()
self.__parse_abstract_func_body(parent)
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token != ';':
raise LanguageSyntaxError('%d: Expected ";" but "%s" was found.' % (
self.__token_stream.linenum(), look_ahead_token))
next_token = self.__token_stream.next()
def __parse_abstract_statement(self, parent):
next_token = self.__token_stream.next()
if not self.is_keyword(next_token):
raise LanguageSyntaxError('%d: Expected a keyword but "%s" was found.' %
(self.__token_stream.linenum(), next_token))
try:
parse_method = getattr(self, '_Parser__parse_%s' % (next_token))
node = Node('abstract', 'statement', parent)
parse_method(node)
except AttributeError:
raise ParserBaseException('Handler for %s is not implemented.' %
(next_token))
def __parse_abstract_stat_sequence(self, parent):
node = Node('abstract', 'statSeq', parent)
self.__parse_abstract_statement(node)
while self.__token_stream.look_ahead() == ';':
self.__token_stream.next()
self.__parse_abstract_statement(node)
def __parse_abstract_type_decl(self, parent):
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token == 'var':
next_token = self.__token_stream.next()
self.__parse_var(parent)
return
elif look_ahead_token == 'array':
next_token = self.__token_stream.next()
dimensions = self.__parse_array(parent)
return dimensions
raise LanguageSyntaxError('%d: Expected "var" or "array" but "%s" was '
'found.' % (self.__token_stream.linenum(), look_ahead_token))
def __parse_abstract_var_decl(self, parent):
node = Node('abstract', 'varDecl', parent)
dimensions = self.__parse_abstract_type_decl(node)
if dimensions:
ident_type = 'array'
else:
ident_type = 'integer'
ident = self.__parse_abstract_ident(node, add=True, ident_type=ident_type,
dimensions=dimensions)
while self.__token_stream.look_ahead() == ',':
self.__token_stream.next()
self.__parse_abstract_ident(node, add=True, ident_type=ident_type,
dimensions=dimensions)
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token != ';':
raise LanguageSyntaxError('%d: Expected ";" but "%s" was found' % (
self.__token_stream.linenum(), look_ahead_token))
next_token = self.__token_stream.next()
raise EndOfStatementFoundException()
def __parse_abstract_func_decl(self, parent):
look_ahead_token = self.__token_stream.look_ahead()
if not (look_ahead_token == 'function' or look_ahead_token == 'procedure'):
raise LanguageSyntaxError(
'%d: Expected "function" or "procedure" but "%s" was found.' % (
self.__token_stream.linenum(), look_ahead_token))
next_token = self.__token_stream.next()
parser_method = getattr(self, '_Parser__parse_%s' % (next_token))
parser_method(parent)
def __parse_abstract_formal_param(self, parent):
node = Node('abstract', 'formalParam', parent)
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token == ')':
self.__parse_rightparen(parent)
self.__parse_abstract_ident(node, add=True, ident_type='integer')
while self.__token_stream.look_ahead() == ',':
self.__token_stream.next()
self.__parse_abstract_ident(node, add=True)
if self.__token_stream.look_ahead() == ')':
self.__token_stream.next()
self.__parse_rightparen(node)
def __parse_abstract_func_body(self, parent):
node = Node('abstract', 'funcBody', parent)
while True:
try:
self.__parse_abstract_var_decl(node)
except EndOfStatementFoundException:
continue
except LanguageSyntaxError, e:
if str(e).startswith('SyntaxError:%d: Expected "var" or "array" but "'
% (self.__token_stream.linenum())):
break
else:
raise
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token != '{':
raise LanguageSyntaxError('%d: Expected "{" but "%s" was found' % (
self.__token_stream.linenum(), look_ahead_token))
self.__token_stream.next()
try:
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token == '}':
self.__token_stream.next()
self.__parse_rightbrace(node)
self.__parse_abstract_stat_sequence(node)
look_ahead_token = self.__token_stream.look_ahead()
if look_ahead_token == '}':
self.__token_stream.next()
self.__parse_rightbrace(node)
except RightBraceFoundException:
return
def __parse_leftbracket(self, parent):
pass
def __parse_rightbracket(self, parent):
raise RightBracketFoundException()
def __parse_leftbrace(self, parent):
pass
def __parse_rightbrace(self, parent):
raise RightBraceFoundException()
def __parse_leftparen(self, parent):
pass
def __parse_rightparen(self, parent):
raise RightParenthesisFoundException()
def __parse_semicolon(self, parent):
pass
def __parse_comma(self, parent):
pass
def __parse_assignment_operator(self, parent):
node = Node('operator', '<-', parent)
def __parse_period_operator(self, parent):
pass
def __parse_generic_operator(self, parent, token):
node = Node('operator', token, parent)
def is_keyword(self, token):
return token in self.KEYWORDS
def is_control_character(self, token):
return token in self.CONTROL_CHARACTERS_MAP
def is_relational_operator(self, token):
return token in self.RELATIONAL_OPERATORS
def is_term_operator(self, token):
return token in self.TERM_OPERATORS
def is_expression_operator(self, token):
return token in self.EXPRESSION_OPERATORS
def bootstrap():
parser = ArgumentParser(description='Compiler arguments.')
parser.add_argument('file_names', metavar='File Names', type=str, nargs='+',
help='name of the input files.')
parser.add_argument('-d', '--debug', action='store_true',
help='Enable debug logging to the console.')
parser.add_argument('-g', '--vcg', metavar='VCG', type=str,
nargs='?', const=True,
help='Generate the Visualization Compiler Graph output.')
args = parser.parse_args()
if args.debug:
LOGGER.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
LOGGER.addHandler(ch)
try:
p = Parser(args.file_names[0])
if args.vcg:
for name, tree in p.trees.items():
parser_vcg_file = open('%s.parser.%s.vcg' % (
filename, name), 'w')
parser_vcg_file.write(tree.generate_vcg(
'TREE-%s' % name) + '\n\n')
parser_vcg_file.close()
parser_vcg_file.close()
return p
except LanguageSyntaxError, e:
print e
sys.exit(1)
if __name__ == '__main__':
p = bootstrap()