基于Python实现语法分析
1. 需求分析
使用 LR(1)法进行语法分析。
根据 C 语言的文法生成 action 表和 goto 表,利用 action 表和 goto 表对词法分析的输出进行语法分析,构建语法树。
2. 文法设计
要求:给出如下语言成分的文法描述。
- 声明语句(包括变量声明、数组声明、记录声明和过程声明)
Defination -> Type Point id
Type -> int | float | bool | id | double | Type [ const ]
Point -> epsilon | *Struct -> struct id { Statement }
Statement -> Defination ; | Defination ; Statement
Function -> Type Point id ( Parameter ) { Process }
Parameter -> epsilon | Defination | Defination , Parameter
Process -> Module Return
Return -> return id Index ; | return Index ; | epsilon
- 表达式及赋值语句(包括数组元素的引用和赋值)
Assignment -> id Index = Value
Value -> Value + Value | Value - Value | Value * Value | Value / Value | Call
Value -> - Value
Value -> ( Value )
Value -> const | id Index
Index -> [ Value ] Index | epsilon
- 分支语句:if_then_else
Control -> If | If_Else | While
If -> if ( Condition ) { Module }
If_Else -> if ( Condition ) { Module } else { Module }
- 循环语句:do_while
While -> while ( Condition ) { Module }
Condition -> Condition and Condition | Condition or Condition | not Condition | ( Condition ) | Value Relop Value | true | false
Relop -> < | <= | == | != | > | >=
- 过程调用语句
Call -> id ( Transmit )
Transmit -> epsilon | Value | Value , Transmit
3. 系统设计
要求:分为系统概要设计和系统详细设计。
(1) 系统概要设计:给出必要的系统宏观层面设计图,如系统框架图、数据流图、功能模块结构图等以及相应的文字说明。
程序主要由几部分组成
- 词法分析结果加载器
- action 表和 goto 表加载器
- 语法分析器
- 语法树打印器
(2) 系统详细设计:对如下工作进行展开描述
- 核心数据结构的设计
用于储存词法分析的结果,保存词法分析后的值、种类和行数。
- 主要功能函数说明
- 程序核心部分的程序流程图
4. 系统实现及结果分析
要求:对如下内容展开描述。
- 系统实现过程中遇到的问题;
- 输出该句法分析器的分析表;
附件 action.txt 与 goto.txt
- 针对一测试程序输出其句法分析结果;
测试程序
输出结果:
Module (1)Function (1)Type (1)int (1)Point (1)id : sum (1)( (1)Parameter (1)Defination (1)Type (1)float (1)Point (1)id : a (1), (1)Parameter (1)Defination (1)Type (1)int (1)Point (1)id : b (1)) (1){ (1)Process (2)Module (2)Defination (2)Type (2)float (2)Point (2)id : ans (2); (2)Module (3)Assignment (3)id : ans (3)Index (3)= (3)Value (3)Value (3)id : a (3)Index (3)+ (3)Value (3)id : b (3)Index (3); (3)Module (3)Return (4)return (4)id : ans (4)Index (4); (4)} (5)Module (7)Struct (7)struct (7)id : student (7){ (7)Statement (8)Defination (8)Type (8)int (8)Point (8)id : x (8); (8)Statement (9)Defination (9)Type (9)int (9)Point (9)id : y (9); (9)} (10)Module (12)Defination (12)Type (12)int (12)Point (12)id : a (12); (12)Module (13)Defination (13)Type (13)Type (13)int (13)[ (13)const : 3 (13)] (13)Point (13)id : b (13); (13)Module (14)Defination (14)Type (14)int (14)Point (14)id : c (14); (14)Module (15)Defination (15)Type (15)int (15)Point (15)id : f (15); (15)Module (16)Defination (16)Type (16)int (16)Point (16)* (16)id : k (16); (16)Module (18)Assignment (18)id : a (18)Index (18)= (18)Value (18)const : 1 (18); (18)Module (19)Assignment (19)id : b (19)Index (19)[ (19)Value (19)const : 0 (19)] (19)Index (19)= (19)Value (19)const : 1 (19); (19)Module (20)Assignment (20)id : c (20)Index (20)= (20)Value (20)Value (20)id : a (20)Index (20)+ (20)Value (20)id : b (20)Index (20)[ (20)Value (20)const : 0 (20)] (20)Index (20); (20)Module (21)Assignment (21)id : f (21)Index (21)= (21)Value (21)Call (21)id : sum (21)( (21)Transmit (21)Value (21)Value (21)id : a (21)Index (21)+ (21)Value (21)id : b (21)Index (21)[ (21)Value (21)const : 0 (21)] (21)Index (21)) (21); (21)Module (23)Control (23)IfElse (23)if (23)( (23)Condition (23)Value (23)id : a (23)Index (23)Relop (23)== (23)Value (23)const : 0 (23)) (23){ (23)Module (24)Assignment (24)id : b (24)Index (24)[ (24)Value (24)const : 1 (24)] (24)Index (24)= (24)Value (24)const : 1 (24); (24)Module (24)} (25)else (25){ (25)Module (26)Assignment (26)id : b (26)Index (26)[ (26)Value (26)const : 1 (26)] (26)Index (26)= (26)Value (26)const : 0 (26); (26)Module (26)} (27)Module (28)Assignment (28)id : a (28)Index (28)= (28)Value (28)const : 1 (28); (28)Module (30)Control (30)While (30)while (30)( (30)Condition (30)Value (30)id : f (30)Index (30)Relop (30)< (30)Value (30)const : 4 (30)) (30){ (30)Module (31)Assignment (31)id : b (31)Index (31)[ (31)Value (31)const : 2 (31)] (31)Index (31)= (31)Value (31)Value (31)id : b (31)Index (31)[ (31)Value (31)const : 2 (31)] (31)Index (31)+ (31)Value (31)const : 1 (31); (31)Module (31)} (32)Module (30)
- 输出针对此测试程序对应的语法错误报告;
第 28 行出现错误,进行报错
Syntax error at Line [28]:illegal ==
- 对实验结果进行分析。
实验结果正确无误
const : 1 (31)
; (31)
Module (31)
} (32)
Module (30)
- 输出针对此测试程序对应的语法错误报告;第 28 行出现错误,进行报错```c++
Syntax error at Line [28]:illegal ==
- 对实验结果进行分析。
实验结果正确无误
基于Python实现语法分析相关推荐
- 基于python的语料库数据处理电子版_基于 Python 自然语言处理工具包在语料库研究中的运用...
基于 Python 自然语言处理工具包在语料库研究中的运用 刘 旭 [摘 要] 摘要:国内当前以语料库为基础的研究,在研究工具方面,多以 AntConc . PowerGREP 为主,使用 Pytho ...
- 【CV】OpenCV(基于Python)学习笔记
以下内容中的页码均来自<OpenCV 4详解 : 基于Python> 目录 第2章 载入.显示与保存数据 2.2 图像的读取与显示 2.2.1 图像读取函数 cv.imread() 2.2 ...
- 基于Python的人工智能美颜系统
基于Python的人工智能美颜系统使用PyQt5模块搭建可视化界面,使用Dlib模型(shape_predictor_68_face_landmarks.dat)实现人脸关键点检测和定位,人脸美颜(美 ...
- python深度神经网络量化_基于Python建立深度神经网络!你学会了嘛?
原标题:基于Python建立深度神经网络!你学会了嘛? 图1 神经网络构造的例子(符号说明:上标[l]表示与第l层:上标(i)表示第i个例子:下标i表示矢量第i项) 单层神经网络 图2 单层神经网络示 ...
- python selenium脚本_怎样开始写第一个基于python的selenium脚本
1.下载并安装python(http://www.python.org/geti/). 2.安装selenium(http://pypi.python.org/pypi/selenium)下载并解压缩 ...
- 【组队学习】【31期】基于Python的办公自动化
基于Python的办公自动化 航路开辟者:牧小熊.刘雯静.张晓东.吴争光.隆军 领航员:六一 航海士:牧小熊.李显.刘羽中.王晓亮 基本信息 开源内容:https://github.com/dataw ...
- 【组队学习】曹志宾:基于Python的会员数据化运营
分享人:曹志宾,Datawhale成员,香港科技大学硕士在读 分享内容: 案例描述与分析 前期准备与数据预处理 RFM模型使用与操作 Excel中的RFM分析 组队学习: 红星:基于Python的会员 ...
- 【组队学习】孙健坤:基于Python的会员数据化运营
分享人:孙健坤,哈尔滨工业大学 分享内容: 什么是会员制? 什么是会员数据化运营? 如何进行会员数据化运营 组队学习: 基于Python的会员数据化运营 开源内容: https://github.co ...
- 【组队学习】【28期】基于Python的会员数据化运营
基于Python的会员数据化运营 论坛版块: http://datawhale.club/c/team-learning/37-category/37 开源内容: https://github.com ...
最新文章
- 在.Net Micro Framework中显示汉字
- 课后作业-阅读任务-阅读提问-3 。
- [Linux]lnmp一键安装包,访问yii/tp5/laravel的时候,报错500或者空白页面
- fluentftp 积极拒绝_【新时代文明实践】光明街道光大社区开展“光盘行动 拒绝浪费 从我做起”新时代文明实践活动...
- 进入Ubuntu图形桌面的方法
- Linux CentOS下如何确认MySQL服务已经启动
- 【简洁代码】1053 住房空置率 (20分)_22行代码AC
- H.264视频编解码的代码移植和优化
- 07.MyBatis中的关联查询
- ios微内核和鸿蒙微内核,华为发布基于微内核、面向全场景的分布式操作系统:鸿蒙OS...
- java webservice 客户端_Java Webservice客户端(最佳方法)
- thinkphp [数据分页]
- 3.1 scrapy框架 -- 安装与基本使用
- Eclipse常用的一些设置
- 音乐自动播放html代码大全,网页HTML音乐播放器代码大全
- 前端工程师如何快速的Mac装机?学会这些技巧让你的装机速度提升至少1倍!!!
- Windows系统主机加固
- ElasticSearch(8)-分布式搜索
- 面试官问我 “String 的不可变真的是因为 final 吗“,我回答 “是“ 然后就被挂了。。。。。。
- 故宫景点功课11:后三宫区(下)
热门文章
- win10网页找不到服务器dns,win10无法找到dns地址是怎么回事|win10无法找到dns地址如何解决...
- 网站备案其实是服务器备案,国内服务器为什么需要备案?国外服务器备案吗?
- 北邮智能车仿真培训(三)—— 给车舞台让它驰骋
- 对不起,精英主义与特斯拉精神背道而驰
- 微信小程序表白墙管理系统带后端带说明
- Nginx 正向代理互联网访问
- Norgen痰液液化缓冲液解决方案
- html第一个子元素选择,css选中父元素下的第一个子元素(:first-child)
- bootstrap4 利用m- p-调整元素之间距离
- 912计算机专业综合,西安交通大学2018年计算机考研814、912考试大纲