-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcparser.py
More file actions
1294 lines (1223 loc) · 43.5 KB
/
cparser.py
File metadata and controls
1294 lines (1223 loc) · 43.5 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
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import string
# C Keywords
# ------------------------------------------------------------------------
types = ["short", "int", "long", "float", "double", "char", "void", "bool", "FILE"]
containers = ["enum", "struct", "union", "typedef"]
modifiers = [ "const", "volatile", "extern", "static", "register", "signed", "unsigned"]
flow = [ "if", "else",
"goto",
"case", "default",
"continue", "break", ]
loops = ["for", "do", "while" "switch", ]
keywords = types + containers + modifiers + flow + loops + [ "return", "sizeof" ]
prefix_operations = ["-","+","*","&","~","!","++","--"]
postfix_operations = ["++", "--"]
selection_operations = [".","->"] # Left-to-Right
multiplication_operations = ["*","/","%"] # Left-to-Right
addition_operations = ["+","-"] # Left-to-Right
bitshift_operations = ["<<",">>"] # Left-to-Right
relation_operations = ["<","<=",">",">="] # Left-to-Right
equality_operations = ["==","!="] # Left-to-Right
bitwise_operations = ["&", "^", "|"] # Left-to-Right
logical_operations = ["&&","||"]
ternary_operations = ["?",":"]
# Ternary () ? () : ()
assignment_operations = ["=", # Right-to-Left
"+=","-=",
"/=","*=","%="
"<<=",">>=",
"&=","^=","|=",
]
binary_operations = multiplication_operations + \
addition_operations + \
bitshift_operations + \
relation_operations + \
equality_operations + \
bitwise_operations + \
logical_operations + \
assignment_operations + selection_operations
operators = prefix_operations + binary_operations + ternary_operations
precedence = [
selection_operations,
multiplication_operations,
addition_operations,
bitshift_operations,
relation_operations,
equality_operations,
["&"],["^"],["|"],
logical_operations,
ternary_operations,
assignment_operations,
]
# Utitlity Functions
# ------------------------------------------------------------------------
def is_keyword(token):
return token in keywords
def isonly(s,chars):
return len(s) and all(map(lambda c: c in chars, s))
def intersection(list1,list2):
try:
return len(set(list1) & set(list2)) > 0
except TypeError:
print "Can't find the intersection of these:"
print list1
print list2
assert(0)
def first_instance(haystack, needles ):
for i,hay in enumerate(haystack):
if hay in needles:
return i
raise ValueError("%s does not contain one of %s"%(str(haystack),str(needles)))
def len_type(tokens):
index = 0
while tokens[index] in modifiers:
index += 1 # The modifier
index += 1 # the type
if tokens[index] == "*":
index += 1 # the pointer
return index
# Token Lexer
# ------------------------------------------------------------------------
class Token(str):
def set(self,line=0,pos=0):
self.line = line
self.pos = pos
def position(self):
return (self.line,self.pos)
def trim(self):
ret = Token( str(self)[:-1] )
ret.set(self.line, self.pos )
return ret
def __add__(self,other):
ret = Token( str(self) + str(other))
ret.set(self.line, self.pos )
return ret
def escape_character( c, line, pos ):
if c == "n":
curtoken = Token("\n")
curtoken.set(line,pos)
elif c == "f":
curtoken = Token("\f") # Form Feed, whatever that is
curtoken.set(line,pos)
elif c == "t":
curtoken = Token("\t")
curtoken.set(line,pos)
elif c == "'":
curtoken = Token("\'")
curtoken.set(line,pos)
elif c == '"':
curtoken = Token("\"")
curtoken.set(line,pos)
elif c == '\\':
curtoken = Token("\\")
curtoken.set(line,pos)
elif c == "0":
curtoken = Token("\0")
curtoken.set(line,pos)
else:
print "Lex Error at Line %d / Char %d - Character '%c' cannot be escaped" % (line, pos, c)
#assert(0)
curtoken = Token(c)
curtoken.set(line,pos)
return curtoken
def tokenize( s ):
symbols = string.punctuation.replace("_","")
digital = string.digits
floating = string.digits + "."
hexal = string.hexdigits
line = 1
pos = 0
curtoken = Token("")
curtoken.set(line,pos)
in_string = False
in_char = False
in_comment = False
in_pragma = False
in_define= False
definitions = {}
def evaluate_pragma( pragma ):
print pragma
#define
if pragma.startswith("#define "):
pragma = pragma.lstrip("#define ")
identifier = pragma[0:pragma.index(" ")]
expansion = pragma[pragma.index(" ")+1:]
definitions[identifier] = expansion
elif pragma.startswith("#undef "):
identifier = pragma.lstrip("#undef ")
if definitions.has_key(identifier):
del definitions[ identifier ]
else:
# Unknown pragma
pass
def _token():
if definitions.has_key(curtoken):
redefined = Token(definitions[curtoken])
redefined.set( *curtoken.position() )
return redefined
else:
return curtoken
for c in s:
pos += 1
#print (c,curtoken)
if in_comment or in_pragma:
if c=="\n":
if in_pragma:
evaluate_pragma( curtoken )
if curtoken.startswith("//") or curtoken.startswith("#") :
curtoken = Token("")
curtoken.set(line,pos)
in_comment = False
in_pragma = False
line += 1
pos = 0
elif c=='/' and curtoken.endswith("*"):
curtoken = Token("")
curtoken.set(line,pos)
in_comment = False
else:
curtoken += c
elif c == '"' and not in_char:
if not in_string:
# Start of new String
if curtoken != "":
yield _token()
in_string = True
curtoken = Token('"')
curtoken.set(line,pos)
elif len(curtoken) and curtoken[-1] == '\\':
curtoken = Token(curtoken[:-1] + "\"")
curtoken.set(line,pos)
else:
# End of String
in_string = False
curtoken += c
yield _token()
curtoken = Token("")
curtoken.set(line,pos)
elif in_string:
if curtoken.endswith('\\'):
# Char Symbols
#curtoken = curtoken.trim()
#curtoken += escape_character( c, line, pos )
curtoken += Token(c)
else:
curtoken += Token(c)
elif in_char:
if curtoken.endswith("\\"):
# Escape this Character
#curtoken = curtoken.trim()
#curtoken += escape_character(c, line, pos)
curtoken += c
elif c == "'":
# End of Character:
curtoken += c
if len(curtoken) != 3:
print "Lex Error at Line %d / Char %d - Character '%s' is too long." % (curtoken.line, curtoken.pos, c)
yield _token()
in_char = False
curtoken = Token("")
curtoken.set(line,pos)
else:
curtoken += c
elif c == "'" and not in_string:
# Start of Character:
if curtoken != "":
yield _token()
curtoken = Token("'")
curtoken.set(line,pos)
in_char = True
elif c == "#":
if curtoken != "":
yield _token()
curtoken = Token("#")
curtoken.set(line,pos)
in_pragma = True
elif curtoken=="/" and c=="*":
curtoken += Token(c)
in_comment = True
elif c == "/" and curtoken == "/":
curtoken += Token(c)
in_comment = True
elif c in symbols:
if (curtoken+c) in operators:
curtoken = Token((curtoken+c))
curtoken.set(line,pos)
elif c=='.' and isonly(curtoken, floating):
curtoken += Token(c)
else:
if curtoken != "":
yield _token()
curtoken = Token(c)
curtoken.set(line,pos)
else:
# Non-Symbols
if isonly(curtoken, symbols):
yield _token()
curtoken = Token("")
curtoken.set(line,pos)
if c in string.whitespace:
if curtoken != "":
yield _token()
if c == "\n":
#yield c
line += 1
pos = 0
curtoken = Token("")
curtoken.set(line,pos)
# Int
elif c in digital and isonly(curtoken,digital):
curtoken += Token(c)
# Float
elif c in floating and isonly(curtoken, floating):
curtoken += Token(c)
# Hex
elif curtoken.startswith("0x") and c in hexal and isonly(curtoken[2:], hexal):
curtoken += Token(c)
elif curtoken == "0" and c in "xX":
curtoken += Token(c)
else:
curtoken += Token(c)
if curtoken not in string.whitespace:
yield _token()
# Token Parser
# ------------------------------------------------------------------------
def parse_value(tokens):
if tokens[0] in prefix_operations:
unary = tokens.pop(0)
if tokens[0] == "(":
tokens.pop(0)
value,tokens = parse_expression( tokens )
if tokens[0]!=")":
print >>sys.stderr, "Parse Error at Line %d / Char %d - ( arguments must end with ')', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
else:
value,tokens = parse_value( tokens )
inner = ('Prefix',(unary,value))
elif is_keyword(tokens[0]):
print >>sys.stderr, "Parse Error at Line %d / Char %d - Value Expected at '%s', found keyword" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
elif tokens[0] in string.punctuation:
print >>sys.stderr, "Parse Error at Line %d / Char %d - Value Expected at '%s', found punctuation" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
elif tokens[0][0] == '"':
name = tokens.pop(0)
str = name[1:-1]
while len(tokens) and tokens[0] and tokens[0][0] == '"':
name = tokens.pop(0)
str += name[1:-1]
inner = ('String',str)
else:
name = tokens.pop(0)
inner = ('Value',name)
#print "Value",name
while len(tokens) and tokens[0] in "([":
if tokens[0] == "(":
tokens.pop(0)
# Get the Arguements
arguments = []
while len(tokens):
# Reached end of Argument List
if tokens[0]==")":
break
arg,tokens = parse_expression( tokens )
arguments.append( arg )
if tokens[0]!=",":
break
else:
tokens.pop(0)
if tokens[0]!=")":
print >>sys.stderr, "Parse Error at Line %d / Char %d - Function must have ')', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
inner = ('Call',(inner,arguments))
elif tokens[0] == "[":
tokens.pop(0)
index,tokens = parse_expression( tokens )
if tokens[0]!="]":
print >>sys.stderr, "Parse Error at Line %d / Char %d - Array Accessor must have ']', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
inner = ('Index',(inner, index) )
else:
#So how did you get here?
assert(0)
# Check for postfix unaray operations
if tokens[0] in postfix_operations:
unary = tokens.pop(0)
#print "Value",unary,name
inner = ('Postfix',(inner,unary))
return inner,tokens
def parse_if( tokens ):
if tokens[0] not in ["if"]:
print >>sys.stderr, "Parse Error at Line %d / Char %d - if must start with 'if', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
if tokens[0]!="(":
print >>sys.stderr, "Parse Error at Line %d / Char %d - if must have '(', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
test,tokens = parse_expression( tokens )
if tokens[0]!=")":
print >>sys.stderr, "Parse Error at Line %d / Char %d - if must have ')', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
action,tokens = parse_statement_or_block(tokens)
alternative = None
if tokens[0]=="else":
tokens.pop(0)
alternative,tokens = parse_statement_or_block(tokens)
#print "If",test,action
return ("If",(test,action,alternative)), tokens
def parse_while( tokens ):
if tokens[0] not in ["while"]:
print >>sys.stderr, "Parse Error at Line %d / Char %d - while must start with 'while', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
if tokens[0]!="(":
print >>sys.stderr, "Parse Error at Line %d / Char %d - while must have '(', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
test,tokens = parse_expression( tokens )
if tokens[0]!=")":
print >>sys.stderr, "Parse Error at Line %d / Char %d - if must have ')', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
action,tokens = parse_statement_or_block(tokens)
#print "While",test,action
return ("While",(test,action)), tokens
def parse_for( tokens ):
if tokens[0] not in ["for"]:
print >>sys.stderr, "Parse Error at Line %d / Char %d - for must start with 'for', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
if tokens[0]!="(":
print >>sys.stderr, "Parse Error at Line %d / Char %d - for must have '(', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
init,tokens = parse_expression( tokens )
if tokens[0]!=";":
print >>sys.stderr, "Parse Error at Line %d / Char %d - for must have first ';', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
test,tokens = parse_expression( tokens )
if tokens[0]!=";":
print >>sys.stderr, "Parse Error at Line %d / Char %d - for must have second ';', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
step,tokens = parse_expression( tokens )
if tokens[0]!=")":
print >>sys.stderr, "Parse Error at Line %d / Char %d - if must have ')', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
action,tokens = parse_statement_or_block(tokens)
#print "For",init,test,step,action
return ("For",(init,test,step,action)), tokens
def parse_cast( tokens ):
# This enforces (int)x or (int)(x), rather than int(x), that's not quite right
if tokens[0]!="(":
print >>sys.stderr, "Parse Error at Line %d / Char %d - cast must start with '(', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
# Get the Cast Type
cast_type,tokens = parse_type(tokens)
if tokens[0] != ")":
for e in expression:
print e
print >>sys.stderr, "Parse Error at Line %d / Char %d - ')' expected after expression %s" % (tokens[0].line, tokens[0].pos, str(inner))
assert(0)
tokens.pop(0)
# Get the Casted Value
if tokens[0] == "(":
tokens.pop(0)
cast_value,tokens = parse_expression(tokens)
if tokens[0] != ")":
print >>sys.stderr, "Parse Error at Line %d / Char %d - ')' expected after expression %s" % (tokens[0].line, tokens[0].pos, str(inner))
assert(0)
tokens.pop(0)
else:
cast_value,tokens = parse_value( tokens )
return ("Cast",(cast_type,cast_value)), tokens
def parse_expression( tokens ):
# This should be a tree not a list
expression = []
while len(tokens):
#TODO: Add Ternary Operator "?:"
#TODO: Add Comma
#TODO: Symbol Symbol should be illegal
if tokens[0] == ";":
break
elif tokens[0] == ",":
break
elif tokens[0] == ")":
break
elif tokens[0] == "]":
break
# Get a value
else:
if tokens[0] == "(":
# Is this an inner expression or a cast?
if tokens[1] in types+modifiers:
inner,tokens = parse_cast( tokens )
else:
tokens.pop(0)
inner,tokens = parse_expression( tokens )
if tokens[0] != ")":
for e in expression:
print e
print >>sys.stderr, "Parse Error at Line %d / Char %d - ')' expected after expression %s" % (tokens[0].line, tokens[0].pos, str(inner))
assert(0)
tokens.pop(0)
#break
else:
inner,tokens = parse_value( tokens )
expression.append( inner )
# TODO: Add Right/Left Associations
if tokens[0] in binary_operations + ternary_operations:
symbol = tokens.pop(0)
expression.append( ("Math", (symbol) ) )
else:
#print "Didn't find an operator, found",str(tokens[0]),"instead"
pass
# Fix precedence
if len(expression) > 2:
while len(expression) > 2:
# The expressions should always be of the form:
# Value Math Value Math Value
symbols = [ sym[1] for sym in expression[1::2] ]
for ops in precedence:
if "?" in ops and "?" in symbols:
i = (2 * symbols.index("?")) + 1
j = (2 * symbols.index(":")) + 1
before,after = expression[:i-1],expression[j+2:]
test,yes,no = expression[i-1],expression[i+1],expression[j+1]
math = ("Ternary",(test,yes,no))
#print math
expression = before + [math] + after
elif intersection( symbols, ops):
i = (2 * first_instance( symbols, ops )) + 1
symbol = expression[i][1]
before,after = expression[:i-1],expression[i+2:]
right,left = expression[i-1],expression[i+1]
math = ("Binary",(symbol,right,left))
#print math
expression = before + [math] + after
break
else:
# Nothing to see here, move along
pass
elif len(expression) == 2:
if expression[0][0] == "Math" and expression[0][1] in prefix_operations:
return ("Prefix",(expression[0][1],expression[1])),tokens
elif expression[1][0] == "Math" and expression[0][1] in postfix_operations:
return ("Postfix",(expression[1][1],expression[0])),tokens
#
if len(expression) == 1:
return expression[0],tokens
elif len(expression) == 0:
return ("Expression",[]),tokens
else:
print >>sys.stderr, "Parse Error at Line %d / Char %d - Couldn't compress expression into tree" % (tokens[0].line, tokens[0].pos)
for e in expression:
print >>sys.stderr, e
assert(0)
def parse_struct( tokens ):
struct = []
if tokens[0] not in ["struct","union"]:
print >>sys.stderr, "Parse Error at Line %d / Char %d - struct must start with 'struct' or 'union', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
kind = "Struct" if (tokens.pop(0) == "struct") else "Union"
if tokens[0]!="{":
print >>sys.stderr, "Parse Error at Line %d / Char %d - Blocks must start with 'struct {', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
while len(tokens):
if tokens[0]=="}":
break
if tokens[0]=="struct" or tokens[0]=="union":
inner,tokens = parse_struct(tokens)
struct.append(inner)
else:
declaration,tokens = parse_declaration(tokens)
struct.append(declaration)
if tokens[0]!=";":
print >>sys.stderr, "Parse Error at Line %d / Char %d - struct values must end in ';', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
if tokens[0]!="}":
print >>sys.stderr, "Parse Error at Line %d / Char %d - Blocks must start with 'struct {', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
#print kind, struct
return (kind, struct), tokens
def parse_switch(tokens):
if tokens[0] not in ["switch"]:
print >>sys.stderr, "Parse Error at Line %d / Char %d - switch must start with 'switch', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
if tokens[0]!="(":
print >>sys.stderr, "Parse Error at Line %d / Char %d - for must have '(', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
test,tokens = parse_expression( tokens )
if tokens[0]!=")":
print >>sys.stderr, "Parse Error at Line %d / Char %d - functions arguments must have ')', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
block,tokens = parse_block( tokens )
return ( "Switch", (test,block) ), tokens
def parse_type(tokens):
mods = []
while tokens[0] in modifiers:
mods.append( tokens.pop(0) )
if not ( tokens[0] in types ):
print >>sys.stderr, "Parse Error at Line %d / Char %d - expected type but found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert( tokens[0] in types )
type = tokens.pop(0)
isPointer = False
if tokens[0] == "*":
isPointer = True
tokens.pop(0)
#print "Type %s" % (" ".join(mods) + type + ("*" if isPointer else ""))
return ("Type", (mods, type, isPointer)), tokens
def parse_declaration( tokens ):
assignments = []
type, tokens = parse_type( tokens )
while len(tokens):
if tokens[0] == "*":
type = ("Type", (type[1][0], type[1][1], True))
tokens.pop(0)
# Check if it's a pointer
name = tokens.pop(0)
#print "Name %s" % name
length = None
if tokens[0]=="[":
tokens.pop(0)
length,tokens = parse_expression( tokens )
if tokens[0]!="]":
print >>sys.stderr, "Parse Error at Line %d / Char %d - Array Definition must end with ']', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
if tokens[0]=="[":
# Get Multi Dimensional Arrays
print >>sys.stderr, "Parse Error at Line %d / Char %d - Multi Dimensional Arrays don't work yet" %(tokens[0].line, tokens[0].pos)
assert(0)
if not is_keyword(name):
if tokens[0]=="=":
# Declaration value
tokens.pop(0)
expression,tokens = parse_expression( tokens )
assignments.append((type,name,length,expression))
else:
# Non-Assignmed value
assignments.append((type,name,length,None))
if tokens[0]==",":
tokens.pop(0)
type = ("Type", (type[1][0], type[1][1], False))
continue
elif tokens[0]==";":
break
if len(tokens):
print >>sys.stderr, "Parse Error at Line %d / Char %d - unknown token encountered at '%s'" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
return ("Declaration", assignments), tokens
def parse_function( tokens ):
returntype,tokens = parse_type(tokens)
name = tokens.pop(0)
if tokens[0]!="(":
print >>sys.stderr, "Parse Error at Line %d / Char %d - Function must have '(', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
# Arguements
arguments = []
while len(tokens):
# Reached end of Argument List
if tokens[0]==")":
break
if tokens[0]== "void" and tokens[1]==")":
tokens.pop(0)
break
type,tokens = parse_type(tokens)
argname = tokens.pop(0)
if is_keyword(name):
print >>sys.stderr, "Parse Error at Line %d / Char %d - Function argument #%d's name '%s' cannot be a keyword" % (len(arguments)+1, name)
assert(0)
arguments.append( (type,argname) )
if tokens[0]!=",":
break
else:
tokens.pop(0)
if tokens[0]!=")":
print >>sys.stderr, "Parse Error at Line %d / Char %d - Functions arguments must have ')', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
if tokens[0]=="{":
block,tokens = parse_block( tokens );
elif tokens[0]==";":
tokens.pop(0)
block = None
else:
print >>sys.stderr, "Parse Error at Line %d / Char %d - Functions must have '{', found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
return ("Function",(returntype,name,arguments,block)), tokens
def parse_statement( tokens ):
statement = []
needsemicolon = True
if tokens[0] == "if":
statement,tokens = parse_if( tokens )
needsemicolon = False
elif tokens[0] == "while":
statement,tokens = parse_while( tokens )
needsemicolon = False
elif tokens[0] == "for":
statement,tokens = parse_for( tokens )
needsemicolon = False
elif tokens[0] in types + modifiers:
statement,tokens = parse_declaration( tokens )
elif tokens[0]=="struct" or tokens[0]=="union":
statement,tokens = parse_struct(tokens)
elif tokens[0] == "switch":
statement,tokens = parse_switch(tokens)
needsemicolon = False
elif tokens[0] == "break":
statement = ("Break",None)
tokens.pop(0)
elif tokens[0] == "continue":
statement = ("Continue",None)
tokens.pop(0)
elif tokens[0] == "case":
tokens.pop(0)
literal,tokens = parse_value(tokens)
statement = ("Case",literal)
if tokens[0]!=":":
print >>sys.stderr, "Parse Error at Line %d / Char %d - case must end in a colon: found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(tokens[0] == ":")
tokens.pop(0)
needsemicolon = False
elif tokens[0] == "default":
tokens.pop(0)
statement = ("default",None)
if tokens[0]!=":":
print >>sys.stderr, "Parse Error at Line %d / Char %d - default must end in a colon: found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(tokens[0] == ":")
tokens.pop(0)
needsemicolon = False
elif tokens[1] == ":":
label = tokens.pop(0)
statement = ("Label",label)
assert(tokens[0] == ":")
tokens.pop(0)
needsemicolon = False
elif tokens[0] == "goto":
tokens.pop(0)
label = tokens.pop(0)
statement = ("Goto",label)
elif tokens[0] == "return":
tokens.pop(0)
expression,tokens = parse_expression( tokens );
statement = ("Return",expression)
else:
expression,tokens = parse_expression(tokens)
statement = ("Statement",expression)
if needsemicolon:
if tokens[0]==";" or tokens[0]==",":
tokens.pop(0)
else:
print >>sys.stderr, "Parse Error at Line %d / Char %d - Statements must end in a semicolon: found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(tokens[0]==";")
#print "Statement",statement,"\n"
return statement, tokens
def parse_block( tokens ):
if tokens[0]!="{":
print >>sys.stderr, "Parse Error at Line %d / Char %d - Blocks must start with a {, found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
block = []
while len(tokens) and tokens[0] != "}":
statement,tokens = parse_statement_or_block(tokens)
block.append( statement )
if tokens[0]!="}":
print >>sys.stderr, "Parse Error at Line %d / Char %d - Blocks must end with a }, found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(0)
tokens.pop(0)
#print "Block", block
return ("Block",block), tokens
def parse_statement_or_block( tokens ):
if tokens[0]=="{":
return parse_block( tokens )
else:
return parse_statement( tokens )
def parse_root( tokens ):
if tokens[ len_type(tokens) + 1 ] == "(":
return parse_function( tokens )
else:
declaration = parse_declaration( tokens )
if tokens[0]==";":
tokens.pop(0)
else:
print >>sys.stderr, "Parse Error at Line %d / Char %d - Non-Function Declarations must end in a semicolon: found %s instead" % (tokens[0].line, tokens[0].pos, tokens[0])
assert(tokens[0]==";")
return declaration
# Print Abstract Syntax Tree (AST)
# ------------------------------------------------------------------------
def print_thing( thing, depth=0 ):
def p(str,d=0):
print "\t"*(depth+d)+ str
try:
name,value = thing
except ValueError:
print "Can't Unpack this variable:"
print thing
assert(0)
#p("THING:", name,value)
if name == "Block":
p("Block")
for num,statement in enumerate(value):
print "\t"*(depth)+ "Statement %d" %(num+1)
print_thing(statement,depth+1)
elif name == "Statement":
print_thing(value,depth)
elif name == "Math":
symbol = value
p("Math")
p(symbol)
assert(0)
elif name == "Cast":
type,expression = value
p("Cast")
print_thing(expression,depth+1)
p("To")
print_thing(type,depth+1)
elif name == "Prefix":
p("Prefix")
symbol, expression = value
p(symbol)
print_thing(expression,depth+1)
elif name == "Postfix":
p("Postfix")
expression, symbol = value
print_thing(expression,depth+1)
p(symbol)
elif name == "Binary":
symbol,left,right = value
p("(")
p("Math '%s'" % symbol)
print_thing(left,depth+1)
p(symbol)
print_thing(right,depth+1)
p(")")
elif name == "String":
p("String")
p('"%s"'%value)
elif name == "Value":
p("Value")
p(value)
elif name == "Index":
p("Index")
var, expression = value
print_thing(var,depth+1)
p("[")
print_thing(expression,depth+1)
p("]")
elif name == "Type":
p("Type")
mods, type, isPointer = value
if len(mods):
type = " ".join(mods) + " " + type
if isPointer:
type = "Pointer to " + type
p(type,1)
elif name == "Declaration":
p(name)
for declaration in value:
type,name,length,assignment = declaration
if length:
p("Array of length",1)
print_thing(length,depth+2)
print_thing(type,depth+1)
p("Name",1)
p(name,2)
if assignment:
p("Assigned the value",1)
print_thing(assignment,depth+2)
elif name == "Expression":
p(name)
p("(")
if value:
print_thing(value,depth+1)
p(")")
elif name=="Struct" or name=="Union":
p(name)
p("{")
for expression in value:
print_thing(expression,depth+1)
p("}")
elif name=="If":
test,action,alternative = value
p(name)
p("TEST",1)
print_thing(test,depth+2)
p("DO",1)
print_thing(action,depth+2)
if alternative:
p("ELSE",1)
print_thing(alternative,depth+2)
elif name=="While":
test,action = value
p(name)
p("TEST",1)
print_thing(test,depth+2)
p("DO",1)
print_thing(action,depth+2)
elif name=="For":
init,test,step,action = value
p(name)
p("INIT",1)
print_thing(init,(depth+1)+1)
p("TEST",1)
print_thing(test,(depth+1)+1)
p("STEP",1)
print_thing(step,(depth+1)+1)
p("DO",1)
print_thing(action,depth+2)
elif name=="Break":
p(name)
elif name=="Continue":
p(name)
elif name=="Return":
p(name)
print_thing(value,depth+1)
elif name=="Case":
p(name)
print_thing(value,depth+1)
elif name=="Label":
p(name)
p(value,1)
elif name=="Goto":
p(name)
p(value,1)
elif name=="default":
p(name)
elif name=="Function":
returntype,name,arguments,block = value
if block:
p("Function Declaration")
else:
p("Function Header")
print_thing(returntype,depth+1)
p(name,1)
if len(arguments):
p("With %d Argument%s" %(len(arguments), "s" if len(arguments) > 1 else ""))
for num,(argtype,argname) in enumerate(arguments):
p("Argument %d:" %(num+1),1)
print_thing(argtype,depth+2)
p("Name",2)
p(argname,3)
else:
p("With No Arguments")
if block:
p("{")
print_thing(block,depth+1)
p("}")
elif name=="Call":
func,arguments = value
print_thing(func,depth+1)
p("(")
for num,arg in enumerate(arguments):
print_thing(arg,depth+1)
if num != len(arguments)-1:
p(",")
p(")")
elif name=="Switch":
test,block = value
p(name)
p("(")
print_thing(test,depth+1)
p(")")
p("{")
print_thing(block,depth+1)