- 1、本文档共11页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
编译原理与实践 第六 七章 答案
The Exercises of Chapter Six
6.2
应该在num→digit产生式中再加一条语义规则:numd.count=1用来进行初始化。
6.4
6.7 Consider the following grammar for simple Pascal-style declarations:
delc → var-list : type
var-list → var-list, id | id
type → integer | real
Write an attribute grammar for the type of a variable.
[Solution]
Grammar Rule Semantic Rules
delc → var-list : type var-list.type = type.type
var-list1 → var-list2, id val-list2.type=var-list1.type
id.type=var-list1.type
var-list → id id.type=var-list.type
type → integer type.type= INTERGER
type → real type.type=REAL
6.10 a. Draw dependency graphs corresponding to each grammar rule of Example 6.14 (Page 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 computations described in part(b).
[Solution]
a. The grammar rules of Example 6.14
S → exp
exp → exp/exp | num | num.num
The dependency graphs for each grammar rule:
S → exp
val S
isFloat etype val exp
exp → exp / exp
isFloat etype val exp
isFloat etype val exp / isFloat etype val exp
exp → num
isFloat etype val exp
val num
exp → num.num
isFloat etype val exp
val num.num
The dependency graphs for the expression: 5/2/2.0
val S
IsFloat etype val exp
isFloat etype val exp / isFloat etype val exp
isFloat etype val exp / isFloat etype val exp val num.num
(2.0)
val num val num
(2)
b. The first pass is to compute the etype from isFloat.
The second pass is to compute the val from etype.
The possible order is as follows:
val S12
2IsFloat etype3 val 11 exp
1 isFloat 4 etype val9 exp / isFloat etype val10 exp
isF
文档评论(0)