《中间代码生成》PPT课件.ppt





《《中间代码生成》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《中间代码生成》PPT课件.ppt(40页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 第七章 中间代码生成 序 7.1 中间语言 7.2 说明语句 7.3 赋值语句 7.4 布尔表达式 7.6 回填 7.7 过程语句 练习7.4 布尔表达式 布尔表达式:用布尔运算符号(and,or,not)作用到布尔变量或关系表达式上而组成 布尔表达式的作用:1.用作计算逻辑值2.用作控制流语句如if-then,if-then-else和 while-do等之中的条件表达式 本节考虑由如下文法生成的布尔表达式:EE or|E and E|not E|(E)|id relop id|true|false 7.4.1 翻译布尔表达式的方法 表示一个布尔表达式的值 方法一:用数值表示真和假,从而对
2、布尔表达式的求值可以象对算术表达式的求值那样一步一步地来计算 方法二:通过程序的控制流,即用程序中控制转移到达的位置来表示布尔表达式的值方法二用于翻译控制流语句中的布尔表达式尤其方便。7.4.2 数值表示法 用1表示真,0表示假来实现布尔表达式的翻译布尔表达式:a or b and not c 翻译成三地址代码序列:100:t1:=not c 101:t2:=b and t1 102:t3:=a or t1 关系表达式:ab 等价于if ab then 1 else 0 翻译成三地址代码序列:100:if ab goto l03 101:t:=0 102:goto l04。103:t:=1 1
3、04:图7.11 关于布尔表达式的数值表示法的翻译模式 EE1 or E2 E.place:=newtemp;emit(E.place:=E1.placeor E2.place)EE1 and E2E.place:=newtemp;emit(E.place:=E1.placeand E2.place)(接上页)Enot E1 E.place:=newtemp;emit(E.place:=not E1.place)Eid1 relop id2 E.place:=newtemp;emit(if id1.place relop.op id2.placegoto nextstat+3);emit(E.
4、place:=0);emit(gotonextstat+2);emit(E.place:=1)Eture E.place:=newtemp;emit(E.place:=1)Efalse E.place:=newtemp;emit(E.place:=0)7.4.3 控制流语句 文法:Sif E then S1|if E then S1 else S2|while E do S1 E.codeS1.codeE.true:.E.false:(a)if-thento E.trueto E.false代码结构:E.codeS1.codeE.true:S2.codeE.false:goto S.next.
5、S.next:to E.trueto E.false(b)if-then-elseE.codeS1.codeE.true:E.false:goto S.begin.S.begin:to E.falseto E.true(c)while-do语法制导定义:产生式语义规则Sif E then S1E.true:=newlabel;E.false:=S.next;S1.next:=S.next S.code:=E.code|gen(E.true:)|S1.code产生式语义规则Sif E then S1else S2E.true:=newlabel;E.false:=newlabel;S1.next
6、:=S.next;S2.next:=S.next;S.code:=E.code|gen(E.true:)|S1.code gen(gotoS.next)|gen(E.false:)|S2.code(接上页)产生式语义规则Swhile E do S1S.begin:=newlabel;E.true:=newlabel;E.false:=S.next;S1.next:=S.begin;S.code:=gen(S.begin:E.code gen(E.true:)S1.code gen(gotoS.begin)(接上页)7.4.4 控制流语句中的布尔表达式的翻译基本思想:假定E 形如ad,则将生成如
7、下的 E的代码:if ab goto E.true 表7.5 语法制导定义 goto E.false 产生式语义规则EE1 or E2E1.true:=E.true;E1.false:=newlabel;E2.true:=E.true;E2.false:=E.false E.code:=E1.code|gen(E1.false:)|E2.code产生式语义规则EE1 and E2E1.true:=newlabel;E1.false:=E.false;E2.true:=E.true;E2.false:=E.false;E.code:=E1.code gen(E1.true:)E2.code(接上
8、页)Enot E1 E1.true:=E.false;E1.false:=E.true;E.code:=E1.code 产生式语义规则Eid1 relop id2E.code:=gen(if id1.place relop.op id2.place goto E.true)|gen(goto E.false)Etrue E.code:=gen(goto E.true)Efalse E.code:=gen(goto E.false)E的true和false属性都是继承属性E(E1)E1.true:=E.true;E1.false:=E.false;E.code:=E1.code 例7.4 考虑如
9、下语句:while ab do if cd then x:=yz else x:y-z 根据前面所述,生成代码如右:L1:if ab goto L2 goto Lnext L2:if cd goto L3 goto L4 L3:t1:=yz x:t1 goto L1 L4:t2:=yz x:t2 goto L1 Lnext:7.5 CASE语句 switch语句的语法:switch expression begin case valuE1:statement1 case valuE2:statement2 .case value n-1:statement n-1 defalt:stateme
10、nt n end switch语句翻译成的三地址代码控制流程:1对表达式求值;2在列出的valuE1,valuE2,,value n-1 这些值中寻找与表达式的值相等的值。如果没有这样的值存在,则让“缺席值”与表达式匹配;3执行在(2)中寻找到的值相联系的语 句(某个statement)。switch语句的目标代码结构:对expression求值并置于t的有关代码 goto test L1:有关statement1的代码 goto next L2:有关statement2的代码 goto next Ln-1:有关statement n-1的代码 goto next Ln:有关statement
11、n的代码 goto next(接上页)test:if tvalue1 goto L1 if tvalue2 goto L2 .if tvaluen-1goto Ln-1 goto Ln next:expression:选择器,将被计算出一个值。valueE1,valueE2,,value n-1:表达式可能取的 值。default:“缺席值”。用来在expression的值不等 于任何value i时来匹配expression。7.6 回填 两遍扫描:从给定的输入构造出一棵语法树;对语法树按深度优先遍历,来进行定义中给出的翻译。一遍扫描:先产生暂时没有填写目标标号的转移指令。对于每一条这样的指
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 中间代码生成 中间 代码 生成 PPT 课件

限制150内