# 1. 需求分析
使用 LR(1)法进行语法分析。
根据 C 语言的文法生成 action 表和 goto 表,利用 action 表和 goto 表对词法分析的输出进行语法分析,构建语法树。
# 2. 文法设计
要求:给出如下语言成分的文法描述。
- 声明语句(包括变量声明、数组声明、记录声明和过程声明)
```c++
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
```
- 表达式及赋值语句(包括数组元素的引用和赋值)
```c++
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
```c++
Control -> If | If_Else | While
If -> if ( Condition ) { Module }
If_Else -> if ( Condition ) { Module } else { Module }
```
- 循环语句:do_while
```c++
While -> while ( Condition ) { Module }
Condition -> Condition and Condition | Condition or Condition | not Condition | ( Condition ) | Value Relop Value | true | false
Relop -> < | <= | == | != | > | >=
```
- 过程调用语句
```c++
Call -> id ( Transmit )
Transmit -> epsilon | Value | Value , Transmit
```
# 3. 系统设计
要求:分为系统概要设计和系统详细设计。
(1) 系统概要设计:给出必要的系统宏观层面设计图,如系统框架图、数据流图、功能模块结构图等以及相应的文字说明。
程序主要由几部分组成
- 词法分析结果加载器
- action 表和 goto 表加载器
- 语法分析器
- 语法树打印器
(2) 系统详细设计:对如下工作进行展开描述
- 核心数据结构的设计
![](https://www.writebug.com/myres/static/uploads/2022/7/31/aec10cab8f7b04a82694d84a3174a8a2.writebug)
用于储存词法分析的结果,保存词法分析后的值、种类和行数。
- 主要功能函数说明
![](https://www.writebug.com/myres/static/uploads/2022/7/31/9cbe606c27359430f6159651f5c7201c.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/7/31/3d7799bdab0bfd8a6e094fe3e1582b52.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/7/31/7f2f4f279053d3adb77cc0ffc39cb11b.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/7/31/a5dd36f444c129253cea79c05d987284.writebug)
- 程序核心部分的程序流程图
![](https://www.writebug.com/myres/static/uploads/2022/7/31/9616848ef8f3d9f54cb08cf93416b03d.writebug)
# 4. 系统实现及结果分析
要求:对如下内容展开描述。
- 系统实现过程中遇到的问题;
- 输出该句法分析器的分析表;
附件 action.txt 与 goto.txt
- 针对一测试程序输出其句法分析结果;
测试程序
![](https://www.writebug.com/myres/static/uploads/2022/7/31/2bd5fb3d6a480dfce0db86530d669a4c.writebug)
输出结果:
```c++
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)
M
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
100011754-基于Python实现语法分析.zip (15个子文件)
grammanalysis
input.txt 1KB
preprocess.py 12KB
action.txt 210KB
语法分析.py 4KB
语法分析.docx 298KB
LICENSE 1KB
CFG.txt 1KB
goto.txt 150KB
error.log 38B
编译原理实验指导书-语法分析.pdf 224KB
test.txt 300B
result.txt 6KB
1180300315-周牧云-语法分析.doc 421KB
README.md 15KB
词法分析.py 8KB
共 15 条
- 1
资源评论
- liushenxuelp1232024-04-28支持这个资源,内容详细,主要是能解决当下的问题,感谢大佬分享~
- 2201_752985652024-01-04感谢资源主分享的资源解决了我当下的问题,非常有用的资源。
神仙别闹
- 粉丝: 2680
- 资源: 7667
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功