📄 5.7.0_3.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.7.0_2b.htm'" ></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.7.0_3b.htm'"></img></td>
</tr>
</table>
<br><br>
<table><tr><td>    </td>
<td class="content">
<p>
我们将通过下面的例题来进一步介绍属性的递归计算。其中每一个非终结符号与多个翻译函数相联系。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
<b>例5.22</b 表5.12中的语法制导定义反映了一个在编译程序中常出现的问题。例如,一个表达式可以有若干个可能的类型。我们可以从上下文提供的信息来为每一个子表达式选择一个可能的类型。我们可以用这样的办法来解决这一向题,即先进行一遍自底上的计算来综合可能的类型,之后再进行一遍自顶向下的计算来缩小到一个类型。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
表5.12中的语义规则是对这个问题的抽象。综合属性s代表一系列可能的类型,而继承属性i代表上下文信息。一个附加的综合属性方代表产生的代码(或其它),它不能在计算s的同一遍中被计算出来。表5.12中的产生式的依赖图如图5.26所示。
</p>
</td></tr></table>
<center>表5.12</center><br>
<table border="1" width="60%" cellspacing=0 align=center height="64">
<tr>
<td width="40%" height="21" align=center> 产生式</td>
<td width="60%" height="21" align="center">
语义规则</td>
</tr>
<tr>
<td width="40%" height="35" align="left" valign="top"> S -> E<br>
<br>
<br>
E -> E<sub>1</sub>E<sub>2</sub><br>
<br>
<br>
<br>
<br>
<br>
E -> id<br>
<br>
</td>
<td width="60%" height="35" valign="top" align="left"> E.i := g(E.s)<br>
S.r:=E.t<br>
<br>
E.s := fs(E<sub>1</sub>.s,E<sub>2</sub>.s)<br>
E<sub>1</sub>.i := fi<sub>1</sub>(E.i)<br>
E<sub>2</sub>.i := fi<sub>2</sub>(E.i)<br>
E.t := fi(E<sub>1</sub>.t,E<sub>2</sub>.t)<br>
<br>
E.s := id.s<br>
E.t := h(E.i)
<br>
</td>
</tr>
</table>
<br>
<center><img src="5_26.gif" width="728" height="157"></center>
<p align=center>图5.26 表5.12中产生式的依赖图</p>
<table><tr><td>    </td>
<td class="content">
<P>
一棵分析树的依赖图是由与产生式的语义规则相对应的更小的图组成的,产生式P的依赖图D<sub>p</sub>仅基于这个产生式的语义规则,即基于产生式左边符号的综合属性和产生式右边文法符号的继承属性的语义规则。这就是说,依赖图D<sub>p</sub>只表示局部关系。例如,图5.26中的E→E<sub>1</sub>E<sub>2</sub>的依赖图中的所有的边都在同一属性的实例之间。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
更具体地说来,按照上面的说明,综合属性s代表类型。如果E<sub>1</sub>.s代表一种类型,而E<sub>2</sub>.s代表另一种类型,就可以根据两个不同类型的量进行运算时的具体规定得到E的类型E.s。这表现在表5.12中的语义规则E.s:=fs(E<sub>1</sub>.s,E<sub>2</sub>.s),E.s的计算是自底向上的。有了E.s,可通过规则E.i:=g(E.s)得到E.i。再由E.i得到E<sub>1</sub>.i和E<sub>2</sub>.i,这一传递上下文信息的过程是自顶向下的。并分别由E<sub>1</sub>.i和E<sub>2</sub>.i得到E<sub>1</sub>.t和E<sub>2</sub>.t。如上所述,综合属性t表示产生的代码,那么就可以根据为E<sub>1</sub>产生的代码E<sub>1</sub>.t和为E<sub>2</sub>产生的代码E<sub>2</sub>.t进行自底向上的计算综合成为E.t,即为E产生的代码。
</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.7.0_2b.htm'" ></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.7.0_3b.htm'"></img></td>
</tr>
</table>
</BODY>
<html><script language="JavaScript">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -