0% found this document useful (0 votes)
8 views19 pages

Names and Bindings in Programming

Chapter 5 discusses names, bindings, and scopes in programming languages, focusing on the characteristics and design issues of names and variables. It explains the concept of binding, including static and dynamic type bindings, and the implications of each on program execution and reliability. The chapter also highlights the importance of variable attributes, such as name, address, type, and value, in the context of programming languages.

Uploaded by

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

Names and Bindings in Programming

Chapter 5 discusses names, bindings, and scopes in programming languages, focusing on the characteristics and design issues of names and variables. It explains the concept of binding, including static and dynamic type bindings, and the implications of each on program execution and reliability. The chapter also highlights the importance of variable attributes, such as name, address, type, and value, in the context of programming languages.

Uploaded by

hadissss810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 5: Names, Bindings,

and Scopes
Week 6 Lecture 1 (1/4)
Objectives
5.1 Introduction

5.2 Names

5.3 Variables

5.4 The Concept of Binding


5.4.1 Binding of Attributes to Variables

5.4.2 Types of Binding


[Link] Static Type Binding

[Link] Dynamic Type Binding

CSCI351 - Concepts of Programming


1-2
Languages
Introduction
• Imperative languages are abstractions of von Neumann
architecture
– Memory (storing instructions & data)

– Processor (operations for modifying contents of the memory)

– Characteristics of abstraction similar to characteristics of cells;


ex: an integer variable is usually represented directly in one or
more bytes of memory

– A variable can be characterized by a collection of properties, or


attributes (type - designing a data type, discussed in Chapter 6)

Note that the phrase C-based languages in the book refers to C,


Objective-C, C++, Java, and C#.
CSCI351 - Concepts of Programming
1-3
Languages
Names (1/4)
• Design Issues for Names/Identifiers:
– used with variables, subprograms, parameters, other
constructs

– Maximum length?

– Are names case sensitive?

– Are special words reserved words or keywords?

CSCI351 - Concepts of Programming


1-4
Languages
Names (2/4)
• Length:
– Name should be connotative
• Example: to declare “length”– use the word instead of “L”

– Name wants reasonable length represent for symbol table


• FORTRAN I: maximum 6

• COBOL: maximum 30

• FORTRAN 90 and ANSI C: maximum 31

• Ada and Java: no limit, and all are significant

• C++: no limit, but implementers often impose one

CSCI351 - Concepts of Programming


1-5
Languages
Names (3/4)
• Case Sensitivity:
– Disadvantage: readability
• Names that look alike are different

• Worse in C++ and Java because predefined names are mixed


case (e.g. IndexOutofBoundsException)

– C, C++, and Java names are case sensitive

– The names in many other languages are not

– “Camel” notation – myltem – embedded uppercase – is a


style choice, not a language decision

CSCI351 - Concepts of Programming


1-6
Languages
Names (4/4)
• Special Words:
– An aid to readability; used to delimit or separate
statement clauses such as „;‟ – “{ }‟ – etc.

– A keyword is a word that is special only in certain


contexts, e.g. in Fortran:
• Real VarName (Real is a data type followed with a name,
therefor Real is a keyword)

• Real = 3.4 (Real is a variable)

– A reserved word is a special word that cannot be used as


a user-defined name.
CSCI351 - Concepts of Programming
1-7
Languages
Variable (1/5)
• Variable Name
– A program variable is an abstraction of a computer memory cell
or collection of cells.

– A variable can be characterized as a sextuple of attributes:


name, address, value, type, lifetime, and scope

– Discussion of variable attributes will lead to examinations of the


important related concepts of aliases, binding, binding times,
declarations, scoping rules, and referencing environments.

– Most variables have names, which can be sometimes called its l-


value.

CSCI351 - Concepts of Programming


1-8
Languages
Variable (2/5)
• Variable Address (1/2)
– Address of a variable is the machine memory address with which
it is associated.

– In some languages, it is possible for the same variable to be


associated with different addresses at different times during the
program execution.
• 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.

CSCI351 - Concepts of Programming


1-9
Languages
Variable (3/5)
• Variable Address (2/2)
– It is possible to have multiple variables that have the same
address; variables are called aliases.

– Aliases are created via pointers, reference variables, C and


C++ unions (discussed more in Chapter 6).

– Aliasing can be created in many languages through


subprogram parameters (discussed in Chapter 9).

– Aliases are harmful to readability (program readers must


remember all of them).

CSCI351 - Concepts of Programming


1-10
Languages
Variable (4/5)
• Variable 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.

CSCI351 - Concepts of Programming


1-11
Languages
Variable (5/5)
• Variable Value
– The value of a variable is the contents of the memory cell or
cells associated with the variable.

– A variable‟s value is sometimes called its r-value because it is


what is required when the name of the variable appears in the
right side of an assignment statement.

• To access the r-value, the l-value must be


determined first.

CSCI351 - Concepts of Programming


1-12
Languages
The Concept of Binding (1/7)
• A binding is an association, such as between:
– An attribute and an entity (e.g., type of a variable),

– An operation and a symbol (e.g., meaning of *),

– A function and its definition

• Binding time is the time at which a binding takes place:


– Design time: bind operator symbols (e.g., *) to operations

– Implementation time: bind floating point type to a representation

– Compile time: bind a variable to a type

– Link time: bind library subprogram to code

– Load time: bind a C static variable to a memory cell

– Run time: bind a non-static local variable to a memory cell

CSCI351 - Concepts of Programming


1-13
Languages
The Concept of Binding (2/7)
• Example: Consider the following C++ assignment statement:

count = count + 5;
– The type of count is bound at compile time.

– The set of possible values of count is bound at compile time.

– The meaning of the operator symbol + is bound at compile time,


when the types of its operands have been determined.

– The internal representation of the literal 5 is bound at design


time.

– The value of count is bound at execution time.

CSCI351 - Concepts of Programming


1-14
Languages
The Concept of Binding (3/7)
• Binding of Attributes to Variables
– A binding is static if it first occurs before run time and
remains unchanged throughout program execution.

– A binding is dynamic if it first occurs during execution or


can change during execution of the program

• Binding Questions
– How is a type specified?
– When does the binding take place?

CSCI351 - Concepts of Programming


1-15
Languages
The Concept of Binding (4/7)
• Type Bindings (1/4)
– Static Type Binding (1/2)
• If static, the type may be specified by either an explicit or an implicit
declaration

• An explicit declaration is a program statement used for declaring the


types of variables: int count;

• An implicit declaration is a default mechanism for specifying types of


variables (the first appearance of the variable in the program)

• FORTRAN, PL/I, BASIC, and Perl provide implicit declarations


– Advantage: writability

– Disadvantage: reliability

CSCI351 - Concepts of Programming


1-16
Languages
The Concept of Binding (5/7)
• Type Bindings (2/4)
– Static Type Binding (2/2)
• Example: Perl Type Binding
– Begin with $ is scalar (string or numeric): $a = 10;

– Begin with @ is array: @ages = (25, 30, 40);

– Begin with % is hash:

%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);

– Different namespaces

– Advantage: can tell from name what type of data it is

CSCI351 - Concepts of Programming


1-17
Languages
The Concept of Binding (6/7)
• Type Bindings (3/4)
– Dynamic Type Binding (1/2)
• Type not specified by declaration, not determined by name
(JavaScript and PHP)

• Specified through an assignment statement e.g., JavaScript


list = [2, 4.33, 6, 8];

list = 17.3;
Advantage: flexibility (generic program units)

Disadvantages:

– High cost (dynamic type checking requires run-time descriptors)

– Type error detection by the compiler is difficult

CSCI351 - Concepts of Programming


1-18
Languages
The Concept of Binding (7/7)
• Type Bindings (4/4)
– Dynamic Type Binding (2/2)
• Example of dynamic type
i = x; // desired

i = y; // typed accidentally, y is array

Later uses of i expect a scalar, but it is now an array so incorrect results


occur.

With static binding, i = y; would generate an error, easier to find


problem.

CSCI351 - Concepts of Programming


1-19
Languages

You might also like