The second work - grammar and language

1. grammar G (Z): Z-> aZb | ab definition of what kind of language?

Because Z-> aZb or Z-> ab

Z=>aZb=>aaZbb=>···=>an-1Zbn-1=>anbn

Therefore, the determined grammar language L (G [Z]) = {aⁿbⁿ | n≥1}

 

2. Write grammar quaternion form materials 22 pages identifiers in example 2.2.

There grammar = G (V N , V T , P, S)

Vn = {P (identifier), M (letters), H (number)}, Vt = {a, b, c, ···, x, y, z, 0,1, ···, 9}

P={  <P>-><M>

  <P>-><P><M>

  <P>-><P><H>

  <M>->a

  <M>->b

  .

  .

  .

  <M>->z

  <H>->0

  <H>->1

  .

  .

  .

  <H>->9 }

S=<P>

3. Write the following expressions leftmost derivation and rightmost derivation syntax tree.

G (E):

E=> E + T | T

T=>T * F | F

F=>(E)| i

  • i*i+i
  • i+i*i
  • i+(i+i)

 Observe different leftmost and rightmost derivation process, as well as the similarities and differences of the syntax tree.

i*i+i:

最左:E=>E+T=>T+T=>T*F+T=>T*i+T=>F*i+T=>i*i+T=>i*i+F=>i*i+i

最右:E=>E+T=>E+F=>E+i=>T+i=>T*F+i=>T*i+i=>F*i+i=>i*i+i

Syntax tree:

 

 

i+i*i:

最左:E=>E+T=>T+T=>F+T=>i+T=>i+T*F=>i+F*F=>i+i*F=>i+i*i

最右:E=>E+T=>E+T*F=>E+T*i=>E+F*i=>E+i*i=>T+i*i=>F+i*i=>i+i*i

Syntax tree:

 

 

i+(i+i):

最左:E=>E+T=>T+T=>F+T=>i+T=>i+F=>i+(E)=>i+(E+T)=>i+(T+T)=>i+(F+T)=>i+(i+T)=>i+(i+F)=>i+(i+i)

最右:E=>E+T=>E+F=>E+(E)=>E+(E+T)=>E+(E+F)=>E+(E+i)=>E+(T+i)=>E+(F+i)=>E+(i+i)=>T+(i+i)=>F+(i+i)=>i+(i+i)

 

Syntax tree

 

Guess you like

Origin www.cnblogs.com/Fishmark/p/11511291.html