📄 6.6.5c.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.6.5b.htm'" ></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.6.5d.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>练习</b></font>
<table><tr><td>    </td>
<td class="content">
6.4当一个过程作为参数被传递时,我们假定有以下三种与此相联系的环境可以考虑,下面的Pascal程序是用来说明这一问题的。一种是词法环境(lexical environment),如此这样的一个过程的环境是由在这一过程定义之处的各标识符的绑定所构成;一种是传递环境(passing environment),是由这一过程作为参数被传递之处的各标识符的绑定所构成;另一种是活动环境(activation environment),是由这一过程活动之处的各标识符的绑定所构成。试考虑在第(11)行上的作为一个参数被传递的函数f。利用对于f的词法环境、传递环境和活动环境,在第(8)行上的非局部量m将分别处在第(6)行、(10)行和(3)行上的m的说明的作用域中。
<br>
(a)试给出程序的输出。 <br>
(b)试为此程序画出活动树。 <br>
(c)分别图示出采用词法环境和活动环境执行到过程f时运行栈的变化及存取链。
<p> (1) <b><font color="#0000FF">program</font></b> param(input,output); <br>
(2) <b> <font color="#0000FF">procedure</font></b> b(function h(n: <font color="#0000FF"><b> integet</b></font>)<br>
: <font color="#0000FF"><b> integer</b></font>); <br>
(3) <b> <font color="#0000FF">var</font></b>
m : <font color="#0000FF"><b> integer</b></font>; <br>
(4) <b> <font color="#0000FF">begin</font></b>
m:=3; writeln(h(2)) <b><font color="#0000FF">end</font></b><font color="#008000">{ b}</font> ; <br>
(5) <b> <font color="#0000FF">procedure</font></b> c; <br>
(6) <b> <font color="#0000FF">var</font></b>
m : <font color="#0000FF"><b> integer</b></font>; <br>
(7) <b> <font color="#0000FF">function</font></b>
f(n : <font color="#0000FF"><b> integer</b></font> ) : <font color="#0000FF"><b> integer</b></font> <br>
(8) <b> <font color="#0000FF">begin</font></b>
f:= m+n <b><font color="#0000FF">end</font></b><font color="#008000">{ f }</font> ; <br>
(9) <b> <font color="#0000FF">procedure</font></b>
r; <br>
(10)
<b><font color="#0000FF">var</font></b> m : integer; <br>
(11)
<b><font color="#0000FF">begin</font></b> m :=7; b(f) <b><font color="#0000FF">end</font></b>
<font color="#008000"> { r }</font> ; <br>
(l2) <b><font color="#0000FF">begin</font></b>
m := 0; r <b><font color="#0000FF">end</font></b> <font color="#008000"> { c }</font> ; <br>
(13) <b><font color="#0000FF">begin</font></b> <br>
(14) c <br>
(15) <b><font color="#0000FF">end</font></b>. </p>
<table align=center width=400>
</table>
<table align=center width=70%>
<tr><td align=right>
<img src="../images/key.gif" onmouseover="javascript:style.cursor='hand'" onclick="javascript:window.open('key/a_6.4.htm','','left=100,top=100,toolbar=no,scrollbars=yes,width=600,height=600')"></img>
</td></tr>
</table>
<hr size=2 align=center color=red><br>
<br>
<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.6.5b.htm'" ></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.6.5d.htm'"></img></td>
</tr>
</table>
</BODY>
<html><script language="JavaScript">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -