structure of programming languages names, bindings, type checking, and scopes

38
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

Upload: dustin-henry

Post on 18-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

Structure of Programming Languages

Names, Bindings, Type Checking, and Scopes

Page 2: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

2

The Concept of Variables

• What do x = 1 ; and x = x + 1; mean?– “=“ is not the equal sign of mathematics!– “x=1” does not mean “x is one” !

• “x” is the name of a variable; refer to a variable – A variable is an abstraction of a

memory cell– “x” is an identifier (name) refers to a

location where certain values can be stored.

Page 3: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-3

Variables

• A variable is an abstraction of a memory cell

• Variables can be characterized as a sextuple of attributes:– Name– Address– Value– Type– Lifetime– Scope

Page 4: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-4

Variables Attributes

• 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

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

unions

Page 5: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-5

Variables Attributes (continued)

• 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

• Abstract memory cell - the physical cell or collection of cells associated with a variable

Page 6: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-6

Names

• Length– 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 one

Page 7: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-7

Names (continued)

• Connectors– Pascal, Modula-2, and FORTRAN 77 don't allow– Others do

Page 8: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-8

Names (continued)

• 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 other languages are not

Page 9: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-9

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

Page 10: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-10

The Concept of Binding

• A binding is an association, such as

between an attribute and an entity, or

between an operation and a symbol

• Binding time is the time at which a

binding takes place.

Page 11: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

The Concept of Binding

• Count = Count + 2

• Type of count is bound (compiler time)

• Set of possible Values of Count is bound

(compiler time)

• Meaning of “+” is bound (compiler time)

• Value of new Count is bounded (run time)

Page 12: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-12

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 FORTRAN 77 variable to a memory cell (or a C static variable)

• Runtime -- bind a nonstatic local variable to a memory cell

Page 13: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-13

Static and Dynamic Binding

• A binding is static if it first occurs before

run time and remains unchanged

throughout program execution.

Page 14: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

Static and Dynamic Binding

• A binding is dynamic if it first occurs during

execution or can change during execution of

the program ( like stdin, and hostname (for

mobile code))

• In PHP

– List = [10.2, 3.5];

– ….

– List = 47;

Page 15: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-15

Categories of Variables by Lifetimes• The lifetime of a variable is the time during

which it is bound to a particular memory cell

• Static--bound to memory cells before execution begins and remains bound to the same memory cell throughout execution, e.g., all FORTRAN 77 variables, C static variables– Advantages: efficiency (direct addressing),

history-sensitive subprogram support– Disadvantage: lack of flexibility (no recursion)

Page 16: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-16

Type 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

Page 17: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-17

Name Type Compatibility

• Name type compatibility 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:

– Subranges of integer types are not compatible with

integer types

– Formal parameters must be the same type as their

corresponding actual parameters (Pascal)

Page 18: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

18

Type Checking

• Given all the type information, (and some inferences) check for consistency:– Assignment statement.

• Given v := e; and v : T1 and e : T2,• We must verify T1 =: T2

– Return statement• Given return e; and e : T,• We must verify T is the return type of the enclosing function.

– Conditional Expression• Condition must be typed to bool.• i.e. Given if c then e1 else e2• We must verify c : bool and if e1:T1 and e2:T2 we must verify T1 =: T2

Page 19: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

19

Type Equivalence

• Structural equivalence– Given

• Celsius is integer • Fahrenheit is integer• c : Celsius• f : Fahrenheit

– Then, we can infer• c + f : integer

– What about structures and unions?• T1 * T2 =: S1 * S2 iff S1 =: T1 and S2 =: T2 • T1 + T2 =: S1 + S2 iff S1 =: T1 and S2 =: T2

– Arrays (or lists)?• T[] =: S[] iff T =: S

Page 20: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-20

Type Compatibility (continued)

• Consider the problem of two structured types:– Are two record types compatible if they are

structurally the same but use different field names?

– Are two array types compatible if they are the same except that the subscripts are different?(e.g. [1..10] and [0..9])

– Are two enumeration types compatible if their components are spelled differently?

– With structural type compatibility, you cannot differentiate between types of the same structure (e.g. different units of speed, both float)

Page 21: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

21

Type Equivalence

