📄 5.7.0b.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.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.7.0_2.htm'"></img></td>
</tr>
</table>
<br><br>
<table><tr><td>    </td>
<td class="content">
<P>
<b>例5.20</b> 考虑表5.7中的语法制导定义。其中非终结符号B有一个继承属性ps和一个综合属性ht。用算法5.1并象上面那样对算法进行修改,我们可以为B建立一个图5.23所示的递归函数。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
函数B把结点n和结点n处与B.ps 相应的值作为形式参数,并返回结点n处与B.ht对应的值。函数中有一个情况语句,列举了每一个左边为B的产生式。与每一个产生式相应的代码用来模拟与这个产生式对应的语义规则。使用语义规则的顺序应是这样的:一个非终结符号的继承属性值必须在调用这个非终结符号的函数以前计算出来。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
例如在对应于产生式B→B<sub>1</sub> sub B<sub>2</sub>的代码中,变量ps,ps1和ps2分别存放继承属性B.ps,B<sub>1</sub>.ps和B<sub>2</sub>.ps的值,ht,ht1和ht2分别存放B.ht,B<sub>1</sub>.ht和B<sub>2</sub>.ht的值。我们用函数child(m,i)来表示结点m的第i个子结点。由于在此情形下的B<sub>2</sub>是结点n的第三个子结点的标志,所以B<sub>2</sub>.ht的值由函数调用B(child(n,2),ps2)来决定。
</p><br><br>
</td></tr></table>
<table><tr><td></td>
<td class="content">
<P>
<b> <font color="#0000FF">function</font></b> B(n,ps);
<br>
<font color="#0000FF">var</font>
ps1,ps2,ht1,ht2; <br>
<b> <font color="#0000FF">begin</font> </b><br>
<b> <font color="#0000FF">case</font></b>
在结点n的产生式 <b><font color="#0000FF">of</font></b> <br>
'B→B<sub>1</sub>B<sub>2</sub>':
<br>
ps1:=ps;
<br>
ht1:=B(child(n,1),ps1);
<br>
ps2:=ps;
<br>
ht2:=B(child(n,2),ps2);
<br>
<b> <font color="#0000FF">return</font></b>
max(ht1,ht2); <br>
'B→B<sub>1</sub>
sub B<sub>2</sub>': <br>
ps1:=ps;
<br>
htl:=B(child(n,1),ps1);
<br>
ps2:=shrink(ps);
<br>
ht2:=B(child(n,3),ps2);
<br>
<b>
<font color="#0000FF">return</font></b> disp(ht1.ht2); <br>
'B→<b>text</b>':
<br>
<b> <font color="#0000FF">return</font></b>
ps*<b>text</b>.h; <br>
<b> <font color="#0000FF">default</font></b>:
<br>
error
<br>
<b> <font color="#0000FF">end</font></b>;<br>
<b> <font color="#0000FF">end</font></b>;<br>
<br><p>
<b>图5.23</b> 在表5.7中非终结符号B的函数<br>
</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.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='5.7.0_2.htm'"></img></td>
</tr>
</table>
</BODY>
<html><script language="JavaScript">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -