📄 7.4.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='7.4.1.htm'" ></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='7.4.2b.htm'" ></td>
</tr>
</table>
<br>
<br>
<font class="title2"><b>7.4.2 数值表示法</b></font> </p>
<table width="100%">
<tr>
<td width="18"> </td>
<td class="content" width="865">让我们首先考虑用1表示真,0表示假来实现布尔表达式的翻译。用这种方法,布尔表达式将从左至右按类似算术表达式的求值方法来计算。例如,对于布尔表达式:
<br>
a <b>or</b> b <b>and not</b> c <br>
将被翻译成如下三地址序列:<br>
t<span class="down"><sub>1</sub></span>:=<b>not</b>
c<BN> <br>
t<span class="down"><sub>2</sub></span>:=b
<b>and</b> t<span class="down"><sub>1</sub></span><br>
t<span class="down"><sub>3</sub></span>:=a
<b>or</b> t<span class="down"><sub>1</sub></span><br>
一个形如a<b的关系表达式可等价地写作<b>if</b>
a<b <b>then </b>1<b> else</b> 0,并可以将它翻译成如下三地址语句序列(我们假定语句序号从100开始):
<br>
100 : <b>if</b> a<b <b>goto</b> l03 <br>
101 : t:=0 <br>
102 : <b>goto</b> l04。 <br>
103 : t:=1 <br>
104: <p>E→E<span class="down"><sub>1</sub></span> <b>or</b> E<span
class="down"><sub>2</sub></span> {E.place:=newtemp; <br>
emit(E.place':='E<span class="down"><sub>1</sub></span>.place'or'E<span class="down"><sub>2</sub></span>.place)}
<br>
E→E<span class="down"><sub>1</sub></span> <b>and</b> E<span
class="down"><sub>2</sub> </span> {E.place:=newtemp; <br>
emit(E.place':='E<span class="down"><sub>1</sub></span>.place'and'E<span class="down"><sub>2</sub></span>.place)}
<br>
E→<b>not</b> E<span class="down"><sub>1</sub></span>
{E.place:=newtemp; <br>
emit(E.place':=' 'not' E<span class="down"><sub>1</sub></span>.place)} <br>
E→<b>id</b><span class="down"><sub>1</sub></span> <b>relop</b> <b>id</b><span
class="down"><sub>2</sub></span> {E.place:=newtemp; <br>
emit('if' <b>id</b><span
class="down"><sub>1</sub></span>.place <b>relop</b>.op <b>id</b><span class="down"><sub>2</sub></span>.place<br>
'goto' nextstat+3); <br>
emit(E.place':='
'0'); <br>
emit('goto'nextstat+2);
<br>
emit(E.place':='
'1')} <br>
E→<b>ture</b> {E.place:=newtemp;
<br>
emit(E.place':=' '1')} <br>
E→<b>false </b> {E.place:=newtemp;
<br>
emit(E.place':=' '0')} <br>
</p>
<p> 图7.11 关于布尔表达式的数值表示法的翻译模式 </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.4.1.htm'" ></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='7.4.2b.htm'" ></td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -