📄 7.3.1.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='7.2.3.htm'" ></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='7.3.1b.htm'" ></td>
</tr>
</table>
<p><br>
<br>
<font class="title2"><b>7.3 赋值语句</b></font> </p>
<table>
<tr>
<td> </td>
<td class="content">本节将讨论赋值语句的翻译。其中表达式的类型可以是整型、实型、数组和记录。作为翻译赋值语句为三地址代码的一个部分,我们将讨论如何在符号表中查找名字及如何存取数组和记录的元素。
</td>
</tr>
</table>
<hr size="2" color="red" width="90%">
<font class="title2"><b>
<p>7.3.1 符号表中的名字</b></font> </p>
<table>
<tr>
<td> </td>
<td class="content">在7.1节的三地址语句中,我们直接采用名字,并且将它理解为指向符号表中相应该名字表项的指针。这里,图7.8显示了如何查找符号表的表项。属性<b>id</b>.name给出了<b>id</b>所代表的名字本身。过程lookup(<b>id</b>.name)检查是否在符号表中存在相应此名字的表项。如果有,则返回一个指向该表项的指针,否则,返回nil表示没有找到。
<p>请注意,在图7.8的语义动作中,直接调用过程emit,它将生成的三地址语句发送到输出文件中,而不是如表7.2中那样置为非终结符号的code属性。于是,只要每条产生式左边非终结符号的code属性是通过产生式右边非终结符号的code属性按其出现的顺序连接起来的,那么,翻译中形成的所有中间代码均可以依次送入到输出文件中。
</td>
</tr>
</table>
<table>
<tr>
<td> </td>
<td class="content"> S→<b>id</b>:=E { p:=lookup(<b>id</b>.name);<br>
<b> if</b>
p<>nil <b>then</b><br>
emit(p':=' E.place)<br>
<b> else</b>
error}<br>
E→E<span class="down"><sub>1</sub></span>+E<span class="down"><sub>2</sub> </span>
{ E.place=newtemp;<br>
emit(E.place':='E<span class="down"><sub>1</sub></span>.place'+'E<span class="down"><sub>2</sub></span>.place)}<br>
E→E<span class="down"><sub>1</sub></span>*E<span class="down"><sub>2</sub> </span>
{ E.place=newtemp;<br>
emit(E.place':='E<span class="down"><sub>1</sub></span>.place'*'E<span class="down"><sub>2</sub></span>.place)}<br>
E→-E<span class="down"><sub>1</sub> </span>
{ E.place:=newtemp;<br>
emit(E.place':=' 'uminus'E<span class="down"><sub>1</sub></span>.place)}<br>
E→(E<span class="down"><sub>1</sub></span>) {
E.place:='E<span class="down"><sub>1</sub></span>.place')}<br>
E→<b>id</b> { p:=lookup(<b>id</b>.name);<br>
<b> if</b>
p<>nil <b>then</b><br>
E.place:=p<br>
<b> else</b>
error}
<p>图7.8 产生赋值语句三地址代码的翻译模式 </p>
</td>
</tr>
</table>
<p><br>
</p>
<table align="right" width="300">
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='7.2.3.htm'" ></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='7.3.1b.htm'" ></td>
</tr>
</table>
</body>
</html>
<html><script language="JavaScript">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -