资源描述:
《编译原理与实践第四章答案》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、TheexercisesofChapterFour4.2Grammar:A(A)A
2、eAssumewehavelookaheadofonetokenasintheexampleonp.144inthetextbook.ProcedureA()if(LookAhcad()丘{'('})thenCallExpectCC)CallA()CallExpect(')')CallA()elseif(LookAhead()w$})thenreturn()else/*error*/fiflend4.3Giventhegrammarstatement-^assign-stm
3、t
4、call-stmt
5、otherassign-stmtidentifier:=expcall-stmt-^identifier(exp-list)[Solution]First,convertthegrammarintofollowingforms:statementidentifier:=cxp
6、identifier(exp-list)
7、otherThen,thepseudocodetoparsethisgrammar:ProcedurestatementBeginCasetokenof(identifer:match(identifer);caset
8、okenof(:=:match(:=);exp;((:match(();exp-list;match());elseeiTor;endcase(other:match(other);elseerror;endcase;endstatement4.7aGrammar:A—>(A)A
9、cFirst(A)={(,£}Follow(A)={$,)}4.7bSeetheoremonP.178inthetextbook1.First{(}nFirst{E}=0>2.£WFist(A),First(A)AFollow(A)=(I)bothconditionsofthet
10、heoremaresatisfied,hencegrammarisLL(1)4.9Considerthefollowinggrammar:lexpfatom卩istatom-^numbcrlidcntificrlist—(lexp-seq)lexp-seq-^lexp,lexp-seq
11、lexpa.Leftfactorthisgrammar.b.ConstructFirstandFollowsetsforthenontenninalsoftheresultinggrammar.c.ShowthattheresultinggrammarisLL(1).d.C
12、onstructtheLL(1)parsingtablefortheresultinggramma匚e.ShowtheactionsofthecorrespondingLL(1)parser,giventheinputstring(a,(b,(2)),(c))・[Solution]a.lexpfatom卩istatom-*number
13、identifierlist—(lcxp・scq)lexp-seq-^lexplexp-seq^lexp-seqJ,lexp-seq
14、£b.First(lexp)={number,identifier,(}First(ato
15、m)={number,identifier}First(list)={(}First(lcxp-scq)={number,identifier,(}First(1exp-seq5)={,,£}Follow(lexp)={,$,}}Fol1ow(atom)={,$,}}Follow(list)={,$,}}Follow(lexp-seq)={$,}}Follow(lexp-seq')={SJ}c.AccordingtothedefinationofLL(1)grammar(Page155),theresultinggrammarisLL(1)aseachta
16、bleentryhasatmostoneproductionasshownin(d).d.TheLL(1)parsingtablefbrtheresultinggrammarM[N,T]numberidentifer()$Lexplexpfatomlexpatomlexp-*listAtomatom—numberatom—identifierListlist(lexp-seq)Lexp-seqlexp-seq—lexplexp-seq5lexp-seq—lexplexp-seq'lexp-seq—lexplexp-seq'Lexp-seq5lexp-seq
17、'->elexp-seq?—,lexp-seqlexp-seq5—