![編譯原理詞法分析器,ll1,lr0,python實現代碼_第1頁](http://file4.renrendoc.com/view/2f460875a357d64199843dfe55c89239/2f460875a357d64199843dfe55c892391.gif)
![編譯原理詞法分析器,ll1,lr0,python實現代碼_第2頁](http://file4.renrendoc.com/view/2f460875a357d64199843dfe55c89239/2f460875a357d64199843dfe55c892392.gif)
![編譯原理詞法分析器,ll1,lr0,python實現代碼_第3頁](http://file4.renrendoc.com/view/2f460875a357d64199843dfe55c89239/2f460875a357d64199843dfe55c892393.gif)
![編譯原理詞法分析器,ll1,lr0,python實現代碼_第4頁](http://file4.renrendoc.com/view/2f460875a357d64199843dfe55c89239/2f460875a357d64199843dfe55c892394.gif)
![編譯原理詞法分析器,ll1,lr0,python實現代碼_第5頁](http://file4.renrendoc.com/view/2f460875a357d64199843dfe55c89239/2f460875a357d64199843dfe55c892395.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、-. z.計算機科學與通信工程學院編譯原理實驗報告題目: 1.詞法分析器2. LL(1)分析器 3. LR(0)分析器班級:: *:指導教師:2017年月-. z.-. z.目錄 TOC o 1-3 h z u HYPERLINK l _Toc483942693一、實驗題目 PAGEREF _Toc483942693 h 1HYPERLINK l _Toc483942694二、實驗目的和要求 PAGEREF _Toc483942694 h 1HYPERLINK l _Toc483942695三、代碼實現 PAGEREF _Toc483942695 h 1HYPERLINK l _Toc4839
2、42696四、總結 PAGEREF _Toc483942696 h 1-. z.-. z.實驗題目詞法分析器分析一段程序代碼,將代碼中的單詞符號分解出來,并對其進展檢查,輸出token表和error表LL(1)文法分析器分析給定文法。求出文法的FIRST集,FOLLOW集,并構建分析表,對給定輸入串進展分析。LR(0)文法分析器分析給定文法。用_CLOSURE方法構造文法的LR(0)工程集規(guī)*族,根據狀態(tài)轉換函數GO構造出文法的DFA,并轉換為分析表,對給定輸入串進展分析。實驗目的和要求學會詞法分析器的實現思路。學會求解FIRST集, FOLLOW集,構造LL(1)分析表。學會_CLOSURE
3、方法,狀態(tài)轉換函數GO, 構造LR(0)分析表。代碼實現詞法分析器program.t*t 中存放要分析的文法:E-TRR-+TR|-TR|T-FGG-*FG|/FG|F-(E)|i:KEYWORD_LIST = while, if, else, switch, caseSEPARATOR_LIST = ;, :, , (, ), , , , OPERATOR_LIST1 = +, -, *OPERATOR_LIST2 = =, , =CATEGORY_DICT = # KEYWORD while: while: , if: if: , else: else: , switch: switch:
4、 , case: case: , # OPERATOR +: +: , -: -: , *: *: , =: relop: LE, =: relop: GE, : relop: GT, =: relop: EQ, =: =: , # SEPARATOR ;: ;: , : : , ,: ,: , (: (: , ): ): , : : , : : , : : , : : ,CONSTANTTABLE = TOKENTABLE = OPERATORTABLE = KEYWORDTABLE = SEPARATORTABLE = UNDEFINEDTABLE = # READ FILEdef rea
5、d_file(path, method): temp_str = try: file = open(path, method) for line in file: line = line.replace(n, ) temp_str += line temp_str = str(temp_str) e*cept IOError as e: print(e) e*it() finally: file.close() return temp_str.strip() + # GETBEdef getbe(): global token getchar() token = return# GETCHAR
6、def getchar(): global character global location while all_stringlocation = : location = location + 1 character = all_stringlocation return character# LINK TOKENdef concatenation(): global token global character token = token + character# IS NUMBERdef digit(): if 0 = character = 9: return True return
7、 False# IS ALPHABETdef letter(): if A = character = Z or a = character = z: return True return False# IS IDENTIFIERdef reserve(): if token in KEYWORD_LIST: return CATEGORY_DICTtoken else: return 0# RETRACTdef retract(): global location global character # location = location - 1 character = return# M
8、AIN FUNCTIONdef main(): global token global character global location s = getchar() getbe() if a = s = z or A = s = Z: while letter() or digit(): concatenation() location = location + 1 character = all_stringlocation retract() c = reserve() if c = 0: TOKENTABLE.append(token) print(這是標識符:, token, :,
9、TOKENTABLE.inde*(token), ) else: KEYWORDTABLE.append(token) print(這是保存字:, CATEGORY_DICTtoken) elif 0 = s = 9: while digit(): concatenation() location = location + 1 character = all_stringlocation retract() CONSTANTTABLE.append(token) print(這是常數:, token, :, CONSTANTTABLE.inde*(token), ) elif s in OPE
10、RATOR_LIST1: location = location + 1 OPERATORTABLE.append(s) print(這是單操作符:, CATEGORY_DICTs) elif s in OPERATOR_LIST2: location = location + 1 character = all_stringlocation if character = =: OPERATORTABLE.append(s + character) print(這是雙操作符:, CATEGORY_DICTs + character) else: retract() location = loc
11、ation + 1 OPERATORTABLE.append(s) print(這是單操作符:, CATEGORY_DICTs) elif s in SEPARATOR_LIST: location = location + 1 SEPARATORTABLE.append(s) print(這是分隔符:, CATEGORY_DICTs) else: location += 1 UNDEFINEDTABLE.append(s) print(error:undefined identity :, s, )if _name_ = _main_: character = token = all_str
12、ing = read_file(program.t*t, r) location = 0 while location + 1 TRR-+TR|-TR|T-FGG-*FG|/FG|F-(E)|i輸入串:i+i*i代碼:NonTermSet = set() # 非終結符集合TermSet = set() # 終結符集合First = # First集Follow = # Follow集GramaDict = # 處理過的產生式Code = # 讀入的產生式AnalysisList = # 分析表StartSym = # 開場符號EndSym = # # 完畢符號為#Epsilon = # 由于沒
13、有epsilon符號用代替# 構造First集def getFirst(): global NonTermSet, TermSet, First, Follow, FirstA for * in NonTermSet: First* = set() # 初始化非終結符First集為空for * in TermSet: First* = set(*) # 初始化終結符First集為自己Change = True while Change: # 當First集沒有更新則算法完畢Change = False for * in NonTermSet: for Y in GramaDict*: k =
14、0 Continue = True while Continue and k len(Y): if not FirstYk - set(Epsilon) 0: # Y1到Yi候選式都有存在Continue = False else: First* |= FirstYk - set(Epsilon) Change = True if Epsilon not in FirstYk: Continue = False k += 1 if Continue: # *-或者Y1到Yk均有產生式First* |= set(Epsilon) # FirstAY |= set(Epsilon)# 構造Foll
15、ow集def getFollow(): global NonTermSet, TermSet, First, Follow, StartSym for A in NonTermSet: FollowA = set() FollowStartSym.add(EndSym) # 將完畢符號參加Follow開場符號中Change = True while Change: # 當Follow集沒有更新算法完畢Change = False for * in NonTermSet: for Y in GramaDict*: for i in range(len(Y): if Yiin TermSet: c
16、ontinue Flag = True for j in range(i + 1, len(Y): # continue if not FirstYj - set(Epsilon) = FollowYi: FollowYi |= FirstYj - set(Epsilon) # 步驟2 FIRST()/ 參加到FOLLOW(B)中。Change = True if Epsilon not in FirstYj: Flag = False break if Flag: if not Follow* ,把FOLLOW(A)加到FOLLOW(B)中FollowYi |= Follow* Change
17、 = True# 構造分析表def getAnalysisList(): for non* in NonTermSet: AnalysisListnon* = dict() row = AnalysisListnon* flag = True for Y in GramaDictnon*: for term in TermSet: if term in FirstY0 and term in Firstnon*: rowterm = non*+-+Y if Epsilon in Firstnon* and flag: flag = False for tmp in Follownon*: ro
18、wtmp = non*+-+Epsilon print(分析表:) for non* in NonTermSet: print( , non*, AnalysisListnon*)# 查詢分析表def findAnalysisList(non, ter): try: tmp = AnalysisListnonter *, Y = tmp.split(-) e*cept E*ception as e: print(find error ) # MA,a為空,發(fā)現語法錯誤print(e) pass return Y# 顯示格式def display(show_list): for item in
19、show_list: print( %-25s % item, end=) print()# LL(1)分析器def analyzer(): head = Stack, StackTop, NowStr, InputStr, Action # inputStr = i+i*i + EndSym inputStr = input(請輸入表達式:) + EndSym print(分析過程:) display(head) stack = location = 0 stack.append(EndSym) stack.append(StartSym) stack_top = stack.pop() w
20、hile stack_top != EndSym and location ,只將A彈出mess = 彈出棧頂符號 + stack_top + 因M + stack_top + , + inputStrlocation + 中為 + stack_top mess = mess + -,故不壓棧 else: # MA,a中的產生式右部符號串按逆序逐一壓入棧中mess = 彈出棧頂符號 + stack_top + ,將M + stack_top + , + inputStr location + 中的 + stack_top + - + result + 的 + result mess = mes
21、s + 逆序壓棧 tmp_list = for char in result: tmp_list.append(char) tmp_list.reverse() stack.e*tend(tmp_list) display(stack, stack_top, inputStrlocation, inputStrlocation + 1: len(inputStr), mess) stack_top = stack.pop() else: break if stack_top = EndSym and inputStrlocation = EndSym: # * = a = # 分析成功,分析器
22、停頓工作display(, #, #, , 匹配,分析成功) print() print(*) print(* Analysis Success *) print(*) else: print(Analysis Error)# 讀取文法defreadGrammar(): try: file = open(grammar.t*t, r) for line in file: line = line.replace(n, ) Code.append(line) e*cept IOError as e: print(e) e*it() finally: file.close() return Code
23、# 初始化def init(): global NonTermSet, TermSet, First, Follow, StartSym, Code Code = readGrammar() n = int(len(Code) print(產生式個數:, n) StartSym = Code00 print(開場符號:, StartSym) print(產生式:G, StartSym, :) for i in range(n): *, Y = Codei.split(-) print( , Codei) NonTermSet.add(*) Y = Y.split(|) for Yi in Y:
24、 TermSet |= set(Yi) if * not in GramaDict: GramaDict* = set() GramaDict* |= set(Y) # 生成產生式集TermSet -= NonTermSet print(非終結符:, NonTermSet) print(終結符:, TermSet) getFirst() getFollow() print(FIRST集:) for k in NonTermSet: print( FIRST, k, : , Firstk) print(FOLLOW集:) for k, v in Follow.items(): print( FO
25、LLOW, k, : , v) TermSet -= set(Epsilon) TermSet |= set(EndSym) getAnalysisList() analyzer()init()運行結果:LR(0)分析器program.t*t 中存放要分析的文法:*-SS-BBB-aBB-b輸入串:abab代碼:VN = # 非終結符VT = # 終結符NFA = # NFA表DFA = # DFA表grammar = # 讀入的文法doted_grammar = # 加點后的文法VN2Int = # 非終結符映射VT2Int = # 終結符映射action = # action表goto =
26、 # goto表DFA_node = # DFA節(jié)點表status_stack = # 狀態(tài)棧symbol_stack = # 符號棧now_state = # 棧頂狀態(tài)input_ch = # 棧頂字符input_str = # 輸入串location = 0 # 輸入位置now_step = 0 # 當前步驟# 讀取文法def read_grammar(file_name): global grammar with open(file_name, r) as file: for line in file: line = line.replace(n, ) grammar.append(li
27、ne) file.close()# 找到終結符和非終結符def find_term_non(): global grammar n = int(len(grammar) temp_vt = l = 0 for i in range(n): *, Y = grammari.split(-) if * not in VN: VN.append(*) VN2Int.update(*: l) l += 1 for Yi in Y: temp_vt.append(Yi) m = 0 for i in temp_vt: if i not in VN and i not in VT: VT.append(i
28、) VT2Int.update(i: m) m += 1 VT.append(#) VT2Int.update(#: m)# 在字符串*個位置加點def add_char2str(grammar_i, i): grammar_i = grammar_i0:i + . + grammar_ii:len(grammar_i) return grammar_i# 給文法加點def add_dot(): global doted_grammar j = 0 n = 0 for i in grammar: for k in range(len(i) - 2): doted_grammar.append(
29、) doted_grammarn.append(add_char2str(i, k + 3) doted_grammarn.append(false) n += 1 j += 1# 顯示加點后的文法def print_doted_grammar(): print(加點后的文法) j = 1for i in doted_grammar: print(%d.%s % (j, i0) j += 1# 顯示讀入文法def print_read_grammar(): print(讀入的文法) j = 0 for i in grammar: print(%d)%s % (j, i) j += 1# 初始化
30、NFAdef init_NFA(): global NFA for row in range(len(doted_grammar): NFA.append() for col in range(len(doted_grammar): NFArow.append()# 找到點的位置def find_pos_point(one_grammar): return one_grammar.find(.)# 文法是否以start開頭,以.開場defis_start(grammar_i, start): if grammar_i0.find(start, 0, 1) + grammar_i0.find(.
31、, 3, 4) = 3: return True else: return False# 查找以start開頭,以.開場的文法,返回個數def find_node(start, grammar_id): num = 0 for i in doted_grammar: if is_start(i, start): grammar_idnum = doted_grammar.inde*(i) num += 1 return num# 構造NFAdef make_NFA(): global NFA grammar_id = for i in range(len(doted_grammar): gra
32、mmar_id.append() init_NFA() i = 0 for grammar_i in doted_grammar: pos_point = find_pos_point(grammar_i0) # 找到點的位置if not pos_point + 1 = len(grammar_i0): NFAii + 1 = grammar_i0pos_point + 1 if grammar_i0pos_point + 1 in VN: # 點后面跟著非終結符j = find_node(grammar_i0pos_point + 1, grammar_id) for k in range(
33、j): NFAigrammar_idk = * add_more(i, grammar_idk) i += 1# 查找關聯(lián)def add_more(i, j): global NFA grammar_id = for k in range(len(doted_grammar): grammar_id.append() pos_point = find_pos_point(doted_grammarj0) if not pos_point + 1 = len(doted_grammarj0): if doted_grammarj0pos_point + 1 in VN: j = find_nod
34、e(doted_grammarj0pos_point + 1, grammar_id) for k in range(j): NFAigrammar_idk = * add_more(i, grammar_idk)# 顯示NFAdef print_NFA(): global NFA, doted_grammar print(NFA鄰接矩陣) print(end= ) for i in range(len(doted_grammar): print(%4d % (i + 1), end=) print() for i in range(len(doted_grammar): print(, en
35、d=) print() for i in range(len(doted_grammar): print(%3d| % (i + 1), end=) for j in range(len(doted_grammar): print(%4s % NFAij, end=) print() for l in range(len(doted_grammar): print(, end=) print()# 初始化DFAdef init_DFA(): global DFA for row in range(len(doted_grammar): DFA.append() for col in range
36、(len(doted_grammar): DFArow.append()# 連接def add_state(to, fro): for i in range(len(doted_grammar): if not NFAtoi = and not NFAtoi = *: DFAtoi = NFAtoi if not NFAfroi = and not NFAfroi = *: # from可連接的點DFAtoi = NFAfroi# 構造DFAdef make_DFA(): global NFA, doted_grammar, DFA_node init_DFA() for i in range
37、(len(doted_grammar): DFA_node.append() for j in range(len(doted_grammar): DFA_nodei.append() for i in range(len(doted_grammar): if doted_grammari1 = false: k = 0 DFA_nodeik = doted_grammari0 k += 1 doted_grammari1 = true for j in range(len(doted_grammar): if NFAij = *: # 有弧DFA_nodeik = doted_grammar
38、j0 k += 1 doted_grammarj1 = true add_state(i, j)# 顯示DFAdef print_DFA(): global DFA, doted_grammar print(DFA鄰接矩陣) print(end= ) for i in range(len(doted_grammar): print(%4d % (i + 1), end=) print() for i in range(len(doted_grammar): print(, end=) print() for i in range(len(doted_grammar): print(%3d| %
39、 (i + 1), end=) for j in range(len(doted_grammar): print(%4s % DFAij, end=) print() for l in range(len(doted_grammar): print(, end=) print()# 初始化LR分析表def init_LR_table(): global doted_grammar, action, goto for i in range(len(doted_grammar): action.append() goto.append() for j in range(len(VT): actio
40、ni.append() for j in range(len(VN): gotoi.append(-1)# 有無規(guī)約項def need_protocol(point): global DFA_node if not DFA_nodepoint0 = : for i in range(len(doted_grammar): if DFA_nodepointi.endswith(.): return DFA_nodepointi else: return None else: return None# 根據文法內容找到文法編號def find_grammar(string): global gra
41、mmar tmp = string0: len(string) - 1 for i in range(len(grammar): if tmp = grammari: return i# 填充LR分析表def fill_LR_table(): global doted_grammar, VT2Int, VN2Int, VN init_LR_table() for i in range(len(doted_grammar): if need_protocol(i): num = find_grammar(need_protocol(i) tmp = r + str(num) for j in r
42、ange(len(VT): if i = 1: actioniVT2Int# = acc else: actionij = tmp else: for j in range(len(doted_grammar): if not DFAij = : if DFAij in VN: gotoiVN2Int.get(DFAij, -1) = j else: tmp = s + str(j) actioniVT2Int.get(DFAij, -1) = tmp# 顯示LR分析表def print_LR_table(): global VT, VN, doted_grammar, action, got
43、o # 表頭print(LR分析表) print(tt|t, end=) print(%3s % ) * (len(VT) - 2), end=) print(Action, end=) print(%3s % ) * (len(VT) - 2), end=) print(t|t, end=) print(%3s % ) * (len(VN) - 2), end=) print(GOTO, end=) print(%3s % ) * (len(VN) - 2), end=) print(t|) print(ttt, end=) for i in VT: print(%3st % i, end=
44、) print(t|t, end=) k = 0 for i in VN: if not k = 0: print(%3st % i, end=) k += 1 print(t|) for i in range(len(doted_grammar): print(, end=) print() # 表體for i in range(len(doted_grammar): print(%5dt|t % i, end=) for j in range(len(VT): print(%4s % actionij, end=) print(t|t, end=) for j in range(len(V
45、N): if not j = 0: if not gotoij = -1: print(%4s % gotoij, end=) else: print(t, end=) print(t|) for i in range(len(doted_grammar): print(, end=) print()# 判斷分析是否完成def is_end(): if input_strlocation:len(input_str) = #: if symbol_stack-1 = * and symbol_stack-2 = #: return True else: return False else: r
46、eturn False# 讀入輸入串def read_input(): global input_str input_str = input(輸入串:) + # 輸出def output(): global now_step, status_stack, symbol_stack, input_str, now_state print(%dtt % now_step, end=) now_step += 1 print(%-20s % status_stack, end=) print(%-25s % symbol_stack, end=) print(%-22s % input_strloc
47、ation:len(input_str), end=)# 統(tǒng)計產生式右部的個數def count_right_num(grammar_i): return len(grammar_i) - 3# 規(guī)約def do_stipulations(): global status_stack, input_str, symbol_stack, location, now_state, input_ch print(分析過程) print(步驟tt, end=) print(%-17s % 狀態(tài)棧, end=) print(%-22s % 符號棧, end=) print(%-20s % 輸入串, end=) print(動作說明) for i in range(len(doted_grammar): print(, end=) print() symbol_sta
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- S-3-Keto-sphinganine-d18-0-hydrochloride-生命科學試劑-MCE-1677
- CP-LC-0729-生命科學試劑-MCE-3226
- Actinobolin-plus-Actinobolin-生命科學試劑-MCE-7644
- 3-4-Methylenedioxy-pyrovalerone-metabolite-2-hydrochloride-生命科學試劑-MCE-1172
- 2025年度國際貿易違約風險預防與處理合同
- 2025年度范文正式版合同文員崗位職責規(guī)范與職業(yè)素養(yǎng)培養(yǎng)協(xié)議
- 二零二五年度2025年競業(yè)禁止及保密協(xié)議模板
- 2025年度風力發(fā)電場租賃定金協(xié)議模板
- 2025年度籃球聯(lián)賽裁判員免責聲明及賽事執(zhí)行合同
- 二零二五年度自媒體合伙人合同版:自媒體平臺內容創(chuàng)作與推廣合同
- 2024-2025學年成都高新區(qū)七上數學期末考試試卷【含答案】
- 企業(yè)員工食堂管理制度框架
- 電力溝施工組織設計-電纜溝
- 《法律援助》課件
- 2024年山東鐵投集團招聘筆試參考題庫含答案解析
- XX區(qū)XXX灌區(qū)水資源論證報告書
- 新教材教科版五年級下冊科學全冊課時練(課后作業(yè)設計)(含答案)
- 電廠鋼結構施工方案(53頁)
- 7.5正態(tài)分布課件(共26張PPT)
- 水體國產載體固化微生物
- 七年級語文課外閱讀理解練習
評論
0/150
提交評論