LexicalNesting - Lexical Nesting Lexical Nesting 5.1...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Lexical Nesting Beta Draft - Do not distribute © 2001, By Randall Hyde Page 1375 Le xical Nesting Chapter Five 5.1 Chapter Overview This chapter discusses nested procedures and the issues associated with calling such procedure and accessing local v ariables in nested procedures. Nesting procedures of fers HLA users a modicum of b uilt-in information hiding support. Therefore, the material in this chapter is v ery important for those w anting to write highly structured code. This information is also important to those who w ant to understand ho w block structured high le v el languages lik e P ascal and Ada operate. 5.2 Lexical Nesting, Static Links, and Displays In block structured languages lik e P ascal 1 it is possible to nest procedures and functions. Nesting one procedure within another limits the access to the nested procedure; you cannot access the nested procedure from outside the enclosing procedure. Lik e wise, v ariables you declare within a procedure are visible inside that procedure and to all procedures nested within that procedure 2 . This is the standard block structured lan - guage notion of scope that should be quite f amiliar to an yone who has written P ascal or Ada programs. There is a good deal of comple xity hidden behind the concept of scope, or le xical nesting, in a block structured language. While accessing a local v ariable in the current acti v ation record is ef fi cient, accessing global v ariables in a block structured language can be v ery inef fi cient. This section will describe ho w a high le v el language lik e P ascal deals with non-local identifi ers and ho w to access global v ariables and call non-local procedures and functions. 5.2.1 Scope Scope in most high le v el languages is a static, or compile-time concept 3 . Scope is the notion of when a name is visible, or accessible, within a program. This ability to hide names is useful in a program because it is often con v enient to reuse certain (non-descripti v e) names. The i v ariable used to control most FOR loops in high le v el languages is a perfect e xample. The scope of a name limits its visibility within a program. That is, a program has access to a v ariable name only within that name’ s scope. Outside the scope, the program cannot access that name. Man y pro - gramming languages, lik e P ascal and C++, allo w you to reuse identifi ers if the scopes of those multiple uses do not o v erlap. As you’ v e seen, HLA pro vides scoping features for its v ariables. There is, ho we v er , another issue related to scope: addr ess binding and variable lifetime . Address binding is the process of associating a memory address with a v ariable name. V ariable lifetime is that portion of a program’ s e x ecution during which a memory location is bound to a v ariable. Consider the follo wing P ascal procedures: procedure One(Entry:integer); var i,j:integer; procedure Two(Parm:integer); var j:integer; begin for j:= 0 to 5 do writeln(i+j); 1. Note that C and C++ are not block structured languages. Other block structured languages include Algol, Ada, and Mod-
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/08/2011 for the course CS 101 taught by Professor Jitenderkumarchhabra during the Summer '11 term at National Institute of Technology, Calicut.

Page1 / 24

LexicalNesting - Lexical Nesting Lexical Nesting 5.1...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online