1.已知文法:
S->a|^|(T)
T->T,S|S
分析句型(T,(^,a)),求全部的短语、直接短语和句柄。
S => (T) => (T,S) => (T,(T))=>(T,(T,S)) => (T,(T,a)) => (T,(S,a)) => (T,(^,a))
短语:(T,(^,a)) T,(^,a) (^,a) ^,a ^ a
直接短语:^,a
句柄:^
2.构造上下文无关文法,描述语言:
{anbn|n≥0}
设:G(T):T => aTb|ab|ε
if n=0: T =>ε
else T=> aTb =>aaTbb => ....=>anbn
{ambn|m≥n≥0}
设:G(T):T => aaTb|aab|ε
if m≥n=0:T => ε
else T => aaTb => aaaaTbb => .... => ambn
{(ab)n|n≥0}
设:G(T):T => abT|ab|ε
if n=0: T => ε
else T=> abT => ababT => .... => (ab)n
{ambn|m,n≥1}
设:G(T):T => AEB
A => aA|a
B => bB|b
E => aEb|ab
if m=n=1: T => ab
else T => AEB => aAaEbbB => .... =>ambn
3.如果if语句的方法:
stmt->if expr then stmt
| if expr then stmt else stmt
| other
句子if E1 then if E2 then S1 else S2是否有两棵不同的语法树?说明了什么?
语法树1:if E1 then if E2 then S1 else S2
语法树2:if E1 then if E2 then S1 else S2
说明了 if语句具有二义性。