資源簡介
用python開發的小型語法分析器,用處不大,但可以剛學習編譯原理要做實驗的小同學參考
代碼片段和文件信息
import?State
fo?=?open(“MC.txt“?“r“)
#token元組#
token?=?[]
state?=?[]
State.init()
#=============================Main======================================#
line?=?fo.read(1)
#讀取每一句#
while?line?!=?‘‘:
????if?line?==?‘?‘?or?line?==?‘\n‘?or?line?==?‘\t‘:#空白狀態處理
????????line?=?fo.read(1)
????elif?line?==?‘(‘?or?line?==?‘)‘?or?line?==?‘{‘?or?line?==?‘}‘?or?line?==?‘;‘?or?line?==?‘‘:#判斷括號狀態
????????token.append(line)
????????state.append(‘Brackets‘)
????????line?=?fo.read(1)
????elif?line?==?‘+‘?or?line?==?‘-‘?or?line?==?‘*‘?or?line?==?‘/‘?or?line?==?‘%‘?or?line?==?‘=‘?or?line?==?‘|‘?or?line?==?‘&‘?or?line?==?‘^‘?or?line?==?‘!‘:
????????token.append(line)
????????state.append(‘Operators‘)
????????line?=?fo.read(1)
????elif?(line?>=?‘a‘?and?line?<=?‘z‘)?or?(line?>=?‘A‘?and?line?<=?‘Z‘)?or?line?==?‘_‘:#字母狀態處理
????????line?=?State.alphabetState(line?fo?token?state)
????elif?(line?>=??‘0‘?and?line?<=?‘9‘):#數字狀態片理
????????line?=?State.numberState(line?fo?token?state)
????else:#錯誤狀態
????????print(“ERROR“)
????????fo.close()#關閉文件
????????break
#讀取每一句#
for?i?in?range(0?len(token)):
????print(token[i]?+?‘\t‘?+?state[i])
#=============================Main======================================#
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????398453??2014-11-20?08:14??CompilerTheory\chapter01.pptx
?????文件?????334930??2014-11-20?08:14??CompilerTheory\chapter02.pptx
?????文件????1107456??2014-11-20?08:14??CompilerTheory\chapter03a.ppt
?????文件????1324544??2014-11-20?08:14??CompilerTheory\chapter03b.ppt
?????文件????1165824??2014-11-20?08:14??CompilerTheory\chapter03c.ppt
?????文件????1007616??2014-11-20?08:14??CompilerTheory\chapter04a.ppt
?????文件?????????48??2014-11-20?11:35??CompilerTheory\Parsing\Parsing\init.txt
?????文件????????231??2014-11-20?11:34??CompilerTheory\Parsing\Parsing\MC.txt
?????文件???????1292??2014-11-20?11:39??CompilerTheory\Parsing\Parsing\Parsing.py
?????文件???????2091??2014-11-20?11:38??CompilerTheory\Parsing\Parsing\Parsing.pyproj
?????文件???????1052??2014-11-20?11:39??CompilerTheory\Parsing\Parsing\State.py
?????文件???????1298??2014-11-20?11:39??CompilerTheory\Parsing\Parsing\__pycache__\State.cpython-34.pyc
?????文件????????826??2014-11-20?08:08??CompilerTheory\Parsing\Parsing.sln
????..A..H.?????22016??2014-11-20?11:39??CompilerTheory\Parsing\Parsing.v12.suo
?????文件?????129173??2014-11-20?08:14??CompilerTheory\實驗一詞法分析程序.docx
?????文件????1588946??2014-10-15?22:15??CompilerTheory\詞法分析器示例\示例P1.jpg
?????文件????1651616??2014-10-15?22:28??CompilerTheory\詞法分析器示例\示例P2.jpg
?????文件????1506133??2014-10-15?22:17??CompilerTheory\詞法分析器示例\示例P3.jpg
?????文件????1434227??2014-10-15?22:12??CompilerTheory\詞法分析器示例\示例P4.jpg
?????文件????6087478??2014-11-20?08:14??CompilerTheory\詞法分析器示例.rar
?????目錄??????????0??2014-11-20?11:39??CompilerTheory\Parsing\Parsing\__pycache__
?????目錄??????????0??2014-11-20?11:39??CompilerTheory\Parsing\Parsing
?????目錄??????????0??2014-11-20?08:08??CompilerTheory\Parsing
?????目錄??????????0??2014-11-20?08:15??CompilerTheory\詞法分析器示例
?????目錄??????????0??2014-11-20?11:39??CompilerTheory
-----------?---------??----------?-----??----
?????????????17765250????????????????????25
評論
共有 條評論