1.已知文法:
S->a|^|(T)
T->T,S|S
分析句型(T,(^,a)),求全部的短语、直接短语和句柄。
语法书为:
由语法树可知全部短语:(T,(^,a)) T,(^,a) (^,a) ^,a ^ a
直接短语:^ a
句柄:^
2.构造上下文无关文法,描述语言:
{anbn|n>=0}
{ambn|m>=n>=0}
{(ab)n|n>=0}
{ambn|m,n>=1}
①S->aSb|ε
if n>0 then S->aa...S...bb
if n=0 then S->ε
else other
②S->BA|ε
A->anbn
B->am
if m>n>0 then S->aaaa...S...bbb (a的个数大于b的个数)
if n=0 then S->ε
else other
③ S->abS|ε
if n>0 then S->abababab...S
if n=0 else S->ε
else other
④S->aSb|ab
if m=n>1 then S->aaa...S...bbb (a的个数等于b的个数)
if m>n>1 then S->aaaa...S...bbb (a的个数大于b的个数)
if n>m>1 then S->aaa...S...bbbb (a的个数小于b的个数)
if n=m=1 then S->ab
else other
3.如果if语句的方法:
stmt->if expr then stmt
| if expr then stmt else stmt
| other
句子if E1 then if E2 then S1 else S2是否有两棵不同的语法树?说明了什么?