欢迎来到天天文库
浏览记录
ID:47466733
大小:154.51 KB
页数:11页
时间:2020-01-11
《编译原理与实践 第六 七章 答案》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、TheExercisesofChapterSix6.2应该在num→digit产生式中再加一条语义规则:numd.count=1用来进行初始化。6.46.7ConsiderthefollowinggrammarforsimplePascal-styledeclarations:delc→var-list:typevar-list→var-list,id
2、idtype→integer
3、realWriteanattributegrammarforthetypeofavariable.[Solution]GrammarRuleSemanticRulesdelc→var-list:typev
4、ar-list.type=type.typevar-list1→var-list2,idval-list2.type=var-list1.typeid.type=var-list1.typevar-list→idid.type=var-list.typetype→integertype.type=INTERGERtype→realtype.type=REAL6.10a.DrawdependencygraphscorrespondingtoeachgrammarruleofExample6.14(Page283),andfortheexpression5/2/2.0.b.Describ
5、ethetwopassesrequiredtocomputetheattributesonthesyntaxtreeof5/2/2.0,includingapossibleorderinwhichthenodescouldbevisitedandtheattributevaluescomputedateachpoint.c.Writepseudcodeforproceduresthatwouldperformthecomputationsdescribedinpart(b).[Solution]a.ThegrammarrulesofExample6.14S→expexp→exp/ex
6、p
7、num
8、num.numThedependencygraphsforeachgrammarrule:S→expvalSisFloatetypevalexpexp→exp/expisFloatetypevalexpisFloatetypevalexp/isFloatetypevalexpexp→numisFloatetypevalexpvalnumexp→num.numisFloatetypevalexpvalnum.numThedependencygraphsfortheexpression:5/2/2.0valSIsFloatetypevalexpisFloatetypevale
9、xp/isFloatetypevalexpisFloatetypevalexp/isFloatetypevalexpvalnum.num(2.0)valnumvalnum(5)(2)b.ThefirstpassistocomputetheetypefromisFloat.Thesecondpassistocomputethevalfrometype.Thepossibleorderisasfollows:valS122IsFloatetype3val11exp1isFloat4etypeval9exp/isFloatetypeval10expisFloat5etypeval6exp/
10、isFloat7etypeval8expvalnum.num(2.0)valnumvalnum(5)(2)c.ThepseudcodeprocedureforthecomputationoftheisFloat.FunctionEvalisFloat(T:treenode):BooleanVartemp1,temp2:BooleanBeginCasenodekindofTofexp:temp1=EvalisFloat(leftchildofT);ifrightchildofTisnotnilthentemp2=EvalisFloat(rightchildofT)returntemp1
11、ortemp2elsereturntemp1;num:returnfalse;num.num:returntrue;endFunctionEvalval(T:treenode,etype:integer):VALUEVartemp1,temp2:VALUEBeginCasenodekindofTofS:Return(Evalval(leftchildofT,etype));Exp:Ifetype=EMPTYthenIfEvalisFloat(T)thene
此文档下载收益归作者所有