+ All Categories
Home > Documents > CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Date post: 27-Dec-2015
Category:
Upload: donna-ellis
View: 218 times
Download: 3 times
Share this document with a friend
Popular Tags:
42
CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann
Transcript
Page 1: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

CS 330Programming Languages

10 / 21 / 2008

Instructor: Michael Eckmann

Page 2: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Today’s Topics• Questions / comments?• Bindings • Type Checking• Scope / Lifetime• Data Types

Page 3: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Binding is an association between • an attribute and an entity• an operation and a symbol

• Times when binding takes place• Language (compiler) design time• Language implementation time• Compile time• Link time (when your compiled code is linked to other

libraries)• Load time (when the program is executed it gets loaded into

memory)• Run time (when the program starts executing after being

loaded into memory)

Page 4: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Example from the text illustrates some of these ideas:

int count;

// ...

count = count + 5;

– The text discusses these assuming the code is in C. Even if we don't know C we should be able narrow down when the bindings below occur.

• type of count is bound when?

• set of possible values of an int is bound when?

• meaning of + operator is bound when?

• internal representation of literal 5 is bound when?

• the value of count is bound when?

Page 5: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• The answers for the C language are:• type of count is bound when? compile time• set of possible values of int is bound when? compiler-

implementation time – what does that imply?

• meaning of + operator is bound when? compile-time – what does that imply?

• internal representation of literal 5 is bound when? compiler-design time

• the value of count is bound when? run-time

Page 6: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Examples of bindings that occur at – link time

• a method/function call bound to the specific code in the library in which it is defined

– load time• global variable may be bound to specific memory

storage at load time

Page 7: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Binding attributes to variables• Static binding – first occurs before run time and stays same

throughout program execution

• note: load-time is considered before run-time• Dynamic binding

• Either it first occurs during run time

• OR it can change during execution

Page 8: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Type bindings• Variable declaration

• Explicit (what we're used to -- declare a variable by giving it a name

and a type.)

• Implicit – associate a type based on naming convention and

declaration happens on first appearance

– I, J, ..., N are implicitly Integer types in Fortran

– (sort of occurs in Perl, where the first char of a variable name specifies its “kind”, e.g. @ is for arrays, $ for scalars, % for keyed arrays (aka hashes). But scalars can hold integers, floats, or strings.)

Page 9: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Type bindings• Variable declaration

• Declaration vs. definition

– In C & C++ there is a difference between declaration and definition

– declarations specify types but do not allocate memory

– definitions specify type and allocate memory

– because C & C++ have a keyword extern which declares a var but does not allocate storage --- it is assumed to be done elsewhere. So, in that way one can declare a var without defining it.

– The var needs to be defined somewhere though.

Page 10: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Dynamic Type Binding (used in JavaScript and PHP among others) - allows any variable to be assigned a value of any type.

• Type is specified through an assignment statement

• e.g., JavaScript list = [2, 4.33, 6, 8];list = 17.3;

• Advantages and Disadvantages to Dynamic Type Binding?

Page 11: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Dynamic Type Binding• Advantage: flexibility (generic program units --- what do you think this

means?)

• Disadvantages:

– Decreased reliability - Why?

• Type error detection by the compiler is difficult or impossible, why?

• compiler can't detect errors involving incorrect assignment of types. Are these kinds of errors detected in Java?

– High cost at run-time due to:

• dynamic type checking

• Pure interpretation is required for these languages b/c if types are not known at compile time, machine language instructions cannot be generated.

• Textbook claims that pure interpreted languages take at least 10 times as long as equivalent machine code.

Page 12: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Type Inference

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• ML is a language with both functional and imperative features and can infer types without the programmer having to specify them.

• Two types are real (floating point) and int. Examples (straight out of the text):

fun curcumf(r) = 3.14159 * r * r;

fun times10(x) = 10 * x;

fun square(x) = x * x;type of r is inferred to be real and circumf's return type is also inferred to

be real due to the 3.14159 being a real.

type of x is inferred to be int and times10's return type is also inferred to be int due to the 10 being and int.

default numeric type is int, so square's return type and parameter x are ints.

fun squarer(x : real) = x * x; (or fun squarer(x) :real = x * x; )

Page 13: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

An aside about memory

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• How is memory divided up and categorized for executing programs? A typical

setup is as follows. I'll draw a diagram on the board.

– executable code

– static data area

• used for statically declared objects like globals and constants

– stack (grows one way)

• used for local variable allocation during “procedure / method / subroutine / function” calls. Note: I will constantly use these four words interchangeably.

– heap (grows the other way)

• used for dynamic objects

– objects that are allocated at runtime, may change and size not known until run time

• e.g. linked lists with unknown number of nodes, trees with unknown number of nodes, etc.

Page 14: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding• Storage bindings and lifetime

• Static variables (lifetime is the total time of execution)

• e.g. globals, static variables

• allocated in the static data area

• Stack-dynamic variables (what's the lifetime of these?)

• e.g. Local variables to methods

• allocated on the stack. When are they allocated and deallocated?

• Explicit heap-dynamic variables

• e.g. Variables used for data structures that shrink and grow during execution, or those only referenced through pointers or references. Can anyone give examples?

• allocated / deallocated on the heap

• Implicit heap-dynamic variables

• All attributes (type, size, etc.) of these are bound when value is assigned. e.g. The JavaScript example of list a few slides ago.

• allocated / deallocated on the heap

Page 15: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Evaluation of these kinds of variables. Think in terms of memory space, cost

of execution, reliability, efficiency etc.

• Static variables

• What are some advantages and disadvantages?

Page 16: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Evaluation of these kinds of variables. Think in terms of memory space, cost

of execution, reliability, efficiency etc.

• Static variables

• What are some advantages and disadvantages?

+ ability to have history sensitive variables inside a method/function

+ efficient - addressing is direct

+ allocation is done before run-time so there is no run-time overhead of allocation and deallocation

- reduced flexibility

with only static variables you couldn't write recursive routines -why?

- could waste memory

can't share storage with other variables that do not have to overlap existence.

Page 17: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2005

• Evaluation of these kinds of variables. Think in terms of memory space, cost

of execution, reliability, efficiency etc.

• Stack-dynamic variables

• What are some advantages and disadvantages?

Page 18: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2005

• Evaluation of these kinds of variables. Think in terms of memory space, cost

of execution, reliability, efficiency etc.

• Stack-dynamic variables

• What are some advantages and disadvantages?

+ allows recursion

+ share memory space with other stack-dynamic variables

- slower because bindings at runtime

Page 19: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2005

• Evaluation of these kinds of variables. Think in terms of memory space, cost

of execution, reliability, efficiency etc.

• Explicit heap-dynamic variables (e.g. Java references to objects)

• What are some advantages and disadvantages?

Page 20: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2005

• Evaluation of these kinds of variables. Think in terms of memory space, cost

of execution, reliability, efficiency etc.

• Explicit heap-dynamic variables (e.g. Java references to objects)

• What are some advantages and disadvantages?

+ flexibility / expressivity

- pointers/references could be difficult to use correctly

- slower at runtime b/c indirect addressing (what is indirect addressing?)

- complex implementation of how these variables are stored and accessed in memory

- complicated and costly heap management (e.g. Java's garbage collection)

Page 21: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2005

• Evaluation of these kinds of variables. Think in terms of memory space, cost

of execution, reliability, efficiency etc.

• Implicit heap-dynamic variables

• What are some advantages and disadvantages?

Page 22: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Binding

Michael Eckmann - Skidmore College - CS 330 - Fall 2005

• Evaluation of these kinds of variables. Think in terms of memory space, cost

of execution, reliability, efficiency etc.

• Implicit heap-dynamic variables

• What are some advantages and disadvantages?

+ extremely flexible, generic code (same code works for many types) easy to write

- slower

- reduced error detection, therefore reduced safety

Page 23: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Type checking --- checks that the operands of an operation are of compatible types

– what does compatible type mean?• If all bindings of variables to types are static then type

checking can be done when?• If any bindings of variables to types are dynamic then type

checking must be done when?

Page 24: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Type checking --- checks that the operands of an operation are of compatible types

– what does compatible type mean?• If all bindings of variables to types are static then type

checking can be done before run-time.• If any bindings of variables to types are dynamic then type

checking is required at run-time. This is Dynamic Type Checking.

Page 25: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Strong Typing – defined by the text as --- a language is strongly typed if type errors are always detected (whether at compile-time or run-time).

• According to this definition, what would you say about Java --- is it strongly typed?

Page 26: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Type Checking / Strong Typing

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Strong Typing – defined by the text as --- a language is strongly typed if type errors are always detected (whether at compile-time or run-time).

• According to this definition, what would you say about Java --- is it strongly typed?– Yes nearly. Only casting (which is explicitly done by

the programmer) could cause an error to go undetected.

– Also, type coercion reduces the usefulness of strong typing to some extent. What is type coercion and why does it reduce the usefulness of strong typing?

Page 27: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Scope is the range of statements in which a variable is visible (which means where it can be referenced.)

• Static scope• Static – scope of variables can be determined prior to

run-time. It is a spatial relationship.– Nesting of

• functions/methods (p. 226) • Blocks (p. 228)

– Answers the question – When we reference a name of a variable which variable is it?

Page 28: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Dynamic scope• Scope of variables are determined at run-time. It is a temporal relationship, based on calling sequence.– Advantage: convenience– Disadvantage: poor readability– Also answers the question – When we reference a

name of a variable which variable is it? -- but we may get a different answer vs. if the language used static scoping.

Page 29: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Dynamic scope example from Perl:$var = 10;

sub1(); # will print Yikes

sub2(); # will print 10

sub sub1

{

local $var = “Yikes”;

sub2();

}

sub sub2

{

print $var . “\n”;

}

Page 30: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Scope

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• I'll let you read up on the evaluation of Static and Dynamic scope in the text.

Page 31: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Scope vs. lifetime

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Scope is typically spatial, lifetime is always temporal.• They are related in many cases, but two examples where they

are clearly different concepts:

• static variables can be declared in a function in C++ & Java. These variables are used to retain values between subsequent calls. e.g. If you wanted to know how many times you called the constructor for some class (i.e. how many instances of that class were created) during the program you could use a static variable that has one added to it each time in that constructor.

• What's the scope of a variable like this? What's its lifetime?

Page 32: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Scope vs. lifetime

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Many languages do not allow you to reference variables of a function that calls a particular function.

• e.g.Function1()

{ // do some stuff here

}

Function2()

{

int x;

Function1();

}

• What's the scope of x? What's its lifetime?

Page 33: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Ch. 6 - Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• A Data type

– defines a set of allowable values – defines the operations on those values

Page 34: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Ch. 6 - Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• How well do the types match real-world problems

• User defined types --- example anyone?

• Abstract data types --- example anyone?

• Structured data types

– Arrays, records

• Data types and operations support in hardware vs. software

• Descriptor

– collection of attributes about a var stored in memory.

– Static (during compilation process) vs. dynamic (during run time)

– Used for type checking and allocation & deallocation

Page 35: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Not defined in terms of other types

• Used with type constructors to provide structured types

• Integer types– Signed vs. unsigned– Sign-magnitude vs. Two's complement vs. one's

complement representation of integers.

• Floating point are approximations to decimal numbers. Why only approximations? Let's convert 0.1 to binary.– Fully continuous? – Precision and range.

Page 36: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Floating point are approximations to decimal numbers.

– Fully continuous? – Precision and range.

• single precision IEEE format:– 1 sign bit, 8 bits for exponent, 23 bits for

fraction• double precision IEEE format:

– 1 sign bit, 11 bits for exponent, 52 bits for fraction

Page 37: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• How to “fix” problems with floating point?

• Intervals– Interval arithmetic could guarantee that result of all

operations are within the interval– Have a min and max bound on results.

Page 38: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Decimal types

– Most languages we use don't have a decimal type.– Cobol does, so does C#.– BCD– 1 or 2 digits per byte– Why have decimal types if they waste storage?

Page 39: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Decimal types

– Most languages we use don't have a decimal type.– Cobol does, so does C#.– BCD– 1 or 2 digits per byte– Why have decimal types if they waste storage?

• Because they precisely store decimal values which floating point types do not

Page 40: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Primitive Data Types

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• Boolean

– Could be represented by a single bit, but are often represented by a byte for more efficient memory access.

• Character– ASCII (8 bit)– ISO 8859-1 (8 bit)– Unicode (16 bit) --- Java & c# use this

• 1st 128 are ASCII

Page 41: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

ASCII

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

Page 42: CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.

Unicode

Michael Eckmann - Skidmore College - CS 330 - Fall 2008

• http://www.unicode.org/standard/WhatIsUnicode.html

• Has characters in alphabets of many languages


Recommended