📄 5.8.1.htm.bak
字号:
<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.0.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.8.1b.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>5.8.1 类型表达式</b></font>
<table><tr><td></td>
<td class="content">
<P><p>
<b>5.8.1.1 类型表达式定义 </b>
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
语言结构的类型由类型表达式指称,类型表达式依赖于程序语言的类型体制。一般来说,类型表达式或者是简单类型表达式,或者是构造符作用在类型表达式上得到的类型表达式。<font class="emphasize">类型表达式的定义如下</font>:
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
<b>(1)类型名和基本类型是类型表达式。</b>integer、char、real、boolean是基本类型,所以它们是类型表达式。另外,void表示"无类型",type_error表示"出错类型",它们也是类型表达式。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
<b>(2)类型构造符作用于类型表达式的结果仍然是类型表达式。</b>类型构造符包括:
<br> (a)数组构造符<b>ARRAY</b>:若T是类型表达式,则ARRAY(I,T)是类型表达式,它指称一个数组类型,其成分类型为T,下标值集合为I。
<br> (b)笛卡儿乘积X:若T<sub>1</sub>、T<sub>2</sub>是类型表达式,则T<sub>1</sub> X T<sub>2</sub>是类型表达式,且X是左结合。
<br> (c)记录类型构造符<b>RECORD</b>:若有标识符N<sub>1</sub>、N<sub>2</sub>……、N<sub>n</sub>与类型表达式T<sub>1</sub>、T<sub>2</sub>、……T<sub>n</sub>,则<b>RECORD</b>(N<sub>1</sub> X T<sub>1</sub>) X (N<sub>2</sub> X T<sub>2</sub>)X……X(N<sub>n</sub> X T<sub>n</sub>))是一个类型表达式,它指称一个记录类型,这个记录有n个域,每个域的域名为N<sub>i</sub>,相应的类型为T<sub>i</sub>指称的类型(i=1、2、……、n)。
<br> (d)指针类型构造符<b>POINTER</b>:若T是类型表达式,则POINTER(T)是类型表达式,它指称一个指针类型。
<br> (e)函数类型构造符→:若D<sub>1</sub>、D<sub>2</sub>、……、D<sub>n</sub>和R是类型表达式,则D<sub>1</sub> X D<sub>2</sub> X …… X D<sub>n</sub>→R是类型表达式,其中X优先于→,它指称从定义域类型D<sub>1</sub> X D<sub>2</sub> X …… X D<sub>n</sub>到值域类型R的映射。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
<b>(3)类型表达式中可出现类型变量,变量值是类型表达式。</b>
</p>
</td></tr></table>
<br>
<hr size=2 color=red width=90%>
<br>
<table><tr><td>    </td>
<td class="content">
<P>
<b>例5.23</b> 设有Pascal 程序片段:
</p>
</td></tr></table>
<table><tr><td>        </td>
<td class="content">
<P>
<b> <font color="#0000FF">TYPE</font> </b><br>
stype=<b><font color="#0000FF">RECORD</font> </b><br>
name:<b><font color="#0000FF">ARRAY</font></b> [1..8] OF char; <br>
score:integer
<br>
<b> <font color="#0000FF">END</font></b>;
<br>
<b> <font color="#0000FF">VAR</font> </b><br>
table:<b><font color="#0000FF">ARRAY</font></b> [1..50]
OF stype; <br>
p: ↑stype;
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
则stype代表的类型表达式
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
<b> <font color="#0000FF">RECORD</font></b>((nameX<b><font color="#0000FF">ARRAY</font></b>(1..8,char))X(scoreXinteger))
</p>
</td></tr></table>
<table width="470"><tr><td width="16">    </td>
<td class="content" width="440">
<P>
和table绑定的类型表达式 <font color="#0000FF">ARRAY</font>(1..50,stype)
</p>
</td></tr></table>
<table width="468"><tr><td width="16">    </td>
<td class="content" width="438">
<P>
和P绑定的类型表达式 <font color="#0000FF">POINTER</font>(stype)
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
<b>例5.24</b> 设有下面的函数定义
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
<b><font color="#0000FF">FUNCTION</font></b> f(P1:T1;P2:T2;…,Pn:Tn):T;
<br> <b><font color="#0000FF">BEGIN</font></b> ……<b><font color="#0000FF">END</font></b>;
<br>
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>和f绑定的类型表达式 T<sub>1</sub>XT<sub>2</sub>X…XT<sub>n</sub>→T
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
具体说来,
<br> <b><font color="#0000FF">FUNCTION</font></b>
f(a,b:<font color="#0000FF">char</font>):↑<font color="#0000FF">integer</font>;
<br> <b><font color="#0000FF">BEGIN</font></b> …<b><font color="#0000FF">END</font></b>;
<br> 和f绑定的类型表达式为
<br> char X char→POINTER(integer)
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
<b>例5.25</b> 在函数式语言中可如下定义恒等函数
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
fun f(x)=x x可以是任何类型的语言结构。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
因此x可以是任何类型f的类型表达式为
<br> α→α α为类型变量,其值是任何类型表达式。
<p>
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
多态类型语言结构的类型表达式中含类型变量,这种语言的类型分析和类型检查应在专门论题中讨论。
</p>
</td></tr></table>
<br>
<hr size=2 color=red width=90%>
<br>
<br>
<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.8.0.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.8.1b.htm'"></img></td>
</tr>
</table>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -