-
Notifications
You must be signed in to change notification settings - Fork 0
/
gram.py~
515 lines (425 loc) · 19.2 KB
/
gram.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
import ply.yacc as yacc
import sys
from scan import C_Lexer
class TerminalSymbol(object):
def __init__(self, symbol):
self.symbol = symbol
def __str__(self): return '"' + str(self.symbol) + '"'
class NonTerminalSymbol(object):
def __init__(self, symbol):
self.symbol = symbol
def __str__(self): return '"' + str(self.symbol) + '"'
class Value(object):
def __init__(self, value):
self.value = value
def __str__(self): return '"' + str(self.value) + '"'
class Node(object):
def __init__(self, production=None, symbol=''):
if not production:
self.children = []
else:
self.children = production[1:]
if production: token_stack = production.lexer.lexmodule.token_stack[:]
for index,child in enumerate(self.children):
if child.__class__ != Node:
n = Node(symbol=TerminalSymbol(child))
for token in token_stack:
if token and token.value == child:
if len(token.type) < 2: break
n = Node(symbol=TerminalSymbol(token.type))
n2 = Node(symbol=Value(child))
n2.graph_color = "#88ff88"
n.children.append(n2)
break
#if token:
#n = Node(symbol='"' + token.type + ", " + str(token.value) + '"')
#n.children.append(Node(symbol='"'+str(child)+'"'))
#else:
#n = Node(symbol='"'+str(child)+'"')
print n
self.children[index] = n
self.children[index].graph_color = "#8888ff"
if production: self.symbol = NonTerminalSymbol(symbol)
else: self.symbol = symbol
def traverse(self, node, i=0):
s = ' '*i + str(node) + '\n'
if 'children' not in dir(node): return s
for child in node.children:
s += self.traverse(child, i+1)
return s
def print_out(self):
return str(self.traverse(self))
def __str__(self):
return str(self.symbol)
class C_Parser(object):
def __init__(self):
self.start = 'code'
self.c_lexer = C_Lexer()
self.c_lexer.build()
self.lexer = self.c_lexer.lexer
self.tokens = self.c_lexer.tokens
self.literals = self.c_lexer.literals
def p_primary_expr(self, p):
'''primary_expr : identifier
| CONSTANT
| STRING_LITERAL
| '(' expr ')' '''
p[0] = Node(p, 'primary_expr')
def p_postfix_expr(self, p):
'''postfix_expr : primary_expr
| postfix_expr '[' expr ']'
| postfix_expr '(' ')'
| postfix_expr '(' argument_expr_list ')'
| postfix_expr '.' identifier
| postfix_expr PTR_OP identifier
| postfix_expr INC_OP
| postfix_expr DEC_OP'''
p[0] = Node(p, 'postfix_expr')
def p_argument_expr_list(self, p):
'''argument_expr_list : assignment_expr
| argument_expr_list ',' assignment_expr'''
p[0] = Node(p, 'argument_expr_list')
def p_unary_expr(self, p):
'''unary_expr : postfix_expr
| INC_OP unary_expr
| DEC_OP unary_expr
| unary_operator cast_expr
| SIZEOF unary_expr
| SIZEOF '(' type_name ')' '''
p[0] = Node(p, 'unary_expr')
def p_unary_operator(self, p):
'''unary_operator : '&'
| '*'
| '+'
| '-'
| '~'
| '!' '''
p[0] = Node(p, 'unary_operator')
def p_cast_expr(self, p):
'''cast_expr : unary_expr
| '(' type_name ')' cast_expr'''
p[0] = Node(p, 'cast_expr')
def p_multiplicative_expr(self, p):
'''multiplicative_expr : cast_expr
| multiplicative_expr '*' cast_expr
| multiplicative_expr '/' cast_expr
| multiplicative_expr '%' cast_expr'''
p[0] = Node(p, 'multiplicative_expr')
def p_additive_expr(self, p):
'''additive_expr : multiplicative_expr
| additive_expr '+' multiplicative_expr
| additive_expr '-' multiplicative_expr'''
p[0] = Node(p, 'additive_expr')
def p_shift_expr(self, p):
'''shift_expr : additive_expr
| shift_expr LEFT_OP additive_expr
| shift_expr RIGHT_OP additive_expr'''
p[0] = Node(p, 'shift_expr')
def p_relational_expr(self, p):
'''relational_expr : shift_expr
| relational_expr '<' shift_expr
| relational_expr '>' shift_expr
| relational_expr LE_OP shift_expr
| relational_expr GE_OP shift_expr'''
p[0] = Node(p, 'relational_expr')
def p_equality_expr(self, p):
'''equality_expr : relational_expr
| equality_expr EQ_OP relational_expr
| equality_expr NE_OP relational_expr'''
p[0] = Node(p, 'equality_expr')
def p_and_expr(self, p):
'''and_expr : equality_expr
| and_expr '&' equality_expr'''
p[0] = Node(p, 'and_expr')
def p_exclusive_or_expr(self, p):
'''exclusive_or_expr : and_expr
| exclusive_or_expr '^' and_expr'''
p[0] = Node(p, 'exclusive_or_expr')
def p_inclusive_or_expr(self, p):
'''inclusive_or_expr : exclusive_or_expr
| inclusive_or_expr '|' exclusive_or_expr'''
p[0] = Node(p, 'inclusive_or_expr')
def p_logical_and_expr(self, p):
'''logical_and_expr : inclusive_or_expr
| logical_and_expr AND_OP inclusive_or_expr'''
p[0] = Node(p, 'logical_and_expr')
def p_logical_or_expr(self, p):
'''logical_or_expr : logical_and_expr
| logical_or_expr OR_OP logical_and_expr'''
p[0] = Node(p, 'logical_or_expr')
def p_conditional_expr(self, p):
'''conditional_expr : logical_or_expr
| logical_or_expr '?' logical_or_expr ':' conditional_expr'''
p[0] = Node(p, 'conditional_expr')
def p_assignment_expr(self, p):
'''assignment_expr : conditional_expr
| unary_expr assignment_operator assignment_expr'''
p[0] = Node(p, 'assignment_expr')
def p_assignment_operator(self, p):
'''assignment_operator : '='
| MUL_ASSIGN
| DIV_ASSIGN
| MOD_ASSIGN
| ADD_ASSIGN
| SUB_ASSIGN
| LEFT_ASSIGN
| RIGHT_ASSIGN
| AND_ASSIGN
| XOR_ASSIGN
| OR_ASSIGN'''
p[0] = Node(p, 'assignment_operator')
def p_expr(self, p):
'''expr : assignment_expr
| expr ',' assignment_expr'''
p[0] = Node(p, 'expr')
def p_constant_expr(self, p):
'''constant_expr : conditional_expr'''
p[0] = Node(p, 'constant_expr')
def p_declaration(self, p):
'''declaration : declaration_specifiers ';'
| declaration_specifiers init_declarator_list ';' '''
p[0] = Node(p, 'declaration')
def p_declaration_specifiers(self, p):
'''declaration_specifiers : storage_class_specifier
| storage_class_specifier declaration_specifiers
| type_specifier
| type_specifier declaration_specifiers'''
p[0] = Node(p, 'declaration_specifiers')
def p_init_declarator_list(self, p):
'''init_declarator_list : init_declarator
| init_declarator_list ',' init_declarator'''
p[0] = Node(p, 'init_declarator_list')
def p_init_declarator(self, p):
'''init_declarator : declarator
| declarator '=' initializer'''
p[0] = Node(p, 'init_declarator')
def p_storage_class_specifier(self, p):
'''storage_class_specifier : TYPEDEF
| EXTERN
| STATIC
| AUTO
| REGISTER'''
p[0] = Node(p, 'storage_class_specifier')
def p_type_specifier(self, p):
'''type_specifier : CHAR
| SHORT
| INT
| LONG
| SIGNED
| UNSIGNED
| FLOAT
| DOUBLE
| CONST
| VOLATILE
| VOID
| struct_or_union_specifier
| enum_specifier
| TYPE_NAME'''
p[0] = Node(p, 'type_specifier')
def p_struct_or_union_specifier(self, p):
'''struct_or_union_specifier : struct_or_union identifier '{' struct_declaration_list '}'
| struct_or_union '{' struct_declaration_list '}'
| struct_or_union identifier'''
p[0] = Node(p, 'struct_or_union_specifier')
def p_struct_or_union(self, p):
'''struct_or_union : STRUCT
| UNION'''
p[0] = Node(p, 'struct_or_union')
def p_struct_declaration_list(self, p):
'''struct_declaration_list : struct_declaration
| struct_declaration_list struct_declaration'''
p[0] = Node(p, 'struct_declaration_list')
def p_struct_declaration(self, p):
'''struct_declaration : type_specifier_list struct_declarator_list ';' '''
p[0] = Node(p, 'struct_declaration')
def p_struct_declarator_list(self, p):
'''struct_declarator_list : struct_declarator
| struct_declarator_list ',' struct_declarator'''
p[0] = Node(p, 'struct_declarator_list')
def p_struct_declarator(self, p):
'''struct_declarator : declarator
| ':' constant_expr
| declarator ':' constant_expr'''
p[0] = Node(p, 'struct_declarator')
def p_enum_specifier(self, p):
'''enum_specifier : ENUM '{' enumerator_list '}'
| ENUM identifier '{' enumerator_list '}'
| ENUM identifier'''
p[0] = Node(p, 'enum_specifier')
def p_enumerator_list(self, p):
'''enumerator_list : enumerator
| enumerator_list ',' enumerator'''
p[0] = Node(p, 'enumerator_list')
def p_enumerator(self, p):
'''enumerator : identifier
| identifier '=' constant_expr'''
p[0] = Node(p, 'enumerator')
def p_declarator(self, p):
'''declarator : declarator2
| pointer declarator2'''
p[0] = Node(p, 'declarator')
def p_declarator2(self, p):
'''declarator2 : identifier
| '(' declarator ')'
| declarator2 '[' ']'
| declarator2 '[' constant_expr ']'
| declarator2 '(' ')'
| declarator2 '(' parameter_type_list ')'
| declarator2 '(' parameter_identifier_list ')' '''
p[0] = Node(p, 'declarator2')
def p_pointer(self, p):
'''pointer : '*'
| '*' type_specifier_list
| '*' pointer
| '*' type_specifier_list pointer'''
p[0] = Node(p, 'pointer')
def p_type_specifier_list(self, p):
'''type_specifier_list : type_specifier
| type_specifier_list type_specifier'''
p[0] = Node(p, 'type_specifier_list')
def p_parameter_identifier_list(self, p):
'''parameter_identifier_list : identifier_list
| identifier_list ',' ELIPSIS'''
p[0] = Node(p, 'parameter_identifier_list')
def p_identifier_list(self, p):
'''identifier_list : identifier
| identifier_list ',' identifier'''
p[0] = Node(p, 'identifier_list')
def p_parameter_type_list(self, p):
'''parameter_type_list : parameter_list
| parameter_list ',' ELIPSIS'''
p[0] = Node(p, 'parameter_type_list')
def p_parameter_list(self, p):
'''parameter_list : parameter_declaration
| parameter_list ',' parameter_declaration'''
p[0] = Node(p, 'parameter_list')
def p_parameter_declaration(self, p):
'''parameter_declaration : type_specifier_list declarator
| type_name'''
p[0] = Node(p, 'parameter_declaration')
def p_type_name(self, p):
'''type_name : type_specifier_list
| type_specifier_list abstract_declarator'''
p[0] = Node(p, 'type_name')
def p_abstract_declarator(self, p):
'''abstract_declarator : pointer
| abstract_declarator2
| pointer abstract_declarator2'''
p[0] = Node(p, 'abstract_declarator')
def p_abstract_declarator2(self, p):
'''abstract_declarator2 : '(' abstract_declarator ')'
| '[' ']'
| '[' constant_expr ']'
| abstract_declarator2 '[' ']'
| abstract_declarator2 '[' constant_expr ']'
| '(' ')'
| '(' parameter_type_list ')'
| abstract_declarator2 '(' ')'
| abstract_declarator2 '(' parameter_type_list ')' '''
p[0] = Node(p, 'abstract_declarator2')
def p_initializer(self, p):
'''initializer : assignment_expr
| '{' initializer_list '}'
| '{' initializer_list ',' '}' '''
p[0] = Node(p, 'initializer')
def p_initializer_list(self, p):
'''initializer_list : initializer
| initializer_list ',' initializer'''
p[0] = Node(p, 'initializer_list')
def p_statement(self, p):
'''statement : labeled_statement
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement'''
p[0] = Node(p, 'statement')
def p_labeled_statement(self, p):
'''labeled_statement : identifier ':' statement
| CASE constant_expr ':' statement
| DEFAULT ':' statement'''
p[0] = Node(p, 'labeled_statement')
def p_left_bracket(self, p):
'''left_bracket : '{' '''
p[0] = Node(p, 'left_bracket')
def p_right_bracket(self, p):
'''right_bracket : '}' '''
p[0] = Node(p, 'right_bracket')
def p_compound_statement(self, p):
'''compound_statement : left_bracket right_bracket
| left_bracket statement_list right_bracket
| left_bracket declaration_list right_bracket
| left_bracket declaration_list statement_list right_bracket'''
p[0] = Node(p, 'compound_statement')
def p_declaration_list(self, p):
'''declaration_list : declaration
| declaration_list declaration'''
p[0] = Node(p, 'declaration_list')
def p_statement_list(self, p):
'''statement_list : statement
| statement_list statement'''
p[0] = Node(p, 'statement_list')
def p_expr_statement(self, p):
'''expression_statement : ';'
| expr ';' '''
p[0] = Node(p, 'expression_statement')
def p_selection_statement(self, p):
'''selection_statement : IF '(' expr ')' statement
| IF '(' expr ')' statement ELSE statement
| SWITCH '(' expr ')' statement'''
p[0] = Node(p, 'selection_statement')
def p_iteration_statement(self, p):
'''iteration_statement : WHILE '(' expr ')' statement
| DO statement WHILE '(' expr ')' ';'
| FOR '(' ';' ';' ')' statement
| FOR '(' ';' ';' expr ')' statement
| FOR '(' ';' expr ';' ')' statement
| FOR '(' ';' expr ';' expr ')' statement
| FOR '(' expr ';' ';' ')' statement
| FOR '(' expr ';' ';' expr ')' statement
| FOR '(' expr ';' expr ';' ')' statement
| FOR '(' expr ';' expr ';' expr ')' statement'''
p[0] = Node(p, 'iteration_statement')
def p_jump_statement(self, p):
'''jump_statement : GOTO identifier ';'
| CONTINUE ';'
| BREAK ';'
| RETURN ';'
| RETURN expr ';' '''
p[0] = Node(p, 'jump_statement')
def p_code(self, p):
'''code : file'''
p[0] = Node(p, 'code')
def p_file(self, p):
'''file : external_definition
| file external_definition '''
p[0] = Node(p, 'file')
def p_external_definition(self, p):
'''external_definition : function_definition
| declaration'''
p[0] = Node(p, 'external_definition')
def p_function_definition(self, p):
'''function_definition : declarator function_body
| declaration_specifiers declarator function_body'''
p[0] = Node(p, 'function_definition')
def p_function_body(self, p):
'''function_body : compound_statement
| declaration_list compound_statement
'''
p[0] = Node(p, 'function_body')
def p_identifier(self, p):
'''identifier : IDENTIFIER'''
p[0] = Node(p, 'identifier')
def p_error(self, p):
sys.stderr.write('ERROR' + str(p))
def build(self, **kwargs):
self.parser = yacc.yacc(module=self, **kwargs)
def parse(self, text):
return self.parser.parse(input=text, lexer=self.lexer, debug=0)
if __name__ == "__main__":
c_parser = C_Parser()
c_parser.build()
f = open('test3.c', 'r')
data = f.read()
f.close()
print c_parser.parse(data).print_out()