⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5.8.1_2.htm

📁 建立《编译原理网络课程》的目的不仅使学生掌握构造编译程序的原理和技术
💻 HTM
字号:
<html>

<head>
<title>编译原理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link type="text/css" rel="stylesheet" href="../css/specification.css">
</head>

<body>

<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.8.1_1.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.8.1_2b.htm'"></img></td>
</tr>
</table>
<br><br>

<font class="title2"><b>5.8.1.2 类型表达式的等价</b></font>         

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
在类型检查中,要判断两个语言结构的类型是否相等,因而,要判断两个语言结构的类型表达式是否等价。根据语言的类型体制和类型表达式的表示方法,分结构等价和各字等价。 
</p>
</td></tr></table>

<br>
<hr size=2 color=red width=90%>
<br>

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
<b>结构等价 </b>
</p>
</td></tr></table>

<table><tr><td></td>
<td class="content">
<P>
所谓两个类型表达式结构相等,是指两个类型表达式要么是相同的基本类型,要么是同样的类型构造符作用于类型等价的类型表达式。也就是说,两个类型表达式结构等价,当且仅当它们完全相同。图5.30是测试两个类型表达式结构等价的算法,假设类型构造符仅有数组、笛卡儿积、指针和函数,这个算法递归地比较两个类型表达式的结构。 
</p>
</td></tr></table>

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
<b><font color="#0000FF">FUNCTION</font></b>  eq(s,t):<font color="#0000FF">boolean</font>; <br>
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">BEGIN</font></b> <br>
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">IF</font></b> s和t是相同的基本类型 <b><font color="#0000FF">THEN</font></b> <br>
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">return</font></b>(<font color="#0000FF">true</font>); <br>
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">ELSE  
        IF</font></b> (s=ARRAY(s<sub>1</sub>,s<sub>2</sub>))  
        <b><font color="#0000FF">and</font></b> (t=ARRAY(t<sub>1</sub>,t<sub>2</sub>))  
        <b><font color="#0000FF">THEN</font></b> 
<br>
        <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">return</font></b>(eq(s<sub>1</sub>,t<sub>1</sub>)  
        <b><font color="#0000FF">and</font></b> eq(s<sub>2</sub>,t<sub>2</sub>))  
<br>
        <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">ELSE  
        IF</font></b> (s=s<sub>1</sub>*s<sub>2</sub>) <b><font color="#0000FF">and</font></b> 
        (t=t<sub>1</sub>*t<sub>2</sub>) <b><font color="#0000FF">THEN</font></b> 
<br>
        <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        <font color="#0000FF">return</font></b>(eq(s<sub>1</sub>,s<sub>2</sub>)  
        <b><font color="#0000FF">and</font></b> (eq(s<sub>2</sub>,t<sub>2</sub>))  
<br>
        <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        &nbsp;<font color="#0000FF">ELSE IF</font></b> (s=POINTER(s<sub>1</sub>)) <b><font color="#0000FF">and</font></b> 
        (t=POINTER(t<sub>1</sub>))<br>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
        &nbsp;&nbsp;&nbsp;<font color="#0000FF">THEN</font></b> 
     &nbsp;&nbsp; <b><font color="#0000FF">return</font></b>(eq(s<sub>1</sub>,t<sub>1</sub>)) 
<br>
        <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">ELSE  
        IF</font></b> (s=s<sub>1</sub>→s<sub>2</sub>) <b><font color="#0000FF">and</font></b> 
        (t=t<sub>1</sub>→t<sub>2</sub>) <b><font color="#0000FF">THEN</font></b> 
<br>
        <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
        &nbsp;&nbsp;&nbsp<font color="#0000FF">return</font></b>(eq(s<sub>1</sub>,t<sub>1</sub>)  
        <b><font color="#0000FF">and</font></b> eq(s<sub>2</sub>,t<sub>2</sub>))  
 <br>
        <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
  <font color="#0000FF">ELSE return</font></b>(<font color="#0000FF">false</font>) <br>
        <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">END</font></b> <br>
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>图5.30</b>  两个类型表达式s和t的结构等价测试 
</p>
</td></tr></table>


<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
在实际使用中,结构等价的概念常常需要修改以反映源语言的实际类型检查规则。例如,当数组作为参数传递时,我们可以不希望它的界作为类型的一部分,图中关于数组等价测试可改变为 
</p>
</td></tr></table>
    

<table width="487"><tr><td width="16">&nbsp&nbsp&nbsp&nbsp</td>
<td class="content" width="457">
<P>
<b><font color="#0000FF">IF</font></b> (s=ARRAY(s<sub>1</sub>,s<sub>2</sub>))  
        <b><font color="#0000FF">and</font></b> (t=ARRAY(t<sub>1</sub>,t<sub>2</sub>))  
        <b><font color="#0000FF">THEN</font></b> <br>
        &nbsp;&nbsp;&nbsp; <b>&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">return</font></b>(eq(s<sub>2</sub>,t<sub>2</sub>)) 
</p>
</td></tr></table>
    
<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
结果,在判断s和t是否等价中忽略了他们的界。 
</p>
</td></tr></table>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -