-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyufa+yuyi.py
542 lines (537 loc) · 19.5 KB
/
yufa+yuyi.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
class yufa:
def __init__(self, signal,signalname,table,command):
self.signal = signal
self.signalname = signalname
self.table = table
self.command = []
def getaddress(self):
return len(self.command)
def gettopname(self):
return self.signalname[0]
def gettop(self):
return self.signal[0]
def gettop2(self):
return self.signal[1]
def pop(self):
del self.signal[0]
del self.signalname[0]
def lookup(self,name):
if name in table:
return 1
else:
return 0
def S(self):
self.HEAD()
if(self.gettop2() != '134'):
self.VS()
self.MAIN()
print("success!")
def HEAD(self):
if(self.gettop() == '133'):
self.pop()
if(self.gettop() == '207'):
self.pop()
if(self.gettop()== '301'):
self.pop()
if(self.gettop()== '208'):
self.pop()
else:
print("error,缺省> HEAD")
else:
print("error,缺省库名 HEAD")
else:
print("error,缺省< HEAD")
exit(0)
self.HEAD()
elif(self.gettop()=='113' or self.gettop()=='109' or self.gettop()=='117'):
pass
else:
print("error, HEAD")
exit(0)
print("HEAD!")
def VS(self):
ding = self.VS_A()
name = ding[0]
attri = ding[1]
self.VS_B(name,attri)
if(self.gettop()=='117' and self.gettop2()=='134'):
pass
elif(self.gettop()=='117' or self.gettop()=='113' or self.gettop()=='109'):
self.VS()
else:
print("error,缺少返回值类型 VS")
exit(0)
print("VS!")
def VS_A(self):
t = self.TYPE()
if(self.gettop() == '301'):
id = self.gettopname()
self.pop()
return (id,t)
else:
print("error,变量缺省 VS_A")
exit(0)
def VS_B(self,name,attri):
if(self.gettop()=='212'):
self.VS_C()
else:
self.VAR_DE_(name,attri)
def VS_C(self):
self.pop()
self.F_PA()
if(self.gettop()=='213'):
self.pop()
self.VS_D()
else:
print("error,期望')' VS_C")
exit(0)
def VS_D(self):
if(self.gettop()=='221'):
self.pop()
elif(self.gettop()=='216'):
self.pop()
self.FUN_B()
if(self.gettop()=='217'):
self.pop()
else:
print("error,期望'}' VS_D")
exit(0)
else:
print("error,期望';' VS_D")
exit(0)
def FUN_B(self):
self.VAR_DE()
self.STATE()
print("函数主体!")
def MAIN(self):
if(self.gettop()=='117'):
self.pop()
if(self.gettop()=='134'):
self.pop()
if(self.gettop()=='216'):
self.pop()
self.FUN_B()
if(self.gettop()=='217'):
self.pop()
else:
print("error,主函数缺省'}' MAIN")
exit(0)
else:
print("error,主函数缺省'{' MAIN")
exit(0)
else:
print("error,期望'mian' MAIN")
exit(0)
else:
print("error,主函数缺少返回值类型'int' MAIN")
exit(0)
print("mian!")
def F_PA(self):
self.TYPE()
if(self.gettop()=='301'):
self.pop()
if(self.gettop()=='212'):
self.pop()
self.F_PA_()
if(self.gettop()=='213'):
self.pop()
else:
print("error,期望')' F_PA")
exit(0)
else:
print("error,期望'(' F_PA")
exit(0)
else:
print("error,函数名错误或缺省 F_PA")
exit(0)
def F_PA_(self):
if(self.gettop()=='221'):
self.pop()
self.F_PA()
elif(self.gettop()=='213'):
pass
else:
print("error,参数表错误 F_PA_")
exit(0)
def VAR_DE(self):
if(self.gettop()=='117' or self.gettop()=='113' or self.gettop()=='109'):
attri = self.gettopname() #属性满足不为空
self.TYPE()
if(self.gettop()=='301'):
name = self.gettopname() #变量满足不为空
if(self.lookup(name)):
print("此变量已存在")
exit(0)
else:
self.table[name] = [attri,'null']
self.pop()
self.VAR_DE_(name,attri)
self.VAR_DE()
else:
print("error,变量定义错误 VAR_DE")
exit(0)
elif(self.gettop()=='301'or self.gettop()=='114' or self.gettop()=='132' or self.gettop()=='116' or self.gettop()=='216' or self.gettop()=='217' or self.gettop()=='120'):
pass
else:
print("error,变量定义错误 VAR_DE")
exit(0)
print("变量定义!")
def VAR_DE_(self,name,attri):
if(self.gettop()=='221'):
self.pop()
elif(self.gettop()=='205'):
self.pop()
self.OB(name,attri)
else:
print("error,期望';'或变量初始化 VAR_DE_")
exit(0)
def STATE(self):
if(self.gettop()=='217'):
pass
else:
self.B_STATE()
self.STATE()
def B_STATE(self):
if(self.gettop()=='301'):
if(self.gettop2()=='205'):
self.A_S()
elif(self.gettop2()=='212'):
self.FU_S()
else:
print("error,期望变量赋值或函数调用 B_STATE")
exit(0)
elif(self.gettop()=='132' or self.gettop()=='108' or self.gettop()=='114'):
self.CY_S()
elif(self.gettop()=='120'):
self.RE_S()
elif(self.gettop()=='116'):
self.IF_S()
elif(self.gettop()=='216'):
self.MUL_S()
else:
print("error,B_STATE")
exit(0)
def A_S(self):
if(self.gettop()=='301'):
name = self.gettopname()
self.pop()
if(self.gettop()=='205'):
self.pop()
result = self.OP_S()
if(result[0] != '_' and result[1] != '_'):
if(self.table[result[0]][0] == 'double' or self.table[result[1]][0] == 'double' or self.table[name][0] == 'double'):
self.table[name] = ['double',self.table[result[0]][1]+self.table[result[1]][1]]
elif(self.table[result[0]][0] == 'float' or self.table[result[1]][0] == 'float' or self.table[name][0] == 'float'):
self.table[name] = ['float',self.table[result[0]][1]+self.table[result[1]][1]]
else:
self.table[name] = ['int',self.table[result[0]][1]+self.table[result[1]][1]]
else:
if(self.table[name][0] < self.table[result[2][0]]):
self.table[name] = [self.table[name][0],self.table[result[2]][0]]
else:
self.table[name] = [self.table[result[2]][0],self.table[result[2]][0]]
self.command[self.getaddress()] = [ '=',result[2],'_',name]
if(self.gettop()=='221'):
self.pop()
else:
print("error,期望';'")
exit(0)
else:
print("error,缺省赋值号 A_S")
exit(0)
else:
print("error,缺省被赋值变量 A_S")
exit(0)
print("赋值语句!")
def OP_S(self):
name1 = self.OB()
if(not self.lookup(name1)):
print("该变量不存在: OP_S")
print(name1)
exit(0)
result = self.OP_S_(name1)
return result
def OP_S_(self,name1):
if(self.gettop()=='201' or self.gettop()=='202' or self.gettop()=='203'or self.gettop()=='204'or self.gettop()=='209'):
operator = self.OP()
name2 = self.OB()
if(not self.lookup(name2)):
print("此变量不存在:OP_S_")
print(name2)
exit(0)
self.command[self.getaddress()] = [operator,name1,name2,'temp1']
return (name1,name2,'temp')
elif(self.gettop()=='213' or self.gettop()=='221'):
return ('_','_',name1)
else:
print("error! 期待操作符或者;")
def OB(self,name = 'null',attri = 'null'):
if(self.gettop()=='301' or self.gettop()=='401'):
assin = self.gettopname()
if(name != 'null'):
if(self.lookup(assin)):
if(attri == table[assin][0]):
table[name][1] = table[assin][1]
else:
print("赋值双方对象类型不匹配!OB")
exit(0)
else:
print("赋值变量不存在 OB")
print(assin)
exit(0)
self.pop()
return assin
else:
print("error,期望'var'或'const' OB()")
exit(0)
def OP(self):
if(self.gettop()=='201' or self.gettop()=='202' or self.gettop()=='203'or self.gettop()=='204'or self.gettop()=='209'):
operator = self.gettopname()
self.pop()
return operator
else:
print("error,缺省算术运算符 OP()")
exit(0)
def RE_S(self):
if(self.gettop()=='120'):
self.pop()
self.OB()
else:
print("error,缺省'return' RE_S()")
exit(0)
print("return 语句")
def FU_S(self):
if(self.gettop()=='301'):
self.pop()
if(self.gettop()=='212'):
self.pop()
self.PA()
if(self.gettop()=="213"):
self.pop()
else:
print("error,期望')' FU_S")
exit(0)
else:
print("error,期望'(' FU_S")
exit(0)
else:
print("error,函数名错误 FU_S")
exit(0)
print("函数调用!")
def PA(self):
if(self.gettop()=='213'):
pass
elif(self.gettop()=='301' or self.gettop()=='401'):
self.OB()
self.PA_()
else:
print("error,(函数调用)参数错误 PA")
exit(0)
def PA_(self):
if(self.gettop()=='213'):
pass
elif(self.gettop()=='220'):
self.pop()
self.PA()
else:
print("error,(函数调用)参数错误 PA_")
exit(0)
def MUL_S(self):
if(self.gettop()=='216'):
self.pop()
self.FUN_B()
if(self.gettop()=='217'):
self.pop()
else:
print("error,期望'}' MUL_S")
exit(0)
return self.getaddress()
else:
print("error,期望'{' MUL_S")
exit(0)
def CY_S(self):
if(self.gettop()=='132'):
self.pop()
if(self.gettop()=='212'):
self.pop()
address = self.CON()
if(self.gettop()=='213'):
self.pop()
processaddress = self.MUL_S()
self.command[address[1]-1][3] = processaddress
else:
print("error,期望')' CY_S")
exit(0)
else:
print("error,期望'(' CY_S")
exit(0)
elif(self.gettop()=='108'):
self.pop()
topaddress = self.getaddress()
processaddress = self.MUL_S()
if(self.gettop()=='132'):
self.pop()
if(self.gettop()=='212'):
self.pop()
address = self.CON()
if(self.gettop()=='213'):
self.pop()
self.command[address[1]-2][3] = topaddress
self.command[address[1]-1][3] = processaddress+2
else:
print("error,期望')' CY_S")
exit(0)
else:
print("error,期望'(' CY_S")
exit(0)
else:
print("error")
exit(0)
elif(self.gettop()=='114'):
self.pop()
if(self.gettop()=='212'):
self.pop()
self.FOR1()
topaddress = self.getaddress()
address = self.CON()
conditionpart = self.command[address[0]:address[1]]
if(self.gettop()=='221'):
self.pop()
for3begin = self.FOR3()
for3end = self.getaddress()
if(for3begin != 'null'):
for3part = self.command[for3begin:for3end]
else:
for3part = []
if(self.gettop()=='213'):
self.pop()
mul_begin = self.getaddress()
mul_end = self.MUL_S()
mul_part = self.command[mul_begin:mul_end]
self.command = self.command[0:address[0]]
truestart = self.getaddress()
self.command = self.command + mul_part + for3part + conditionpart
self.command[self.getaddress()-1][3] = self.getaddress()
self.command[self.getaddress()-2][3] = truestart
else:
print("error,期望')' CY_S")
exit(0)
else:
print("error,期望';' CY_S")
exit(0)
else:
print("error,期望'(' CY_S")
exit(0)
else:
print("error,未识别到循环语句开始符 CY_S")
exit(0)
print("循环体!")
def FOR1(self):
if(self.gettop() == '221'):
pass
elif(self.gettop()=='301'):
self.A_S()
else:
print("error,for语句初始化部分错误 FOR1")
exit(0)
def FOR3(self):
if(self.gettop()=='213'):
return 'null'
elif(self.gettop()=='301'):
for3beginaddress = self.getaddress()
name = self.gettopname()
self.pop()
if(self.gettop()=='205'):
self.pop()
result = self.OP_S()
self.command[self.getaddress()] = ['=',result[2],'_',name]
return for3beginaddress
else:
print("error,期望'='")
exit(0)
else:
print("error,for语句迭代错误 FOR3")
exit(0)
def IF_S(self):
if(self.gettop()=='116'):
self.pop()
if(self.gettop()=='212'):
self.pop()
address = self.CON()
if(self.gettop()=='213'):
self.pop()
processaddress = self.MUL_S()
processaddress = self.IF_S_()
self.command[address[1]-1][3] = processaddress
else:
print("error,期望')' IF_S")
exit(0)
else:
print("error,期望'(' IF_S")
exit(0)
else:
print("error,期望'if' IF_S")
exit(0)
print("IF 语句!")
def IF_S_(self):
if(self.gettop()=='110'):
elseaddress = self.getaddress()
self.pop()
self.MUL_S()
return elseaddress
elif(self.gettop()=='301'or self.gettop()=='114'or self.gettop()=='132'or self.gettop()=='116'or self.gettop()=='217'or self.gettop()=='216'or self.gettop()=='120'or self.gettop()=='108'):
return self.getaddress()
else:
print("error,if语句错误 IF_S_")
exit(0)
def CON(self):
if(self.gettop()=='211'):
self.gettopname()
self.pop()
if(self.gettop()=='301'):
name = self.gettopname()
self.pop()
ifaddress = self.getaddress()
self.command[self.getaddress()] = ['!',name,'_','temp1']
self.command[self.getaddress()] = ['jud','temp1','_',(self.getaddress()+2)]
self.command[self.getaddress()] = ['ju','_','_','somewherefalse']
iffalseaddress = self.getaddress()
return (ifaddress,iffalseaddress)
else:
print("error,期望'var' CON")
exit(0)
elif(self.gettop()=='301' or self.gettop()=='401'):
name1 = self.OB()
result = self.CON_(name1)
return result
else:
print("error,条件错误 CON")
exit(0)
def CON_(self,name1):
if(self.gettop()=='213'):
ifaddress = self.getaddress()
self.command[ifaddress] = ['jud',name1,'_',(ifaddress+2)]
self.command[ifaddress+1] = ['ju','_','_','somewherefalse']
falseaddress = self.getaddress()
return (ifaddress,falseaddress)
elif(self.gettop()=='207' or self.gettop() == '225'or self.gettop()== '208'or self.gettop()=='226'or self.gettop()== '227'):
operator = self.gettopname()
self.pop()
name2 = self.OB()
ifaddress = self.getaddress()
self.command[ifaddress] = [operator,name1,name2,(ifaddress+2)]
self.command[ifaddress+1] = ['ju','_','_','somewherefalse']
falseaddress = self.getaddress()
return(ifaddress,falseaddress)
else:
print(self.signal[0])
print("error,条件错误 CON_")
exit(0)
def TYPE(self):
if(self.gettop()=='113'or self.gettop()=='117' or self.gettop()=='109'):
t = self.gettopname()
self.pop()
return t
else:
print("error,无此变量类型 TYPE")
exit(0)