0% found this document useful (0 votes)
14 views26 pages

PL Chapter 4 Part 1

Uploaded by

the future
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views26 pages

PL Chapter 4 Part 1

Uploaded by

the future
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Names, Bindings, and

Scopes
Programming Languages
Chapter 4 Part 1
Introduction
 Imperative PLs are, to varying degrees, abstractions of
the underlying von Neumann computer architecture.
 The architecture’s two primary components are its

memory, which stores both instructions and data, and


its processor, which provides operations for modifying
the contents of the memory.
 The abstractions in a language for the memory cells of

the machine are variables.


 A variable can be characterized by a collection of

properties, or attributes, the most important of which is


type, a fundamental concept in PLs.

PL chapter 4 Names and Data Typ 2


es
Cont…
 Designing the data types of a language requires that a
variety of issues be considered.
 Among the most important of these issues are the scope

and lifetime of variables.


 Functional PLs allow expressions to be named. These

named expressions appear like assignments to variable


names in imperative languages, but are fundamentally
different in that they cannot be changed.
 Pure functional languages do not have variables that

are like those of the imperative languages. However,


many functional languages do include such variables.

PL chapter 4 Names and Data Typ 3


es
Names
 Names are also associated with subprograms, formal
parameters, and other program constructs.
 The term identifier is often used interchangeably with

name.

PL chapter 4 Names and Data Typ 4


es
Design Issues
 The following are the primary design issues for
names:

Are names case sensitive?

 Are the special words of the language reserved


words or keywords?

PL chapter 4 Names and Data Typ 5


es
Name Forms
 A name is a string of characters used to identify some
entity in a program.
 Names in most PLs have the same form: a letter

followed by a string consisting of letters, digits, and


underscore characters ( _ ).
 In many languages, notably the C-based languages,

uppercase and lowercase letters in names are distinct;


that is, names in these languages are case sensitive.

PL chapter 4 Names and Data Typ 6


es
Special Words
 Special words in PLs are used to make programs more
readable by naming actions to be performed.
 They also are used to separate the syntactic parts of

statements and programs.


 In most languages, special words are classified as

reserved words, which means they cannot be redefined


by programmers, but in some they are only keywords,
which means they can be redefined.
 A keyword is a word of a programming language that

is special only in certain contexts.

PL chapter 4 Names and Data Typ 7


es
Cont…
 A reserved word is a special word of a PL that cannot
be used as a name.
 As a language design choice, reserved words are better

than keywords because the ability to redefine keywords


can be confusing.
 There is one potential problem with reserved words: If

the language includes a large number of reserved


words, the user may have difficulty making up names
that are not reserved.

PL chapter 4 Names and Data Typ 8


es
Variables
 A program variable is an abstraction of a computer
memory cell or collection of cells.
 Programmers often think of variable names as names

for memory locations, but there is much more to a


variable than just a name.
 The move from machine languages to assembly

languages was largely one of replacing absolute


numeric memory addresses for data with names,
making programs far more readable and therefore
easier to write and maintain.

PL chapter 4 Names and Data Typ 9


es
Cont…
 That step also provided an escape from the problem of
manual absolute addressing, b/c the translator that
converted the names to actual addresses also chose
those addresses.

 A variable can be characterized as a sextuple of


attributes: (name, address, value, type, lifetime, and
scope).

PL chapter 4 Names and Data Typ 10


es
Name
 Variable names are the most common names in
programs.
 Most variables have names.

PL chapter 4 Names and Data Typ 11


es
Address

 The address of a variable is the machine memory address


with which it is associated.
 In many languages, it is possible for the same variable
to be associated with different addresses at different
times in the program.
 For example, if a subprogram has a local variable that

is allocated from the run-time stack when the


subprogram is called, different calls may result in that
variable having different addresses.

PL chapter 4 Names and Data Typ 12


es
Cont…
 The address of a variable is sometimes called its l-
value, because the address is what is required when the
name of a variable appears in the left side of an
assignment.
 It is possible to have multiple variables that have the

same address.
 When more than one variable name can be used to

access the same memory location, the variables are


called aliases.
 Aliasing is a hindrance to readability because it allows

a variable to have its value changed by an assignment


to a different variable.
PL chapter 4 Names and Data Typ 13
es
Type
 The type of a variable determines the range of values
the variable can store and the set of operations that are
defined for values of the type.

 For example, the int type in Java specifies a value


range of -2147483648 to 2147483647 and arithmetic
operations for addition, subtraction, multiplication,
division, and modulus.

PL chapter 4 Names and Data Typ 14


es
Value
 The value of a variable is the contents of the memory
cell or cells associated with the variable.
 It is convenient to think of computer memory in terms

of abstract cells, rather than physical cells.


 The physical cells, or individually addressable units, of

most contemporary computer memories are byte-size,


with a byte usually being eight bits in length.
 This size is too small for most program variables. An

abstract memory cell has the size required by the


variable with which it is associated.

PL chapter 4 Names and Data Typ 15


es
Names, Bindings and Scopes

 A name is a mnemonic character string representing


something else:
• x, func1, f, prog1, null are names
• 1, 2, 3 are not names
• +, <=, . . . may be names if they are not built-in operators

 Design issues for identifiers (names)

• Does the language have a maximum length?


-most languages either have no restriction or the
restriction is large enough to be immaterial
(e.g., 31 in C and Pascal, 30 in COBOL)
• Does the language have legal connectors?
-most languages use _ or “camel” notation
-COBOL uses – (hyphen) detracting from
readability

PL chapter 4 Names and Data Typ 16


es
• Are letters case sensitive?
-this can detract from readability and
writability or both
• Are the special words of the language context
sensitive (key words) or reserved?

A variable (identifier) has six attributes

 Name
 Address
 Type
 Representation (value
 Scope
 Lifetime

PL chapter 4 Names and Data Typ 17


es
 Name
• can be one-one, many-one, none-one mapping to
memory
 Address
• Point to a location in memory
• Can vary dynamically
• Two names for the same address = aliasing
 Type
• Range of values + legal operations
 representation/value
• Interpreted content of the location
• L-value (address)
• R-value (content/value)

PL chapter 4 Names and Data Typ 18


es
Binding
 A binding is an association between two

entities:
• Name and memory location (for variables)
• Name and function
 Typically a binding is between a name and
the object it refers to.
 The scope of a binding is the region of a

program or time interval(s) in the program’s


execution during which the binding is active.
 A scope is a maximal region of the program

where no bindings are destroyed (e.g., body


of a procedure).

PL chapter 4 Names and Data Typ 19


es
Possible binding times
 Language design time

• bind operator symbols to operations


 Language implementation time
• Bind floating point type to a representation
 Compile time
• Map high-level language constructs to machine code
• Layout static data in memory
 Link time
• Resolve references between separately compiled modules
 Load time
• Assign machine addresses to static data
 Run time
• Bind values to variables
• Allocate dynamic data and assign to variables
• Allocate local variables of procedures on the stack

PL chapter 4 Names and Data Typ 20


es
 Static and Dynamic binding

 A binding is static if it occurs before runtime


and remains unchanged throughout
program execution
 A binding is dynamic if it occurs during

execution or can change during execution


 The binding times of various attributes is an

important characteristic of a language

PL chapter 4 Names and Data Typ 21


es
 Example: count = count + 5;

• The type of count is bound at compile time.


• The set of possible values of count is bound

at compiler design time.


• The meaning of the operator + is bound at
compile time, when the types of its
operands
have been determined.
• The internal representation of the literal 5 is
bound at compiler design time.
• The value of count is bound at execution
time with this statement.
PL chapter 4 Names and Data Typ
es
22
 Importance of Binding Time

 Early binding
• Faster code
• Typical in compiled languages
 Late binding
• Greater flexibility
• Typical in interpreted languages

PL chapter 4 Names and Data Typ 23


es
 Storage Bindings and Lifetime

• Allocation is the process of acquiring a


memory cell or calls from a pool of
available
memory.
• Deallocation is the process of returning an
unbound memory cell or cell back to the
pool
of available memory.
• The lifetime of a variable is the time that a
variable is bound to a specific memory
location.
PL chapter 4 Names and Data Typ 24
es
Object and Binding Lifetime

 Object lifetime
• Period between the creation and destruction of the object
• Example: time between creation and destruction of a
dynamically allocated variable in C++ using new and
delete
 Binding lifetime
• Period between the creation and destruction of the
binding (name-to-object association)

 Two common mistakes


• Dangling reference: no object for a binding (e.g., a pointer
refers to an object that has already been deleted)
• Memory leak: no binding for an object (preventing the
object from being deallocated)

PL chapter 4 Names and Data Typ 25


es
Storage Allocation
An object’s lifetime corresponds to the mechanism
used to manage the space where the object resides.
 Static object

• Object stored at a fixed absolute address


• Object’s lifetime spans the whole execution of the
program.
 Object on stack
• Object allocated on stack in connection with a subroutine
call
• Object’s lifetime spans period between invocation of the
subroutine and return from the subroutine.
 Object on heap
• Object stored on heap
• Object created/destroyed at arbitrary times
– Explicitly by programmer or
– Implicitly by garbage collector
PL chapter 4 Names and Data Typ 26
es

You might also like