编译原理与实践 第四章 答案.doc

编译原理与实践 第四章 答案.doc

ID:56714995

大小:125.50 KB

页数:6页

时间:2020-07-05

编译原理与实践  第四章  答案.doc_第1页
编译原理与实践  第四章  答案.doc_第2页
编译原理与实践  第四章  答案.doc_第3页
编译原理与实践  第四章  答案.doc_第4页
编译原理与实践  第四章  答案.doc_第5页
资源描述:

《编译原理与实践 第四章 答案.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、TheexercisesofChapterFour4.2Grammar:A→(A)A

2、εAssumewehavelookaheadofonetokenasintheexampleonp.144inthetextbook.ProcedureA()if(LookAhead()∈{‘(‘})thenCallExpect(‘(‘)CallA()CallExpect(‘)’)CallA()elseif(LookAhead()∈{‘)‘,$})thenreturn()else/*error*/fifiend4.3Giventhegrammarstatement→assign-stmt

3、

4、call-stmt

5、otherassign-stmt→identifier:=expcall-stmt→identifier(exp-list)[Solution]First,convertthegrammarintofollowingforms:statement→identifier:=exp

6、identifier(exp-list)

7、otherThen,thepseudocodetoparsethisgrammar:ProcedurestatementBeginCasetokenof(identifer:match(identifer);casetokenof(:=:

8、match(:=);exp;((:match(();exp-list;match());elseerror;endcase(other:match(other);elseerror;endcase;endstatement4.7aGrammar:A→(A)A

9、εFirst(A)={(,ε}Follow(A)={$,)}4.7bSeetheoremonP.178inthetextbook1.First{(}∩First{ε}=Φ2.ε∈Fist(A),First(A)∩Follow(A)=Φbothconditionsofthetheoremaresatisfied,henc

10、egrammarisLL(1)4.9Considerthefollowinggrammar:lexp→atom

11、listatom→number

12、identifierlist→(lexp-seq)lexp-seq→lexp,lexp-seq

13、lexpa.Leftfactorthisgrammar.b.ConstructFirstandFollowsetsforthenonterminalsoftheresultinggrammar.c.ShowthattheresultinggrammarisLL(1).d.ConstructtheLL(1)parsingtableforth

14、eresultinggrammar.e.ShowtheactionsofthecorrespondingLL(1)parser,giventheinputstring(a,(b,(2)),(c)).[Solution]a.lexp→atom

15、listatom→number

16、identifierlist→(lexp-seq)lexp-seq→lexplexp-seq’lexp-seq’→,lexp-seq

17、εb.First(lexp)={number,identifier,(}First(atom)={number,identifier}First(list)={(}Firs

18、t(lexp-seq)={number,identifier,(}First(lexp-seq’)={,,ε}Follow(lexp)={,$,}}Follow(atom)={,$,}}Follow(list)={,$,}}Follow(lexp-seq)={$,}}Follow(lexp-seq’)={$,}}c.AccordingtothedefinationofLL(1)grammar(Page155),theresultinggrammarisLL(1)aseachtableentryhasatmostoneproductionasshownin(d).d.TheL

19、L(1)parsingtablefortheresultinggrammarM[N,T]numberidentifer(),$Lexplexp→atomlexp→atomlexp→listAtomatom→numberatom→identifierListlist→(lexp-seq)Lexp-seqlexp-seq→lexplexp-seq’lexp-seq→lexplexp-seq’lexp-seq→lexplexp-seq’Lexp-seq’lexp-seq’→εlexp-seq’→,lexp-seqlexp

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。