当前位置:网站首页>ZUCC_编译语言原理与编译_实验04 语言与文法
ZUCC_编译语言原理与编译_实验04 语言与文法
2022-06-24 06:59:00 【星星不想卷】
编译语言原理与编译实验报告
| 课程名称 | 编程语言原理与编译 |
|---|---|
| 实验项目 | 语言与文法 |
实验目的
- 了解文法的历史
- 理解产生式规则
- 掌握最左推导,最右推导
- 掌握文法的二义性
- 掌握文法的分类与层次
实验内容
一、阅读课件完成课后习题
1、给出语言 L2={a^n b^m| m≥n≥1} 的文法
P 1 : S → A B A → a A b ∣ a b B → b B ∣ ε P_{1}:S\to AB\\ A\to aAb\mid ab\\ B\to bB\mid \varepsilon P1:S→ABA→aAb∣abB→bB∣ε
2、给出语言 L1={a^(2n+1)|n≥0} 的文法
P 2 : A → a A a ∣ a P_{2}:A\to aAa\mid a P2:A→aAa∣a
3、给出语言L3={a^n b^m c^k| n,m,k≥0}的文法
P 3 : S → A B C A → A A ∣ a ∣ ε B → B B ∣ b ∣ ε C → C C ∣ c ∣ ε P_{3}:S\to ABC\\ A\to AA\mid a\mid \varepsilon \\ B\to BB\mid b\mid \varepsilon \\ C\to CC\mid c\mid \varepsilon \\ P3:S→ABCA→AA∣a∣εB→BB∣b∣εC→CC∣c∣ε
4、写一个文法,使其语言是正偶数的集合,每个偶数不以0开头
P 4 : S → N N → 2 ∣ 4 ∣ 6 ∣ 8 ∣ N M M → 0 ∣ 2 ∣ 4 ∣ 6 ∣ 8 P_{4}:S\to N \\ N\to 2\mid 4\mid 6\mid 8\mid NM \\ M\to 0\mid 2\mid 4\mid 6\mid 8 P4:S→NN→2∣4∣6∣8∣NMM→0∣2∣4∣6∣8
5、给出语言L={1^n 0^m 1^m 0^n | n,m≥0}的文法
P 5 : S → 1 S 0 ∣ 0 A 1 ∣ ε A → 0 A 1 ∣ ε P_{5}:S\to 1S0\mid 0A1\mid \varepsilon \\ A\to 0A1\mid \varepsilon P5:S→1S0∣0A1∣εA→0A1∣ε
6、给出语言L={ WaW^t | W∈{0|1}* },其中W^t表示W的逆的文法
P 6 : S → a ∣ 0 S 0 ∣ 1 S 1 P_{6}:S\to a\mid 0S0\mid 1S1 P6:S→a∣0S0∣1S1
7、文法G[A]=({A},{a,b},{A→bA | a}, A) 所生成的语言是什么?
L ( G [ A ] ) = { b n a ∣ n ≥ 0 } L(G[A])=\left \{ b^{n} a\mid n\ge 0 \right \} L(G[A])={ bna∣n≥0}
8、文法G[N]为:
N → N D ∣ D D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 N\to ND\mid D\\ D\to 0\mid 1\mid 2\mid 3\mid 4\mid 5\mid 6\mid 7\mid 8\mid 9 N→ND∣DD→0∣1∣2∣3∣4∣5∣6∣7∣8∣9
(1) G[N]所生成的语言是什么?
L ( G [ A ] ) = { α ∣ α 为 数 字 串 } L(G[A])=\left \{ \alpha \mid \alpha 为数字串 \right \} L(G[A])={ α∣α为数字串}
(2) 给出句子0127的最左、最右推导。
最左推导
N * N D * N D D * N D D D * D D D D * 0 D D D * 01 D D * 012 D * 0127 N\Longrightarrow ND\Longrightarrow NDD\Longrightarrow NDDD\Longrightarrow DDDD\Longrightarrow 0DDD\Longrightarrow 01DD\Longrightarrow 012D\Longrightarrow 0127 N*ND*NDD*NDDD*DDDD*0DDD*01DD*012D*0127
最右推导
N * N D * N 7 * N D 7 * N 27 * N D 27 * N 127 * D 127 * 0127 N\Longrightarrow ND\Longrightarrow N7\Longrightarrow ND7\Longrightarrow N27\Longrightarrow ND27\Longrightarrow N127\Longrightarrow D127\Longrightarrow 0127 N*ND*N7*ND7*N27*ND27*N127*D127*0127
9、已知文法G[S]=( {A,B},{a,b,c,d}, P, S ) , 其中 P 为:
S → A B A → a A b ∣ a b B → c B d ∣ c d S\to AB\\ A\to aAb\mid ab\\ B\to cBd\mid cd S→ABA→aAb∣abB→cBd∣cd
该文法 所生成的语言是什么?
L ( G [ S ] ) = { a n b n c m d m ∣ n , m ≥ 1 } L(G[S])=\left \{ a^{n}b^{n}c^{m}d^{m}\mid n,m \ge 1\right \} L(G[S])={ anbncmdm∣n,m≥1}
10、已知文法G[E]:
E → E + T ∣ E − T ∣ T T → T ∗ F ∣ T / F ∣ F F → ( E ) ∣ i E\to E+T\mid E-T\mid T\\ T\to T*F\mid T/F\mid F\\ F\to (E)\mid i E→E+T∣E−T∣TT→T∗F∣T/F∣FF→(E)∣i
证明 E+T*F 是它的一个句型,指出这个句型的短语、直接短语和句柄
E * E + T * E + T ∗ F 短 语 : E + T ∗ F 、 T ∗ F 直 接 短 语 : T ∗ F 句 柄 : T ∗ F E\Longrightarrow E+T\Longrightarrow E+T*F\\\\ 短语:E+T*F、T*F\\ 直接短语:T*F\\ 句柄:T*F E*E+T*E+T∗F短语:E+T∗F、T∗F直接短语:T∗F句柄:T∗F
11、已知文法G[S]:
S → ( A S ) ∣ ( b ) A → ( S a A ) ∣ ( a ) S\to (AS)\mid (b)\\ A\to (SaA)\mid (a) S→(AS)∣(b)A→(SaA)∣(a)
试找出符号串(a)和(A((SaA)(b)))的短语﹑直接短语和句柄(如果有的话)
符号串(a))不是文法的句型,因此它没有短语﹑直接短语和句柄
S * ( A S ) * ( A ( A S ) ) * ( A ( A ( b ) ) ) * ( A ( ( S a A ) ( b ) ) ) 短 语 : ( A ( ( S a A ) ( b ) ) ) 、 ( ( S a A ) ( b ) ) 、 ( S a A ) 、 ( b ) 直 接 短 语 : ( S a A ) 、 ( b ) 句 柄 : ( S a A ) S\Longrightarrow (AS)\Longrightarrow (A(AS))\Longrightarrow (A(A(b)))\Longrightarrow (A((SaA)(b)))\\\\ 短语:(A((SaA)(b)))、((SaA)(b))、(SaA)、(b)\\ 直接短语:(SaA)、(b)\\ 句柄:(SaA) S*(AS)*(A(AS))*(A(A(b)))*(A((SaA)(b)))短语:(A((SaA)(b)))、((SaA)(b))、(SaA)、(b)直接短语:(SaA)、(b)句柄:(SaA)
12、设有文法G[S]: S→iSeS| iS | i,试证明文法G[S]有二义性。

13、设有文法G[N]:
N → S E ∣ E S → S D ∣ D E → 0 ∣ 2 ∣ 4 ∣ 6 ∣ 8 ∣ 10 D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 N\to SE\mid E\\ S\to SD\mid D\\ E\to 0\mid 2\mid 4\mid 6\mid 8\mid 10\\ D\to 0\mid 1\mid 2\mid 3\mid 4\mid 5\mid 6\mid 7\mid 8\mid 9 N→SE∣ES→SD∣DE→0∣2∣4∣6∣8∣10D→0∣1∣2∣3∣4∣5∣6∣7∣8∣9
(1)试证明文法G[N]有二义性。

(2)此文法所描述的语言是什么?
该文法所描述的语言是所有无符号偶数的集合(可以0开头)。
(3)试写出另一文法G’使L(G’)=L(G)且G’是无二义性的
N → S E ∣ E S → S D ∣ D E → 0 ∣ 2 ∣ 4 ∣ 6 ∣ 8 D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 N\to SE\mid E\\ S\to SD\mid D\\ E\to 0\mid 2\mid 4\mid 6\mid 8\\ D\to 0\mid 1\mid 2\mid 3\mid 4\mid 5\mid 6\mid 7\mid 8\mid 9 N→SE∣ES→SD∣DE→0∣2∣4∣6∣8D→0∣1∣2∣3∣4∣5∣6∣7∣8∣9
二、阅读教材了解直线式语言SPL
代码与文法的对应关系
将代码等价转换为树形结构来描述,只注重语句和表达式,形成了直线式程序。
文法的产生式规则

与type 类型定义的关系
可以将文法直接翻译成数据结构的定义,其中每个文法符号对应这个数据结构中的一个typedef,例如:

而其中的A_stm其实是一个结构体。
与解释器interp的关系
通过解释器,利用构造器来解释执行语言,并对其环境、抽象语法、树数据结构的递归性予以展示。
三、参考简单语言解释器 Naive.fs 代码,尝试写出其文法

四、下面文法生成的语言分别是什么
G1 : S-> AB
A -> aA|ε
B->bc|bBc
G2: S ->aA|a
A ->aS
G1:任意a个字符(包括0个)后跟着n个b和c,n至少为1
G2:任意奇数个a(至少一个)组成的字符串
边栏推荐
- 自动化测试的未来趋势
- Solution of electric education system for intelligent supervision station
- 2021-03-16 COMP9021第九节课笔记
- io模型初探
- 【毕业季】你好陌生人,这是一封粉色信笺
- 2021-03-04 comp9021 class 6 notes
- Easyplayerpro win configuration full screen mode can not be full screen why
- More than observation | Alibaba cloud observable suite officially released
- DHCP, TFTP Foundation
- Model effect optimization, try a variety of cross validation methods (system operation)
猜你喜欢

Swift extension chainlayout (UI chain layout) (source code)

Model effect optimization, try a variety of cross validation methods (system operation)

Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022

Pipeline concept of graphic technology

2022年制冷与空调设备运行操作上岗证题库及模拟考试

The first exposure of Alibaba cloud's native security panorama behind the only highest level in the whole domain

The article takes you to understand the security of Windows operating system and protect your computer from infringement

2021-03-16 COMP9021第九节课笔记

问题4 — DatePicker日期选择器,2个日期选择器(开始、结束日期)的禁用

Swift extension networkutil (network monitoring) (source code)
随机推荐
VR is destined to reappear in the Jianghu?
Teach you how to use the reflect package to parse the structure of go - step 1: parameter type check
The applet reads more than 20 data, and the cloud function reads more than 100 restrictions
Question 1: the container that holds the most water
12--合并两个有序链表
Simple summary of lighting usage
Industrial computer anti cracking
pyQt 常用系统的事件
Paper notes: multi label learning dm2l
Solution of electric education system for intelligent supervision station
Application of JDBC in performance test
2021-03-16 comp9021 class 9 notes
[introduction to point cloud dataset]
1279_VMWare Player安装VMWare Tools时VSock安装失败解决
"Adobe international certification" about Adobe Photoshop, creating and modifying brush tutorials?
【无标题】
05-ubuntu安装mysql8
Simple refraction effect
2021-03-09 comp9021 class 7 Notes
Upgrade Mysql to the latest version (mysql8.0.25)