资源描述:
《编译原理与实践第三章答案》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、TheexercisesofChapterThree3.2GiventhegrammarA→AA
2、(A)
3、εa.Describethelanguageitgenerates;b.Showthatitisambiguous.[Solution]:a.Generatesastringofbalancedparenthesis,includingtheemptystring.b.parsetreesof():AAAA()εεA()εA3.3Giventhegrammarexp®expaddopterm
4、
5、termaddop®+
6、-term®termmulopfactor
7、factormulop®*factor®(exp)
8、numberWritedownleftmostderivations,parsetrees,andabstractsyntaxtreesforthefollowingexpression:a.3+4*5-6b.3*(4-5+6)c.3-(4+5*6)[Solution]:a.Theleftmostderivationsfortheexpression3+4*5-6:Exp=>ex
9、paddopterm=>expaddoptermaddopterm=>termaddoptermaddopterm=>factoraddoptermaddopterm=>3addoptermaddopterm=>3+termaddopterm=>3+termmulopfactoraddopterm=>3+factormulopfactoraddopterm=>3+4mulopfactoraddopterm=>3+4*factoraddopterm=>3+4*5addopterm=>3+4*5-te
10、rm=>3+4*5-factor=>3+4*5-63.5WriteagrammarforBooleanexpressionsthatincludestheconstantstrueandfalse,theoperatorsand,orandnot,andparentheses.Besuretogiveoralowerprecedencethanandandandalowerprecedencethatnotandtoallowrepeatednot’s,asintheBooleanexpressi
11、onnotnottrue.Alsobesreyourgrammarisnotambiguous.[solution]bexp→bexporA
12、AA→AandB
13、BB→notB
14、CC→(bexp)
15、true
16、falseEx:notnottrueboolExp→A→B→notB→notnotB→notnotC→notnottrue3.8Giventhefollowinggrammarstatement→if-stmt
17、other
18、εif-stmt→if(exp)statementelse-partel
19、se-part→elsestatement
20、εexp→0
21、1a.Drawaparsetreeforthestringif(0)if(1)otherelseelseotherb.whatisthepurposeofthetwoelse’s?Thetwoelse’sallowtheprogrammertoassociateanelseclausewiththeoutmostelse,whentwoifstatementsarenestedandthefirstdoesnothaveanelseclau
22、se.c.IssimilarcodepermissibleinC?Explain.ThegrammarinClookslike:if-stmt→if(exp)statement
23、if(exp)statementelsestatementthewaytooverride“danglingelse”problemistoenclosetheinnerifstatementin{}s.e.g.if(0){if(1)other}elseother.3.10a.Translatethegrammarofex
24、ercise3.6intoEBNF.b.DrawsyntaxdiagrammsfortheEBNFofpart(a).[Solution]a.Theoriginalgrammarlexp→atom
25、listatom→number
26、identifierlist→(lexp-seq)lexp-seq→lexp-seqlexp
27、lexpTheEBNFoftheabovegrammar:lexp→atom
28、listatom→number
29、identifierlist→(lexp-seq)l