Top Banner
CS 363 Comparative Programming Languages Names, Type Checking, and Scopes
62

CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

Jan 20, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Comparative Programming Languages

Names, Type Checking, and Scopes

Page 2: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 2

Names

• User-defined names include variables, functions, classes, types…

• Design issues for names:– Maximum length?– Are connector characters (_,-,…) allowed?– Are names case sensitive?– Are special words reserved words or keywords?

Page 3: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 3

Names

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

Page 4: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 4

Names

• Case sensitivity – Disadvantage: readability (names that look

alike are different)• In C++ /Java because predefined names are mixed

case (e.g. IndexOutOfBoundsException)

– C, C++, and Java names are case sensitive (b and B are different variables)

– The names in some languages are not

Page 5: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 5

Names

• Special words: keywords, reserved words– Ex: while, for, …

– An aid to readability; used to delimit or separate statement clauses

• Def: A keyword is a word that is special only in certain contexts– Disadvantage: poor readability, compiling

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

Page 6: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 6

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, and scope)

• Not all variables have names (anonymous)

Page 7: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 7

Variables

• Address - the memory address with which a variable is associated – A variable may have different addresses at different

times during execution (variable local to a function)

– A variable may have different addresses at different places in a program (variable name used in multiple scopes)

– l-value of a variable (x := …)

Page 8: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 8

Variables

• If two variable names can be used to access the same memory location, they are called aliases– Aliases are harmful to readability (program readers

must remember all of them)

• How aliases can be created:– Pointers, reference variables, C and C++ unions, (and

through parameters - discussed in Chapter 9)– Some of the original justifications for aliases are no

longer valid; e.g. memory reuse in FORTRAN– Replace them with dynamic allocation

Page 9: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 9

Variables

• Type - determines the size of memory location, range of values of variables and the set of operations that are defined for values of that type, precision (floating point)

• Value - the contents of the location with which the variable is associated– r-value of a variable (… := x …)

Page 10: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 10

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: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 11

Possible Binding Times• Language design time – e.g., operator symbols to

operations• Language implementation time – e.g., bind

floating point type to a representation• Compile time – e.g., bind a variable to a type• Load time – e.g., bind a FORTRAN 77 variable to

a memory cell (or a C static variable)• Runtime – e.g., bind a local variable to a memory

cellDifferent languages make different choices about

binding times.

Page 12: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 12

The Concept of Binding

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

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

Page 13: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 13

Overloading

• More than one binding for a name in a given scope.

• All languages offer limited overloading (+ for example)

• Subroutine names (Ada, C++, Java) – differentiated by the arguments

• Built-in Operators (Ada, C++, Fortran 90)

Page 14: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 14

Type Bindings

• How is a type specified?

• When does the binding take place?

• If static, the type may be specified by either an explicit or an implicit declaration

Page 15: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 15

Types

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

• Def: 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 (less trouble with Perl)

Page 16: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 16

Types

• Dynamic Type Binding (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 and interpretation)

• Type error detection by the compiler is difficult

Page 17: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 17

Types

• Type Inferencing (ML, Miranda, and Haskell)– Rather than by assignment statement, types are

determined from the context of the reference

Page 18: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 18

Type Checking• Generalize the concept of operands and operators

to include subprograms and assignments• Def: Type checking is the activity of ensuring that

the operands of an operator are of compatible types• Def: 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.

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

Page 19: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 19

Type Checking

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

• If type bindings are dynamic, type checking must be dynamic

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

Page 20: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 20

Strong Typing

• Advantage of strong typing: allows the detection of the misuses of variables that result in type errors

• What languages are strongly typed?– FORTRAN 77 is not: parameters, EQUIVALENCE– Pascal is not: variant records– C and C++ are not: parameter type checking can be

avoided; unions are not type checked– Ada is, almost (UNCHECKED CONVERSION is

explicit loophole) (Java is similar)

Page 21: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 21

Strong Typing

• Coercion rules strongly affect strong typing--they can weaken it considerably (C++ versus Ada)

• Although Java has just half the assignment coercions of C++, its strong typing is still far less effective than that of Ada

Page 22: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 22

Type Compatibility

• Our concern is primarily for structured types

• Def: 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 23: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 23

Type Compatibility

• Def: Structure type compatibility means that two variables have compatible types if their types have identical structures

• More flexible, but harder to implement

Page 24: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 24

Type Compatibility • 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 25: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 25

Type Compatibility

• Language examples:– Pascal: usually structure, but in some cases

name is used (formal parameters)– C: structure, except for records– Ada: restricted form of name

• Derived types allow types with the same structure to be different

• Anonymous types are all unique, even in:

A, B : array (1..10) of INTEGER:

Page 26: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 26

Variable Lifetime

• Storage Bindings & Lifetime– Allocation - getting a cell from some pool of available

cells

– Deallocation - putting a cell back into the pool

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

• Lifetime dictated by the type of variable: static, stack, explicit heap, implicit heap.

Page 27: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 27

Lifetime Categories

• 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 28: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 28

Lifetime Categories• Stack-dynamic--Storage bindings are created for

variables when their declaration statements are elaborated.– If scalar, all attributes except address are statically bound

e.g. local variables in C subprograms and Java methods– Advantage: allows recursion; conserves storage– Disadvantages:

• Overhead of allocation and deallocation• Subprograms cannot be history sensitive• Inefficient references (indirect addressing)

Page 29: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 29

Lifetime Categories

• Explicit heap-dynamic--Allocated and deallocated 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 management

– Disadvantage: inefficient and unreliable

Page 30: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 30

Lifetime Categories• Implicit heap-dynamic--Allocation and

deallocation caused by assignment statementse.g. all variables in APL; all strings and arrays in Perl and JavaScript

– Advantage: flexibility– Disadvantages:

• Inefficient, because all attributes are dynamic

• Loss of error detection

Page 31: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 31

Scope

• Def: The scope of a variable declaration is the range of program statements over which it is visible

• The scope rules of a language determine how references to names are associated with variables

• The terms ‘scope’ and ‘name space’ are sometimes used interchangably.

• Two approaches: static and dynamic

Page 32: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 32

Fortran 77 Name Space

f1()variablesparameterslabels

f2()variablesparameterslabels

f3()variablesparameterslabels

common block a

common block b

Global

Global scope holds procedure namesand common blocknames. Procedureshave local variables parameters, labels and can import common blocks

Page 33: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 33

Scheme Name Space

• All objects (built-in and user-defined) reside in single global namespace

• ‘let’ expressions create nested lexical scopes

Global

map

2

cons

var

f1()f2()

let

let

let

Page 34: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 34

C Name Space• Global scope holds

variables and functions

• No function nesting

• Block level scope introduces variables and labels

• File level scope with static variables that are not visible outside the file (global otherwise)

Global a,b,c,d,. . .

File scope static namesx,y,z

File scope static namesw,x,y

f1() f2()

f3()

variablesparameterslabels

variables

variables, param

Block Scopevariableslabels

Block scope

Block scope

Page 35: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 35

Java Name Space

• Limited global name space with only public classes

• Fields and methods in a public class can be public visible to classes in other packages

• Fields and methods in a class are visible to all classes in the same package unless declared private

• Class variables visible to all objects of the same class.

Public Classes

package p1 package p2

package p3

public class c1

class c2

fields: f1,f2method: m1 localsmethod: m2locals

fields: f3method: m3

Page 36: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 36

Scope

Understanding scope rules of a given language allows us to answer the following:

• Where is a given variable visible?

• What variables are visible at a given statement in the program?

Page 37: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 37

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– A variable is local to a procedure if the declaration

occurs in that procedure – A variable is nonlocal to a procedure if it is visible in

the procedure but not declared there

Page 38: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 38

Scope

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

• C++ and Ada allow access to these "hidden" variables– In Ada: unit.name– In C++: class_name::name

Page 39: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 39

Referencing Environments

• Def: The referencing environment of a statement is the collection of all names that are visible to the statement

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

Page 40: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 40

Example: Pascal-like languageProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1body of main

Main

sub1

sub2 sub3

Page 41: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 41

ExampleProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1body of main

Main has localvariables a,b,c,and sub1

Page 42: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 42

ExampleProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1body of main

sub1 has localvariables a,d, sub2and sub3, as well as non-local variablesb and c

Page 43: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 43

ExampleProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1body of main

sub2 has localvariables c,d andnon-local variablesa,b and sub1 (andpotentially sub3 depending on therules of the language)

Page 44: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 44

ExampleProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) body of sub3 body of sub1body of main

sub3 has localvariable a andnon-local variablesb,c,d,sub2, and sub1

Page 45: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 45

Static Scope

• Advantages– Readability– Based on program text can be evaluated by a

compiler– Constant time implementation

• Disadvantages:– Encourages global variables

Page 46: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 46

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 the chain of subprogram calls (runtime stack) that forced execution to this point

Page 47: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 47

Scope ExampleMAIN - declaration of x SUB1 - declaration of x - ... call SUB2 ...

SUB2 ... - reference to x - ...

... call SUB1 …

MAIN calls SUB1SUB1 calls SUB2SUB2 uses x

Which x??

Page 48: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 48

Scope ExampleMAIN - declaration of x SUB1 - declaration of x - ... call SUB2 ...

SUB2 ... - reference to x - ...

... call SUB1 …

MAIN calls SUB1SUB1 calls SUB2SUB2 uses x

For static scoping,it is main’s x

Page 49: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 49

Scope Example

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

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

Page 50: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 50

Scope ExampleMAIN - declaration of x SUB1 - declaration of x - ... call SUB2 ...

SUB2 ... - reference to x - ...

... call SUB1 …

MAIN calls SUB1SUB1 calls SUB2SUB2 uses x

For dynamic scoping,it is sub1’s x

MAIN (x)

SUB1 (x)

SUB2

Page 51: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 51

Dynamic Scoping

• Evaluation of Dynamic Scoping:– Advantage: convenience (easy to implement)– Disadvantage: poor readability, unbounded

search time

Page 52: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 52

Scope and Lifetime

• Scope and lifetime are closely related, but are different concepts

• Consider a static variable in a C or C++ function– Lifetime = entire program execution– Scope = limited to statements in the function

Page 53: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 53

Static Scope & Runtime

• Activation record – keep information associated with each procedure call instance: parameters, local variables, return address, return values …

• Procedure call time – new activation pushed onto runtime stack

• Procedure return time – activation popped off runtime stack

Page 54: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 54

Static Scope & Runtime

• At runtime, we need to be able to find the correct instance of a variable being used.

• Additional field in activation record –a pointer (static link) to the activation record for the closest instance of enclosing scope. – Pointers form a static chain back to the ‘main’.

– ‘Search’ back along these enclosing link pointers to find non-local variables

– Chain never gets longer than the scope depth.

Page 55: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 55

Static linksProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) call sub2 if E call sub1 else call sub3call sub1

Maina,b,c

Page 56: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 56

Static linksProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) call sub2 if E call sub1 else call sub3call sub1

Main sub1a,d

Maina,b,c

Page 57: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 57

Static linksProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) call sub2 if E call sub1 else call sub3call sub1

Main sub1 sub1a,d

Maina,b,c

sub1a,d

Page 58: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 58

Static linksProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) call sub2 if E call sub1 else call sub3call sub1

Main sub1 sub1 sub1a,d

Maina,b,c

sub1a,d

sub1a,d

Page 59: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 59

Static linksProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) call sub2 if E call sub1 else call sub3call sub1

Main sub1a,d

sub1a,d

sub1a,d

sub3a

Maina,b,c

Page 60: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 60

Static linksProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) call sub2 if E call sub1 else call sub3call sub1

Main sub1a,d

sub1 sub1 sub3a

sub2c,d

Maina,b,c

sub1a,d

sub1a,d

Page 61: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 61

Static Scope & Runtime

Static Chain.– Chain never gets longer than the maximum

scope depth.– For a given function, the compiler can

compute 1. the exact number of links to traverse to find the

required instance and

2. The variable offset (location) in the given activation record

Page 62: CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.

CS 363 Spring 2005 GMU 62

Static linksProgram main; a,b,c: real; procedure sub1(a: real); d: int; procedure sub2(c: int); d: real; body of sub2 procedure sub3(a:int) call sub2 if E call sub1 else call sub3call sub1

Main sub1a,d

sub1 sub1 sub3a

sub2c,d

Maina,b,c

sub1a,d

sub1a,d

In sub2, variable a isalways 1 link back andvariable b is always 2links back.