Session 3

Names, Bindings, and Scopes

October  7th, 2017

In this session we will going to discuss about Names, Bindings, and Scopes in programming language.

before we start to our topics, lets discuss about imperative programming, so what is imperative programming and what is it relation to our topic in this session (names, bindings, and scopes) ? well as you can see from our previous session,

“imperative, imperative programming is a programming paradigm that uses statements that change a program’s state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform.”

Imperative programming languages are abstractions of the von Neumann Computer architecture.  The architecture’s two primary components are its memory and its processor.

  • Memory: stores both instructions and data
  • Processor: provides operations for modifying the contents of

The abstractions in a language for the memory cells of the machine are variables.

Variables characterized by attributes
– Type: to design, must consider scope, lifetime, type checking,
initialization, and type compatibility

so by that we could conclude that , in other the computer to run as our intended to, we must put a command, like names, binding , and scopes. Names, binding , and scopes helps the user and the computer when perform programming, it helps the users to perform a calculation , write an algorithm, and understand the basic form of computer programming. In the other side, the computer, could manipulate the data that was sended by the user(in the form of variables, function, and etc), so that the output that  computer send, is similar to what the user ordered.

first thing first, lets talk about Names.

Names

Design issues for names:
• Maximum length?
• Are connector characters allowed?
• Are names case sensitive?
• Are special words reserved words or keywords?

Name Forms
ƒ A name is a string of characters used to identify some entity in a program.

ƒ If too short, they cannot be connotative

ƒ Language examples:

• 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 a length limitationbecause they do not want the symbol table in which identifiers are
stored during compilation to be too large and also to simplify the
maintenance of that table.

ƒ Names in most programming languages have the same form: a letter
followed by a string consisting of letters, digits, and (_).

ƒ Although the use of the _ was widely used in the 70s and 80s, that
practice is far less popular.

ƒ C-based languages (C, C++, Java, and C#), replaced the _ by the “camel”
notation, as in myStack.

ƒ Prior to Fortran 90, the following two names are equivalent:
Sum Of Salaries // names could have embedded spaces
SumOfSalaries // which were ignored

• Case sensitivity
– Disadvantage: readability (names that look alike are different)

• worse in C++ and Java because predefined names are
mixed case (e.g. IndexOutOfBoundsException)

• In C, however, exclusive use of lowercase for names.
– C, C++, and Java names are case sensitive Î rose, Rose, ROSE
are distinct names “What about Readability”

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.
• Ex: Fortran
Real Apple // Real is a data type followed with a
name, therefore Real is a keyword
Real = 3.4 // Real is a variable name
• Disadvantage: poor readability. Compilers and users must recognize the
difference.
• A reserved word is a special word that cannot be used as a user-defined
name.
• As a language design choice, reserved words are better than keywords.
• Ex: In Fortran, one could have the statements
Integer Real // keyword “Integer” and variable “Real”
Real Integer // keyword “Real” and variable “Integer”

Variables
• A variable is an abstraction of a memory cell(s).
• Variables can be characterized as a sextuple of attributes:
• Name
• Address
• Value
• Type
• Lifetime
• Scope

Name
– Not all variables have names: Anonymous, heap-dynamic variables
Address

• The memory address with which it is associated
• A variable name may have different addresses at different places and at
different times during execution.
// sum in sub1 and sub2
• A variable may have different addresses at different times during
execution. If a subprogram has a local var that is allocated from the run
time stack when the subprogram is called, different calls may result in that
var having different addresses.
// sum in sub1
• The address of a variable is sometimes called its l-value because that is
what is required when a variable appears in the left side of an assignment
statement.

Aliases
• 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 and C++
unions.
• Aliases are harmful to readability (program readers must remember all of
them)

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.
• 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.

Value
• The value of a variable is the contents of the memory cell or cells
associated with the variable.
• Abstract memory cell – the physical cell or collection of cells associated
with a variable.
• A variable’s value is sometimes called its r-value because that is what is
required when a variable appears in the right side of an assignment
statement.

Bindings

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.

Static Bindings : 

It first occurs before run time and remains unchanged throughout program execution.

  • Advantage : efficiency
  • Disadvantage : Lack of Flexibility

Ex :

  • int x;
  • int data type range from negative 2,147,483,648 to positive 2,147,483,647

Time during operation :

  • Language design time — bind operator symbols to operations
    For example, the asterisk symbol (*) is bound to the multiplication operation.
  • Language implementation time– bind floating point type to a representation
  • Compile time — bind a variable to a type in C or Java

Dynamic Bindings :

first occurs during execution or can change during execution of the program.

  • Advantage: flexibility (generic program units)
  • Disadvantages:

1.High cost (dynamic type checking and interpretation)
• Dynamic type bindings must be implemented using pure interpreter not compilers.
• Pure interpretation typically takes at least ten times as long as to execute equivalent machine code.

2.Type error detection by the compiler is difficult because any variable can be assigned a value of any type.

  • Load time — bind a C or C++ static variable to a memory cell)
  • Runtime — bind a nonstatic local variable to a memory cell

Ex :

  • Long double pi = 3,14159265358979323846;
  • int assignment = 95;
    int mid  = 80;
    int final  = 100;

float score;

score = (0.2*assginment)+(0.3*mid)+(0.5*final);

In general, early binding times are associated with greater efficiency Later binding times are associated with greater flexibility

Scopes

Variable Attributes: Scope

  • The scope of a variable is the range of statements over which it is visible
  • The local variables of a program unit are those that are declared in that unit
  • The nonlocal variables of a program unit are those that are visible in the unit but not declared there
  • Global variables are a special category of nonlocal variables
  • The scope rules of a language determine how references to names are associated with variables

Static Scope

  • Based on program text
  • To connect a name reference to a variable, you (or the compiler) must find the declaration
  • Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name
  • Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent
  • Some languages allow nested subprogram definitions, which create nested static scopes (e.g., Ada, JavaScript, Common LISP, Scheme, Fortran 2003+, F#, and Python)
  • Variables can be hidden from a unit by having a “closer” variable with the same name
  • Ada allows access to these “hidden” variables

–E.g.,  unit.name

Global Scope

  • C, C++, PHP, and Python support a program structure that consists of a sequence of function definitions in a file

These languages allow variable declarations to appear outside function definitions

  • C and C++have both declarations (just attributes) and definitions (attributes and storage)

A declaration outside a function definition specifies that it is defined in another file

  • PHP

–Programs are embedded in HTML markup documents, in any number of fragments, some statements and some function definitions

–The scope of a variable (implicitly) declared in a function is local to the function

–The scope of a variable implicitly declared outside functions is from the declaration to the end of the program, but skips over any intervening functions (Global variables can be accessed in a function through the $GLOBALS array or by declaring it global)

  • Python

–A global variable can be referenced in functions, but can be assigned in a function only if it has been declared to be global in the function

Evaluation of Static Scoping

  • Works well in many situations
  • Problems:

–In most cases, too much access is possible

–As a program evolves, the initial structure is destroyed and local variables often become global; subprograms also gravitate toward become global, rather than nested

Dynamic Scope

  • Based on calling sequences of program units, not their textual layout (temporal versus spatial)
  • References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point

thats all about this session today, Thank you for your attention.

Sources :

https://www2.southeastern.edu/Academics/Faculty/kyang/2014/Fall/CMPS401/ClassNotes/CMPS401ClassNotesChap05.pdf CMPS401ClassNotesChap05.doc

https://web.cs.dal.ca/~nzeh/Teaching/3136/

http://www3.cs.stonybrook.edu/~pfodor/courses/CSE307/L03_names.pdf

http://cms.binus.ac.id/Backend2/ContentCoNEW//T0152/CO/T01520030220134031Pertemuan%203%20-%20Names,%20Bindings,%20and%20Scopes.ppt

 

This entry was posted in Session Summary. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *