0% found this document useful (0 votes)
165 views41 pages

Names, Bindings and Scopes: CSE 325/CSE 425: Concepts of Programming Language

This document provides an outline and introduction for a lecture on names, bindings, and scopes in programming languages. It discusses key concepts such as: - Names are used to identify entities in a program and have attributes like case sensitivity and length. - Variables are characterized by attributes like name, address, value, type, lifetime, and scope. Binding is the association between entities like a variable and its type or value. - Scope determines where in a program a name can be referenced. Languages use static or dynamic scoping. - Memory allocation and lifetimes are determined by the environment, which can be static, dynamic, or mixed for different languages. The stack and heap are used to allocate space.

Uploaded by

Shinthi Tasnim
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)
165 views41 pages

Names, Bindings and Scopes: CSE 325/CSE 425: Concepts of Programming Language

This document provides an outline and introduction for a lecture on names, bindings, and scopes in programming languages. It discusses key concepts such as: - Names are used to identify entities in a program and have attributes like case sensitivity and length. - Variables are characterized by attributes like name, address, value, type, lifetime, and scope. Binding is the association between entities like a variable and its type or value. - Scope determines where in a program a name can be referenced. Languages use static or dynamic scoping. - Memory allocation and lifetimes are determined by the environment, which can be static, dynamic, or mixed for different languages. The stack and heap are used to allocate space.

Uploaded by

Shinthi Tasnim
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
You are on page 1/ 41

LECTURE 05

NAMES, BINDINGS AND


SCOPES
CSE 325/CSE 425:
CONCEPTS OF
PROGRAMMING LANGUAGE

INSTRUCTOR: DR. F. A. FAISAL


OUTLINE
•  Introduction
•  Names
•  Variables
•  The Concept of Binding
•  Scope
•  Scope and Lifetime
•  Referencing Environments
•  Named Constants
INTRODUCTION

•  Imperative languages are abstractions of


von Neumann architecture
•  Memory
•  Processor
•  Variables are characterized by attributes
•  To design a type, must consider scope,
lifetime, type checking, initialization, and
type compatibility.
NAMES

•  A name is a string of characters to


identify some entity in a program.
•  Design issues for names:
•  Are names case sensitive?
•  Are special words reserved words or
keywords?
NAMES (CONT.)
•  Length
•  If too short, they cannot be connotative
•  Language examples:
•  Fortran 95: maximum 31
•  C99: no limit but only the first 63 are
significant; also, external names are limited
to a maximum of 31
•  C#, Ada and java: no limit, and all are
significant
•  C++: no limit, but implementers often
impose one
NAMES (CONT.)

•  PHP: all variable names must begin with


dollar signs
•  Perl: all variable names begin with
special characters, which specify the
variable’s type
•  Ruby: variable names that begin with @
are instance variables; those that begin
with @@ are class variables.
NAMES (CONT.)
•  Case sensitivity
•  Disadvantage: readability (names that
look alike are different)
•  Names in the C-based languages are case
sensitive
•  Names in others are not
•  Worse in C++, Java, C# because
predefined names are mixed case (e.g.
IndexOutofBoundsException)
NAMES (CONTINUED)
•  Special words
•  An aid to readability; used to delimit or separate
statement clauses
•  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, therefore 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
•  Potential problem with reserved words: if there
are too many many collisions occur (e.g. COBOL
has 300 reserved words)
KEYWORD(PROGRAMIZ)
VARIABLES
•  A variables is an abstraction of a memory cell
•  Variables can be characterized as a six type of
attributes:
•  Names
•  Address
Related Concepts:
•  Value •  Aliases
•  Type •  Binding
•  Binding Times
•  Lifetime •  Declarations
•  Scoping Rules
•  Scope •  Referencing Environments
VARIABLES
ATTRIBUTES
•  Names- not all variables have them but most of
them have
•  Address- the memory address with which it is
associated
•  A variable may have different addresses at
different times during execution
•  A variable may have different addresses at
different places in a program
•  If two variable names can be used to access the
same memory location, they are called aliases
•  Aliases are created via pointers, reference
variables, C, C++ unions
ALIASING IN C++
VARIABLES
ATTRIBUTES (CONT)

•  Type- Determines the range of values of variables and the


