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.

Slides:



Advertisements
Similar presentations
Names and Bindings.
Advertisements

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,
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.
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.
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.
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
Lecture 6 Concepts of Programming Languages
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.
College of Computer Science and Engineering
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 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.
C H A P T E R S I X Data Types.
CS 363 Comparative Programming Languages Data Types.
1 CS Programming Languages Class 08 September 19, 2000.
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.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Evolution of Data Types: FORTRAN I (1956) - INTEGER, REAL, arrays … Ada (1983) - User can.
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.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Dr. Vamsi Paruchuri University of Central Arkansas
Data Types I Subject : T0152 – Programming Language Concept
Chapter 6 Data Types.
Chapter 6 – Data Types CSCE 343.
CMP 339/692 Programming Languages Day 14 Tuesday, March 20, 2012
Chapter 6 Data Types.
Concepts of Programming Languages
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
Instructor : Ahmed Alalawi Slides from Chung –Ta King
Records Design Issues: 1. What is the form of references?
Concepts of programming languages
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.
Presentation transcript:

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 object represents an instance of a user- defined (abstract data) type

3 Why have Types? Types provide context for operations –a+b  what kind of addition? –pointer p = new object  how much space? Limit valid set of operations –int a = “string”  know error before run time

4 Defining/Declaring a Type Fortran: variable’s type known from name –Letters I -N = integer type –Otherwise = real (floating-point) type Scheme, Smalltalk: run-time typing Most languages (such as C++): user explicitly declares type –int a, char b

5 Primitive Data Types Almost all programming languages provide a set of primitive data types Primitive data types: Those not defined in terms of other data types Some primitive data types are merely reflections of the hardware Others require little non-hardware support

Pre-defined types Built-in types: –E.g. int, boolean, float, double in C/Java Types of literals –0,1,… (integer) –true:boolean, false:boolean –0.0:float, 1.745: float (or double??) –0.0d : double, 1.745d: double –“Hello Mars!” : string

Pre-defined types Types of built-in operators –+, -, *, /, % : integer,integer  integer –+, - : integer  integer –, >= : integer,integer  bool –&, |, ^ : bool,bool  bool –! : bool  bool –[ ] : array(int), int  int –[ ] : array(float), int  float –… –[ ]= : array(float), int, float  array(float)

8 Primitive Data Types: Integer Almost always an exact reflection of the hardware so the mapping is trivial There may be as many as eight different integer types in a language Java’s signed integer sizes: byte, short, int, long

9 Primitive Data Types: Floating Point Model real numbers, but only as approximations Languages for scientific use support at least two floating-point types (e.g., float and double ; sometimes more Usually exactly like the hardware, but not always IEEE Floating-Point Standard 754

Range of numbers Normalized (positive range; negative is symmetric) Unnormalized × (1+0) = × ( ) smallest largest smallest largest × (2 -23 ) = × ( ) ( ) ( ) Positive underflow Positive overflow 10

11 Primitive Data Types: Decimal For business applications (money) –Essential to COBOL –C# offers a decimal data type Store a fixed number of decimal digits Advantage: accuracy

12 Primitive Data Types: Boolean Simplest of all Range of values: two elements, one for “true” and one for “false” Could be implemented as bits, but often as bytes

13 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

14 Character String Types Values are sequences of characters Design issues: –Is it a primitive type or just a special kind of array? –Should the length of strings be static or dynamic?

15 Character String Types Operations Typical operations: –Assignment and copying –Comparison (=, >, etc.) –Catenation –Substring reference –Pattern matching

16 Character String Type in Certain Languages C and C++ –Not primitive –Use char arrays and a library of functions that provide operations SNOBOL4 (a string manipulation language) –Primitive –Many operations, including elaborate pattern matching Java –Primitive via the String class

17 Character String Length Options Static: COBOL, Java’s String class Limited Dynamic Length: C and C++ –In C-based language, a special character is used to indicate the end of a string’s characters, rather than maintaining the length Dynamic (no maximum): SNOBOL4, Perl, JavaScript Ada supports all three string length options

18 Records Also known as ‘structs’ and ‘types’. –C–C struct resident { char initials[2]; int ss_number; bool married; }; fields – the components of a record, usually referred to using dot notation.

19 Nesting Records Most languages allow records to be nested within each other. –Pascal type two_chars = array [1..2] of char; type married_resident = record initials: two_chars; ss_number: integer; incomes: record husband_income: integer; wife_income: integer; end;

20 Definition of Records COBOL uses level numbers to show nested records; others use recursive definition Record Field References 1. COBOL field_name OF record_name_1 OF... OF record_name_n 2. Others (dot notation) record_name_1.record_name_2.... record_name_n.field_name

21 Definition of Records in COBOL COBOL uses level numbers to show nested records; others use recursive definition 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.

22 Definition of Records in Ada Record structures are indicated in an orthogonal way type Emp_Rec_Type is record First: String (1..20); Mid: String (1..10); Last: String (1..20); Hourly_Rate: Float; end record; Emp_Rec: Emp_Rec_Type;

23 Operations on Records Assignment is very common if the types are identical Ada allows record comparison Ada records can be initialized with aggregate literals COBOL provides MOVE CORRESPONDING –Copies a field of the source record to the corresponding field in the target record

24 Sets Introduced by Pascal, found in most recent languages as well. Common implementation uses a bit vector to denote “is a member of”. –Example: U = {‘a’, ‘b’, …, ‘g’} A = {‘a’, ‘c’, ‘e’, ‘g’} = Hash tables needed for larger implementations. –Set of integers = (2 32 values) / 8 = 536,870,912 bytes

25 Enumerations enumeration – set of named elements –Values are usually ordered, can compare enum weekday {sun,mon,tue,wed,thu,fri,sat} if (myVarToday > mon) {... } Can choose ordering in C enum weekday {mon=0,tue=1,wed=2…}

26 Enumeration Types All possible values, which are named constants, are provided in the definition C# example enum days {mon, tue, wed, thu, fri, sat, sun}; Design issues –Is an enumeration constant allowed to appear in more than one type definition, and if so, how is the type of an occurrence of that constant checked? –Are enumeration values coerced to integer? –Any other type coerced to an enumeration type?

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

28 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 Ada explicitly uses parentheses to show uniformity between array references and function calls because both are mappings –Most other languages use brackets

29 Arrays Index (Subscript) Types FORTRAN, C: integer only Pascal: any ordinal type (integer, Boolean, char, enumeration) Ada: integer or enumeration (includes Boolean and char) Java: integer types only C, C++, Perl, and Fortran do not specify range checking Java, ML, C# specify range checking

30 Array Dimensions C uses 0 -> (n-1) as the array bounds. –float values[10];// ‘values’ goes from 0 -> 9 Fortran uses 1 -> n as the array bounds. –real(10) values! ‘values’ goes from 1 -> 10 Some languages let the programmer define the array bounds. –var values: array [3..12] of real; (* ‘values’ goes from 3 -> 12 *)

31 Array Initialization Some language allow initialization at the time of storage allocation –C, C++, Java, C# example int list [] = {4, 5, 7, 83} –Character strings in C and C++ char name [] = “freddie”; –Arrays of strings in C and C++ char *names [] = {“Bob”, “Jake”, “Joe”]; –Java initialization of String objects String[] names = {“Bob”, “Jake”, “Joe”};

32 Arrays Operations APL provides the most powerful array processing operations for vectors and matrixes as well as unary operators (for example, to reverse column elements) Ada allows array assignment but also catenation Fortran provides elemental operations because they are between pairs of array elements –For example, + operator between two arrays results in an array of the sums of the element pairs of the two arrays

33 Rectangular and 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 matrix has rows with varying number of elements –Possible when multi-dimensioned arrays actually appear as arrays of arrays

34 Accessing Multi-dimensioned Arrays Two common ways: –Row major order (by rows) – used in most languages –column major order (by columns) – used in Fortran

35 Memory Layout Options Ordering of array elements can be accomplished in two ways: –row-major order – Elements travel across rows, then across columns. –column-major order – Elements travel across columns, then across rows. Row-majorColumn-major

36 Pointer and Reference Types A pointer type variable has a range of values that consists of memory addresses and a special value, nil Provide the power of indirect addressing Provide a way to manage dynamic memory A pointer can be used to access a location in the area where storage is dynamically created (usually called a heap)

37 Pointer Operations Two fundamental operations: assignment and dereferencing 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 –Dereferencing can be explicit or implicit –C++ uses an explicit operation via * j = *ptr sets j to the value located at ptr

38 Pointer Assignment Illustrated The assignment operation j = *ptr

39 Pointer Arithmetic in C and C++ float stuff[100]; float *p; p = stuff; *(p+5) is equivalent to stuff[5] and p[5] *(p+i) is equivalent to stuff[i] and p[i]

40 Pointers in Fortran 95 Pointers can only point to variables that have the TARGET attribute The TARGET attribute is assigned in the declaration: INTEGER, TARGET :: NODE

41 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 refer to call instances C# includes both the references of Java and the pointers of C++

42 Garbage Collection Language implementation notices when objects are no longer useful and reclaims them automatically –essential for functional languages –trend for imperative languages When is object no longer useful? –Reference counts –Mark and sweep –“Conservative” collection

43 Mark-and-Sweep Idea … when space low 1.Mark every block “useless” 2.Beginning with pointers outside the heap, recursively explore all linked data structures and mark each traversed as useful 3.Return still marked blocks to freelist Must identify pointers –in every block –use type descriptors

Consider the grammar:  ”(” ”)” | ”[” ”]” | " " (a) Is the sentence ([]) syntactically correct? Using shift reduce method (b) Is the sentence ([)] syntactically correct? Using LL method (c) Can this grammar is used for top down parser? Why? How it can be modified to use if not? 44

Given the following grammar: ::= id = ::= | + | - ::= | * | / ::= id | integer | ( ) Using LL(1) draw the parse tree for: B = A / (B+G) – D 45