-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_pro1.py
443 lines (366 loc) · 7.63 KB
/
compile_pro1.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
import sys
import Node as n
import dfs_search as dfs
tokens = (
'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'EQUALS',
'LT', 'GT', 'LE', 'GE', 'EQ', 'NE', 'COMMA', 'SEMICOLON', 'COLON',
'ID', 'LPAREN', 'RPAREN', 'LBRK', 'RBRK', 'LBRACE', 'RBRACE',
'INTNUM', 'FLOATNUM',
'INT', 'FLOAT','RETURN', 'WHILE', 'DO', 'FOR', 'IF', 'ELSE', 'SWITCH', 'CASE', 'BREAK', 'DEFAULT',
)
t_ignore = " \t"
def t_FLOATNUM(t):
r'[0-9]+\.[0-9]+'
return t
def t_INTNUM(t):
r'[0-9]+'
return t
def t_PLUS(t):
r'\+'
return t
def t_MINUS(t):
r'-'
return t
def t_TIMES(t):
r'\*'
return t
def t_DIVIDE(t):
r'/'
return t
def t_EQ(t):
r'=='
return t
def t_LE(t):
r'<='
return t
def t_GE(t):
r'>='
return t
def t_NE(t):
r'!='
return t
def t_EQUALS(t):
r'='
return t
def t_LT(t):
r'<'
return t
def t_GT(t):
r'>'
return t
def t_COMMA(t):
r','
return t
def t_SEMICOLON(t):
r';'
return t
def t_COLON(t):
r':'
return t
def t_LPAREN(t):
r'\('
return t
def t_RPAREN(t):
r'\)'
return t
def t_LBRK(t):
r'\['
return t
def t_RBRK(t):
r'\]'
return t
def t_LBRACE(t):
r'\{'
return t
def t_RBRACE(t):
r'\}'
return t
def t_ID(t):
r'[A-Za-z][A-Za-z0-9_]*'
a = 0;
if t.value == 'int':
t.type = 'INT'
elif t.value == 'float':
t.type = 'FLOAT'
elif t.value == 'return':
t.type = 'RETURN'
elif t.value == 'while':
t.type = 'WHILE'
elif t.value == 'do':
t.type = 'DO'
elif t.value == 'for':
t.type = 'FOR'
elif t.value == 'if':
t.type = 'IF'
elif t.value == 'else':
t.type = 'ELSE'
elif t.value == 'switch':
t.type = 'SWITCH'
elif t.value == 'case':
t.type = 'CASE'
elif t.value == 'default':
t.type = 'DEFAULT'
elif t.value == 'break':
t.type = 'BREAK'
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
import ply.lex as lex
lexer = lex.lex()
# Parsing rules
precedence = (
('nonassoc', 'IFX'),
('nonassoc', 'ELSE'),
('right', 'EQUALS'),
('left', 'EQ', 'NE'),
('left', 'GT', 'GE', 'LT', 'LE'),
('left', 'PLUS', 'MINUS'),
('left', 'TIMES', 'DIVIDE'),
('right','UMINUS'),
('left', 'LPAREN', 'RPAREN'),
)
# dictionary of names
def p_Program(t):
'''Program : DeclList FuncList
| DeclList
| FuncList
| '''
if len(t) == 1 :
t[0] = n.Program()
elif len(t) == 2 :
t[0] = n.Program(t[1])
elif len(t) == 3 :
t[0] = n.Program(t[1], t[2])
else:
print("program error!")
dfs.Program_dfs(t[0], option)
def p_DeclList(t):
'''DeclList : Declaration
| DeclList Declaration'''
if len(t) == 2 :
t[0] = n.Decllist(declaration = t[1])
elif len(t) == 3 :
t[0] = n.Decllist(t[2],t[1])
else:
print("declist error!")
def p_FuncList(t):
'''FuncList : Function
| FuncList Function'''
if len(t) ==2:
t[0] = n.Funclist(t[1])
elif len(t) ==3:
t[0] = n.Funclist(t[2], t[1])
else:
print("funclist error!")
def p_Declaration(t):
'Declaration : Type IdentList SEMICOLON'
t[0] = n.Declaration(t[1], t[2])
def p_IdentList(t):
'''IdentList : identifier
| IdentList COMMA identifier'''
if len(t) ==2:
t[0] = n.Identlist(t[1])
elif len(t) ==4:
t[0] = n.Identlist(t[3], t[1])
else :
print("identlist error!")
def p_identifier(t):
'''identifier : ID
| ID LBRK INTNUM RBRK'''
if len(t) == 2:
t[0] = n.identifier(id = t[1])
else :
t[0] = n.identifier(t[1], t[3])
def p_Function(t):
'''Function : Type ID LPAREN RPAREN CompoundStmt
| Type ID LPAREN ParamList RPAREN CompoundStmt'''
if len(t) == 6:
t[0] = n.Function(t[1], t[2], t[5])
elif len(t) == 7:
t[0] = n.Function(t[1], t[2], t[6], t[4])
else:
print("function error!")
def p_ParamList(t):
'''ParamList : Type identifier
| ParamList COMMA Type identifier'''
if len(t) == 3:
t[0] = n.Paramlist(t[1], t[2])
elif len(t) == 5:
t[0] = n.Paramlist(t[3], t[4], t[1])
else:
print("paramlist error!")
def p_Type(t):
'''Type : INT
| FLOAT'''
t[0] = n.Type(t[1])
def p_CompoundStmt(t):
'''CompoundStmt : LBRACE DeclList StmtList RBRACE
| LBRACE StmtList RBRACE'''
if len(t) == 5 :
t[0] = n.Compoundstmt(t[3], t[2])
else :
t[0] = n.Compoundstmt(t[2])
def p_StmtList(t):
'''StmtList : Stmt StmtList
| '''
if len(t) == 3:
t[0] = n.Stmtlist(t[1], t[2])
else:
t[0] = n.Stmtlist()
def p_Stmt(t):
'''Stmt : AssignStmt
| CallStmt
| RetStmt
| WhileStmt
| ForStmt
| IfStmt
| SwitchStmt
| CompoundStmt
| SEMICOLON'''
t[0] = n.Stmt(t[1])
def p_AssignStmt(t):
'AssignStmt : Assign SEMICOLON'
t[0] = n.Assignstmt(t[1])
def p_Assign(t):
'''Assign : ID EQUALS Expr
| ID LBRK Expr RBRK EQUALS Expr'''
if len(t) == 4:
t[0] = n.Assign(t[1], t[3])
else:
t[0] = n.Assign(t[1], t[3], t[6])
def p_CallStmt(t):
'CallStmt : Call SEMICOLON'
t[0] = n.Callstmt(t[1])
def p_Call(t):
'''Call : ID LPAREN ArgList RPAREN
| ID LPAREN RPAREN'''
if len(t) == 5 :
t[0] = n.Call(t[1], t[3])
else:
t[0] = n.Call(t[1])
def p_RetStmt(t):
'''RetStmt : RETURN SEMICOLON
| RETURN Expr SEMICOLON'''
if len(t) == 3 :
t[0] = n.Retstmt()
else:
t[0] = n.Retstmt(t[2])
def p_WhileStmt(t):
'''WhileStmt : WHILE LPAREN Expr RPAREN Stmt
| DO Stmt WHILE LPAREN Expr RPAREN SEMICOLON'''
if len(t) == 6:
t[0] = n.Whilestmt(t[3], t[5])
else:
t[0] = n.Whilestmt(t[5], t[2], True)
def p_ForStmt(t):
'ForStmt : FOR LPAREN Assign SEMICOLON Expr SEMICOLON Assign RPAREN Stmt'
t[0] = n.Forstmt(t[3], t[5], t[7], t[9])
def p_IfStmt(t):
'''IfStmt : IF LPAREN Expr RPAREN Stmt %prec IFX
| IF LPAREN Expr RPAREN Stmt ELSE Stmt'''
if len(t) ==6 :
t[0] = n.Ifstmt(t[3], t[5])
else:
t[0] = n.Ifstmt(t[3], t[5], t[7])
def p_SwitchStmt(t):
'''SwitchStmt : SWITCH LPAREN identifier RPAREN LBRACE CaseList RBRACE'''
t[0] = n.Switchstmt(t[3], t[6])
def p_CaseList(t):
'''CaseList : CaseStmt CaseList
| DefaultStmt
| '''
if len(t)==3:
t[0] = n.Caselist(t[1], t[2])
elif len(t) == 2:
t[0] = n.Caselist(None,None,t[1])
elif len(t) ==1:
t[0] = n.Caselist()
else:
print("caselist error!")
def p_CaseStmt(t):
'''CaseStmt : CASE INTNUM COLON StmtList BREAK SEMICOLON
| CASE INTNUM COLON StmtList'''
if len(t)== 7:
t[0] = n.Casestmt(t[2], t[4])
else:
t[0] = n.Casestmt(t[2], t[4], False)
def p_DefaultStmt(t):
'''DefaultStmt : DEFAULT COLON StmtList BREAK SEMICOLON
| DEFAULT COLON StmtList'''
if len(t) == 6:
t[0] = n.DefaultStmt(t[3])
else:
t[0] = n.DefaultStmt(t[3], False)
def p_Expr(t):
'''Expr : Unop
| Bioperation
| Call
| INTNUM
| FLOATNUM
| ID
| ID LBRK Expr RBRK
| LPAREN Expr RPAREN'''
if len(t) == 2 :
t[0] = n.Expression(t[1])
elif len(t) == 5:
t[0] = n.Expression(t[3], t[1])
else:
t[0] = n.Expression(t[2], isPAREN = True)
def p_unop(t):
'Unop : MINUS Expr %prec UMINUS'
t[0] = n.Unop(t[2])
def p_Bioperation(t):
'''Bioperation : Expr PLUS Expr
| Expr MINUS Expr
| Expr TIMES Expr
| Expr DIVIDE Expr
| Expr GT Expr
| Expr LT Expr
| Expr GE Expr
| Expr LE Expr
| Expr EQ Expr
| Expr NE Expr'''
t[0] = n.Binop(t[1], t[3], t[2])
def p_ArgList(t):
'''ArgList : Expr
| ArgList COMMA Expr'''
if len(t) == 2 :
t[0] = n.Arglist(t[1])
elif len(t) == 4 :
t[0] = n.Arglist(t[3], t[1])
else:
print("arglist error!")
def p_error(t):
print()
print("Syntax error at '%s'" % t.value)
import ply.yacc as yacc
parser = yacc.yacc()
'''
while True:
try:
s = input('>> ')
except EOFError:
break
parser.parse(s)
'''
try:
global option
if len(sys.argv) == 3:
option = sys.argv[1]
file_name = sys.argv[2]
elif len(sys.argv) == 2:
option = None
file_name= sys.argv[1]
else:
print(len(sys.argv))
f = open(file_name)
l = f.read()
except:
print("There is no such file")
parser.parse(l)
#dfs.print_st()