当前位置:网站首页>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:SABAaAbabBbBε

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:AaAaa

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:SABCAAAaεBBBbεCCCcε

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:SNN2468NMM02468

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:S1S00A1εA0A1ε

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:Sa0S01S1

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])={ bnan0}

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 NNDDD0123456789

(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 SABAaAbabBcBdcd

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])={ anbncmdmn,m1}

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 EE+TETTTTFT/FFF(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+TF short language :E+TFTF straight Pick up short language :TF sentence Handle :TF

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 .

image-20220406004643958

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 NSEESSDDE0246810D0123456789

(1) Proof grammar G[N] Ambiguous .

image-20220406005019587

(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 NSEESSDDE02468D0123456789

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

 scanning 2022 year 4 month 6 Japan 01.20

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 :

 scanning 2022 year 4 month 6 Japan 01.23

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

image-20220406022253377

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

原网站

版权声明
本文为[The stars don't want to roll]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240605218856.html