ics 313
play

ICS 313 1 2 Lecture #7 Lecture #7 Data Types Data Types - PDF document

ICS 313 1 2 Lecture #7 Lecture #7 Data Types Data Types Programming Language Theory Programming Language Theory Primitive Data Types ICS313 ICS313 Character String Types Fall 2007 Fall 2007 User-Defined Ordinal Types


  1. ICS 313 1 2 Lecture #7 Lecture #7 Data Types Data Types Programming Language Theory Programming Language Theory � Primitive Data Types ICS313 ICS313 � Character String Types Fall 2007 Fall 2007 � User-Defined Ordinal Types � Array Types � Associative Arrays Nancy E. Reed � Record Types � Union Types nreed@hawaii.edu � Pointer Types � Ref: Chapter 6 in text 3 4 Memory Layout : Overview Memory Layout : Overview Data Types Data Types � Data type - defines � Text � Text : : code, constant data 0 • a collection of data objects and � Data � Data : : Text • a set of predefined operations on those objects Data � Evolution of data types: • initialized global & static variables • FORTRAN I (1957) - INTEGER, REAL, arrays • Ada (1983) - User can create unique types and system enforces the • global & static variables – 0 Heap types initialized or un-initialized � Descriptor - collection of the attributes of a variable (blank) � Design issues for all data types: � Heap � Heap : : dynamic memory 1. Syntax of references to variables � Stack � Stack : : dynamic - local 2. Operations defined and how to specify Stack Stack variables � What is the mapping to computer representation? xxxxxxxx 5 6 Primitive Data Types Primitive Data Types IEEE Floating Point Format Standards IEEE Floating Point Format Standards Not defined in terms of other data types � 1. Integer • Almost always an exact reflection of the Single precision hardware, so the mapping is trivial • There may be as many as eight different integer types in a language (size, signed/unsigned) Double precision � 2. Floating Point • Model real numbers, but only as approximations • Languages for scientific use support at least two floating-point types; sometimes more • Usually exactly like the hardware, but not always 1

  2. ICS 313 7 8 Primitive Data Types Primitive Data Types Character String Types Character String Types 3. Decimal – base 10 � Sequences of characters • For business applications (usually money) � Design issues: • Store a fixed number of decimal digits - coded, not as floating point. 1. Is it a primitive type or just a special kind of array? • Advantage: accuracy – no round off error, no exponent, binary representation can’t do this 2. Is the length of objects static or dynamic? • Disadvantages: limited range, takes more memory � Operations: • Example: binary coded decimal (BCD) – use 4 bits per decimal digit – takes as much space as hexadecimal • Assignment 4. Boolean (true/false) • Comparison (=, >, etc.) • Could be implemented as bits, but often as bytes or words • Concatenation • Advantage: readability • Substring reference 5. Character • Pattern matching • Stored as numeric codes (e.g., ASCII, EBCDIC, Unicode) 9 10 Character String Types Character String Types Character String Type Examples Character String Type Examples � Java � Pascal • Not primitive type; • String class (not arrays of char ) • assignment and comparison only (of packed arrays) • Objects cannot be changed (immutable) � Ada, FORTRAN 90, and BASIC • StringBuffer is a class for changeable string objects � Perl and JavaScript • Somewhat primitive • Assignment, comparison, concatenation, substring reference • Patterns are defined in terms of regular expressions • FORTRAN has an intrinsic for pattern matching • A very powerful facility e.g. (Ada) • e.g., N := N1 & N2 (concatenation) /[A-Za-z][A-Za-z\d]+/ N(2..4) (substring reference) � SNOBOL4 (string manipulation language) � C and C++ • Primitive string type • Not primitive • Many operations, including elaborate pattern matching • Use char arrays and a library of functions that provide operations predefined 11 12 Character String Length Options Character String Type Evaluation Character String Length Options Character String Type Evaluation � Strings aid writability and readability 1. Static - FORTRAN 77, Ada, COBOL � Static length primitive type e.g. (FORTRAN 90) CHARACTER (LEN = 15) NAME; • Inexpensive to provide, why not include them? � Dynamic length 2. Limited Dynamic Length - C and C++ • Weigh flexibility vs. cost to provide actual length is indicated by a null � Implementation: character • Static length - compile-time descriptor 3. Dynamic - SNOBOL4, Perl, • Limited dynamic length - may need a run-time descriptor JavaScript, Common Lisp for length (but not in C and C++) • Dynamic length - need run-time descriptor; allocation/de- allocation is the biggest implementation problem 2

  3. ICS 313 13 14 Character String Types Character String Types User- User -Defined Ordinal Types Defined Ordinal Types � Ordinal type - the range of possible values = the set of positive integers (or can be easily associated with them) � Enumeration Types - user enumerates all * possible values, which are symbolic constants • Represented with ordinal numbers � Design Issues • Can symbolic constants be in more than one type definition? Compile-time Run-time descriptor - hair = {red,brown,blonde}, descriptor for for limited dynamic - cat = {brown,striped,black} static strings strings • Can they be read/written as symbols? • Allowed as array indices, subranges? 15 16 User User- -Defined Ordinal Type Examples Defined Ordinal Type Examples Evaluation of User- Evaluation of User -Defined Ordinal Types Defined Ordinal Types � Pascal 1. Aid to readability • cannot reuse values, no input or output of values � e.g. no need to code a color as a number • can be used for array subscripts, for variables, case selectors 2. Aid to reliability • can be compared � e.g. compiler can check � Ada 3. Operations specified (don’t allow • constants can be reused (overloaded literals) • can be used as in Pascal; CAN be input and output colors to be added, for example) � C and C++ - 4. Ranges of values can be checked • like Pascal, except they can be input and output as integers 1. E. g. if you have 7 colors, code them as integers � Java does not include an enumeration type, (1..7), 9 is a legal integer (and thus a `legal • but provides the Enumeration interface color’)! 17 18 Evaluation and Implementation of Evaluation and Implementation of Subrange Types Types Subrange Sub- Sub -range Types range Types � An ordered contiguous subsequence of an ordinal � Aid to readability type � Reliability - restricted ranges adds error � Design Issue: How can they be used? � Pascal detection • Sub-range types behave as their parent types; can be used as for � Enumeration types are implemented as variables and array indices e.g. type pos = 0 .. MAXINT; integers � Ada � Sub-range types are the parent types with • Subtypes are not new types, just constrained existing types (so they are compatible); can be used as in Pascal, plus case constants code inserted (by the compiler) to restrict e.g. assignments to sub-range variables subtype POS_TYPE is INTEGER range 0..INTEGER'LAST; 3

Recommend


More recommend