编译原理龙书课后部分答案(英文版)文档格式.doc
- 文档编号:14569208
- 上传时间:2022-10-23
- 格式:DOC
- 页数:9
- 大小:73.50KB
编译原理龙书课后部分答案(英文版)文档格式.doc
《编译原理龙书课后部分答案(英文版)文档格式.doc》由会员分享,可在线阅读,更多相关《编译原理龙书课后部分答案(英文版)文档格式.doc(9页珍藏版)》请在冰豆网上搜索。
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.
b)Doesleftfactoringmakethegrammarsuitablefortop-downparsing?
No,leftrecursionisstillinthegrammar.
c)Inadditiontoleftfactoring,eliminateleftrecursionfromtheoriginalgrammar.
rtermrexpr’
rexpr’->
+rtermrexpr|e
rfactorrterm’
rterm’->
rfactorrterm|e
rprimaryrfactor’
rfactor’->
*rfactor’|e
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:
0S’
S’->
S1|1
s’();
s’(){
match(‘1’);
else
match(‘1’);
}NowwewillbuildthePPtable
First(S)={0}
First(S’)={0,1}
Follow(S)={1,$}
Follow(S’)={1,$}
Non-Terminal
InputSymbol
1
$
S
S->
0S’
S’
S’->
S1
Thepredictiveparsingalgorithmonpage227(fig4.19and4.20)canusethistablefornon-recursivepredictiveparsing.
b)Thegrammarofexercise4.2.2(b).
4.2.2b)S->
+SS|*SS|awithstring+*aaa.
Leftfactoringdoesnotapplyandthereisnoleftrecursiontoremove.
if(lookahead==‘+’)
match(‘+’);
s();
elseif(lookahead==‘*’)
match(‘*’);
elseif(lookahead==‘a’)
match(‘a’);
report(“syntaxerror”);
First(S)={+,*,a}
Follow(S)={$,+,*,a}
+
*
a
+SS
*SS
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
.syn=Esubone'
4.T->
FT'
1.T'
2.T.syn=T'
5.T'
*FTsubone'
1.Tsubone'
.inh=T'
.inh*F.val
2.T'
.syn=Tsubone'
6.T'
epsilon
.syn=T'
.inh
7.E'
1.E'
.syn=E'
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:
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 课后 部分 答案 英文