1.文法G(Z):Z->aZb|ab定义的是什么样的语言?
产生式:Z->aZb, Z->ab 》 Z=>ab, Z=>aZb=>a²b²
重复使用产生式:Z->aZb,
得 Z=>aZb=>a²Zb²...=>aⁿ﹣¹Zbⁿ﹣¹=>aⁿbⁿ
所以语言为L(G[Z])={aⁿbⁿ|n≥1}
2.写出教材22页例2.2中标识符的文法四元组形式。
文法G=(Vn,Vt,P,S).
Vt={a,b,c,...,x,y,z,0,1,...,9},Vn={A(标识符),B(字母),C(数字)}
P={ <A>-><B>
<A>-><A><B>
<A>-><A><C>
<B>->a
<B>->b
<B>->z
<C>->0
<C>->1
<C>->9 }
S=<A>
3.写出下列表达式的最左推导、最右推导和语法树
G(E):
E=> E + T | T
T=>T * F | F
F=>(E)| i
i*i+i
最左推导:
E=>E+T=>T+T=>T*F+T=>F*F+T=>i*F+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
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
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)