📄 4.6.2.0.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='4.6.1.0c.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.6.2.0b.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>4.6.2 算符优先关系表的构造</b></font>
<table><tr><td>    </td>
<td class="content">
<p>构造算符优先关系的目的是希望算符优先分析算法能正确的工作。我们可以自由地选择适当的方法去构造算符优先关系。对于象文法(4.13)所生成的算术表达式语言,用直观的方法,利用运算符传统的可结合性和优先规则来构遵算符优先关系是简单方便的。这个方法的一般原则如下:</p>
<p> 1.若运算符号θ<sub>1</sub>具有比θ<sub>2</sub>较高的优先级,则规定θ<sub>1</sub>·>θ<sub>2</sub>和θ<sub>2</sub><·θ<sub>1</sub>。例如*具有比+较高的优先级,则规定*·>+和+<·*。这些关系保证了在形如E+E*E+E的表达式中,中间的E*E将是首先被归约的句柄。</p>
<p>2.若θ1和θ2是两个优先级相等的运算符号,甚至它们可能是相同的运算符号。如果运算符号是左结合的,则规定θ<sub>1</sub>·>θ<sub>2</sub>和θ<sub>2</sub>·>θ<sub>1</sub>。如果是右结合的,则规定θ<sub>1</sub><·θ<sub>2</sub>和θ<sub>2</sub><·θ<sub>1</sub>。例如+和-是左结合的,因而有+·>+,+·>-、-·>-.-·>+。例如^是右结合的,因而有^<·^。这些关系保证了句型E-E+E将有句柄E-E。并且E^ E^ E将以后面的E^E作为句柄。</p>
<p>3.因为id是最基本的运算量,并且需要优先处理括号中的量,而$是被用作句子的左右分界符号,所以对任何运算符号θ,规定 </p>
<table align=center width=550 class="content">
<tr><td>θ<·id</td><td> id·>θ </td><td>θ<·(</td><td>(<·θ</td></tr>
<tr><td>)·>θ</td><td>θ·> )</td><td>θ·>$</td><td>$<·θ</td></tr>
</table>
    且
<table align=center width=550 class="content">
<tr><td>(<img src="images/equalpoint.gif" width="20" height="19">)</td><td>$<·(</td><td>$<·id</td><td>(<·(</td><td> id·>$ </td></tr>
<tr><td>)·>$</td><td>(<·id</td><td>id·>)</td><td>)·>)</td><td></td></tr>
</table>
<p>这些规则保证了id和(E)将被归约到E。$作为左终点与右终点的标志,用来发现在两个$之间的任何地方可能有的可归约串。</p>
<br><hr size=2 width=90% align=center color=red><br>
<p><font class="example">例4.13 </font>设文法(4.13)的算符优先关系满足:</p>
<p>1.^有最高优先级,并且是右结合的。</p>
<p>2.*和/有次优先级,并且是左结合的。</p>
<p>3.+和-有最低优先级,并且是左结合的。</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='4.6.1.0c.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.6.2.0b.htm'"></img></td>
</tr>
</table>
</BODY>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -