📄 4.2.3.2b.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.2.htm'" width="24" height="24"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.2.3.3.htm'"></img></td>
</tr>
</table>
<br><br>
</table>
<table>
<tr>
<td>
<font class="definition"><img border="0" src="images/dingyi.gif" width="32" height="31">算法4.2 </font> 计算nullable、<b>FIRST</b>和<b>FOLLOW</b>
<br><br>
<p>初始化:FIRST和FOLLOW全为空集(除FOLLOW(s)={$}外),nullable全为false</p>
<font size="3"><font color="#0000FF">FOR</font> 任一a∈V<sub>T</sub>
<font color="#0000FF">DO</font> <b>FIRST</b>(a):={a};<br>
<font color="#0000FF">REPEAT</font><br>
<font color="#0000FF">FOR</font> 任一A→Y<sub>1</sub> Y<sub>2</sub> …Y<sub>n</sub> ∈P
<font color="#0000FF">
DO</font><br>
<font color="#0000FF">BEGIN</font><br>
<font color="#0000FF">IF</font> 任一i(1≤i≤n)
nullable(Y<sub>i</sub> )=true <font color="#0000FF">THEN</font> <br>
nullable(A):=true;<br>
<font color="#0000FF">FOR</font> i:=1
<font color="#0000FF"> TO</font> n <font color="#0000FF"> DO</font><br>
<font color="#0000FF">BEGIN</font><br>
<font color="#0000FF">IF</font>
(i=1)OR 任一j(1≤j≤i-1) nullable(Y<sub>j</sub> )=true <font color="#0000FF">THEN </font>
<b><br>
FIRST</b>(A)=<b>FIRST</b>(A)∪<b>FIRST</b>(Y<sub>i</sub> );<br>
<font color="#0000FF">IF</font>
Y<sub>i</sub> ∈V<sub>N</sub> <font color="#0000FF">THEN <br>
BEGIN</font><br>
<font color="#0000FF">IF</font>
(i=n) <font color="#0000FF"> OR</font> 任一j(i+1≤j≤n) nullable(Y<sub>j</sub> )=true<br>
<font color="#0000FF">THEN</font>
<b>FOLLOW</b>(Y<sub>i</sub> )=<b>FOLLOW</b>(Y<sub>i</sub> )∪<b>FOLLOW</b>(A);<br>
<font color="#0000FF">IF</font>
Y<sub>i+1</sub> ∈V<sub>T</sub> <br>
<font color="#0000FF">THEN</font>
Y<sub>i+1</sub> ∈<b>FOLLOW</b>(Y<sub>i</sub> )<br>
<font color="#0000FF">ELSE <br>
FOR</font> k:=i+1 <font color="#0000FF"> TO</font> n <font color="#0000FF"> DO</font><br>
 <font color="#0000FF">IF</font> (k=i+1)<font color="#0000FF">OR</font> 任一j(i+1≤j≤k-1)nullable(Y<sub>j</sub> )=true<br>
<font color="#0000FF">THEN</font>
FOLLOW</b>(Y<sub>i</sub> )=<b>FOLLOW</b>(Y<sub>i</sub> )∪<b>FIRST</b>(Y<sub>k</sub> )<br>
<font color="#0000FF">END</font><br>
<font color="#0000FF">END</font><br>
<font color="#0000FF">END</font><br>
<font color="#0000FF">UNTIL</font> <b>FIRST</b>,<b>FOLLOW</b>,nullable 不再改变<br>
</font><br>
</p>
</td>
</tr>
</table>
<p><font class="example">例4.5</font> 把算法4.2应用于文法G[E](4.3),得到下面的计算结果:</p>
<p>
<center class="content">表4.3 文法G[E](4.3)的<b>FIRST</b>和<b>FOLLOW</b>集
<table width="88%" border="1" height="146" cellspacing="0" cellpadding="0" class="content">
<tr>
<td> </td>
<td>
<div align="center">
nullable
</div>
</td>
<td>
<div align="center">
FIRST
</div>
</td>
<td>
<div align="center">
FOLLOW
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
E
</div>
</td>
<td>
<div align="center">
false
</div>
</td>
<td>
<div align="center">
(, id
</div>
</td>
<td>
<div align="center">
), $
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
E'
</div>
</td>
<td>
<div align="center">
true
</div>
</td>
<td>
<div align="center">
+
</div>
</td>
<td>
<div align="center">
), $
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
T
</div>
</td>
<td>
<div align="center">
false
</div>
</td>
<td>
<div align="center">
(, id
</div>
</td>
<td>
<div align="center">
+, ), $
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
T'
</div>
</td>
<td>
<div align="center">
true
</div>
</td>
<td>
<div align="center">
*
</div>
</td>
<td>
<div align="center">
+, ), $
</div>
</td>
</tr>
<tr>
<td>
<div align="center">
F
</div>
</td>
<td>
<div align="center">
false
</div>
</td>
<td>
<div align="center">
(, id
</div>
</td>
<td>
<div align="center">
*, +, ), $
</div>
</td>
</tr>
</table>
</center>
<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.2.htm'" width="24" height="24"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='4.2.3.3.htm'"></img></td>
</tr>
</table>
</BODY>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -