编译原理龙书课后部分答案(英文版).doc

上传人:b****2 文档编号:1646238 上传时间:2022-10-23 格式:DOC 页数:9 大小:73.50KB
下载 相关 举报
编译原理龙书课后部分答案(英文版).doc_第1页
第1页 / 共9页
编译原理龙书课后部分答案(英文版).doc_第2页
第2页 / 共9页
编译原理龙书课后部分答案(英文版).doc_第3页
第3页 / 共9页
编译原理龙书课后部分答案(英文版).doc_第4页
第4页 / 共9页
编译原理龙书课后部分答案(英文版).doc_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

编译原理龙书课后部分答案(英文版).doc

《编译原理龙书课后部分答案(英文版).doc》由会员分享,可在线阅读,更多相关《编译原理龙书课后部分答案(英文版).doc(9页珍藏版)》请在冰豆网上搜索。

编译原理龙书课后部分答案(英文版).doc

1)Whatisthedifferencebetweenacompilerandaninterpreter?

·Acompilerisaprogramthatcanreadaprograminonelanguage-thesourcelanguage-andtranslateitintoanequivalentprograminanotherlanguage–thetargetlanguageandreportanyerrorsinthesourceprogramthatitdetectsduringthetranslationprocess.

·Interpreterdirectlyexecutestheoperationsspecifiedinthesourceprogramoninputssuppliedbytheuser.

2)Whataretheadvantagesof:

(a)acompileroveraninterpreter

a.Themachine-languagetargetprogramproducedbyacompilerisusuallymuchfasterthananinterpreteratmappinginputstooutputs.

(b)aninterpreteroveracompiler?

b.Aninterpretercanusuallygivebettererrordiagnosticsthanacompiler,becauseitexecutesthesourceprogramstatementbystatement.

3)Whatadvantagesaretheretoalanguage-processingsysteminwhichthecompiler

producesassemblylanguageratherthanmachinelanguage?

Thecompilermayproduceanassembly-languageprogramasitsoutput,because

assemblylanguageiseasiertoproduceasoutputandiseasiertodebug.

4.2.3Designgrammarsforthefollowinglanguages:

a)Thesetofallstringsof0sand1ssuchthatevery0isimmediatelyfollowedbyatleast1.

S->SS|1|01|e

4.3.1Thefollowingisagrammarfortheregularexpressionsoversymbolsaandbonly,using+inplaceof|forunions,toavoidconflictwiththeuseofverticalbarasmeta-symbolingrammars:

rexpr->rexpr+rterm|rterm

rterm->rtermrfactor|rfactor

rfactor->rfactor*|rprimary

rprimary->a|b

a)Leftfactorthisgrammar.

rexpr->rexpr+rterm|rterm

rterm->rtermrfactor|rfactor

rfactor->rfactor*|rprimary

rprimary->a|b

b)Doesleftfactoringmakethegrammarsuitablefortop-downparsing?

No,leftrecursionisstillinthegrammar.

c)Inadditiontoleftfactoring,eliminateleftrecursionfromtheoriginalgrammar.

rexpr->rtermrexpr’

rexpr’->+rtermrexpr|e

rterm->rfactorrterm’

rterm’->rfactorrterm|e

rfactor->rprimaryrfactor’

rfactor’->*rfactor’|e

rprimary->a|b

d)Istheresultinggrammarsuitablefortop-downparsing?

Yes.

Exercise4.4.1Foreachofthefollowinggrammars,derivepredictiveparsersandshowtheparsingtables.Youmayleft-factorand/oreliminateleft-recursionfromyourgrammarsfirst.Apredictiveparsermaybederivedbyrecursivedecentorbythetabledrivenapproach.Eitherwayyoumustalsoshowthepredictiveparsetable.

a)Thegrammarofexercise4.2.2(a).

4.2.2a)S->0S1|01

Thisgrammarhasnoleftrecursion.Itcouldpossiblybenefitfromleftfactoring.HereistherecursivedecentPPcode.

s(){

match(‘0’);

if(lookahead==‘0’)

s();

match(‘1’);

}

Or

Leftfactoringthegrammarfirst:

S->0S’

S’->S1|1

s(){

match(‘0’);s’();

}

s’(){

if(lookahead==‘0’)

s();match(‘1’);

else

match(‘1’);

}NowwewillbuildthePPtable

S->0S’

S’->S1|1

First(S)={0}

First(S’)={0,1}

Follow(S)={1,$}

Follow(S’)={1,$}

Non-Terminal

InputSymbol

0

1

$

S

S->0S’

S’

S’->S1

S’->1

Thepredictiveparsingalgorithmonpage227(fig4.19and4.20)canusethistablefornon-recursivepredictiveparsing.

b)Thegrammarofexercise4.2.2(b).

4.2.2b)S->+SS|*SS|awithstring+*aaa.

Leftfactoringdoesnotapplyandthereisnoleftrecursiontoremove.

s(){

if(lookahead==‘+’)

match(‘+’);s();s();

elseif(lookahead==‘*’)

match(‘*’);s();s();

elseif(lookahead==‘a’)

match(‘a’);

else

report(“syntaxerror”);

}

First(S)={+,*,a}

Follow(S)={$,+,*,a}

Non-Terminal

InputSymbol

+

*

a

$

S

S->+SS

S->*SS

S->a

Thepredictiveparsingalgorithmonpage227(fig4.19and4.20)canusethistablefornon-recursivepredictiveparsing.

5.1.1a,b,c:

InvestigatingGraphVizasasolutiontopresentingtrees

5.1.2:

 ExtendtheSDDofFig.5.4tohandleexpressionsasinFig.5.1:

1.L->EN

1.L.val=E.syn

2.E->FE'

1.E.syn=E'.syn

2.E'.inh=F.val

3.E'->+TEsubone'

1.Esubone'.inh=E'.inh+T.syn

2.E'.syn=Esubone'.syn

4.T->FT'

1.T'.inh=F.val

2.T.syn=T'.syn

5.T'->*FTsubone'

1.Tsubone'.inh=T'.inh*F.val

2.T'.syn=Tsubone'.syn

6.T'->epsilon

1.T'.syn=T'.inh

7.E'->epsilon

1.E'.syn=E'.inh

8.F-> digit

1.F.val= digit.lexval

9.F->(E)

1.F.val=E.syn

10.E->T

1.E.syn=T.syn

5.1.3a,b,c:

InvestigatingGraphVizasasolutiontopresentingtrees

5.2.1:

 WhatareallthetopologicalsortsforthedependencygraphofFig.5.7?

1.1,2,3,4,5,6,7,8,9

2.1,2,3,5,4,6,7,8,9

3.1,2,4,3,5,6,7,8,9

4.1,3,2

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 材料科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1