set of operations that are defined for values of that type; in
the case of floating point, type also determines the
precision
•  Value- the contents of the location with which the variable
is associated.
•  The I-value of a variable is its address
•  The r-value of a variable is its value
•  Abstract memory cell- the physical cell or collection of
cells associated with a variable
THE CONCEPT OF
BINDING

•  A binding is an association between an


entity and an attribute, such as between a
variable and its type or value, or between
an operation and a symbol
•  Binding time is the time at which a
binding takes place.
POSSIBLE BINDING
TIMES
•  Language design time – bind operator symbols
to operations
•  Language implementation time – bind floating
point type to a representation
•  Compile time – bind a variable to a type in C or
Java
•  Load time – bind a C or C++ static variable to a
memory cell
•  Runtime – bind a nonstatic local variable to a
memory cell
STATIC AND DYNAMIC
BINDING

•  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.
EXAMPLE (STATIC
BINDING)
EXAMPLE (DYNAMIC
BINDING)
FUNCTION CALLING
IN JS
//JavaScript-C24.2.0 (SpiderMonkey)

function addSquares(a, b) {
function square(x) {
return x * x;
}
return square(a) + square(b);
}
a = addSquares(2, 3); // returns 13
b = addSquares(3, 4); // returns 25
c = addSquares(4, 5); // returns 41

print (a);
print (b);
print (c);
SCOPES
•  We already know the scopes in C, Java!!!
•  In Ada, the block structured language-
•  A block is formed by a begin-end pair-

declare x: integer;
y: boolean;

begin
x := 2;
y := true;
z := x + 1;
….
end;
EXAMPLE (RUN IT)
with Ada.Text_IO; use Ada.Text_IO;

procedure Hello is

begin

declare x: integer;

y: boolean;

begin

x := 2;

y := true;

x := x + 1;

declare x: integer;

y: boolean;

begin

x := 3;

y := false;

x := x + 6;

Put_Line("x=" & Integer'Image(x));

end;

Put_Line("x=" & Integer'Image(x));

end;

end Hello;
C
PRO
GRA
M
IF ELSE (ADA)
SYMBOL TABLE(IMP)
SYMBOL TABLE(IMP)
SYMBOL TABLE(IMP)
SYMBOL TABLE(IMP)

•  This is a static scoping.


•  If the symbol table is
managed by a compiler
and the bindings of the
declaration are all static
DYNAMIC SCOPING
DYNAMIC SCOPING
DYNAMIC SCOPING
EQUIVALENT TO ADA
STRUCT (SYMBOL
TABLE)
PROCEDURE
OVERLOADING
IN ADA

•  Both Ada and C++


allows operator
overloading, but
Java don’t allow
o p e r a t o r
overloading
ALLOCATION, LIFETIMES,
AND THE ENVIRONMENT
•  The environment may be constructed statically, dynamically,
or with a mixture of the two.
•  A language that uses a completely static environment is
FORTRAN- all locations are bound statically.
•  A language that uses a completely dynamic environment is
LISP- all locations are bound during execution.
•  C, C++, Java and Ada languages are in the middle- some
statically and some dynamically.
•  However, some names may be not bound to locations. For
example names with constants and data types may represent
purely compile-time quantities that have no existence at load
or execution time.
•  const int MAX = 11;
EXAMPLE •  When the code is executed
and a block is entered,
memory for the variables
declared at the beginning of
each block is allocated.
•  When a block is exited, this
memory is deallocated.
CONT.

Those are the stack-


based allocations
ENVIRONMENT WITH
STACK AND A HEAP
•  To allow for arbitrary allocation and
deallocation using new and delete (or
malloc and free), the environment
must have area in memory.

•  From this locations can be allocated
in response to calls to new and to
which locations can be reclaimed in
response to calls to delete.

•  Such area is called as heap (but no
relation with heap data structures).

•  Allocation on the heap is usually
referred to as dynamic allocation.

GLOBAL SCOPE IN
PHP
<?php //php 7.0.8

$day = "Monday";
$Month = "January";

function calendar(){
$day = "Tuesday";
global $Month;
print "local day is $day <br />";
$gday = $GLOBALS['day'];
print "global day is $gday <br />";
print "global month is $Month <br />";
}

calendar();
?>
ALIASES
DANGLING
REFERENCES

A Dangling References
occurs if an object can be
accessed beyond its lifetime
in the environment.
THANKS

You might also like