資源簡介
編譯原理的表達式計算器作業,上機實驗題,用的是python寫的

代碼片段和文件信息
import?re
str=open(“2.txt“).readlines()
r=re.compile(r‘[0-9|+|\-|*|/|.|(|)]‘)?#正則匹配合法字符
j=0
def?judge(ab):????#判斷是無符號整數還是浮點數
????if(little==0):
????????print(“(5{0}{1}int)“.format(wordword))
????else:
????????print(“(8{0}{1}double)“.format(wordword))
for?s?in?str:
????s=s.lstrip()??#清除字符串前面的空格
????
????j+=1
????if?j!=len(str):
???????s=s[:-1]????????#讀文件操作
????print(“\n“)?
????print(“待分析的字符為:“)
????print(s)
????print(“分析的結果為:“)
????little=0?????????????#?是否為小數狀態?0表示整數?1表示浮點數
????word=‘‘??????????????#?用字符串存數字
????index=0?????????????#?計算當前遍歷字符串的下標
????error=0???????????#?記錄錯誤數
????Error=[]????????????????#?存放錯誤信息
????for?i?in?s:
????????index+=1
????????if(r.match(i)):
????????????d=re.compile(r‘[0-9]|\.‘)???????#?匹配是否為數字或小數點
????????????if(d.match(i)):??????????????????#
????????????????word+=i
????????????????if(i==‘.‘):??????????????????#?若是小數點則把little改為1
????????????????????little=1
????????????????????
????????????if(i==‘+‘):
????????????????if(word!=‘‘):?????????????????#?如果word有存儲數字則輸出數字
????????????????????judge(wordlittle)???????#
????????????????????word=‘‘??????????????????#?輸出數字后把dig和little改為初始化
????????????????????little=0
????????????????print(‘(1+NULLNULL)‘)
????????????????
????????????if(i==‘-‘):
????????????????if(word!=‘‘):
????????????????????judge(wordlittle)
????????????????????word=‘‘
????????????????????little=0
????????????????print(‘(2-NULLNULL)‘)
????????????????
????????????if(i==‘*‘):
????????????????if(word!=‘‘):
????????????????????judge(wordlittle)
????????????????????word=‘‘
????????????????????little=0
????????????????print(‘(3*NULLNULL)‘)
????????????????
????????????if(i==‘/‘):
????????????????if(word!=‘‘):
????????????????????judge(wordlittle)
????????????????????word=‘‘
????????????????????little=0
????????????????print(‘(4/NULLNULL)‘)
????????????????
????????????if(i==‘(‘):
????????????????if(word!=‘‘):
????????????????????judge(wordlittle)
????????????????????word=‘‘
????????????????????little=0
????????????????print(‘(6(NULLNULL)‘)
????????????????
????????????if(i==‘)‘):
????????????????if(word!=‘‘):
????????????????????judge(wordlittle)
????????????????????word=‘‘
????????????????????little=0
????????????????print(‘(7)NULLNULL)‘)
????????????if(i==‘?‘):???????????????????????#?若遍歷到空格則跳過???
????????????????continue
????????????if(index==len(s)?and?word!=‘‘):????#?判斷若數字結尾則輸出數字
????????????????judge(wordlittle)
????????????????
????????else:
????????????Error.append(“Error:第{0}個字符存在非法字符‘{1}‘“.format(indexi))????#?發現非法字符則把錯誤信息存入
????????????error+=1????????????????????????????????????????????????????#?error錯誤數增加
????if?error!=0:????????????????????????????????????????????????????????#?按error的數進行遍歷輸出t中存儲的錯誤信息
????????print(‘ERROR‘)
????????for?i?in?range(error):
????????????print(Error[i])
????????print(‘‘)
????else:
????????print(‘‘)
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-01-12?22:04??計算器表達式\
?????文件????????3372??2019-01-12?22:02??計算器表達式\bianyi.py
?????文件???????16471??2019-01-12?22:04??計算器表達式\編譯原理.docx
- 上一篇:python數據分析.docx
- 下一篇:python dpkt 包分析庫
評論
共有 條評論