




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、the exercises of chapter six6.2应该在numdigit产生式中再加一条语义规则:numd.count=1用来进行初始化。6.46.7 consider the following grammar for simple pascal-style declarations:delc var-list : typevar-list var-list, id | idtype integer | real write an attribute grammar for the type of a variable.solution grammar rulesemantic
2、rulesdelc var-list : typevar-list.type = type.typevar-list1 var-list2, id val-list2.type=var-list1.typeid.type=var-list1.typevar-list idid.type=var-list.typetype integer type.type= intergertype realtype.type=real6.10 a. draw dependency graphs corresponding to each grammar rule of example 6.14 (page
3、283) , 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 c
4、omputations described in part(b).solutiona.the grammar rules of example 6.14s expexp exp/exp | num | num.numthe dependency graphs for each grammar rule:s exp valsisfloat etype valexpexp exp / exp isfloat etype valexpisfloat etype valexp/isfloat etype valexpexp numisfloat etype valexp valnumexp num.n
5、umisfloat etype valexp valnum.numthe dependency graphs for the expression: 5/2/2.0 valsisfloat etype val expisfloat etype valexp/isfloat etype valexpisfloat etype valexp /isfloat etype valexpvalnum.num (2.0) val numvalnum(5) (2)b. the first pass is to compute the etype from isfloat. the second pass
6、is to compute the val from etype. the possible order is as follows: vals12 2isfloat etype3 val 11 exp 1isfloat 4 etype val9exp/isfloat etype val10expisfloat 5etype val6exp /isfloat 7etype val8expvalnum.num (2.0) val numvalnum(5) (2)c. the pseudcode procedure for the computation of the isfloat.functi
7、on evalisfloat(t: treenode): booleanvar temp1, temp2: booleanbegincase nodekind of t ofexp: 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: tre
8、enode, etype:integer): valuevar temp1, temp2: valuebegincase nodekind of t ofs:return(evalval(left child of t, etype);exp: if etype=empty thenif evalisfloat(t) then etype:=float;elseetype=int;temp1=evalval(left child of t, etype)if right child of t is not nil thentemp2=evalval(right child of t, etyp
9、e);if etype=float thenreturn temp1/temp2;elsereturn 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
10、 extension of the grammar of figure 6.22(page 329) 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
11、 if exp then stmt | id:=expexp exp + exp| exp or exp | 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 declaratio
12、n and function calls(represented by the rule exp id(exps),similar to rules of table 6.10(page 330).solutiona. one suitable tree structure for the new function type structure:fun(id).type-exptype-expthe typeequal function for two function type:function typeequal-fun(t1,t2 : typefun): booleanvar temp
13、: boolean;p1,p2:typeexpbeginp1:=t1.lchild;p2:=t2.lchild;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
14、 declaration and function call:fun-decls fun id (var-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 exerc
15、ise of chapter seven7.2 draw a possible organization 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;char *s = “hello”int f(int i, int b ) int j=i;a: int i=j;
16、char c = bi;return 0;void g(char *s)char c=s0;b:int a5;mainint x=1x = f(x,a);g(s);return 0;solutiona. after entry into block a in function f.activation record of mainglobal/static areaa9a8a7a6a5a4a3a2a1a0*(s+4): o *(s+3):l*(s+2):l*(s+1):e*s:hx: 1fpspactivation record of f after entering the block ai
17、: 1b9b8b7b6b5b4b3b2b1b0 control linkreturn addressj:1i:1c:b1b. after entry into block b in function g.activation record of mainglobal/static areaa9a8a7a6a5a4a3a2a1a0*(s+4): o *(s+3):l*(s+2):l*(s+1):e*s:hx: 0spfpactivation record of g after entering the block b*(s+4): o *(s+3):l*(s+2):l*(s+1):e*s:h c
18、ontrol linkreturn addressc: ha4a3a2a1a07.8 in languages that permit 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 woul
19、d be to reorganize the activation record to make the first argument 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(beside
20、s the sp and fp), which is usually called the ap (argument pointer). 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.spfp control linkreturn addressargument 1argument nlocal-var.the
21、 calling sequence will be:(1) store the fp as the control link 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.apfpargument 1argument n sp control linkreturn addresslocal-var. the calling sequence will be:(1) set
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 共情教育塑造孩子情感智商的必由之路
- 智慧办公楼宇的水资源管理与节能创新
- 医学伦理与心理关怀的结合
- BJ-13-生命科学试剂-MCE
- 2024年天津市河北区九上化学期末联考试题含解析
- 石家庄财经职业学院《摄影摄像基础》2023-2024学年第一学期期末试卷
- 新疆博乐市第九中学2025届九年级化学第一学期期末质量跟踪监视模拟试题含解析
- 湖北铁道运输职业学院《西方史学史》2023-2024学年第一学期期末试卷
- 公共卫生设施建设在城市社区安全防范体系中的应用与研究报告
- 共享出行市场2025年共享电动平衡车商业模式创新报告:竞争格局演变
- 第五讲社会建设
- GB/T 35273-2020信息安全技术个人信息安全规范
- GB/T 20303.1-2006起重机司机室第1部分:总则
- GB 18068-2000水泥厂卫生防护距离标准
- 教师调动登记表(模板)
- 《长方形和正方形》 完整版课件
- 2022年医院收费员考试试题及答案
- 国家开放大学电大专科《市场营销学》2021期末试题及答案(试卷号2175)
- 再遇青春同学聚会画册PPT模板
- 钢筋下料单(参考模板)
- OPGW光缆计算
评论
0/150
提交评论