📄 5.8.2_2.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.2.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.8.2_2b.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>5.8.2.2 类型检查 </b></font>
<table><tr><td>    </td>
<td class="content">
<P><p>
类型检查是语义分析的主要任务之一,通过类型检查,将尽可能发现使用数据不当的错误。下面讨论的类型检查是在编译时做的静态类型检查,目前大多数语言的类型体制都支持这种策略。
</p>
</td></tr></table>
<br>
<hr size=2 color=red width=90%>
<br>
<table><tr><td>    </td>
<td class="content">
<P>
<b>表达式的类型检查</b>
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
对表达式进行类型检查,主要检查运算对象之间类型是否满足相容条件。图5.33 给出了文法G[P](5.10)的表达式类型检查的翻译模式,E的综合属性type是E的类型表达式。函数lookup(e)取符号表中保存在条目e中的类型,当标识符出现在表达式中时,从符号表中取出它的类型并赋给属性type。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
根据图5.33读者能够把它进行扩充,以允许表达式有其它的类型和运算。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
E→num {E.type:=integer} <br>
E→id {E.type:=lookup(id.entry)} <br>
E→E<sub>1</sub> MOD E<sub>2</sub> {E.type:=IF(E<sub>1</sub>.type=integer)<br>
AND(E<sub>2</sub>.type=integer)
<br>
THEN
integer <br>
ELSE
type_error} <br>
E→E<sub>1</sub>[E<sub>2</sub>]
{E.type:=IF(E<sub>2</sub>.type=integer)<br>
AND(E<sub>1</sub>.type=ARRAY(s,t))
<br>
THEN
t <br>
ELSE
type_error} <br>
E→E<sub>1</sub>↑ {E.type:=IF
E<sub>1</sub>.type=POINTER(t) <br>
THEN
t <br>
ELSE
type_error} <br><br>
<b>图5.33</b> 表达式的类型检查的翻译模式
</p>
</td></tr></table>
<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.2.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.8.2_2b.htm'"></img></td>
</tr>
</table>
</BODY>
<html><script language="JavaScript">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -