Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann.

Similar presentations


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

1 CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Today’s Topics Questions / comments? Midterm grading explanation Chapter 5 –scope / lifetime Chapter 6 –Data types

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

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

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

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

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

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

9 Ch. 6 - Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 A Data type –defines a set of allowable values –defines the operations on those values

10 Ch. 6 - Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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

11 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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.

12 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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

13 Primitive Data Types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 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.

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

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

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

17 ASCII Michael Eckmann - Skidmore College - CS 330 - Fall 2007

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

19 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Implemented as –Character array vs. primitive type –Static vs. dynamic length C & C-style C++ strings are terminated with a null character. –Length is not maintained but can be determined. The end of the string is determined by the null character. –All that needs to be stored is a pointer to the first character in the string. –Limited dynamic length (limited because there is a maximum length C++ also has static length strings (string in the standard class lib.)

20 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Java has two different types for Strings. –String Static length constant strings –StringBuffer Dynamic length and allows direct subscripting Fortran 95 –String is a primitive type –Has typical operations for strings Issues –What to do when assigning strings of different lengths? etc.

21 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Pattern matching with character strings –Perl, JavaScript, Ruby and PHP have built in pattern matching with regular expressions –Included in class libraries of C++, Java, Python and C# –The Perl regular expressions we learned, even if you don't use Perl, are not for naught as there are similar capabilities in other languages that you might use.

22 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Length –Static length Static and set when the string is created –Limited dynamic length Changeable but up to some maximum –Dynamic length Requires dynamic storage allocation and deallocation Maximum flexibility As we just discussed, some languages provide some or all of these types. Why have multiple?

23 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Implementation –Most often software (as opposed to hardware) implementation of storage, retrieval and manipulation Descriptor –Type name –Length (for static strings) –Address of first character

24 Character strings Descriptor for static strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007

25 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Descriptor for limited dynamic –Type name (Limited dynamic string) –Maximum length –Current length –Address of first character For dynamic length strings –Could be stored in linked list –Could be stored in adjacent storage cells What about when length changes? How can this be done?

26 Character strings Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Linked list vs. adjacent evaluation –Linked lists string operations become complex (following pointers around) allocation and deallocation is simple and fast but requires more storage (for the pointers or references) –Adjacent faster string operations (because contiguous) less storage but allocation and deallocation is slower

27 Ordinal / Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Ordinal types –Range of values are associated with positive integers Enumeration types –Named constants represent positive integers –Design issues Are enumeration type values coerced to integer? –Same operations and range of values of integers Are any types coerced to enumeration type values? –Could break the allowable values of the enum type (how so?) –Range of values intentionally limited.

28 Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2007 e.g. In C# enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; e.g. In C++ enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; Represented as 0 to 11 typically but could be given programmer specified values in the declaration. Look the same, but C# enum types not coerced to integer whereas C++'s are --- so, C# doesn't allow operations that don't make sense, while C++ does. In C++, we can add 1 to an enum

29 Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2007 e.g. In C++ enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; // declares currentMonth of type months and assigns // an initial value months currentMonth = Jun; In C++, we can add 1 to an enum –e.g. currentMonth++; Any danger here?

30 Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2007 As of Java 1.5 (aka 5.0), Java does support enumerations. When used, they create full classes for the enum. See: http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html What are advantages to enumeration types?

31 Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2007 What are advantages to enumeration types? –All forms of implementation of them offer better readability –Reliability No arithmetic operations on the enum types in Ada and C# --- this prevents adding which might not make sense like in adding months together Ada and C# also restrict values within the range for the type. C treats them like integers so we don't get those advantages

32 Enumeration Michael Eckmann - Skidmore College - CS 330 - Fall 2007 –Reliability C++ allows numeric values to be assigned to enum types if they are explicitly cast. C++ also allows values to be assigned to enum types but the range is checked for validity. –This just checks that the integer is between the lowest and highest enum value. Enum values need not be consecutive integers. Any problem here?

33 Subrange types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Subrange types e.g. In Ada: type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); subtype Weekdays is Days range Mon..Fri; Day1 : Days; Day2 : Weekdays; Day2 := Day1; Only allowed if Day 1 isn't Sat or Sun.

34 Subrange types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Compiler generates range-checking code for every assignment to a subrange var. –When do you think they're type checked? –When do you think they're range checked?

35 Subrange types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Compiler generates range-checking code for every assignment to a subrange var. –When do you think they're type checked? Types are checked for compatibility at compile-time. –When do you think they're range checked? runtime

36 Subrange types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Only current language that supports is Ada 95. Subrange types are implemented as their parent types but with additional range checks. Advantages / Disadvantages?

37 Subrange types Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Advantages / Disadvantages? –Increased code size –Increased execution time due to range checking –Readability increases and reliability increases

38 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Unions A Union is a type that can store one of different type values during the execution of a program. Why? Design issues –Type checking Free –No type checking Discriminated –Has type checking –Are unions allowed in records?

39 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Unions A Union is a type that can store one of different type values during the execution of a program. Why? Design issues –Type checking Free (Fortran, C and C++ have this type) –No type checking Discriminated (Ada has this type) –Has type checking Java and C# (the newer languages) do not have unions.

40 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Unions An example in C++. union Number { int x; float y; } // in some function somewhere we can: Number num; num.x = 100; // then we print the contents of num.x and num.y num.y = 25.4; // then we print the contents of num.x and num.y note: this example is a modified version from Deitel & Deitel's C++ How to Program.

41 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Unions Discriminated union example of memory storage

42 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Unions Evaluation of Unions.

43 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2007 We all know what arrays are. Design issues –Legal types for subscripts –Are references to subscript expressions range checked? –When are subscript ranges bound? –When is allocation bound? –Are ragged and multidimensional arrays allowed? –Is initialization allowed at allocation time? –Slices?

44 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Most languages use the name of the array followed by an index in square brackets to specify an element of an array –e.g. array_of_names[5] Ada uses parentheses surrounding the index. Ada also uses parentheses for subprogram (function) calls to enclose the parameters. Any idea why they might have done that? Is it a good idea?

45 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Ada chose to use parentheses in these two ways because both array references and function calls are similar in that they map the index (or parameters) to a value which is returned. It certainly reduces readability to some degree because the reader of the program can't tell the difference between an array reference and a function call with one parameter.

46 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2007 There are two types associated with any array –The type of the elements of the array –The type of the subscript Most commonly integers (or subranges of integers) Ada allows boolean, characters and enumeration types as subscripts (how might they do that?) Range checking the subscripts (indices) When are we talking here? –C, C++, Perl, and Fortran --- no range checking –Java, ML, C# do range check –Ada range checks by default, but can be programmer disabled –Why not range check?

47 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Lower bound of subscripts –Usually 0 (C-based languages) Fortran95 defaults to 1. –Why use 0?

48 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Five categories of arrays –Static – subscript ranges and storage allocation is static. Statically bound variables are bound BEFORE run-time. –Fixed stack-dynamic – subscript ranges statically bound, but allocation done at declaration elaboration time. When is declaration elaboration time? –Let's compare these two. What are the advantages of each?

49 Arrays Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Static vs. Fixed stack-dynamic –Let's compare these two. What are the advantages of each? –Static --- speed efficient --- no dynamic allocation or deallocation (at runtime) so they're faster. –Fixed stack-dynamic --- space efficient --- if have block scoped arrays, they're only allocated when they need to be in memory instead of allocating all of them before run-time.


Download ppt "CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann."

Similar presentations


Ads by Google