当前位置:网站首页>ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
2022-06-24 08:30:00 【The stars don't want to roll】
Compiling language principle and compiling experiment report
| Course name | Programming language principle and compilation |
|---|---|
| Experimental projects | Language and grammar |
The experiment purpose
- Understand the history of grammar
- Understand production rules
- Master the leftmost derivation , Rightmost derivation
- Grasp the ambiguity of grammar
- Master the classification and level of grammar
Experimental content
One 、 Read the courseware and complete the exercises after class
1、 Give language L2={a^n b^m| m≥n≥1} Grammar
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、 Give language L1={a^(2n+1)|n≥0} Grammar
P 2 : A → a A a ∣ a P_{2}:A\to aAa\mid a P2:A→aAa∣a
3、 Give language L3={a^n b^m c^k| n,m,k≥0} Grammar
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、 Write a grammar , Make its language a set of positive even numbers , Each even number does not begin with 0 start
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、 Give language L={1^n 0^m 1^m 0^n | n,m≥0} Grammar
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、 Give language L={ WaW^t | W∈{0|1}* }, among W^t Express W The inverse grammar of
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、 Grammar G[A]=({A},{a,b},{A→bA | a}, A) What is the generated language ?
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、 Grammar G[N] by :
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] What is the generated language ?
L ( G [ A ] ) = { α ∣ α by Count word strand } L(G[A])=\left \{ \alpha \mid \alpha Is a numeric string \right \} L(G[A])={ α∣α by Count word strand }
(2) Give sentences 0127 Leftmost of 、 Rightmost derivation .
Leftmost inference
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
Rightmost derivation
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、 Known grammar G[S]=( {A,B},{a,b,c,d}, P, S ) , among P by :
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
The grammar What is the generated language ?
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、 Known grammar 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
prove E+T*F It's a sentence pattern , A phrase that points out this sentence pattern 、 Direct phrases and handles
E * E + T * E + T ∗ F short language : E + T ∗ F 、 T ∗ F straight Pick up short language : T ∗ F sentence Handle : T ∗ F E\Longrightarrow E+T\Longrightarrow E+T*F\\\\ The phrase :E+T*F、T*F\\ Direct phrase :T*F\\ Handle :T*F E*E+T*E+T∗F short language :E+T∗F、T∗F straight Pick up short language :T∗F sentence Handle :T∗F
11、 Known grammar 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)
Try to find the symbol string (a) and (A((SaA)(b))) 's phrases ﹑ Direct phrases and handles ( If any )
Symbol string (a)) Not a grammatical sentence pattern , So it has no phrases ﹑ Direct phrases and handles
S * ( A S ) * ( A ( A S ) ) * ( A ( A ( b ) ) ) * ( A ( ( S a A ) ( b ) ) ) short language : ( A ( ( S a A ) ( b ) ) ) 、 ( ( S a A ) ( b ) ) 、 ( S a A ) 、 ( b ) straight Pick up short language : ( S a A ) 、 ( b ) sentence Handle : ( S a A ) S\Longrightarrow (AS)\Longrightarrow (A(AS))\Longrightarrow (A(A(b)))\Longrightarrow (A((SaA)(b)))\\\\ The phrase :(A((SaA)(b)))、((SaA)(b))、(SaA)、(b)\\ Direct phrase :(SaA)、(b)\\ Handle :(SaA) S*(AS)*(A(AS))*(A(A(b)))*(A((SaA)(b))) short language :(A((SaA)(b)))、((SaA)(b))、(SaA)、(b) straight Pick up short language :(SaA)、(b) sentence Handle :(SaA)
12、 With grammar G[S]: S→iSeS| iS | i, Proof grammar G[S] Ambiguous .

13、 With grammar 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) Proof grammar G[N] Ambiguous .

(2) What language does this grammar describe ?
The language described by this grammar is a set of all unsigned even numbers ( Sure 0 start ).
(3) Try to write another grammar G’ send L(G’)=L(G) And G’ There is no ambiguity
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
Two 、 Read the textbook to understand the linear language SPL
The correspondence between code and grammar
The code is equivalently transformed into a tree structure to describe , Focus only on statements and expressions , Formed a linear program .
Production rules of grammar

And type Type defined relationships
Grammar can be translated directly into the definition of data structure , Each grammar symbol corresponds to one of the data structures typedef, for example :

And one of the A_stm It's actually a structure .
And the interpreter interp The relationship between
Through the interpreter , Using constructors to interpret execution languages , And its environment 、 Abstract syntax 、 The recursion of tree data structure is demonstrated .
3、 ... and 、 Refer to the simple language interpreter Naive.fs Code , Try to write the grammar

Four 、 What are the following grammatically generated languages
G1 : S-> AB
A -> aA|ε
B->bc|bBc
G2: S ->aA|a
A ->aS
G1: arbitrarily a Characters ( Include 0 individual ) After follow n individual b and c,n At least for 1
G2: Any odd number a( At least one ) Composed string
边栏推荐
猜你喜欢

Question 3 - MessageBox pop-up box, modify the default background color

longhorn安装与使用

ZUCC_编译语言原理与编译_实验05 正则表达式、有限自动机、词法分析

MAYA重新拓布

List of Li Bai's 20 most classic poems

Future trends in automated testing

蓝桥杯_N 皇后问题

【无标题】

ZUCC_编译语言原理与编译_实验02 FSharp OCaml语言

Utilisation de la fermeture / bloc de base SWIFT (source)
随机推荐
Question 3 - MessageBox pop-up box, modify the default background color
Promise的使用场景
DHCP, TFTP Foundation
etcd备份恢复原理详解及踩坑实录
Qmenu response in pyqt
05-ubuntu安装mysql8
Paper notes: multi label learning dm2l
12-- merge two ordered linked lists
自动化测试的未来趋势
将mysql的数据库导出xxx.sql,将xxx.sql文件导入到服务器的mysql中。项目部署。
Vscode topic recommendation
Opening chapter of online document technology - rich text editor
Future trends in automated testing
Opencv get (propid) common values
2022茶艺师(中级)上岗证题库及在线模拟考试
12--合并两个有序链表
How to implement approval function in Tekton
2022 tea artist (intermediate) work license question bank and online simulation examination
常用日期格式符与Qt获取当前时间的办法
2021-03-09 COMP9021第七节课笔记