PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000 1 PZ04A - Scalar and composite data Programming Language.

Slides:



Advertisements
Similar presentations
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
Advertisements

Names and Bindings.
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.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
COEN Expressions and Assignment
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Primitive Data Types: Numbers Strings Ordinal Types Pointers
PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05B - Type equality Programming Language Design and.
Floating Point Numbers
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Simple Data Type Representation and conversion of numbers
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
1 CS 2104 Prog. Lang. Concepts Lecture 3 Dr. Abhik Roychoudhury School of Computing.
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Fixed-Point Arithmetics: Part II
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Data Representation and Computer Arithmetic
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Elsa Gunter who’s.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Data Representation Dr. Ahmed El-Bialy Dr. Sahar Fawzy.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
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
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Chapter 9 Computer Arithmetic
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Data Representation Binary Numbers Binary Addition
Integer Division.
Chapter 6: Data Types Lectures # 10.
William Stallings Computer Organization and Architecture 7th Edition
Data Structures Mohammed Thajeel To the second year students
Chapter 2 Bits, Data Types & Operations Integer Representation
Introduction to Abstract Data Types
ECEG-3202 Computer Architecture and Organization
PZ04A - Scalar and composite data
Numbers with fractions Could be done in pure binary
PZ05B - Type equality Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 6.4 PZ05B.
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Data objects Scalar data objects: Numeric (Integers, Real) Booleans Characters Enumerations Composite objects: String Pointer Structured objects: Arrays Records Lists Sets Abstract data types: Classes Active Objects: Tasks Processes

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Binding of data objects A compiler creates two classes of objects: Memory locations Numeric values A variable is a binding of a name to a memory location: Contents of the location may change

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Data types Each data object has a type: Values: for objects of that type Operations: for objects of that type Implementation: (Storage representation) for objects of that type Attributes: (e.g., name) for objects of that type Signature: (of operation f): f: type x type  type

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, L-value and R-value Location for an object is its L-value. Contents of that location is its R-value. Where did names L-value and R-value come from? Consider executing: A = B + C; 1. Pick up contents of location B 2. Add contents of location C 3. Store result into address A. For each named object, its position on the right-hand- side of the assignment operator (=) is a content-of access, and its position on the left-hand-side of the assignment operator is an address-of access. address-of then is an L-value contents-of then is an R-value Value, by itself, generally means R-value

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Subtypes A is a subtype of B if every value of A is a value of B. Note: In C almost everything is a subtype of integer. Conversion between types: Given 2 variables A and B, when is A:=B legal? Explicit: All conversion between different types must be specified Implicit: Some conversions between different types implied by language definition

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Coersion examples Examples in Pascal: var A: real; B: integer; A := B - Implicit, called a coersion - an automatic conversion from one type to another A := B is called a widening since the type of A has more values than B. B := A (if it were allowed) would be called a narrowing since B has fewer values than A. Information could be lost in this case. In most languages widening coersions are usually allowed; narrowing coersions must be explicit: B := round(A); Go to integer nearest A B := trunc(A); Delete fractional part of A

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Integer numeric data Integers: Binary representation in 2's complement arithmetic For 32-bit words: Maximum value: Minimum value: Positive values Negative values

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Real numeric data Float (real): hardware representations Exponents usually biased e.g., if 8 bits (256 values) +128 added to exponent so exponent of 128 = = 0 is true exponent so exponent of 129 = = 1 is true exponent so exponent of 120 = = -8 is true exponent

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, IEEE floating point format IEEE standard 754 specifies both a 32- and 64-bit standard. Numbers consist of three fields: S: a one-bit sign field. 0 is positive. E: an exponent in excess-127 notation. Values (8 bits) range from 0 to 255, corresponding to exponents of 2 that range from -127 to 128. M: a mantissa of 23 bits. Since the first bit of the mantissa in a normalized number is always 1, it can be omitted and inserted automatically by the hardware, yielding an extra 24th bit of precision.

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Decoding IEEE format Given E, and M, the value of the representation is: Parameters Value E=255 and M  0 An invalid number E=255 and M = 0  0<E<255 2 {E-127} (1.M) E=0 and M  0 2 {-126}.M E=0 and M=0 0

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Example floating point numbers +1= 2 0 *1= 2 { } *(1).0 (binary) = 2 0 *1.5= 2 { } *(1).1 (binary) = -2 2 *1.25= 2 { } *(1).01 (binary) This gives a range from to In 64-bit format,the exponent is extended to 11 bits giving a range from to +1023, yielding numbers in the range to

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Other numeric data Short integers (C) - 16 bit, 8 bit Long integers (C) - 64 bit Boolean or logical - 1 bit with value true or false Byte - 8 bits Character - Single 8-bit byte characters ASCII is a 7 bit 128 character code In C, a char variable is simply 8-bit integer numeric data

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Enumerations typedef enum thing {A, B, C, D } NewType; Implemented as small integers with values: A = 0, B = 1, C = 2, D = 3 NewType X, Y, Z; X = A Why not simply write: X=0 instead of X=A? Readability Error detection Example: enum { fresh, soph, junior, senior} ClassLevel; enum { old, new } BreadStatus; BreadStatus = fresh; An error which can be detected

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Declaring decimal data Fixed decimal in PL/I and COBOL (For financial applications) DECLARE X FIXED DECIMAL(p,q); p = number of decimal digits q = number of fractional digits Example of PL/I fixed decimal: DECLARE X FIXED DECIMAL (5,3), Y FIXED DECIMAL (6,2), Z FIXED DECIMAL (6,1); X = ; Y = ;

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Using decimal data What is Z=X+Y?: By hand you would line up decimal points and add: = FIXED DECIMAL(8,3) p=8 since adding two 4 digit numbers can give 5 digit result and need 3 places for fractional part. p=8 and q=3 is known before addition Known during compilation - No runtime testing needed.

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Implementing decimal data Algorithm: 1. Store each number as an integer (12345, ) Compiler knows scale factor (S=3 for X, S=2 for Y). True value printed by dividing stored integer by 10 S 2. To add, align decimal point. Adjust S by 1 by multiplying by *Y+X = = , Compiler knows S=3 4. S=1 for Z, so need to adjust S of addition by 2; divide by 10 2 (98888) 5. Store into Z. Compiler knows S=1 Note: S never appears in memory, and there is no loss of accuracy by storing data as integers.

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Composite data Character Strings: Primitive object made up of more primitive character data. Fixed length: char A(10) - C DCL B CHAR(10) - PL/I var C packed array [1..10] of char - Pascal Variable length: DCL D CHAR(20) VARYING - PL/I - 0 to 20 characters E = “ABC” - SNOBOL4 - any size, dynamic F = `ABCDEFG\0' - C - any size, programmer defined

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, String implementations

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, String operations In C, arrays and character strings are the same. Implementation: L-value(A[I]) = L-value(A[0]) + I

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Pointer data Use of pointers to create arbitrary data structures Each pointer can point to an object of another data structure In general a very error prone construct and should be avoided

PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, Pointer aliasing