⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6.5.4b.htm

📁 建立《编译原理网络课程》的目的不仅使学生掌握构造编译程序的原理和技术
💻 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.5.4.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.5.4c.htm'"></img></td>
</tr>
</table>
<br><br>

 
<font class="title2">附录 <b>  过程作为参数</b></font>        

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<p><p>
我们首先考虑图6.24中给出的程序,过程q有两个参数i和r, 其中i是变量而r是参数。在过程P中,定义了一个函数f,在P的过程体中,以x和f为实在参数调用过程q。在q中执行i:=r(i), 实际上要调用函数f。现在的主要问题是,在f的活动中,如何实现非局部访问? 
</p>
</td></tr></table>

<table><tr><td>&nbsp</td>
<td class="content">
<P><b><font color="#0000FF">Program</font></b> main <br>   
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">var</font></b> x:integer;<br>   
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">Precedure</font></b> q(var i:integer;function r(j:integer):integer); <br> 
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">var</font></b> x:integer; <br>   
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">begin</font></b> x:=4; <br>   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i:=r(i) <br>  
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">end</font></b>; <br>  
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">procedure</font></b> P; <br>   
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">var</font></b> i:integer; <br>   
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">function</font></b> f(k:integer):integer; <br>   
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">begin</font></b> &nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x:=x+k; <br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f:=i+k <b>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">end</font></b>;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b><font color="#0000FF">begin</font></b> <br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n i:=2; <br>   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q(x,f); <br>  
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">end</font></b>; <br>  
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">begin</font></b> x:=7; <br>   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;P; <br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writeln("in main,x=",x)<br>   
<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000FF">end</font></b>.  
</p>  
<p> &nbsp;&nbsp;&nbsp;<b>图6.24</b>  含有以过程为参数的Pascal程序 
<p>
</p>
</td></tr></table>

<table><tr><td></td>
<td class="content">
<P>
通常,调用一个过程时,为此过程设置一个非局部环境。在执行过程活动期间,当要访问一个非局部变量时,就使用这个环境。但是,函数过程f作为一个实参从调用过程P传给被调用过程,当在q中调用f,哪个应是f使用的非局部环境?
</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='6.5.4.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.5.4c.htm'"></img></td>
</tr>
</table>

</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -