Compiler theory learning (3) grammar and language

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

   Grammar G (Z) has two production rules:

  (1)Z->aZb

  (2)Z->ab

  By using the n-1 times to generate a first type, and then use a formula to generate a second, to give Z-> aZb-> aaZbb-> aaaZbbb-> aaaZbbb -> ...... A n-1 B n- -1 -> A n- B n-

 

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

  Grammar quad form:

  Grammar = G (V N , V T , P, S)

  V n- = {W is (identifier), Q (letters), K (number)}

  Vt={a,b,c,d...........z,0,1.......9}

  P={W-->Q | K }

  S={W}

 

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

    Leftmost derivation:

    E=>E+T=>T*F+T=>F*F+T=>i*F+T=>i*i+T=>i*i+F=>i*i+i

    Rightmost derivation:

    E=>E+T=>E+F=>E+i=>T*i=>T*F+i=>T*T+i=>T*T+i=>T*i+i=>i*i+i

    

    

   (2)i+i*i

    Leftmost derivation:

    E=>E+T=>T+T=>F+T=>i+T=>i+T*F=>i+F*F=>i+i*i

    最右推导:

    E=>E+T=>E+F=>E+E=>E+E+T=>E+T*F=>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+(F+i)=>E+(i+i)=>T+(i+i)=>F+(i+i)=>i+(i+i)

    

Guess you like

Origin www.cnblogs.com/xyqzzz/p/11515150.html