• Structural equivalence: – Consider Point = struct { x:Int, y:Int } and

FooBar = struct { a:Int, b:Int }; – then Point =: Int x Int and FooBar =: Int x Int – and by structural equivalence Point =: FooBar.

• Name equivalence: – Since Point and FooBar are different names Point <:> FooBar

• These are the two typical options for equivalence. Alternatives are possible:– Consider modeling Point as a set of tagged pairs:

Point =: ({x} x Int) x ({y} x Int)Then FooBar =: ({a} x Int) x ({b} x Int)  

Now, if a language supports structural equivalence, Point =: FooBar can be achieved by the programmer by choosing appropriate labels – same set for both.

Page 22: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-22

Variable Attributes: Scope

• The scope of a variable is the range of

statements over which it is visible

• The nonlocal variables of a program unit

are those that are visible but not declared

there

• The scope rules of a language determine

how references to names are associated

with variables

Page 23: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-23

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

Page 24: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-24

Blocks

– A method of creating static scopes inside program units--from ALGOL 60

– Examples:

C and C++: for (...) {int index;

... } Ada: declare LCL : FLOAT; begin

... end

Page 25: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-25

Evaluation of Static Scoping

• Assume MAIN calls A and B A calls C and D B calls A and E

MAINMAIN

E

A

C

D

B

A B

C D E

Page 26: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

Scoping

int x = 0;

int f( )

{ return x; }

int g( )

{ int x = 1; return f(); }

Page 27: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

Why Scope?

• Given multiple bindings of a single name, how do we know which binding does an occurrence of this name refer to?

Page 28: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

An Example of Block Scope in C

Page 29: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

29

Static vs. Dynamic Scoping

Page 30: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

program main   var y: Real;   procedure compute()      var x : Integer;      procedure initialize()         var z: Real;      begin {initialize}      z=x; ...      end {initialize}      procedure transform()         var x: Real;      begin {transform}      ...      end {transform}   begin {compute}   ...   end {compute}begin {main}...end {main}

Page 31: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-31

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

Page 32: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-32

Scope Example

• Static scoping – Reference to x is to MAIN's x

• Dynamic scoping – Reference to x is to SUB1's x

• Evaluation of Dynamic Scoping:– Advantage: convenience– Disadvantage: poor readability

Page 33: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-33

Scope and Lifetime

• Scope and lifetime are sometimes closely

related, but are different concepts

• Consider a static variable in a C or C++

function

Page 34: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-34

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, e.g., in Java

int sum = 0;

Page 35: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

1-35

Named Constants

• A named constant is a variable that is bound to a

value only when it is bound to storage

• Advantages: readability and modifiability

• Used to parameterize programs

• The binding of values to named constants can be

either static (called manifest constants) or dynamic

• Languages:

– FORTRAN 90: constant-valued expressions

– Ada, C++, and Java: expressions of any kind

Page 36: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

Assuming static scoping or dynamic scoping rules are used. What value does the program print?

• var x : integer; /* global variable */• procedure Update• x := x + 1;• procedure DoCall(P: procedure)• var x : integer;• x := 1;• P();• write_integer(x);• begin /* body of main program */• x := 0;• DoCall(Update);• end /* body of main program */

Page 37: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

static scoping, dynamic scoping

• x : integer /* global */• procedure one• x := 1• procedure two• x : integer• x := 2• one()• begin /* main program */• x := 0• two()• write_integer(x)• end /* main program */

Page 38: Structure of Programming Languages Names, Bindings, Type Checking, and Scopes

• Consider the following outline of a QuickSort procedure in Pascal:• procedure QuickSort(var A: Array)• procedure Partition(var A: Array; j,k: integer)• var pivot: integer;• procedure Swap(var A: Array; s,t: integer)• var temp: Type;• begin (* Swap *)• ... <== [1]• end; (* Swap *)• begin (* Partition *)• ... <== [2]• end; (* Partition *)• begin (* QuickSort *)• ... <== [3]• end; (* QuickSort *)• Indicate which variables, arguments, and subroutines are in scope at the

indicated points [1], [2], and [3] in the program. Mark the variables and arguments with the procedure that defines it (for example, at [1] A of Swap is in scope).