📄 4.3.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.3.1.2b.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.3.2.0b.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>4.3.2 提取左因子 </b></font>
<table><tr><td>    </td>
<td class="content">
<p>提取左因子是对文法进行改写的一种方法,改写以后的文法适用于前面介绍的预测分析法。</p>
<p>例如,如果我们考虑以下两个产生式:</p>
<table align=center width=450 class="content">
<tr><td>stmt→</td><td>if expr then stmt else stmt </td></tr>
<tr><td></td><td>|if expr then stmt</td></tr>
</table>
<p>在进行语法分析时,当输入符号为if,我们不能立即知道应该选择哪一个产生式来展开stmt。但不难看出它们有一个公共的左因子“if expr then stmt”,可以考虑提取左因子。一般说来,如果A→αβ<sub>1</sub>|αβ<sub>2</sub>是两个产生式,并且从α可以推得非空符号串,我们不知道是否把A展为αβ<sub>1</sub>或是展为αβ<sub>2</sub>。然而我们可以提取公共左因子α,原来的产生式变为</p>
<table align=center width=450 class="content">
<tr><td>A→αA'</td></tr>
<tr><td>A'→β<sub>1</sub>|β<sub>2</sub></td></tr>
</table>
<p>于是,当分析完α之后,我们展开A'为β<sub>1</sub>或为β<sub>2</sub>。</p>
<p>同样,若有A→αβ<sub>1</sub>|αβ<sub>2</sub>|…|αβ<sub>n</sub>|γ,其中γ是不以α开头的候选式,那么这些产生式可用如下的产生式代替:</p>
<table align=center width=450 class="content">
<tr><td>A→αA'|γ</td></tr>
<tr><td>A'→β<sub>1</sub>|β<sub>2</sub>|…|β<sub>n</sub></td></tr>
</table>
<p>这里A'是一个新的非终结符号。</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.3.1.2b.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.3.2.0b.htm'"></img></td>
</tr>
</table>
</BODY>
</html>
<html><script language="JavaScript">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -