编译原理作业集-第七章(4)

2019-08-03 10:59

编译原理作业集-第七章 语义分析和中间代码产生

产生式 动作 S→for E do S1 S.begin := newlabel S.first := newtemp S.last := newtemp S.curr:= newtemp S.code:=gen(S.first “:=” E.init) ||gen(S.last “:=” E.final) ||gen(“if” S.first “>” S.last “goto” S.next) ||gen(S.curr “:=” S.first) ||gen(S.begin “:” ) ||gen(“if ” S.curr “>” S.Last “goto” S.next) ||S1.code ||gen(S.curr := succ(S.curr)) ||gen(“goto” S.begin) E→v:=initial to final E.init := initial.place E.final := final.place

10. 假如有下面的Pascal说明

TYPE atype=ARRAY [0..9,-10..10] OF integer; cell=RECORD a,b:integer END;

pcell=↑cell;

foo=ARRAY [1..100] OF cell;

FUNCTION bar(r:integer;y:cell):pcell; BEGIN……END;

写出atype,cell,pcell,foo和bar的类型表达式。

10.答案:

atype: ARRAY(0..9, ARRAY(-10..10, integer)); cell: RECORD((a× integer)× (b×integer)); pcell: POINTER(cell);

或: POINTER(RECORD((a ×integer)× (b× integer))); foo: ARRAY(1..100, cell);

或 : ARRAY(1..100, RECORD((a ×integer)× (b× integer))); bar: integer× cell→pcell;

或 : integer× cell→POINTER(RECORD((a×integer) ×(b×integer)));

11. 已知语句序列的类型检查翻译方案:

S→id:=E { if id.type=E.type then S.type:=void else S.type:=type_error } S→if E then S1 { if E.type=boolean

西安理工大学计算机科学与工程学院,张发存编写 2013-5-20,9:37:18

- 16 - - -

编译原理作业集-第七章 语义分析和中间代码产生

then S.type:=S1.type else S.type:=type_error } S→while E do S1 { if E.type=boolean then S.type:=S1.type else S.type:=type_error } S→S1:S2 { if S1.type=void and S2.type=void then S.type:=void else S.type:=type_error } 修改该翻译方案,使之能够处理:

(1)语句有值。赋值语句的值是赋值号右边的表达式的值。条件语句或当语句的值是语句体的值,语句表的值是表中最后一个语句的值。

(2)布尔表达式。加上逻辑算符and,or和not,还有关系算符的产生式,然后给出适当的翻译规则,计算出这些表达式的类型。 11. 答案:(1)

S -> id:=E { S.type:= IF id.type=E.type THEN E.type ELSE type_error; S.val:= IF id.type=E.type THEN E.val ELSE val_error } S -> IF E THEN S1 {S.type:= IF E.type=boolean THEN S1.type ELSE type_error S.val:= IF E.type=boolean THEN S1.val ELSE val_error}

S -> WHILE E DO S1 {S.type:= IF E.type=boolean THEN S1.type ELSE type_error S.val:= IF E.type=boolean THEN S1.val ELSE val_error} S -> S1;S2 {S.type:=S2.type; S.val:=S2.val} (2)

E -> E1 AND E2 {E.type:= IF(E1.type=boolean)AND(E2.type=boolean) THEN boolean ELSE type_error}

E -> E1 OR E2 {E.type:= IF(E1.type=boolean)AND(E2.type=boolean) THEN boolean ELSE type_error} E -> NOT E1 {E.type:= IF(E1.type=boolean) THEN boolean ELSE type_error}

西安理工大学计算机科学与工程学院,张发存编写 2013-5-20,9:37:18

- 17 - - -

编译原理作业集-第七章 语义分析和中间代码产生

E -> E1 op E2 {E.type:= IF(E1.type=E2.type) THEN boolean ELSE type_error} 注:op为关系运算符,包括 =, <>, <, >, <=, >=

12. 文法G[P]及其产生式如下:

P→D;E

D→D;D|id:T

T→list of T|char|integer E→(L)|Literal|num|id L→E,L|E

这个文法产生由字面常量组成的表。符号的解释和文法(7.11)相同,增加类型List,它表示类型为T的元素构成的表。写一个翻译模式,以确定表达式(E)和表(L)的类型(注:表中每个元素的类型是一样的)。

12.答案:

D -> id:T {addtype(id.entry,T.type))} T -> char {T.type:=char} T -> integer {T.type:=integer}

T -> list of T1 {T.type:=list(T1.type)} E -> num {E.type:=integer} E -> id {E.type:=lookup(id.entry)} E -> Literal {E.type:=char} E -> (L) {E.type:=list(L.type)}

L -> E,L1 {L.type:=IF (E.type=L1.type) THEN E.type ELSE type_error} L -> E {L.type:=E.type}

西安理工大学计算机科学与工程学院,张发存编写 2013-5-20,9:37:18 - 18 - - -


编译原理作业集-第七章(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:避税 节税 逃税问题的研究

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: