Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.

Similar presentations


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

1 CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Today’s Topics Questions / comments? Chapter 5 –names –bindings –type checking –scope

3 Names Michael Eckmann - Skidmore College - CS 330 - Fall 2007 What's a name? A name (aka identifier) is associated with variables, labels, subprograms, parameters, etc... What possible design criteria are there for names in a language? Any ideas?

4 Names Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Case sensitive? (Java, C++) Does it allow spaces or other whitespace? If so, are they ignored? What's the allowable length? What characters does it allow? What can it start with? Are words that are part of the language (e.g. while, class etc.) allowed as names? –those allowed as names are called keywords –those not allowed as names are called reserved words predefined names are related to these ideas –e.g. Ada has Integer and Float types are allowed to be redefined by an Ada program –Java packages, C++ libraries when imported the predefined names within those are now visible to a program and not able to be redefined.

5 Names Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Why might there be a restriction on length of names? Does it matter to the compiler? –some allow any length but only the first x characters are significant. any problems or advantages to this? What about readability, writability, reliability, cost? –Which of these ideas about names enhance or are a detriment to these language evaluation criteria?

6 Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2007 What's a variable? Is it the name of a memory location? Yes. But it is more. what else?

7 Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2007 What's a variable? Is it the name of a memory location? Yes. But it is more. Name, address, value, type, size And two more things: –lifetime (how long the name is attached to the memory location), –scope (where in the code can it be referenced)

8 Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Memory address –variables are associated with a memory address –may be possible for same name to be associated with different addresses at different times during execution at different places in the code –can anyone give an example of these? –the address of a variable is sometimes called its l-value Aliases –anyone want to define alias in terms of names and addresses?

9 Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Aliases –more than one name associated to the same memory address. –example: 2 variables x and y both refer to same address, so if I change the value of x, y's value changes too because it refers to the same address –effects on readability or writability? –An example of aliases in Java are that they are created when passing certain kinds of parameters (e.g. a reference to a class, or an array reference) Can aliases occur anywhere else in Java? Type –What does a type do? Size Value –sometimes referred to as a variable's r-value. Why do you think?

10 Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Type –determines the kind and range of possible values –and the operations that can be performed on variables of the type Size –how much memory --- determined by the type Value –contents of the variable at any given time during run-time –sometimes referred to as a variable's r-value.

11 Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Lifetime (how long the name is attached to the memory location), Scope (where in the code can it be referenced) example: public int factorial(int num) { int result = 1; while (num > 1) { result *= num; num--; } return result; }

12 Variables Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Lifetime (how long the name is attached to the memory location), Scope (where in the code can it be referenced) example: public void meth1() { //.... } public void meth2() { String s = “Hey”; //.... meth1(); }

13 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Binding is an association between an attribute and an entity an operation and a symbol Times when binding can take 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)

14 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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?

15 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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

16 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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

17 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Binding attributes to variables –Static vs. Dynamic binding Static binding – first occurs before run time and stays same throughout program execution note: load-time is occurs before run-time Dynamic binding Either it first occurs during run time OR it can change during execution it is dynamic

18 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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 type, e.g. @ is for arrays, $ for scalars, % for keyed arrays (aka hashes). But scalars can hold integers, floats, or strings.)

19 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Type bindings Variable declaration When a variable is declared it is given a type (and a size) and a memory address 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.

20 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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?

21 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Dynamic Type Binding Advantage: flexibility (generic program units --- what do you think this means?) Disadvantages: –Decreased reliability - Why? –High cost at run-time due to what?

22 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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.

23 Type Inference Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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 circumf(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; )

24 An aside about memory Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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.

25 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

26 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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?

27 Binding Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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.

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

29 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

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

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

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

33 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

34 Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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. 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).

35 Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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?

36 Type Checking / Strong Typing Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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 do you think I mean by this?

37 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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. 227) –Answers the question – When we reference a name of a variable which variable is it?

38 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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.

39 Scope Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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”; }

40 Scope vs. lifetime Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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?

41 Scope vs. lifetime Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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?

42 Constants and initialization Michael Eckmann - Skidmore College - CS 330 - Fall 2007 What good are named constants? –Enhances readability. Anything else? Initialization –Binds a value to a variable when that variable is bound to storage (i.e. when memory is allocated.) –Occurs once for static variables –Occurs each time code is executed for dynamic variables (either stack-dynamic or heap-dynamic.)


Download ppt "CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann."

Similar presentations


Ads by Google