首页 编译原理与实践第三章答案

编译原理与实践第三章答案

举报
开通vip

编译原理与实践第三章答案TheexercisesofChapterThree3.2GiventhegrammarA→AA|(A)|Describethelanguageitgenerates;Showthatitisambiguous.[Solution]:Generatesastringofbalancedparenthesis,includingtheemptystring.parsetreesof():AAAA(A)(A)3.3Giventhegrammarexpexpaddopterm|termaddop+|-termtermmu...

编译原理与实践第三章答案
TheexercisesofChapterThree3.2GiventhegrammarA→AA|(A)|Describethelanguageitgenerates;Showthatitisambiguous.[Solution]:Generatesastringofbalancedparenthesis,includingtheemptystring.parsetreesof():AAAA(A)(A)3.3Giventhegrammarexpexpaddopterm|termaddop+|-termtermmulopfactor|factormulop*factor(exp)|numberWritedownleftmostderivations,parsetrees,andabstractsyntaxtreesforthefollowingexpression:a.3+4*5-6b.3*(4-5+6)c.3-(4+5*6)[Solution]:a.Theleftmostderivationsfortheexpression3+4*5-6:Exp=>expaddopterm=>expaddoptermaddopterm=>termaddoptermaddopterm=>factoraddoptermaddopterm=>3addoptermaddopterm=>3+termaddopterm=>3+termmulopfactoraddopterm=>3+factormulopfactoraddopterm=>3+4mulopfactoraddopterm=>3+4*factoraddopterm=>3+4*5addopterm=>3+4*5-term=>3+4*5-factor=>3+4*5-63.5WriteagrammarforBooleanexpressionsthatincludestheconstantstrueandfalse,theoperatorsand,orandnot,andparentheses.Besuretogiveoralowerprecedencethanandandandalowerprecedencethatnotandtoallowrepeatednot’s,asintheBooleanexpressionnotnottrue.Alsobesreyourgrammarisnotambiguous.[solution]bexp→bexporA|AA→AandB|BB→notB|CC→(bexp)|true|falseEx:notnottrueboolExp→ABnotBnotnotBnotnotCnotnottrue3.8Giventhefollowinggrammarstatement→if-stmt|other|εif-stmt→if(exp)statementelse-partelse-part→elsestatement|εexp→0|1a.Drawaparsetreeforthestringif(0)if(1)otherelseelseotherb.whatisthepurposeofthetwoelse’s?Thetwoelse’sallowtheprogrammertoassociateanelseclausewiththeoutmostelse,whentwoifstatementsarenestedandthefirstdoesnothaveanelseclause.c.IssimilarcodepermissibleinC?Explain.ThegrammarinClookslike:if-stmt→if(exp)statement|if(exp)statementelsestatementthewaytooverride“danglingelse”problemistoenclosetheinnerifstatementin{}s.e.g.if(0){if(1)other}elseother.3.10a.Translatethegrammarofexercise3.6intoEBNF.b.DrawsyntaxdiagrammsfortheEBNFofpart(a).[Solution]a.Theoriginalgrammarlexp→atom|listatom→number|identifierlist→(lexp-seq)lexp-seq→lexp-seqlexp|lexpTheEBNFoftheabovegrammar:lexp→atom|listatom→number|identifierlist→(lexp-seq)lexp-seq→lexp{lexp}b.ThesyntaxdiagrammsfortheaboveEBNF:lexpatomlistatomNUMIDElist(lexp-seq)lexp-seqlexplexp3.12.UnaryminusescanbeaddedinseveralwaystothesimplearithmeticexpressiongrammarofExercise3.3.RevisetheBNFforeachofthecasesthatfollowsothatitsatisfiesthestatedrule.a.Atmostoneunaryminusisallowedineachexpression,anditmustcomeatthebeginningofanexpression,so-2-3islegal(andevaluatesto-5)and-2-(-3)islegal,but-2--3isnot.expexpaddopterm|termaddop+|-termtermmulopfactor|factormulop*factor(exp)|(-exp)|number|b.Atmostoneunaryminusareallowedbeforeanumberorleftparenthesis,so-2--3islegalbut--2and-2---3arenot.expexpaddopterm|termaddop+|-termtermmulopfactor|factormulop*factor(exp)|-(exp)|number|-numberc.Arbitrarilymanyunaryminusesareallowedbeforenumbersandleftparentheses,soeverythingaboveislegal.3.19Insomelanguages(Modula-2andAdaareexamples),aproceduredeclarationisexpectedtobeterminatedbysyntaxthatincludesthenameoftheprocedure.Forexample,inModular-2aprocedureisdeclaredasfollows:PROCEDUREP;BEGIN⋯⋯ENDP;NotetheuseoftheprocedurenamePaltertheclosingEND.Cansucharequirementbecheckedbyaparser?Explain.[Answer]Thisrequirementcannotbehandledaspartofthegrammarwithoutmakinganewruleforeachlegalvariablename,whichmakesitintractableforallpracticalpurposes,evenifvariablenamesarerestrictedtoaveryshortlength.Theparserwilljustcheckthestructure,thatanidentifierfollowsthekeywordPROCEDUREandanidentifieralsofollowsthekeywordEND,howevercheckingthatitisthesameidentifierisleftforsemanticanalysis.Seethediscussiononpages131-132ofyourtext.3.20a.Writearegularexpressionthatgeneratethesamelanguageasthefollowinggrammar:A→aA|B|εB→bB|Ab.Writeagrammarthatgeneratesthesamelanguageasthefollowingregularexpression:(a|c|ba|bc)*(b|ε)[Solution]a.Theregularexpression:(a|b)*b.Thegrammar:Step1:A→BCB→aB|cB|baB|bcB|εC→b|εStep2:A→Bb|BB→aB|cB|baB|bcB|ε
本文档为【编译原理与实践第三章答案】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
春天像花儿一样
暂无简介~
格式:doc
大小:58KB
软件:Word
页数:10
分类:
上传时间:2022-07-09
浏览量:22