Lecture 6 Concepts of Programming Languages

Slides:



Advertisements
Similar presentations
CS 355 – Programming Languages
Advertisements

Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ISBN Lecture 06 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Lecture 06 Topics Introduction Primitive Data.
ISBN Chapter 6 Data Types: Structured types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Definitions A data type defines a collection of data.
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
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 355 – Programming Languages
ISBN 0-321— Chapter 6 Data Types. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Chapter 6 Data Types. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character String Types.
College of Computer Science and Engineering
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Associative Arrays Record Types Tuple Types List Types Union Types.
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.
6-1 Chapter 6: Data Types Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types.
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.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2015 Pearson. 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.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Chapter 6 Topics Introduction Primitive Data.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
C H A P T E R S I X Data Types.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data 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.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-1 Subscript Bindings and Array Categories In some languages the lower bound of the subscript range.
ISBN 0-321— Chapter 6 Data Types. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
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 0-321— Chapter 6 Data Types. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 6 Topics Introduction Primitive Data Types Character.
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
Data Types Chapter 6: Data Types Lectures # 12. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Data Types Chapter 6: Data Types Lectures # 11. Topics Introduction Primitive Data Types Character String Types Array Types Associative Arrays Record.
Data Types In Text: Chapter 6.
Dr. Vamsi Paruchuri University of Central Arkansas
Chapter 6 Data Types.
Data Types I Subject : T0152 – Programming Language Concept
Chapter 6 Data Types.
Chapter 6 – Data Types CSCE 343.
Chapter 6: Data Types Saad Alshahrani
CMP 339/692 Programming Languages Day 14 Tuesday, March 20, 2012
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.
6.1 Introduction 6.2 Primitive Data Types - Evolution of Data Types:
Chapter 6: Data Types Lectures # 10.
Structure of Programming Language
Records Design Issues: 1. What is the form of references?
Chapter 6 Data Types.
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.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Chapter 6 Data Types.
Presentation transcript:

Lecture 6 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea

Concepts of Programming Languages Topics Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer and Reference Types Concepts of Programming Languages

Concepts of Programming Languages Introduction A data type defines a collection of data objects and a set of predefined operations on those objects Design issue for data types: What operations are defined and how are they specified? Concepts of Programming Languages

Concepts of Programming Languages Primitive Data Types Primitive data types: Data types that are not defined in terms of other data types Some primitive data types are some times merely reflections of the hardware Others require only a little non-hardware support for their implementation (Code for supporting floating-point numbers on processor that does not support these.) Almost all programming languages provide a set of primitive data types Concepts of Programming Languages

Primitive Data Types: Integer For representing numbers without fractions (without dot) Often there are several different integer types with different sizes: E.g.: Java’s signed integer sizes: byte, short, int, long Concepts of Programming Languages

Primitive Data Types: Floating Point Model real numbers (but only as approximations) Many languages support at least two floating-point types (e.g., float and double) of different sizes There is a standard for floating-point number: IEEE Floating-Point Standard 754 Concepts of Programming Languages

Representation of floating-point values 32 bit single precision float 64 bit double precision float Concepts of Programming Languages

Floating point example IEEE representation scheme scheme: Example: exponent fraction bit 1 bit 23 exponent is 1 Concepts of Programming Languages

Problems with floating point values The IEEE representation scheme can result in unexpected approximations in the context of decimal numbers: E.g. the decimal value 0.1 Experiment: sum up 1Mio times 0.1 and check the result Concepts of Programming Languages

Primitive Data Types: Complex Some languages support a complex type, (e.g., Fortran and Python) Representation: Each value consists of two floats, the real part and the imaginary part Example: Literal form in Python: (7 + 3j), where 7 is the real part and 3 is the imaginary part Concepts of Programming Languages

Primitive Data Types: Decimal Stores a number by using decimal digits (4 bits per digit) Advantage: accuracy Disadvantages: limited range, wastes memory Supported by e.g.: COBOL C# Important in the context business applications (money) Concepts of Programming Languages

Primitive Data Types: Boolean Simplest of all Range of values: “true” and “false” Could be implemented as bits, but often as bytes Concepts of Programming Languages

Primitive Data Types: Character Stored as numeric codings Most commonly used coding: ASCII An alternative, 16-bit coding: Unicode Includes characters from most natural languages Originally used in Java C# and JavaScript also support Unicode Concepts of Programming Languages

Character String Types Values are sequences of characters Design issues: Supported as primitive data type or build on top of other data-types (e.g. arrays) Support of “immutable” strings Pattern-matching as part of the language definition Concepts of Programming Languages

Character String Types Operations Typical operations: Assignment and copying Comparison (=, >, etc.) Catenation Substring reference Pattern matching (support of regular expressions) Concepts of Programming Languages

Character String Type in Certain Languages C and C++ Not primitive Use char arrays and a library of functions that provide operations C++ Not primitive two approaches (C-strings and std::string-class) Fortran and Python Primitive type with assignment and several operations Java Semi-primitive via the String and StringBuffer classes Perl, JavaScript, Ruby, and PHP Primitive type built-in pattern matching by using regular expressions Concepts of Programming Languages

Concepts of Programming Languages Ordinal Types An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers Examples of primitive types that represent ordinal types integer, char, boolean User defined ordinal types Enumerated types Subrange types Concepts of Programming Languages

Concepts of Programming Languages Enumeration Types Possible values are named constants The named constants are provided in the context of data type definition Examples: C#: enum days {mon, tue, wed, thu, fri, sat, sun}; Ada: type Days is (mon, tue, wed, thu, fri, sat, sun); Represent user defined ordinal types Concepts of Programming Languages

Enumeration Types (cont.) Design Issue: are enumeration values coerced to integer? In C, C++ coerced to integer types In Ada, C#, Java 5.0 no coercion to integer types Purposes: Aid to readability, e.g., no need to code a color as a number Aid to reliability, due to improved type safeness. (But only if not coerced to integers) Concepts of Programming Languages

Concepts of Programming Languages Subrange Types Ordered contiguous subsequence of an ordinal type Ada examples: subtype Weekdays is Days range mon..fri; subtype Index is Integer range 1..100; Concepts of Programming Languages

Purposes of subrange types Improves readability Informs readers that variables can only store certain range of values Improves reliability Allows convenient subrange checking / subrange monitoring Concepts of Programming Languages

Implementation of User-Defined Ordinal Types Enumeration types are normally implemented as integers Subrange types are implemented like the parent types with code inserted (by the compiler) for range checking Concepts of Programming Languages

Concepts of Programming Languages Array Types An array is an aggregate of homogeneous data elements in which an individual element is identified by its position in the aggregate, relative to the first element. Concepts of Programming Languages

Concepts of Programming Languages Array Indexing Indexing (or subscripting) is a mapping from indices to elements array_name (index_value_list)  an element Index Syntax FORTRAN, PL/I, Ada use parentheses Most other languages use brackets Concepts of Programming Languages

Concepts of Programming Languages Array Indexing (cont.) Types of array indices: Integer only: Java, C, C++, Fortran Ordinal types in general: Ada Index Range Checking By default: Java, C# Not at all: C, Fortran On demand: C++, Ada Concepts of Programming Languages

Concepts of Programming Languages Integration of Arrays Direct via the language definition Examples: C, Fortran, Perl Indirect via an appropriate class definition Examples: Java, C# In this case arrays represent objects. Languages can support both concepts simultaneously Example: C++ Concepts of Programming Languages

Concepts of Programming Languages Arrays Memory Aspects Location of array: Static (allocation during compile time) Stack (allocation during run time) Heap (allocation during run time) Size management Static array sizes (known during compile time) Dynamic management during runtime (automatic expansion if required) Concepts of Programming Languages

Multidimensional arrays Rectangular versus Jagged Arrays A rectangular array is a multi-dimensioned array in which all of the rows have the same number of elements and all columns have the same number of elements A jagged array has rows with varying number of elements Possible when multi-dimensioned arrays actually appear as arrays of arrays Concepts of Programming Languages

Addressing schemes for rectangular arrays Two common ways: Row major order (by rows) – used in most languages column major order (by columns) – used in Fortran Concepts of Programming Languages

Row-order addressing scheme Formula: Location (a[I,j]) = address of a [row_lb,col_lb] + (((I - row_lb) * n) + (j - col_lb)) * element_size Concepts of Programming Languages

Concepts of Programming Languages Array Initialization Many languages allow an array initialization in the context of the array definition. Example for C, C++, Java, C#: int list [] = {4, 5, 7, 83}; Concepts of Programming Languages

Concepts of Programming Languages Heterogeneous Arrays A heterogeneous array is one in which the elements need not be of the same type Supported by Perl, Python, JavaScript, and Ruby Concepts of Programming Languages

Concepts of Programming Languages Slices A slice is some substructure of an array more or less a referencing mechanism Examples: Newer editions of Fortran (e.g. Fortran 95) Concepts of Programming Languages

Concepts of Programming Languages Slices (cont.) Concepts of Programming Languages

Concepts of Programming Languages Associative Arrays An associative array is an array where the index (subscript) is not of ordinal type. E.g. Indices of type string Design issue: - What is the form of references to elements? Concepts of Programming Languages

Associative Arrays in Perl Names begin with %; literals are delimited by parentheses %hi_temps = ("Mon" => 77, "Tue" => 79, "Wed" => 65); Subscripting is done using braces and keys $hi_temps{"Wed"} = 83; Concepts of Programming Languages

Record Types / Structures A record is a possibly heterogeneous aggregate of data elements in which the individual elements are identified by names Design issues: Syntactic form of references to fields Support of elliptical references Concepts of Programming Languages

Concepts of Programming Languages C++ Example Definition of Datatype Emp_Rec_Type: struct Emp_Rec_Type { std::string First; std::string Mid; std::string Last; float Hourly_Rate; }; Data definition: Emp_Rec_Type Emp_Rec; Concepts of Programming Languages

Concepts of Programming Languages Ada Example Definition of Datatype Emp_Rec_Type: type Emp_Rec_Type is record First: String; Mid: String; Last: String; Hourly_Rate: Float; end record; Data definition: Emp_Rec: Emp_Rec_Type; Concepts of Programming Languages

COBOL Example COBOL uses level numbers for creating nested records Definition of record EMP-REC: 01 EMP-REC. 02 EMP-NAME. 05 FIRST PIC X(20). 05 MID PIC X(10). 05 LAST PIC X(20). 02 HOURLY-RATE PIC 99V99. string consisting of 20 characters Level numbers Concepts of Programming Languages

References to Record/Structure fields Record field references Dot notation (almost all languages): record.field_name Example: Emp_Rec.Mid Exception COBOL field_name OF record Example: FIRST OF EMP-NAME OF EMP-REC Concepts of Programming Languages

Fully qualified versus elliptical references Fully qualified references must include all components Elliptical references allow leaving out record names as long as the reference is unambiguous, Example in COBOL: FIRST, FIRST OF EMP-NAME, and FIRST of EMP-REC are elliptical references to the employee’s first name Concepts of Programming Languages

Concepts of Programming Languages Comparison to Arrays Records are used when the collection of data values is heterogeneous Access to array elements is a bit slower than access to record fields, because subscripts are dynamic (field names are static) Concepts of Programming Languages

Concepts of Programming Languages Unions Types A union is a type whose variables are allowed to store different type values at different times during execution Design issues Type-checking on union level Embedding with respect to records/structures Concepts of Programming Languages

Discriminated vs. Free Unions Fortran, C, and C++ provide union constructs without support of type checking for unions This type of union is called free union Unions with type-checking (during runtime) are called tagged unions or discriminated unions Requires additional type indicator Supported e.g by Ada Concepts of Programming Languages

Ada Example for record with union type Shape is (Circle, Triangle, Rectangle); type Colors is (Red, Green, Blue); type Figure (Form: Shape) is record Filled: Boolean; Color: Colors; case Form is when Circle => Diameter: Float; when Triangle => Leftside, Rightside: Integer; Angle: Float; when Rectangle => Side1, Side2: Integer; end case; end record; Concepts of Programming Languages

Example graphically illustrated Concepts of Programming Languages

Concepts of Programming Languages Unions - Discussion Free unions are unsafe Do not support type checking during runtime Java and C# do not support unions Reflective of growing concerns for safety in programming language Descriminated unions are safer than free unions but you have to pay a price Type check during runtime consumes additional computational resources Concepts of Programming Languages

Pointer and Reference Types A pointer type variable has a range of values that consists of memory addresses and a special value, nil/null/NULL Provides the power of indirect addressing / memory access Provides a way for dynamic memory management Heap allocation / access Concepts of Programming Languages

Concepts of Programming Languages Issues with Pointers Existence of untyped pointer variables (something like void * in C) Existence of reference types (e.g. C++) Explicit or implicit dereferencing C/C++: explicit via * Java: implicit (all non primitive types are references) In the case of heap-management via pointers: Allocation/Deallocation scheme By hand using some new and delete Automatic allocation and automatic deallocation (automatic garbage collection) Concepts of Programming Languages

Concepts of Programming Languages Pointer Operations Two fundamental operations Assignment is used to set a pointer variable’s value to some useful address Dereferencing yields the value stored at the location represented by the pointer’s value Concepts of Programming Languages

Pointer Assignment Illustrated The assignment operation j = *ptr Concepts of Programming Languages

Problems with Pointers and heaps Dangling pointers A pointer points to a heap-dynamic variable that has been deallocated Lost heap-dynamic variable / lost heap space There is an allocated heap-area that is no longer referred by any pointer Resulting effect of “losing memory” is called memory leakage Concepts of Programming Languages

Concepts of Programming Languages Pointers in C and C++ Extremely flexible but must be used with care Pointers can point at any variable regardless of when or where it was allocated Used for dynamic storage management and addressing Pointer arithmetic is possible Explicit dereferencing and address-of operators Untype pointers (void *) void * can point to any type.. Concepts of Programming Languages

Concepts of Programming Languages Reference Types C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters Advantages of both pass-by-reference and pass-by-value Java extends C++’s reference variables and allows them to replace pointers entirely References are references to objects, rather than being addresses C# includes both the references of Java and the pointers of C++ Concepts of Programming Languages

Dangling Pointer Problem Tombstone: extra heap cell that is a pointer to the heap-dynamic variable The actual pointer variable points only at tombstones When heap-dynamic variable de-allocated, tombstone remains but set to nil Costly in time and space Locks-and-keys: Pointer values are represented as (key, address) pairs Heap-dynamic variables are represented as variable plus cell for integer lock value When heap-dynamic variable allocated, lock value is created and placed in lock cell and key cell of pointer Concepts of Programming Languages

Automatic Heap Management Complex run-time process Single-size cells vs. variable-size cells Two approaches to reclaim garbage Reference counters (eager approach): reclamation is gradual Mark-sweep (lazy approach): reclamation occurs in memory tight situations Concepts of Programming Languages

Concepts of Programming Languages Reference Counter Reference counters: Maintain a counter in every cell that store the number of pointers currently pointing at the cell Advantage: intrinsically incremental, so significant delays in the application execution are avoided Disadvantage: complications for cells connected circularly Concepts of Programming Languages

Concepts of Programming Languages Mark-Sweep The run-time system allocates storage cells as requested and disconnects pointers from cells as necessary. Method: Every heap cell has an extra bit used by collection algorithm All cells initially set to garbage All pointers traced into heap, and reachable cells marked as not garbage All garbage cells returned to list of available cells Disadvantage: When done, it causes significant delays during application execution. Concepts of Programming Languages

Concepts of Programming Languages Marking Algorithm Concepts of Programming Languages

Concepts of Programming Languages Variable-Size Cells All the difficulties of single-size cells plus more Required by most programming languages If mark-sweep is used, the marking process becomes more difficult Concepts of Programming Languages