CS 330 Programming Languages 11 / 07 / 2006 Election Day Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Names and Bindings.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
ICE1341 Programming Languages Spring 2005 Lecture #9 Lecture #9 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
ISBN Lecture 06 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Lecture 06 Topics Introduction Primitive Data.
CS 330 Programming Languages 11 / 06 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 18 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types: Structured types.
CS 330 Programming Languages 11 / 08 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Definitions A data type defines a collection of data.
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
Elementary Data Types Scalar Data Types Numerical Data Types Other
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Data Types. Primitives Integer Float Character Boolean Pointers Aggregates Strings Records Enumerated Arrays Objects.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 355 – Programming Languages
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
CS 330 Programming Languages 10 / 23 / 2008 Instructor: Michael Eckmann.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.
College of Computer Science and Engineering
CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann.
ISBN 0-321— Chapter 6 Data Types. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
6-1 Chapter 6: Data Types Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
ISBN 0-321— Chapter 6 Structured Data Types Arrays Associated Arrays Records Unions.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
CS 330 Programming Languages 11 / 04 / 2008 Election Day Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
CS 330 Programming Languages 11 / 09 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 30 / 2007 Instructor: Michael Eckmann.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
C H A P T E R S I X Data Types.
CS 330 Programming Languages 10 / 31 / 2006 Boo! Instructor: Michael Eckmann.
1 CS Programming Languages Class 08 September 19, 2000.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data Types.
1 Data Types Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer Types.
Data Types W E E K F O U R. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character String.
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
CS 330 Programming Languages 11 / 01 / 2007 Instructor: Michael Eckmann.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Chapter 6 Data Types.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
Chapter 6 Data Types.
Chapter 6 Data Types.
Presentation transcript:

CS 330 Programming Languages 11 / 07 / 2006 Election Day Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2006 Today’s Topics Questions / comments? Chapter 6 –Character Strings –Ordinal Types Enumeration and Subrange types –Unions

Ch. 6 - Data Types Michael Eckmann - Skidmore College - CS Fall 2006 Languages maintain a descriptor at compile-time and/or run-time for each variable 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 –What is stored is only what is necessary to determine correct types, where to find the values in memory, etc. –e.g. Type Name, Address, Length/Size

Character strings Michael Eckmann - Skidmore College - CS Fall 2006 Implemented as –Character array vs. primitive type –Static vs. dynamic length C & C++ strings are terminated with a null character (ASCII 0). –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)

Character strings Michael Eckmann - Skidmore College - CS Fall 2006 Java has two different types for Strings. –String Static length constant strings –StringBuffer & StringBuilder Dynamic length and allows insert and delete substring methods Fortran 95 –String is a primitive type –Has typical operations for strings Issues –What to do when assigning strings of different lengths? etc.

Character strings Michael Eckmann - Skidmore College - CS Fall 2006 Pattern matching with character strings –Perl, JavaScript, PHP built in pattern matching with regular expressions –Included in class libraries of C++, Java and C#

Character strings Michael Eckmann - Skidmore College - CS Fall 2006 Length –Static length –Limited dynamic length –Dynamic length Requires dynamic storage allocation and deallocation Maximum flexibility

Character strings Michael Eckmann - Skidmore College - CS Fall 2006 Implementation –Most often software (as opposed to hardware) implementation of storage, retrieval and manipulation Descriptor for Static Strings contain: –Type name –Length –Address of first character This descriptor is only required during compile-time.

Character strings Descriptor for static strings Michael Eckmann - Skidmore College - CS Fall 2006

Character strings Michael Eckmann - Skidmore College - CS Fall 2006 Descriptor for limited dynamic –Type name (Limited dynamic string) –Maximum length –Current length –Address of first character Do you think this is a run-time descriptor?

Character strings Michael Eckmann - Skidmore College - CS Fall 2006 Descriptor for limited dynamic –Type name (Limited dynamic string) –Maximum length –Current length –Address of first character Do you think this is a run-time descriptor? –Yes it is. One reason is because if the program might need to know the maximum length so the current length can't increase past this max. –However C & C++ use limited dynamic strings but since they don't maintain their length and don't do any range checking this run-time descriptor is not needed.

Character strings Michael Eckmann - Skidmore College - CS Fall 2006 For dynamic length strings (changeable with no maximum length) –Could be stored in linked list –Could be stored in adjacent storage cells What about when length changes? How can this be done?

Character strings Michael Eckmann - Skidmore College - CS Fall 2006 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 (why?)

Ordinal / Enumeration Michael Eckmann - Skidmore College - CS Fall 2006 Ordinal types –Range of values associated with positive integers (examples?) Many languages provide support for user defined ordinal types like Enumerations. 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 –Range of values intentionally limited.

Enumeration Michael Eckmann - Skidmore College - CS Fall 2006 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}; // declare a variable of type months months m; Represented as 0 to 11 typically but could be given programmer specified values in the declaration. They 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. e.g. In C++, we can add 1 to an enum

Enumeration Michael Eckmann - Skidmore College - CS Fall 2006 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?

Enumeration Michael Eckmann - Skidmore College - CS Fall 2006 As of Java 1.5 (aka 5.0), Java does support enumerations. When used, they create full classes for the enum. See: What are advantages to enumeration types?

Enumeration Michael Eckmann - Skidmore College - CS Fall 2006 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/subtracting 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

Enumeration Michael Eckmann - Skidmore College - CS Fall 2006 –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?

Subrange types Michael Eckmann - Skidmore College - CS Fall 2006 Subrange types are another kind of user-defnd ordinal type 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.

Subrange types Michael Eckmann - Skidmore College - CS Fall 2006 Subrange types –When do you think they're type checked? –When do you think they're range checked?

Subrange types Michael Eckmann - Skidmore College - CS Fall 2006 Only current language that supports is Ada 95. Subrange types are implemented as their parent types but with additional range checks (the compiler must generate this range checking code for every assignment to a subrange type.) Advantages / Disadvantages?

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

Michael Eckmann - Skidmore College - CS Fall 2006 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?

Michael Eckmann - Skidmore College - CS Fall 2006 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.

Michael Eckmann - Skidmore College - CS Fall 2006 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.

Michael Eckmann - Skidmore College - CS Fall 2006 Unions When num.x = 100; and we print the contents of num.x and num.y, num.x will print fine, but a reference to num.y will be a logic error and will not contain 100. num.y will interpret the bits of how 100 was stored as an int as a float. Same problem if we assign num.y a float value and try to access num.x These problems exist because C++ has Free Unions. There is no type checking done to prevent reference to the wrong name within the union at any given time. Discriminated Unions would have to carry what kind of information to disallow the problem described above in C++?

Michael Eckmann - Skidmore College - CS Fall 2006 Unions Discriminated union example of memory storage

Michael Eckmann - Skidmore College - CS Fall 2006 Unions Evaluation of Unions.

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 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?

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 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?

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 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.

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 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?

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

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 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?

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 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.

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Back to the five categories of arrays –Besides static and fixed stack-dynamic –Stack-dynamic Subscript ranges dynamically bound Storage allocation is dynamic (on the stack)

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Compare Stack-dynamic vs. static and fixed stack-dynamic –Stack-dynamic Subscript ranges dynamically bound Storage allocation is dynamic (on the stack) Both are fixed after they're bound What advantage does this have over static and fixed stack- dynamic?

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Static and fixed stack-dynamic VS. Stack-dynamic –Size of the array doesn't need to be known until it is going to be used.

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Back to the five categories of arrays –Besides static and fixed stack-dynamic and stack-dynamic –Fixed heap-dynamic Subscript ranges dynamically bound Storage allocation is dynamic (on the heap) Both are fixed after they're bound which is different from: –Heap-dynamic Subscript ranges dynamically bound Storage allocation is dynamic (on the heap) Both are changeable during execution.

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 In functions (therefore block scope) –C & C++ use static arrays if programmer uses the static modifier –C & C++ use fixed stack-dynamic arrays by default (without the static modifier) Textbook says C & C++ also use fixed heap-dynamic arrays –But it also says they can be dynamically allocated and the sizes can change over execution time because there is no index range checking. That sounds to me like heap-dynamic. Anyone agree or disagree? Java - fixed heap-dynamic arrays –Their subscripts and allocation are dynamically bound but fixed afterwards. What kind of arrays do you think Perl has?

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Perl and Javascript (and C# has capability) –Heap-dynamic arrays –Perl has push and pop functions that treat arrays like a stack therefore changing the length --- I had not mentioned these before. –I did mention though that you could do things like: = (1, 2, 3, 4); –$arr[4] = 5; –Which also obviously lengthens the array dynamically

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Array initialization –Sometimes the initialization implicitly states the length –In C, C++, C# and Java this happens –Ada has an interesting feature that allows initialization of some elements of the array to certain values and all others to another value.

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Array operations (operate on array as a whole) –Some languages allow concatenation of arrays and can use relational operaters on arrays –Others like APL have arithmetic operations on arrays like vector multiplication and matrix transpose, etc.

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Rectangular vs. jagged arrays –Only makes sense when talking about multidimensional arrays –Jagged if rows can have different numbers of columns in the same array –I made can bold above, because Java, C and C++ support jagged arrays but do not support rectangular arrays. –This is because multidimensional arrays are implemented as arrays of arrays. –Fortran, Ada and C# provide rectangular arrays –When are different numbers of columns for a row useful?

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Slices –Not a new data type –Instead, it is a way to reference part of an array as a seperate unit. –e.g. One row, or some subset of consecutive elements in a column, or some rectangular subset of rows etc. –Anyone know any languages that support slices?

Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Implementation of array types –How to access array elements Code needs to produce element addresses –Descriptor for arrays Needs to have information in it to construct the access function That is, when an element of an array is referenced, the index along with the information in the descriptor needs to be able to find the address in memory where that element lives. So if run-time range checking is done in some language, what do you think the descriptor will contain?

Multidimensional Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Design issue: –How to store the multidimensional array in memory? –A 1-D array is typically stored sequentially in memory

Multidimensional Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Design issue: –How to store the multidimensional array in memory? –Row major order –vs. –Column major order –Fortran uses column, all others use row Would the programmer care whether the multidimensional array is stored in row or column major order?

Multidimensional Arrays Michael Eckmann - Skidmore College - CS Fall 2006 How to access 2-dimensional array elements at run-time (assuming row major order and all rows have same number of columns and index starts at 0). –We have the subscripts to look for –We need address of first element –What else do we need?

Multidimensional Arrays Michael Eckmann - Skidmore College - CS Fall 2006 How to access 2-dimensional array elements at run-time (assuming row major order and all rows have same number of columns and index starts at 0). –We have the subscripts to look for –We need address of first element –We need the size of one element (determined from the type of the array.) –We need to know the number of columns per row –Luckily all this info is stored in the desriptor for the array

Multidimensional Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Address of element at row i, column j is = address_of_array + (num_cols * i + j) * size_of_element;

Associative Arrays Michael Eckmann - Skidmore College - CS Fall 2006 Unordered collection of data indexed by keys. Each element of an associative array is a pair: –key, value The reason Perl's associative arrays are called hashes is because there are implemented using a hash table and hash function. Because the data is unordered, Perl can store it in memory in any order it cares to.

Hashing Michael Eckmann - Skidmore College - CS Fall 2006 A hash table is a 1d array of linked lists. Each key is supplied to a hash function which returns a number. The index to the array is determined by the number returned from the hash function mod array_length. Then go sequentially through the linked list in that element of the array to find the value. The length of the array is chosen wisely (usually a prime number so the mod works well) and the hash function is chosen wisely (to distribute the keys well throughout the hash table.)