Grammar and language second job

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

Rules, also known as rewrite rules, production or production style.
Z means that can produce aZb or ab, can be said to rule on Z's.

 

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

Provided: an identifier represented by I, represented by the letter L: L-> a | b | ... | z, represented by numbers N: N-> 0 | 1 | ... | 9

∴VN={I,L,N}、VT={a,b,c,…,x,y,z,0,1,…,9}

  P={ I->L

    I-> IL

    I->IN

    L->a

    …

    L->z

    N->0

    …

    N->9 }

    S=<I>

 

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.

(1) i*i+i

最左推导:E=>E+T=>T+T=>T*F+T=>F*F+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

 

 

(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

 

 

(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/av10492/p/11515178.html