当前位置:网站首页>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(至少一个)组成的字符串
边栏推荐
- 13 -- 移除无效的括号
- Chart list Performance Optimization: minimum resource consumption in the visualization area
- Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022
- Interview tutorial - multi thread knowledge sorting
- 11--无重复字符的最长子串
- 5g industrial router Gigabit high speed low delay
- Search and recommend those things
- 基金的募集,交易与登记
- Coordinate transformation of graphic technology
- Saccadenet: use corner features to fine tune the two stage prediction frame | CVPR 2020
猜你喜欢
The article takes you to understand the security of Windows operating system and protect your computer from infringement
2022年制冷与空调设备运行操作上岗证题库及模拟考试
Pagoda panel installation php7.2 installation phalcon3.3.2
有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
Introduction to software engineering - Chapter 2 - feasibility study
根据网络上的视频的m3u8文件通过ffmpeg进行合成视频
Application of JDBC in performance test
Swift 基礎 閉包/Block的使用(源碼)
Swift Extension NetworkUtil(網絡監聽)(源碼)
3D数学基础[十七] 平方反比定理
随机推荐
Swift extension networkutil (network monitoring) (source code)
How to design a highly available and extended image storage function
Review SGI STL secondary space configurator (internal storage pool) | notes for personal use
对于flex:1的详细解释,flex:1
C语言_字符串与指针的爱恨情仇
3D数学基础[十七] 平方反比定理
OC extension detects whether an app is installed on the mobile phone (source code)
51单片机_外部中断 与 定时/计数器中断
June 27, 2021: given a positive array arr, it represents the weight of several people
Application of JDBC in performance test
All you know is the test pyramid?
搜索与推荐那些事儿
Swift Extension ChainLayout(UI的链式布局)(源码)
问题3 — messageBox弹框,修改默认背景色
2022茶艺师(中级)上岗证题库及在线模拟考试
Pat 1157: school anniversary
有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
Swift 基础 闭包/Block的使用(源码)
2022年制冷与空调设备运行操作上岗证题库及模拟考试
C language_ Love and hate between string and pointer