📄 4.3.1.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.2.3.4c.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.3.1.0b.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>4.3 书写文法</b></font>
<table><tr><td>    </td>
<td class="content">
<p>
一般用于描述程序语言的文法,往往不是LL(1)文法,也不能用于递归预测分析器的构造。因此,当编译器的设计者选用自顶向下的预测分析方法后,就要对文法进行等价变换,以便能用来构造预测分析器。
</p>
</td></tr></table>
<hr size=2 width=90% align=center color=red><br>
<font class="title2"><b>4.3.1 消除左递归</b></font>
<table><tr><td>    </td>
<td class="content">
<p>
一个文法G,若存在推导A<img src="images/equalplus.gif" width="20" height="19">Aα,其中A∈V<sub>N</sub>,α∈(V<sub>T</sub>∪V<sub>N</sub>)<sup>*</sup>,则称G是左递归的。存在上述推导的原因是文法G中存在左递归规则。例如,描述表达式常用的语法G[E],其产生式如下:</p>
<table align=center width=450 class="content">
<tr><td>E→E+T|T</td><td></td></tr>
<tr><td>T→T*F|F</td><td>(4.6)</td></tr>
<tr><td>F→(E)|id </td><td></td></tr>
</table>
<p>其中关于E和T的规则都是左递归规则。这种规则更简单的代表是S→Sa|b,显然,FIRST(Sa)∩FIRST(b)≠Φ。由S产生的句子是{ba<sup>n</sup>|n≥0}。例如,对于输入baaa$,从左到右扫描输入串,开始,向前看符号是b,此时,选用S的哪条候选式?若选用S→b,则肯定构造不出S=>baaa;若选用S→Sa,则面对向前看符号b,下次仍选用S→Sa,不知何时选用S→b。构造分析树的过程如图4.9所示。 </p>
</td></tr></table>
<p>
<center><img src="images/4.9.gif" width="204" height="262"></center><br>
<center class="content">图4.9 含直接左递归文法的分析树结构</center></p>
<br>
<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.2.3.4c.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.3.1.0b.htm'"></img></td>
</tr>
</table>
</BODY>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -