1 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main...

31
1 1 Chapter 5 Names Bindings Type Checking Scope

Transcript of 1 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main...

11

Chapter 5

NamesBindings

Type Checking Scope

22

High-Level Programming LanguagesHigh-Level Programming Languages

• Two main goals:Two main goals:– Machine independenceMachine independence– Ease of programmingEase of programming

• High-level programming language are independent High-level programming language are independent of any particular instruction setof any particular instruction set

– But compilation to assembly code requires a target But compilation to assembly code requires a target instruction setinstruction set

– There is a trade-off between machine independence and There is a trade-off between machine independence and efficiencyefficiency

» E.g.E.g. Java vs. C Java vs. C

33

Ease of ProgrammingEase of Programming

• The driving problem in programming language designThe driving problem in programming language design

– NamesNames– Control FlowControl Flow– TypesTypes– SubroutinesSubroutines– Object OrientationObject Orientation– ConcurrencyConcurrency– Declarative ProgrammingDeclarative Programming

Programming Programming LanguageLanguage

44

NamingNaming

• NamingNaming is the process by which the programmer is the process by which the programmer associates a name with a potentially complicated associates a name with a potentially complicated program fragmentprogram fragment

– The goal is to hide complexityThe goal is to hide complexity– Programming languages use name to designate variables, Programming languages use name to designate variables,

types, classes, methods, operators,…types, classes, methods, operators,…

• Naming provides Naming provides abstractionabstraction – E.g.E.g. Mathematics is all about the formal notation (i.e. Mathematics is all about the formal notation (i.e.

naming) that lets us explore more and more abstract naming) that lets us explore more and more abstract conceptsconcepts

55

Variable NamesVariable Names

Design issues - What should the maximum length be? - Are connector characters allowed? - Are names case sensitive? - Are special words reserved words or keywords?

Length

- FORTRAN I: maximum 6 - COBOL: maximum 30 - FORTRAN 90 and ANSI C: maximum 31 - Ada: no limit, and all are significant - C++: no limit, but implementers often impose one

66

Case sensitivity

Disadvantage: readability (names that look alike are different)

• C, C++, Java, and Modula-2 names are case sensitive• The names in other languages are not

Special words

A keyword is a word that is special only in certain contextsDisadvantage: poor readability

A reserved word is a special word that cannot be used as a user-defined name

Variable NamesVariable Names

77

A variable is an abstraction of a memory cell

Variables can be characterized as a collection of attributes: name, address, value, type, lifetime, and scope

Name - not all variables have them

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

Variable NamesVariable Names

88

Categories of variables by lifetimes

Static--bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. e.g. C static variables Advantage: efficiency (direct addressing)Disadvantage: lack of flexibility (no recursion)

Stack-dynamic--Storage bindings are created for variables when their declaration statements are elaborated. Advantage: allows recursion; conserves storage

Disadvantages: - Overhead of allocation and de-allocation - Subprograms cannot be history sensitive - Inefficient references (indirect addressing)

99

Explicit heap-dynamic--Allocated and de-allocated by explicit directives, specified by the programmer, which take effect during execution

• Referenced only through pointers or references e.g. dynamic objects in C++ (via new and delete) all objects in Java

Advantage: provides for dynamic storage managementDisadvantage: inefficient and unreliable

Implicit heap-dynamic--Allocation and de-allocation caused by assignment statements

Advantage: flexibilityDisadvantages:

- Inefficient, because all attributes are dynamic - Loss of error detection

Categories of variables by lifetimes

1010

Type CheckingType Checking

Type checking is the activity of ensuring that the operands of an operator are of compatible types

A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type. This automatic conversion is called a coercion.

A type error is the application of an operator to an operand of an inappropriate type

• If all type bindings are static, nearly all type checking can be static• If type bindings are dynamic, type checking must be dynamic

A programming language is strongly typed if type errors are always detected

1111

Strong TypingStrong Typing

Allows the detection of the misuses of variables that result in type errors.

C and C++ don’t have strong typing: parameter type checking can be avoided; unions are not type checked (Java is similar)

1212

Type CompatibilityType Compatibility

Type compatibility by name means the two variables have compatible types if they are in either the same declaration or in declarations that use the same type name

- Easy to implement but highly restrictive- Sub-ranges of integer types are not compatible with integer types- Formal parameters must be the same type as their corresponding actual parameters (Pascal)

Type compatibility by structure means that two variables have compatible types if their types have identical structures

- More flexible, but harder to implement

1313

ScopeScope

The scope of a variable is the range of statements over which it is visibleThe non-local variables of a program unit are those that are visible but not declared thereThe scope rules of a language determine how references to names are associated with variables

Static scopeBased on program text. To connect a name reference to a variable, 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

1414

ScopeScope

Variables can be hidden from a unit by having a "closer" variable with the same name

C++ allows access to these "hidden" variables

Blocks - a method of creating static scopes inside program units

Examples:

C and C++:

for (...) { int index; ... }

1515

Static ScopingStatic Scoping

• Java exampleJava example

Time t = new Time(“Feb 1 2002”);Time t = new Time(“Feb 1 2002”);

System.out.println(t);System.out.println(t);

t = new Time(“Jan 30 2002”);t = new Time(“Jan 30 2002”);

System.out.println(t);System.out.println(t);

t = new Time(“Jan 28 2002”);t = new Time(“Jan 28 2002”);

Time.mysteriousMethod(t)Time.mysteriousMethod(t)

System.out.println(t);System.out.println(t);

1616

Nested SubroutinesNested SubroutinesClosest Nested Subroutine RuleClosest Nested Subroutine Rule

1717

Referencing Referencing EnvironmentsEnvironments

The referencing environment of a statement is the collection of all names that are visible in the statement

In a static scoped language, that is the local variables plus all of the visible variables in all of the enclosing scopes

A subprogram is active if its execution has begun but has not yet terminated

In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms

1818

A named constant is a variable that is bound to a value only when it is bound to storage

Advantages: readability and modifiability

The binding of values to named constants can be either static (called manifest constants) or dynamic

Variable Initialization

The binding of a variable to a value at the time it is bound to storage is called initialization

Initialization is often done on the declaration statement

Referencing Referencing EnvironmentsEnvironments

1919

Control and Data AbstractionControl and Data Abstraction

• Control abstractionControl abstraction allows the programmer to hide allows the programmer to hide an arbitrarily complicated code behind a simple an arbitrarily complicated code behind a simple interfaceinterface

– SubroutinesSubroutines– ClassesClasses

• Data AbstractionData Abstraction allows the programmer to hide allows the programmer to hide data representation details behind abstract operationsdata representation details behind abstract operations

– ADTsADTs– ClassesClasses

2020

Binding TimeBinding Time

• A A bindingbinding is an association between two things is an association between two things– E.g. Name of an object and the objectE.g. Name of an object and the object

• Binding timeBinding time is the time at which a binding is is the time at which a binding is createdcreated

– Language design timeLanguage design time– Language implementationLanguage implementation– Program writing timeProgram writing time– Compile TimeCompile Time– Link TimeLink Time– Load TimeLoad Time– Run TimeRun Time

2121

Static vs. Dynamic BindingStatic vs. Dynamic Binding

A binding is static if it occurs before run time and remains unchanged throughout program execution.

A binding is dynamic if it occurs during execution or can change during execution of the program.

Type Bindings 1. How is a type specified? 2. When does the binding take place?

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

• An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program)

2222

Dynamic Type BindingDynamic Type Binding

Advantage: flexibility (generic program units)Disadvantages: 1. High cost (dynamic type checking and interpretation) 2. Type error detection by the compiler is difficult

Type Inferencing (ML, Miranda, and Haskell)

Rather than by assignment statement, types are determined from the context of the reference

Storage Bindings

Allocation - getting a cell from some pool of available cellsDeallocation - putting a cell back into the pool

The lifetime of a variable is the time during which it is bound to a particular memory cell

2323

Binding Time ImpactBinding Time Impact

• Binding times have a Binding times have a crucialcrucial impact in programming impact in programming languageslanguages

– They are a fundamental design decisionThey are a fundamental design decision

• In general, early binding is associated with greater In general, early binding is associated with greater efficiencyefficiency

– E.g.E.g. C string vs. Java’s StringBuffer C string vs. Java’s StringBuffer

• In general, late binding is associated with greater In general, late binding is associated with greater flexibilityflexibility

– E.g. E.g. Class method vs. SubroutineClass method vs. Subroutine

• Compiled languages tend to bind names earlier than Compiled languages tend to bind names earlier than interpreted languagesinterpreted languages

2424

• Events in the life of a bindingEvents in the life of a binding– CreationCreation– DestructionDestruction

» A binding to an object that no longer exists is called a A binding to an object that no longer exists is called a dangling dangling referencereference

– Deactivation and ReactivationDeactivation and Reactivation

Object LifetimeObject Lifetime

Object LifetimeObject Lifetime

• Events in the life of an objectEvents in the life of an object– CreationCreation– DestructionDestruction

2525

Storage Allocation MechanismsStorage Allocation Mechanisms

• In In static allocationstatic allocation, objects are given an absolute , objects are given an absolute address that is retained throughout the program’s address that is retained throughout the program’s executionexecution

– E.g. E.g. Global variables, Non-recursive Subroutine Global variables, Non-recursive Subroutine ParametersParameters

2626

Storage Allocation MechanismsStorage Allocation Mechanisms

• In In static allocationstatic allocation, (static) objects are given an , (static) objects are given an absolute address that is retained throughout the absolute address that is retained throughout the program’s executionprogram’s execution

– E.g. E.g. Global variables, Non-recursive Subroutine Global variables, Non-recursive Subroutine ParametersParameters

• In In stack-based allocationstack-based allocation, (stack) objects are , (stack) objects are allocated in last-in, first-out data structure, a allocated in last-in, first-out data structure, a stackstack..

– E.g.E.g. Recursive subroutine parameters Recursive subroutine parameters

2727

Static Allocation ExampleStatic Allocation Example

• Calculator language exampleCalculator language example

readread A A

readread B B

sum := A + Bsum := A + B

writewrite sum sum

MemoryMemory

AA

BB

sumsum

Size of IntegerSize of Integer

2828

Storage Allocation MechanismsStorage Allocation Mechanisms

• In In static allocationstatic allocation, (static) objects are given an , (static) objects are given an absolute address that is retained throughout the absolute address that is retained throughout the program’s executionprogram’s execution

– E.g. E.g. Global variables , Non-recursive Subroutine Global variables , Non-recursive Subroutine ParametersParameters

• In In stack-based allocationstack-based allocation, (stack) objects are , (stack) objects are allocated in last-in, first-out data structure, a allocated in last-in, first-out data structure, a stackstack..

– E.g.E.g. Subroutine parameters Subroutine parameters

• In In heap-based allocationheap-based allocation, (heap) objects may be , (heap) objects may be allocated and deallocated at arbitrary timesallocated and deallocated at arbitrary times

– E.g.E.g. objects created with C++ objects created with C++ newnew and and deletedelete

2929

Heap-based AllocationHeap-based Allocation

• The The heapheap is a region of storage in which sub-block is a region of storage in which sub-block can be allocated and deallocatedcan be allocated and deallocated

– This This notnot the the heap data structureheap data structure

3030

Heap Space ManagementHeap Space Management

• In general, the heap is allocated sequentiallyIn general, the heap is allocated sequentially

• This creates space This creates space fragmentationfragmentation problems problems– Internal fragmentationInternal fragmentation

» If size of object to allocated is larger than the size of the available If size of object to allocated is larger than the size of the available heapheap

– External fragmentationExternal fragmentation» If size of object to allocated is not larger than the size of the If size of object to allocated is not larger than the size of the

available heap, but the available space in the heap is scaterred available heap, but the available space in the heap is scaterred through the head in such a way that no contiguous allocation is through the head in such a way that no contiguous allocation is possiblepossible

• Automatic de-allocation after an object has no Automatic de-allocation after an object has no bindings is called bindings is called garbage collectiongarbage collection

– E.g.E.g. Java Java

3131

End of LectureEnd of Lecture

• Read Chapter 5Read Chapter 5