编译原理词法分析器,ll1,lr0,python实现代码

上传人:第*** 文档编号:61711750 上传时间:2018-12-10 格式:DOCX 页数:29 大小:280.69KB
返回 下载 相关 举报
编译原理词法分析器,ll1,lr0,python实现代码_第1页
第1页 / 共29页
编译原理词法分析器,ll1,lr0,python实现代码_第2页
第2页 / 共29页
编译原理词法分析器,ll1,lr0,python实现代码_第3页
第3页 / 共29页
编译原理词法分析器,ll1,lr0,python实现代码_第4页
第4页 / 共29页
编译原理词法分析器,ll1,lr0,python实现代码_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《编译原理词法分析器,ll1,lr0,python实现代码》由会员分享,可在线阅读,更多相关《编译原理词法分析器,ll1,lr0,python实现代码(29页珍藏版)》请在金锄头文库上搜索。

1、计算机科学与通信工程学院编译原理实验报告题目: 1.词法分析器2. LL(1)分析器 3. LR(0)分析器班级: 姓名: 学号: 指导老师: 2017年 月 目录一、实验题目1二、实验目的和要求1三、代码实现2四、总结27 一、 实验题目1. 词法分析器分析一段程序代码,将代码中的单词符号分解出来,并对其进行检查,输出token表和error表2. LL(1)文法分析器分析给定文法。求出文法的FIRST集,FOLLOW集,并构建分析表,对给定输入串进行分析。3. LR(0)文法分析器分析给定文法。用_CLOSURE方法构造文法的LR(0)项目集规范族,根据状态转换函数GO构造出文法的DFA,

2、并转换为分析表,对给定输入串进行分析。二、 实验目的和要求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, caseSEPARATOR_LIST = ;, :, , (, ), , , , OPERATOR_LIST1 = +, -, *OPE

3、RATOR_LIST2 = =, , =CATEGORY_DICT = # KEYWORD while: while: , if: if: , else: else: , switch: switch: , case: case: , # OPERATOR +: +: , -: -: , *: *: , =: relop: LE, =: relop: GE, : relop: GT, =: relop: EQ, =: =: , # SEPARATOR ;: ;: , : : , ,: ,: , (: (: , ): ): , : : , : : , : : , : : ,CONSTANTTAB

4、LE = 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(temp_str) except IOError as e: print(e) exit() finally: file

5、.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 character# LINK TOKENdef concatenation(): global token globa

6、l 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 in KEYWORD_LIST: return CATEGORY_DICTtoken else: return 0# R

7、ETRACTdef 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 digit(): concatenation() location = location + 1 character = a

8、ll_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() location = location + 1 character = all_stringlocation ret

9、ract() 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 + 1 character = all_stringlocation if character = =: OPERATOR

10、TABLE.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) print(这是分隔符:, CATEGORY_DICTs) else: location += 1 UNDEFINED

11、TABLE.append(s) print(error:undefined identity :, s, )if _name_ = _main_: character = token = all_string = read_file(program.txt, r) location = 0 while location + 1 len(all_string): main() print(KEYWORDTABLE:, KEYWORDTABLE) print(TOKENTABLE:, TOKENTABLE) print(CONSTANTTABLE:, CONSTANTTABLE) print(OPERATORTABLE:

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号