Homework 6

(1)针对以下C函数,给出其函数体三地址码。

#define N 32
int a[N],b[N];
int arr[N+1][N+1];
void lcs()
{for (i = 1; i <= length1; ++i){for (j = 1; j <= length2; ++j) {if (a[i - 1] == b[j - 1]) { //串中的下标从0开始arr[i][j] = arr[i - 1][j - 1] + 1;}else {arr[i][j] = arr[i - 1][j] > arr[i][j - 1] ? arr[i - 1][j] : arr[i][j - 1];}}
}
} // end of lcs()

画出该函数体的语法树如下:

#mermaid-svg-jHndSs2E3hDRsOR4 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-jHndSs2E3hDRsOR4 .error-icon{fill:#552222;}#mermaid-svg-jHndSs2E3hDRsOR4 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-jHndSs2E3hDRsOR4 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-jHndSs2E3hDRsOR4 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-jHndSs2E3hDRsOR4 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-jHndSs2E3hDRsOR4 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-jHndSs2E3hDRsOR4 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-jHndSs2E3hDRsOR4 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-jHndSs2E3hDRsOR4 .marker.cross{stroke:#333333;}#mermaid-svg-jHndSs2E3hDRsOR4 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-jHndSs2E3hDRsOR4 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-jHndSs2E3hDRsOR4 .cluster-label text{fill:#333;}#mermaid-svg-jHndSs2E3hDRsOR4 .cluster-label span{color:#333;}#mermaid-svg-jHndSs2E3hDRsOR4 .label text,#mermaid-svg-jHndSs2E3hDRsOR4 span{fill:#333;color:#333;}#mermaid-svg-jHndSs2E3hDRsOR4 .node rect,#mermaid-svg-jHndSs2E3hDRsOR4 .node circle,#mermaid-svg-jHndSs2E3hDRsOR4 .node ellipse,#mermaid-svg-jHndSs2E3hDRsOR4 .node polygon,#mermaid-svg-jHndSs2E3hDRsOR4 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-jHndSs2E3hDRsOR4 .node .label{text-align:center;}#mermaid-svg-jHndSs2E3hDRsOR4 .node.clickable{cursor:pointer;}#mermaid-svg-jHndSs2E3hDRsOR4 .arrowheadPath{fill:#333333;}#mermaid-svg-jHndSs2E3hDRsOR4 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-jHndSs2E3hDRsOR4 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-jHndSs2E3hDRsOR4 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-jHndSs2E3hDRsOR4 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-jHndSs2E3hDRsOR4 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-jHndSs2E3hDRsOR4 .cluster text{fill:#333;}#mermaid-svg-jHndSs2E3hDRsOR4 .cluster span{color:#333;}#mermaid-svg-jHndSs2E3hDRsOR4 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-jHndSs2E3hDRsOR4 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

S
F
for i = 1 to length1
S
F
S
for j =1 to length2
if
E
E
V
Elist
]
a
[
E
i-1
==
E
b[j-1]
then
M
S
V
Elist
Elist
arr
=
E
arr[i-1,j-1]+1
]
,
E
j
[
E
i
N
else
S
V
arr[i,j]
=
E
arr[i - 1][j] > arr[i][j - 1] ? arr[i - 1][j] : arr[i][j - 1]

三地址码:

i=1
F1:
if i>length1 goto Fnext
j = 1
F2:
if j>length2 goto F4
#计算a[i-1]:
t0 = i-1
t1 = a
t2 = t0 * 4
t3 = t1[t2]
#计算b[j-1]:
t4 = j - 1
t5 = b
t6 = t4 * 4
t7 = t5[t6]
if t3 == t7 goto M1
goto M2
M1:
#计算arr[i,j]:
t8 = i * 33
t8 = t8 + j
t9 = arr
t10 = t8 * 4
#计算arr[i-1,j-1]+1
t11 = i-1
t12 = j-1
t13 = t11 * 33
t13 = t13 + t12
t14 = arr
t15 = t13 * 4
t16 = t14[t15]
t17 = 16 + 1
#arr[i][j] = arr[i-1,j-1]+1
t9[t10] = t17
goto F3
M2:
#计算arr[i][j]
t18 = i * 33
t18 = t18 + j
t19 = arr
t20 = t18 * 4
t33 = t19[t20]
#计算arr[i-1][j]
t21 = i-1
t22 = j
t23 = t21 * 33
t23 = t23 + t22
t24 = arr
t25 = t23 * 4
t26 = t24[t25]
#计算arr[i][j-1]
t27 = i
t28 = j-1
t29 = t27 * 33
t29 = t29 + t28
t30 = arr
t31 = t29 * 4
t32 = t30[t31]
if t26 > t32 goto L1
t33 = t32
goto F3
L1:
t33 = t26
F3:
j = j + 1
goto F2
F4:
i = i + 1
goto F1

(2)按要求给出以下C表达式的三地址代码:

i && j && i > j && j < 10
|| (i>10)
|| !(i <= 100) && ( j <= 100) && !( i>50 )
|| !(j > 20 && i < -10)

(2.1)数值计算方式

# i
if i goto L0
t0 = 0
goto L1
L0:
t0 = 1
L1:
# j
if j goto L2
t1 = 0
goto L3
L2:
t1 = 1
# i > j
L3:
t2 = t0 and t1
if i > j goto L4
t3 = 0
goto L5
L4:
t3 = 1
# j < 10
L5:
t4 = t2 and t3
if j < 10 goto L6
t5 = 0
goto L7
L6:
t5 = 1
L7:
# i > 10
t6 = t4 and t5
if i >10 goto L8
t7 = 0
goto L9
L8:
t7 = 1
L9:
t8 = t6 or t7
# !(i <= 100)
if i<= 100 goto L10
t9 = 0
goto L11
L10:
t9 = 1
L11:
t10 = not t9
# j<=100
if j <= 100 goto L12
t11 = 0
goto L13
L12:
t11 = 1
L13:
t12 = t10 and t11
# i>50
if i>50 goto L14
t13 = 0
goto L15
L14:
t13 = 1
L15:
t14 = not t13
t15 = t12 and t14
t16 = t8 or t15
# j>20
if j>20 goto L16
t17 = 0
goto L17
L16:
t17 = 1
L17:
# i < -10
if i < -10 goto L18
t18 = 0
goto L19
L18:
t18 = 1
L19:
t19 = t17 and t18
t20 = not t19
t21 = t16 or t20

(2.2)短路计算方式:

​ (a)翻译方案

if i goto L0
goto L3
L0:
if j goto L1
goto L3
L1:
if i > j goto L2
goto L3
L2:
if j < 10 goto T
goto L3
L3:
if i>10 goto T
goto L4
L4:
if i<=100 goto L7
goto L5
L5:
if j<= 100 goto L6
goto L7
L6:
if i>50 goto L7
goto T
L7:
if j>20 goto L8
goto T
L8:
if i < -10 goto F
goto T

​ (b)更精简的短路代码翻译方案

if !i goto L0
if !j goto L0
if i<=j goto L0
if j<10 gotoT
L0:
if i>10 goto T
if i<=100 goto L1
if j>100 goto L1
if i<=50 goto T
L1:
if j<=20 goto T
if i>=-10 goto T
goto F

编译原理—计算三地址码、布尔语句翻译相关推荐

  1. 【编译原理系列】布尔表达式及控制语句翻译

    布尔表达式 从高到低:not and or 短路计算可以回避指针为空时对ptr^.data=x的判断,从而 直接计算的语法制导翻译 (1)E→E1 or E2 { E.place := newtemp ...

  2. 【编译原理】 三地址语句的具体实现

    三地址语句的具体实现 三地址代码是由下面一般形式的语句构成的序列: x:=y op z 其中, x.y.z为名字.常数或编译时产生的临时变量:op代表运算符号. 每个语句的右边只能有一个运算符.(有重 ...

  3. 编译原理|第7章|布尔表达式的翻译

    数值表示法 如同计算算数表达式一样,一步步计算(先算括号,后外面) 例子: a or b and not c 翻译成: T1=not c T2=b and T1 T3=a and T2 2.   a& ...

  4. 编译原理(十)语法制导翻译

    语法制导定义 Syntax-Driect Definition SDD 语法制导的翻译方案Syntax-driected translation scheme SDT,可以看作SDD的具体实现方案,S ...

  5. 编译原理--语法制导的翻译

    前言 把一些属性附加到代表语言构造的文法符号上, 以把信息和一个语言的构造联系起来. 通过语义规则来说明文法符号的属性值如何确定. 产生式 语义规则 E -> E_{1} + T E.code ...

  6. 【编译原理笔记10】语法制导翻译:在递归预测过程中进行翻译,L属性定义的自底向上翻译

    本次笔记内容: 5-7 在递归预测过程中进行翻译 5-8 L属性定义的自底向上翻译 本节课幻灯片,见于我的 GitHub 仓库:第10讲 语法制导翻译_3 文章目录 在递归的预测分析过程中进行翻译 算 ...

  7. 【编译原理笔记09】语法制导翻译:语法制导翻译方案,在非递归的预测分析过程中进行翻译

    本次笔记内容: 5-5 语法制导翻译方案 5-6 在非递归的预测分析过程中进行翻译 本节课幻灯片,见于我的 GitHub 仓库:第9讲 语法制导翻译_2 文章目录 语法制导翻译方案 语法制导翻译方案 ...

  8. 【编译原理笔记08】语法制导翻译:语法制导定义,SSD的求值顺序,S属性定义与L属性定义

    本次笔记内容: 5-1 语法制导翻译概述 5-2 语法制导定义 5-3 SSD的求值顺序 5-4 S属性定义与L属性定义 本节课幻灯片,见于我的 GitHub 仓库:第8讲 语法制导翻译_1 文章目录 ...

  9. [编译原理]DO-WHILE循环语句的翻译程序设计(LR(1)方法、输出四元式)C++实现

    题目: DO-WHILE循环语句的翻译程序设计(LR(1)方法.输出四元式) 1 课设任务概述 初始条件: ​ 理论:完成编译原理,数据结构.高级编程语言.汇编语言等相关课程的学习,基于计算机专业知识 ...

最新文章

  1. Android Studio使用总结
  2. sizeof 使用注意
  3. CSS——清除浮动的六种解决方案
  4. 三甲医院需要部署哪些网络安全设备
  5. 转载:指定浏览器打开指定的网页
  6. 国考临近——送给奔跑在国考路上的人的一些话
  7. android textview动态设置,android – 如何动态设置文本到TextView?
  8. 基于deepin-wine的windows软件打包deb安装包教程
  9. android新闻客户端发展趋势,基于Android平台的新闻客户端设计与实现
  10. c#使用资源文件完成国际化
  11. AngularJS 控制器 ng-controller
  12. MATLAB 图像伪彩色处理
  13. uview实现点击预览图片效果
  14. 高斯消元求解四种方程组
  15. godaddy 服务器位置,Godaddy DNS服务器列表
  16. linux怎么查看终端号,怎么查看linux 图形终端设备号
  17. 声学模型GMM-HMM训练
  18. 阿里云产品以及平时的维护
  19. # Windows下关于安装Geany编辑器过程中的一点小发现(可能对初次安装的人有用)
  20. SpringSecurity-基于微服务的认证与权限访问

热门文章

  1. 无法将“pyinstall”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
  2. Android apk破解反编译应用-EasyDump
  3. Python矩阵相乘
  4. 华为消费者云背后的微服务实践经验
  5. 长得漂亮的女人比有才华的女人更吃香吗?
  6. 计算机数据存储ROM与RAM
  7. LCN 分布式事务框架
  8. 兄弟连官方微博上线!http://t.sina.com.cn/lampbrother
  9. flv.js及flv.min.js
  10. iOS使用UIAppearance