1.文法G(Z):Z->aZb|ab定义的是什么样的语言?
答:Z => aZb => aaZbb => aaabbb
L(G[Z])={a^n*b^n|n>=1},Z可以产生aZb或者ab,是一条关于Z的规则。
2.写出教材22页例2.2中标识符的文法四元组形式。
答:G=(VN,VT,P,S)
VN={标识符,字母,数字},VT={a,b,c,d,~~~,y,z,0,1,2,~~~,9}
P={<字母>->a|b|....|z
<数字>->0|1|2|...|9
}
S=<标识符>
3.写出下列表达式的最左推导、最右推导和语法树。
- i*i+i
- i+i*i
- i+(i+i)
答:i*i+i
最左推导:E=>E+T=>T+T=>T*F+T=>F*F+T=>I*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
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)