📄 6.4.3.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='6.4.2.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.4.3_1.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>6.4.3 含有嵌套过程的词法作用域</b></font>
<table><tr><td>    </td>
<td class="content">
<P>
在Pascal过程中的一个名字a的非局部出现是在静态程序正文中的a的最近嵌套说明的作用域之内。图6.18的Pascal程序中
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
<br>
(1) <b><font color="#0000FF">program</font></b> sort(input,output); <br>
(2) <b> <font color="#0000FF">var</font></b> a:array[0..10] of
integer; <br>
(3) x: integer;
<br>
(4) <b> <font color="#0000FF">procedure</font></b> readarray; <br>
(5) <b> <font color="#0000FF">var</font></b>
i: integer; <br>
(6) <b> <font color="#0000FF">begin</font></b>...a...
<b><font color="#0000FF">end</font></b> {readarray}; <br>
(7) <b> <font color="#0000FF">procedure</font></b> exchange(i,j:integer) <br>
(8) <b> <font color="#0000FF">begin</font></b>
<br>
(9) x:=
a[i] ;a[i]:=a[j];a[j]:=x <br>
(10) <b><font color="#0000FF">end</font></b> {exchange}; <br>
(11) <b><font color="#0000FF">procedure</font></b> quicksort(m,n: integer);
<br>
(12) <b><font color="#0000FF">var</font></b> k,v:integer; <br>
(13) <b><font color="#0000FF">function</font></b>
partition(y,z: integer): integer; <br>
(14)
<b><font color="#0000FF">var</font></b> i,j: integer; <br>
(15)
<b><font color="#0000FF">begin</font></b> ...a...<br>
(16) ...v...
<br>
(17) ...exchange(i,j);...
<br>
(18)
<b><font color="#0000FF">end</font></b> {partition}; <br>
(19) <b><font color="#0000FF">begin</font></b>...<b><font color="#0000FF">end</font></b>
{ quicksort}; <br>
(20) <b><font color="#0000FF">begin</font></b>…<b><font color="#0000FF">end</font></b> {sort}。 <br>
</p>
<p align="left">
<b>图6.18</b>带有嵌套过程的一个Pascal程序<br>
<br><br>
</p>
</td></tr></table>
<table><tr><td></td>
<td class="content">
<p>
的嵌套的过程定义由下面的锯齿状的排列指出:
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<p>
sort <br>
readarray <br>
exchange <br>
quicksort <br>
partition <br><br>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<p>
在图6.18的第(15)行上的a的出现是在函数partition之中,它嵌套在过程quicksort之内。a的最近嵌套说明是在第(2)行,位于包括整个程序的过程中。最近嵌套规则也同样用于过程名字。如第(17)行的由partition 调用的过程exchange对于partition而言是非局部的。应用这个规则我们首先检查exchange是否在quicksort之内被定义,由于它不是,我们在主程序sort中找到了它。
</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='6.4.2.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.4.3_1.htm'"></img></td>
</tr>
</table>
</BODY>
<html><script language="JavaScript">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -