![編譯原理與實(shí)踐第六七章_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/7/6d97a6dc-f24d-4a66-b6a8-e5665d6cfa11/6d97a6dc-f24d-4a66-b6a8-e5665d6cfa111.gif)
![編譯原理與實(shí)踐第六七章_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/7/6d97a6dc-f24d-4a66-b6a8-e5665d6cfa11/6d97a6dc-f24d-4a66-b6a8-e5665d6cfa112.gif)
![編譯原理與實(shí)踐第六七章_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/7/6d97a6dc-f24d-4a66-b6a8-e5665d6cfa11/6d97a6dc-f24d-4a66-b6a8-e5665d6cfa113.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、The Exercises of Chapter Six6.2應(yīng)該在 num digit 產(chǎn)生式中再加一條語義規(guī)則:numd.count=1 用來進(jìn)行初始化。6.46.7 Consider the following grammar for simple Pascal-style declarations:delc var-list : typevar-list var-list, id | idtype integer | realWrite an attribute grammar for the type of a variable.SolutionGrammar RuleSemanti
2、c Rulesdelc var-list : typevar-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.10 a. Draw dependency graphs corresponding to each grammar rule of Example 6.14 (Page2
3、83) , and for the expression 5/2/2.0.b. Describe the two passes required to compute the attributes on the syntax tree of 5/2/2.0,including a possible order in which the nodes could be visited and the attribute values computed at each point.c. Write pseudcode for procedures that would perform the com
4、putations described in part(b).Solutiona. The grammar rules of Example 6.14 S expexp exp/exp | num | num.numThe dependency graphs for each grammar rule:S expvalSisFloatetypevalexpexpexp / expisFloatetypevalexpisFloatetypevalexp/isFloatetypevalexpexp numisFloatetypevalexpvalnumexp num.numisFloatetype
5、valexpvalnum.numThe dependency graphs for the expression: 5/2/2.0valSIsFloatetypevalexpisFloatetypevalexp/isFloatetypevalexpisFloatetypevalexp/isFloatetypevalexpvalnum.num(2.0)valnum(5)valnum(2)b. The first pass is to compute theetype from isFloat .The second pass is to compute the val from etype.Th
6、e possible order is as follows:valS122IsFloatetype3 val11exp1isFloat 4 etypeval9exp/isFloatetypeval10expisFloat5etypeval6exp/isFloat7etypeval8expvalnum.num(2.0)valnumvalnum(5)(2)c. The pseudcode procedure for the computation of the isFloat. Function EvalisFloat(T: treenode): BooleanVar temp1, temp2:
7、 Boolean BeginCase nodekind of T of exp:temp1= EvalisFloat(left child of T);if right child of T is not nil thentemp2=EvalisFloat( right child of T)return temp1 or temp2elsereturn temp1;num:return false;num.num:return true;endFunction Evalval(T: treenode, etype:integer): V ALUEVar temp1, temp2: VALUE
8、BeginCase nodekind of T ofS:Return(Evalval(left child of T, etype);Exp:If etype=EMPTY thenIf EvalisFloat(T) then etype:=FLOAT;Else etype=INT;Temp1=Evalval(left child of T, etype)If right child of T is not nil thenTemp2=Evalval(right child of T, etype);If etype=FLOAT thenReturn temp1/temp2;ElseReturn
9、 temp1 div temp2;ElseReturn(temp1);Num:If etype=INTReturn(T.val);ElseReturn(T.val);Num.num:Return(T.val).6.11Dependency graphs corresponding to the numbered grammar rules in 6.4:Dependency graph for the string 3 *(4+5) *6:6.21 Consider the following extension of the grammar of Figure 6.22(page 329)
10、to include function declarations and calls:program var-decls;fun-decls;stmtsvar-decls var-decls;var-decl|var-declvar-decl id: type-exptype-exp int|bool|array num of type-expfun-decls fun id (var-decls):type-exp;bodybody expstmts stmts;stmt| stmtstmt if exp then stmt | id:=expexp exp + exp| exp or ex
11、p | expexp|id(exps)|num|true|false|idexps exps,exp|expa. Devise a suitable tree structure for the new function type structure, and write a typeEqual function for two function types.b. Write semantic rules for the type checking of function declaration and function calls(represented by the rule exp id
12、(exps),similar to rules of table 6.10(page330).Solutiona. One suitable tree structure for the new function type structure:Fun(id)Type-exp .Type-expThe typeEqual function for two function type:Function typeEqual-Fun(t1,t2 : TypeFun): BooleanVar temp : Boolean;p1,p2:TypeExpbeginp1:=t1.lchild;p2:=t2.lc
13、hild;temp:=true;while temp and p1<>nil and p2<>nil dobegintemp=typeEqual-Exp(p1,p2);p1=p1.sibling;p2=p2.sibling;endif temp then return(typeEqual-Exp(t1.rchild,t2.rchild);return(temp);endb. The semantic rules for type checking of function declaration and function call:fun-decls fun id (va
14、r-decls):type-exp; bodyid.type.lchild:=var-decls.type;id.type.rchild:=type-exp.type;insert(,id.typefun)exp id(exps)if isFunctionType(id.type) andtypeEqual-Exp(id.type.lchild,exps.type) thenexp.type=id.type.rchild;else type-error(exp)The exercise of chapter seven7.2 Draw a possible organizatio
15、n for the runtime environment of the following C program, similar to that of Figure 7.4 (Page 354).a. After entry into block A in function f.b. After entry into block B in function a10;void g(char *s)mainchar *s =“ hello ”char c=s0;int x=1B: int a5;x = f(x,a);Int f(int i, int b )g(s);int j=i;r
16、eturn 0;A: int i=j;Char c = bI;return 0;Solutiona. After entry into block A in function f.a9a8a7Global/static areaa6a5a4a3a2a1a0*(s+4): o*(s+3):l*(s+2):l*(s+1):e*s:hx: 1Activation record of maini: 1b9b8b7b6b5b4Activationrecord of f afterb3entering the Block Ab2b1b0fpcontrol linkreturn addressj:1i:1c
17、:b1spfpspb. After entry into block B in function g.a9a8a7a6a5a4a3a2a1a0*(s+4): o*(s+3):l*(s+2):l*(s+1):e*s:hx: 0*(s+4): o*(s+3):l*(s+2):l*(s+1):e*s:hcontrol linkreturn addressc: ha4a3a2a1a0Global/static areaActivation record of mainActivationrecordofg afterentering the Block B7.8 In languages that p
18、ermit variable numbers of arguments in procedure calls, one way to find the first argument is to compute the arguments in reverse order, as described in section 7.3.1, page 361.a. One alternative to computing the arguments in reverse would be to reorganize the activation record to make the first arg
19、ument available even in the presence of variable arguments. Describe such an activation record organization and the calling sequence it would need.b. Another alternative to computing the arguments in reverse is to use a third point(besides the sp and fp), which is usually called the ap (argument poi
20、nter). Describe an activation record structure that uses an ap to find the first argument and the calling sequence it would need.Solutiona. The reorganized activation record.Control linkfpReturn addressArgument 1argument nLocal-varsp.The calling sequence will be:(1) store the fp as the control link
21、in the new activation record;(2) change the fp to point to the beginning of the new activation record;(3) store the return address in the new activation record;(4) compute the arguments and store their in the new activation record in order;(5) perform a jump to the code of procedure to be called. b. The reorganized activation record.apArgument 1argument nfpControl linkReturn addressspLocal-var.The calling sequence will be:(1) set ap point t
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 人教版地理八年級下冊6.2《白山黑水-東北三省》聽課評課記錄1
- 蘇科版九年級數(shù)學(xué)聽評課記錄:第50講 二次函數(shù)y
- 七年級下聽評課記錄數(shù)學(xué)
- 新版湘教版秋八年級數(shù)學(xué)上冊第四章一元一次不等式組課題一元一次不等式的應(yīng)用聽評課記錄
- 申請?jiān)诩易詫W(xué)的協(xié)議書(2篇)
- 電價(jià)變更合同范本(2篇)
- 蘇科版數(shù)學(xué)七年級下冊聽評課記錄8.1同底數(shù)冪的乘法
- 湘教版數(shù)學(xué)九年級下冊2.5《直線與圓的位置關(guān)系》聽評課記錄3
- 一年級上冊數(shù)學(xué)聽評課記錄《3.8 小雞吃食 》 北師大版
- 2025年錫焊專用設(shè)備合作協(xié)議書
- 小學(xué)數(shù)學(xué)三年級下冊第八單元《數(shù)學(xué)廣角-搭配(二)》大單元集體備課整體設(shè)計(jì)
- (高清版)TDT 1031.6-2011 土地復(fù)墾方案編制規(guī)程 第6部分:建設(shè)項(xiàng)目
- 2024年江蘇省高中學(xué)業(yè)水平測試生物試卷
- 露天采場危險(xiǎn)有害因素辨識(shí)
- 蘇教版一年級上、下冊勞動(dòng)與技術(shù)教案
- 七上-動(dòng)點(diǎn)、動(dòng)角問題12道好題-解析
- 山東曲阜的孔廟之旅
- 一到六年級語文詞語表人教版
- 中煤集團(tuán)綜合管理信息系統(tǒng)運(yùn)維服務(wù)解決方案-V3.0
- 直播營銷與運(yùn)營(第2版)全套教學(xué)課件
- 高二英語閱讀理解30篇
評論
0/150
提交評論