Grammar and language

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

From the meaning of the questions: Z-> aZb or Z-> ab

 

Z->ab,Z->aZb->aabb,Z->aZb->aaZbb->aaabbb,Z->aZb->aaZbb->aaaZbbb->aaaabbbb

Z=>aZb=>a²Zb²...=>aⁿ﹣¹Zbⁿ﹣¹=>aⁿbⁿ

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

2, p. 22 write grammar textbook example 2.2 identifier quadruple form .

 

Provided: an identifier represented by A, represented by the letter B: A-> a | b | ... | z, is represented by C number: C-> 0 | 1 | ... | 9

 

VN={A,B,C}、VT={a,b,c,…,x,y,z,0,1,…,9}

 

  P={ A->B,A->AB,A->AC;    

    B->a

    …

 

    B->z

 

    C->0

 

    …

 

    C->9 }

 

S=<A>

 

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 the leftmost and rightmost derivation is different, and the similarities and differences between the syntax tree

 

(1)i*i+i

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

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

Syntax tree:

(2)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:

(3)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+(T+i)=>E+(F+i)=>E+(i+i)=>T+(i+i)=>F+(i+i)=>i+(i+i) 语法树:

Guess you like

Origin www.cnblogs.com/kushoulder/p/11515000.html