编译原理词法分析器,ll1,lr0,python实现代码.doc
《编译原理词法分析器,ll1,lr0,python实现代码.doc》由会员分享,可在线阅读,更多相关《编译原理词法分析器,ll1,lr0,python实现代码.doc(27页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流编译原理词法分析器,ll1,lr0,python实现代码.精品文档.计算机科学与通信工程学院编译原理实验报告题目: 1.词法分析器2. LL(1)分析器 3. LR(0)分析器班级: 姓名: 学号: 指导老师: 2017年 月 目录一、实验题目1二、实验目的和要求1三、代码实现2四、总结27一、 实验题目1. 词法分析器分析一段程序代码,将代码中的单词符号分解出来,并对其进行检查,输出token表和error表2. LL(1)文法分析器分析给定文法。求出文法的FIRST集,FOLLOW集,并构建分析表,对给定输入串进行分析。3. LR(0)文
2、法分析器分析给定文法。用_CLOSURE方法构造文法的LR(0)项目集规范族,根据状态转换函数GO构造出文法的DFA,并转换为分析表,对给定输入串进行分析。二、 实验目的和要求1. 学会词法分析器的实现思路。2. 学会求解FIRST集, FOLLOW集,构造LL(1)分析表。3. 学会_CLOSURE方法, 状态转换函数GO, 构造LR(0)分析表。三、 代码实现1. 词法分析器program.txt 中存放要分析的文法:E-TRR-+TR|-TR|T-FGG-*FG|/FG|F-(E)|i代码:KEYWORD_LIST = while, if, else, switch, caseSEPAR
3、ATOR_LIST = ;, :, , (, ), , , , OPERATOR_LIST1 = +, -, *OPERATOR_LIST2 = =, , =CATEGORY_DICT = # KEYWORD while: while: , if: if: , else: else: , switch: switch: , case: case: , # OPERATOR +: +: , -: -: , *: *: , =: relop: LE, =: relop: GE, : relop: GT, =: relop: EQ, =: =: , # SEPARATOR ;: ;: , : : ,
4、 ,: ,: , (: (: , ): ): , : : , : : , : : , : : ,CONSTANTTABLE = TOKENTABLE = OPERATORTABLE = KEYWORDTABLE = SEPARATORTABLE = UNDEFINEDTABLE = # READ FILEdef read_file(path, method): temp_str = try: file = open(path, method) for line in file: line = line.replace(n, ) temp_str += line temp_str = str(t
5、emp_str) except IOError as e: print(e) exit() finally: file.close() return temp_str.strip() + # GETBEdef getbe(): global token getchar() token = return# GETCHARdef getchar(): global character global location while all_stringlocation = : location = location + 1 character = all_stringlocation return c
6、haracter# LINK TOKENdef concatenation(): global token global character token = token + character# IS NUMBERdef digit(): if 0 = character = 9: return True return False# IS ALPHABETdef letter(): if A = character = Z or a = character = z: return True return False# IS IDENTIFIERdef reserve(): if token i
7、n KEYWORD_LIST: return CATEGORY_DICTtoken else: return 0# RETRACTdef retract(): global location global character # location = location - 1 character = return# MAIN FUNCTIONdef main(): global token global character global location s = getchar() getbe() if a = s = z or A = s = Z: while letter() or dig
8、it(): concatenation() location = location + 1 character = all_stringlocation retract() c = reserve() if c = 0: TOKENTABLE.append(token) print(这是标识符:, token, :, TOKENTABLE.index(token), ) else: KEYWORDTABLE.append(token) print(这是保留字:, CATEGORY_DICTtoken) elif 0 = s = 9: while digit(): concatenation()
9、 location = location + 1 character = all_stringlocation retract() CONSTANTTABLE.append(token) print(这是常数:, token, :, CONSTANTTABLE.index(token), ) elif s in OPERATOR_LIST1: location = location + 1 OPERATORTABLE.append(s) print(这是单操作符:, CATEGORY_DICTs) elif s in OPERATOR_LIST2: location = location +
10、1 character = all_stringlocation if character = =: OPERATORTABLE.append(s + character) print(这是双操作符:, CATEGORY_DICTs + character) else: retract() location = location + 1 OPERATORTABLE.append(s) print(这是单操作符:, CATEGORY_DICTs) elif s in SEPARATOR_LIST: location = location + 1 SEPARATORTABLE.append(s)
11、print(这是分隔符:, CATEGORY_DICTs) else: location += 1 UNDEFINEDTABLE.append(s) print(error:undefined identity :, s, )if _name_ = _main_: character = token = all_string = read_file(program.txt, r) location = 0 while location + 1 TRR-+TR|-TR|T-FGG-*FG|/FG|F-(E)|i输入串:i+i*i代码:NonTermSet = set() # 非终结符集合Term
12、Set = set() # 终结符集合First = # First集Follow = # Follow集GramaDict = # 处理过的产生式Code = # 读入的产生式AnalysisList = # 分析表StartSym = # 开始符号EndSym = # # 结束符号为“#“Epsilon = # 由于没有epsilon符号用“”代替# 构造First集def getFirst(): global NonTermSet, TermSet, First, Follow, FirstA for X in NonTermSet: FirstX = set() # 初始化非终结符Fi
13、rst集为空 for X in TermSet: FirstX = set(X) # 初始化终结符First集为自己 Change = True while Change: # 当First集没有更新则算法结束 Change = False for X in NonTermSet: for Y in GramaDictX: k = 0 Continue = True while Continue and k len(Y): if not FirstYk - set(Epsilon) 0: # Y1到Yi候选式都有存在 Continue = False else: FirstX |= First
14、Yk - set(Epsilon) Change = True if Epsilon not in FirstYk: Continue = False k += 1 if Continue: # X-或者Y1到Yk均有产生式 FirstX |= set(Epsilon) # FirstAY |= set(Epsilon)# 构造Follow集def getFollow(): global NonTermSet, TermSet, First, Follow, StartSym for A in NonTermSet: FollowA = set() FollowStartSym.add(End
15、Sym) # 将结束符号加入Follow开始符号中 Change = True while Change: # 当Follow集没有更新算法结束 Change = False for X in NonTermSet: for Y in GramaDictX: for i in range(len(Y): if Yi in TermSet: continue Flag = True for j in range(i + 1, len(Y): # continue if not FirstYj - set(Epsilon) = FollowYi: FollowYi |= FirstYj - set
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 词法 分析器 ll1 lr0 python 实现 代码
限制150内