Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

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

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Today’s Topics Questions / comments? Bindings Type Checking Scope / Lifetime Data Types

3 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)‏

4 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?

5 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

6 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

7 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

8 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.)‏

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

10 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?

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

12 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; )‏

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

14 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

15 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?

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

17 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?

18 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

19 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?

20 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)‏

21 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?

22 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

23 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?

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

25 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?

26 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?

27 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?

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

29 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”; }

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

31 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?

32 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?

33 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

34 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

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

36 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

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

38 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?

39 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

40 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 1 st 128 are ASCII

41 ASCII Michael Eckmann - Skidmore College - CS 330 - Fall 2008

42 Unicode Michael Eckmann - Skidmore College - CS 330 - Fall 2008 http://www.unicode.org/standard/WhatIsUnicode.html Has characters in alphabets of many languages


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

Similar presentations


Ads by Google