📄 5.8.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.1_2c.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.8.2_2.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>5.8.2 类型分析</b></font>
<br><br>
<font class="title2"><b>5.8.2.1 变量标识符和类型表达式的绑定</b></font>
<table><tr><td>    </td>
<td class="content">
<P>
程序是由说明和语句序列组成的,且说明在前,语句序列在后。语句序列是计算,而说明部分建立计算环境,其中说明了每个变量标识符以及与之绑定的类型。语法(5.10)是一个简单的程序语言语法,假设数组的下标从1开始。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
文法G[P],产生式如下:
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
P→D;E <br>
D→D;D|id:T <br>
T→char|integer|ARRAY[num] OF
T|↑T (5.10) <br>
E→num|id|E MOD E| E[E] |E↑
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
语义分析程序首先处理类型说明,建立类型表达式,然后处理变量说明,建立变量和类型表达式的绑定。具体实现是把变量标识符的类型信息记录在其在符号表的表项中,过程addtype(id.enery,T.type)完成这个任务,其翻译模式由图5.32给出。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
P→D;E <br>
D→D;D <br>
D→id:T {addtype(id.enery,T.TYPE)}
<br>
T→char {T.type:=char}
<br>
T→integer {T.type:=integer}
<br>
T→↑T<sub>1</sub> {T.type:=POINTER(T<sub>1</sub>.type)}
<br>
T→ARRAY[num] OF T<sub>1</sub>
{T.type:=ARRAY(num.val,T<sub>1</sub>.type)} <br><br>
<b>图5.32 </b> 建立变量标识符和类型属性绑定的翻译模式
<p>
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
待语义分析程序处理语句序列时,借助于符号表中变量和类型属性的绑定信息,分析确定每个语言结构的类型表达式,从而完成类型检查及有关语义分析的任务。
</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.1_2c.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.8.2_2.htm'"></img></td>
</tr>
</table>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -