语义分析算法 python_语义分析:C语言表达式的语法树生成——Python实现

1 '''

2 ____________________________Syntax & Syntax Tree3 Parenthesis:4 ["(",None]5 [")",None]6 Operators(grouped by precedence):7 Unary :8 1 + - ! ~ ["+",None] ["-",None] ["!",None] ["~",None]9 Binary :10 2 * / % ["*",None] ["/",None] ["%",None]11 3 + - ["+",None] ["-",None]12 4 << >> ["<<",None] [">>",None]13 5 > >= < <= [">",None] [">=",None] ["<",None] ["<=",None]14 6 == != ["==",None] ["!=",None]15 7 & ["&",None]16 8 ^ ["^",None]17 9 | ["|",None]18 10 && ["&&",None]19 11 || ["||",None]20 Ternary :21 12 expr ? expr : expr ["?",None] [":",None] ["@expr","?:",listPtr0,listPtr1,listPtr2]22 13 expr , expr , expr...23 Var,Num,Expr,Function:24 ["@var","varName"]25 ["@num","num_string"]26 ["@expr","Operator",listPtr,...]27 ["@func","funcName",listPtr1,...]28 ["@expr_list",["@var"|"@num"|"@expr"|"@func",...],...]29 '''

30

31 ######################################## global list

32 OperatorList=['+','-','!','~',\33 '*','/','%',\34 '+','-',\35 '<<','>>',\36 '>','>=','<','<=',\37 '==','!=',\38 '&',\39 '^',\40 '|',\41 '&&',\42 '||',\43 '?',':'\44 ',']45 '''31 + 8 * 9'''

46 listToParse=[ ['@num','31'] , ['+',None] , ['@num','8'] , ['*',None] , ['@num','9'] ]47

48 ########### return value :

49 ############# 0 parsed some expresions

50 ############# 1 done nothing but no errors happened

51 ################# + =: ^+A... | ...Op+A...

52 defmodule_1_0(lis,i):53

54 #left i right are both indexes :)

55 left=i-1

56 right=i+1

57

58 #process: ^+A...

59 if i==0 and len(lis)>=2:60 if lis[right][0][0]=='@':61 rightPtr=lis[right]62 del lis[0:2]63 lis.insert(0,["@expr","+",rightPtr])64 return065 #process: ...Op+A...

66 if i>=1 and len(lis)>=3 and right

74 return 1

75

76 ########### return value :

77 ############# 0 parsed some expresions

78 ############# 1 done nothing but no errors happened

79 ################# - =: ^-A... | ...Op-A...

80 defmodule_1_1(lis,i):81

82 #left i right are both indexes :)

83 left=i-1

84 right=i+1

85

86 #process: ^-A...

87 if i==0 and len(lis)>=2:88 if lis[right][0][0]=='@':89 rightPtr=lis[right]90 del lis[0:2]91 lis.insert(0,["@expr","-",rightPtr])92 return093 #process: ...Op-A...

94 if i>=1 and len(lis)>=3 and right

102 return 1

103

104 ########### return value :

105 ############# 0 parsed some expresions

106 ############# 1 done nothing but no errors happened

107 ################# ! =: ...!A...

108 defmodule_1_2(lis,i):109

110 #left i right are both indexes :)

111 left=i-1

112 right=i+1

113

114 #process: ...!A...

115 if len(lis)>=2 and right

122 return 1

123

124 ########### return value :

125 ############# 0 parsed some expresions

126 ############# 1 done nothing but no errors happened

127 ################# ~ =: ...~A...

128 defmodule_1_3(lis,i):129

130 #left i right are both indexes :)

131 left=i-1

132 right=i+1

133

134 #process: ...~A...

135 if len(lis)>=2 and right

142 return 1

143

144 ########### return value :

145 ############# 0 parsed some expresions

146 ############# 1 done nothing but no errors happened

147 ################# * =: ...A*A...

148 defmodule_2_0(lis,i):149

150 #left i right are both indexes :)

151 left=i-1

152 right=i+1

153

154 #process: ...A*A...

155 if i>=1 and len(lis)>=3 and right

163 return 1

164

165 ########### return value :

166 ############# 0 parsed some expresions

167 ############# 1 done nothing but no errors happened

168 ################# / =: ...A/A...

169 defmodule_2_1(lis,i):170

171 #left i right are both indexes :)

172 left=i-1

173 right=i+1

174

175 #process: ...A/A...

176 if i>=1 and len(lis)>=3 and right

184 return 1

185

186 ########### return value :

187 ############# 0 parsed some expresions

188 ############# 1 done nothing but no errors happened

189 ################# % =: ...A%A...

190 defmodule_2_2(lis,i):191

192 #left i right are both indexes :)

193 left=i-1

194 right=i+1

195

196 #process: ...A%A...

197 if i>=1 and len(lis)>=3 and right

205 return 1

206

207 ########### return value :

208 ############# 0 parsed some expresions

209 ############# 1 done nothing but no errors happened

210 ################# + =: ...A+A...

211 defmodule_3_0(lis,i):212

213 #left i right are both indexes :)

214 left=i-1

215 right=i+1

216

217 #process: ...A+A...

218 if i>=1 and len(lis)>=3 and right

226 return 1

227

228 ########### return value :

229 ############# 0 parsed some expresions

230 ############# 1 done nothing but no errors happened

231 ################# - =: ...A-A...

232 defmodule_3_1(lis,i):233

234 #left i right are both indexes :)

235 left=i-1

236 right=i+1

237

238 #process: ...A-A...

239 if i>=1 and len(lis)>=3 and right

247 return 1

248

249 ########### return value :

250 ############# 0 parsed some expresions

251 ############# 1 done nothing but no errors happened

252 ################# << =: ...A<

253 defmodule_4_0(lis,i):254

255 #left i right are both indexes :)

256 left=i-1

257 right=i+1

258

259 #process: ...A<

260 if i>=1 and len(lis)>=3 and right

268 return 1

269

270 ########### return value :

271 ############# 0 parsed some expresions

272 ############# 1 done nothing but no errors happened

273 ################# >> =: ...A>>A...

274 defmodule_4_1(lis,i):275

276 #left i right are both indexes :)

277 left=i-1

278 right=i+1

279

280 #process: ...A>>A...

281 if i>=1 and len(lis)>=3 and right>",leftPtr,rightPtr])287 return0288

289 return 1

290

291 ########### return value :

292 ############# 0 parsed some expresions

293 ############# 1 done nothing but no errors happened

294 ################# > =: ...A>A...

295 defmodule_5_0(lis,i):296

297 #left i right are both indexes :)

298 left=i-1

299 right=i+1

300

301 #process: ...A>A...

302 if i>=1 and len(lis)>=3 and right",leftPtr,rightPtr])308 return0309

310 return 1

311

312 ########### return value :

313 ############# 0 parsed some expresions

314 ############# 1 done nothing but no errors happened

315 ################# >= =: ...A>=A...

316 defmodule_5_1(lis,i):317

318 #left i right are both indexes :)

319 left=i-1

320 right=i+1

321

322 #process: ...A>=A...

323 if i>=1 and len(lis)>=3 and right=",leftPtr,rightPtr])329 return0330

331 return 1

332

333 ########### return value :

334 ############# 0 parsed some expresions

335 ############# 1 done nothing but no errors happened

336 ################# < =: ...A

337 defmodule_5_2(lis,i):338

339 #left i right are both indexes :)

340 left=i-1

341 right=i+1

342

343 #process: ...A

344 if i>=1 and len(lis)>=3 and right

352 return 1

353

354 ########### return value :

355 ############# 0 parsed some expresions

356 ############# 1 done nothing but no errors happened

357 ################# <= =: ...A<=A...

358 defmodule_5_3(lis,i):359

360 #left i right are both indexes :)

361 left=i-1

362 right=i+1

363

364 #process: ...A<=A...

365 if i>=1 and len(lis)>=3 and right

373 return 1

374

375 ########### return value :

376 ############# 0 parsed some expresions

377 ############# 1 done nothing but no errors happened

378 ################# == =: ...A==A...

379 defmodule_6_0(lis,i):380

381 #left i right are both indexes :)

382 left=i-1

383 right=i+1

384

385 #process: ...A==A...

386 if i>=1 and len(lis)>=3 and right

394 return 1

395

396 ########### return value :

397 ############# 0 parsed some expresions

398 ############# 1 done nothing but no errors happened

399 ################# != =: ...A!=A...

400 defmodule_6_1(lis,i):401

402 #left i right are both indexes :)

403 left=i-1

404 right=i+1

405

406 #process: ...A!=A...

407 if i>=1 and len(lis)>=3 and right

415 return 1

416

417 ########### return value :

418 ############# 0 parsed some expresions

419 ############# 1 done nothing but no errors happened

420 ################# & =: ...A&A...

421 defmodule_7_0(lis,i):422

423 #left i right are both indexes :)

424 left=i-1

425 right=i+1

426

427 #process: ...A&A...

428 if i>=1 and len(lis)>=3 and right

436 return 1

437

438 ########### return value :

439 ############# 0 parsed some expresions

440 ############# 1 done nothing but no errors happened

441 ################# ^ =: ...A^A...

442 defmodule_8_0(lis,i):443

444 #left i right are both indexes :)

445 left=i-1

446 right=i+1

447

448 #process: ...A^A...

449 if i>=1 and len(lis)>=3 and right

457 return 1

458

459 ########### return value :

460 ############# 0 parsed some expresions

461 ############# 1 done nothing but no errors happened

462 ################# | =: ...A|A...

463 defmodule_9_0(lis,i):464

465 #left i right are both indexes :)

466 left=i-1

467 right=i+1

468

469 #process: ...A|A...

470 if i>=1 and len(lis)>=3 and right

478 return 1

479

480 ########### return value :

481 ############# 0 parsed some expresions

482 ############# 1 done nothing but no errors happened

483 ################# && =: ...A&&A...

484 defmodule_10_0(lis,i):485

486 #left i right are both indexes :)

487 left=i-1

488 right=i+1

489

490 #process: ...A&&A...

491 if i>=1 and len(lis)>=3 and right

499 return 1

500

501 ########### return value :

502 ############# 0 parsed some expresions

503 ############# 1 done nothing but no errors happened

504 ################# || =: ...A||A...

505 defmodule_11_0(lis,i):506

507 #left i right are both indexes :)

508 left=i-1

509 right=i+1

510

511 #process: ...A||A...

512 if i>=1 and len(lis)>=3 and right

520 return 1

521

522 ########### return value :

523 ############# 0 parsed some expresions

524 ############# 1 done nothing but no errors happened

525 ################# ?: =: ...A?A:A...

526 ################# ^

527 defmodule_12_0(lis,i):528

529 #left i right are both indexes :)

530 first=i-3

531 leftOp=i-2

532 left=i-1

533 right=i+1

534

535 #process: ...A?A:A...

536 #^

537 if i>=3 and len(lis)>=5 and right

547 return 1

548

549 ########### return value :

550 ############# 0 parsed some expresions

551 ############# 1 done nothing but no errors happened

552 ################# , =: A,A,...A,A

553 defmodule_13_0(lis,i):554

555 #process: A,A,...A,A

556 if len(lis)==1 and lis[0][0][0]!='@':557 return 1

558 if len(lis)==1 and lis[0][0][0]=='@':559 return0560 if (len(lis)%2)==1:561 i=1

562 if lis[0][0][0]!='@':563 return 1

564 while i

567 else:568 return 1

569 ls=[['@expr_list']]570 i=0571 while i

574 dellis[:]575 lis[:]=ls[:]576 return0577 return 1

578

579 ######################################## global list

580 #construct a module dictionary

581 #module_dic_tuple[priority]['Operator'](lis,i)

582 module_dic_tuple=({}, { '+':module_1_0,'-':module_1_1,'!':module_1_2,'~':module_1_3 },\583 { '*':module_2_0,'/':module_2_1,'%':module_2_2 }, \584 { '+':module_3_0,'-':module_3_1 },\585 { '<<':module_4_0,'>>':module_4_1 },\586 { '>':module_5_0,'>=':module_5_1,'<':module_5_2,'<=':module_5_3 },\587 { '==':module_6_0,'!=':module_6_1 },\588 { '&':module_7_0 },\589 { '^':module_8_0 },\590 { '|':module_9_0 },\591 { '&&':module_10_0 },\592 { '||':module_11_0 },\593 { '?:':module_12_0 },\594 { ',':module_13_0 } )595

596 operator_priority_tuple=( () , ('+', '-', '!', '~') , ('*','/','%'),\597 ('+','-'),('<<','>>'),\598 ('>','>=','<','<='),('==','!='),\599 ('&'),('^'),('|'),('&&'),('||'),('?',':'),(',') )600

601 ############################# parse:unary,binary,ternary,comma expr

602 ########### return value :

603 ############# 0 parsed sucessfully

604 ############# 1 syntax error

605 defparse_simple_expr(lis):606 if len(lis)==0:607 return 1

608 #if lis[len(lis)-1][0][0]!='@':

609 #return 1

610 #if lis[0][0][0]!='@' and lis[0][0] not in ('+','-','!','~'):

611 #return 1

612 for pri in range(1,12): #pri 1,2,3,4,5,6,7,8,9,10,11

613 i=0614 while 1:615 if len(lis)==1 and lis[0][0][0]=='@':616 return0617 if i>=len(lis):618 break

619 if lis[i][0] inoperator_priority_tuple[pri]:620 if module_dic_tuple[pri][lis[i][0]](lis,i)==0:621 i=0622 continue

623 else:624 i=i+1

625 continue

626 else:627 i=i+1

628 for pri in range(12,13): #pri 12 # parse ...A?A:A...

629 i=0630 while 1:631 if len(lis)==1 and lis[0][0][0]=='@':632 return0633 if i>=len(lis):634 break

635 if lis[i][0]==':':636 if module_dic_tuple[pri]['?:'](lis,i)==0:637 i=0638 continue

639 else:640 i=i+1

641 continue

642 else:643 i=i+1

644 return module_dic_tuple[13][','](lis,0)645 return 1

646

647 ########### return value :[intStatusCode,indexOf'(',indexOf')']

648 ############# intStatusCode

649 ############# 0 sucessfully

650 ############# 1 no parenthesis matched

651 ############# 2 list is null :(

652 defmodule_parenthesis_place(lis):653 length=len(lis)654 err=0655 x=0656 y=0657 if length==0:658 return [2,None,None]659 try:660 x=lis.index([")",None])661 except:662 err=1

663 lis.reverse()664 try:665 y=lis.index(["(",None],length-x-1)666 except:667 err=1

668 lis.reverse()669 y=length-y-1

670 if err==1:671 return [1,None,None]672 else:673 return[0,y,x]674

675

676 ############################# parse:unary binary ternary prenthesis function expr

677 ########### return value :

678 ############# 0 parsed sucessfully

679 ############# 1 syntax error

680 ############################# find first ')'

681 defparse_comp_expr(lis):682 while 1:683 if len(lis)==0:684 return 1

685 if len(lis)==1:686 if lis[0][0][0]=='@':687 return0688 else:689 return 1

690 place=module_parenthesis_place(lis)691 if place[0]==0:692 mirror=lis[(place[1]+1):place[2]]693 if parse_simple_expr(mirror)==0:694 if place[1]>=1 and lis[place[1]-1][0]=='@var':695 '''func'''

696 funcName=lis[place[1]-1][1]697 del lis[place[1]-1:(place[2]+1)]698 lis.insert(place[1]-1,["@func",funcName,mirror[0]])699 else:700 del lis[place[1]:(place[2]+1)]701 lis.insert(place[1],mirror[0])702 else:703 return 1

704 else:705 returnparse_simple_expr(lis)706 return 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值