1 CS 2104 Prog. Lang. Concepts Lecture 3 Dr. Abhik Roychoudhury School of Computing.

Slides:



Advertisements
Similar presentations
PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04B - Arrays and records Programming Language Design.
Advertisements

Arrays and records Programming Language Design and Implementation
Names and Bindings.
Chapter 7:: Data Types Programming Language Pragmatics
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,
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
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
Type Checking.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
ISBN Chapter 6 Data Types: Structured types.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
Elementary Data Types Scalar Data Types Numerical Data Types Other
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 © 2002 by Addison Wesley Longman, Inc Names - We discuss all user-defined names here - Design issues for names: - Maximum length? - Are.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CS 2104 Prog. Lang. Concepts Subprograms
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)
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Arithmetic Expressions
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.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
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
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Names, Bindings, Type Checking, and Scopes
Expressions and Assignment Statements
Chapter 6 – Data Types CSCE 343.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Chapter 6: Data Types Lectures # 10.
Lecture 16: Introduction to Data Types
CS 326 Programming Languages, Concepts and Implementation
Instructor : Ahmed Alalawi Slides from Chung –Ta King
PZ04A - Scalar and composite data
Arrays and records Programming Language Design and Implementation
Arrays and records Programming Language Design and Implementation
Arrays and records Programming Language Design and Implementation
Arrays and records Programming Language Design and Implementation
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

1 CS 2104 Prog. Lang. Concepts Lecture 3 Dr. Abhik Roychoudhury School of Computing

2 Announcements Textbook to be returned by Co-op next week to the publisher. From the next homework, write your name and Matric number in the.txt or.doc file submitted.

3 Topics to be covered 1. Very Quick Recap of Axiomatic Semantics [ Discussed in last class and this week’s tutorial (in details) ] 2. Scalar and Composite Types. (Pointer, String, Enumerations etc.) [ Chapter 4.1 – 4.3, 5.3 ] 3. Structured Types (Arrays, Records etc.) [ Chapter 5.4,5.5 ] Acknowledgements: (2) and (3) lecture notes prepared with help from Pratt/Zelkowitz lecture notes.

4 Hoare Triples (recap) Of the form {Pre} C {Post} Pre, post are assertions C is a code fragment/program Proving such a Hoare Triple requires proving If we start in a state in which Pre holds Then execution of C produces a state C in which Post holds, provided the program C terminates

5 Proving Hoare Triples Proving correctness of a program amounts to setting appropriate pre- and post-conditions and then proving the corresponding Hoare Triple. To prove a Hoare Triple, we will use certain rules, based on the structure of the program under question. These rules are stated in the same manner as operational semantics rules.

6

7

8

9

10 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

11 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

12 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

13 Data Types - Example

14 L-value and R-value Location for an object is its L-value. Contents of that location is its R-value.

15 L-value and 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.

16 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

17 Coercion examples Examples in Pascal: var A: real; B: integer; A := B - Implicit, called a coercion - an automatic conversion from one type to another A := B is called a widening since the type of A has more values than B.

18 Coercion examples 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 coercions are usually allowed; narrowing coercions must be explicit: B := round(A); Go to integer nearest A B := trunc(A); Delete fractional part of A

19 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 and Error detection

20 Enumerations Example: enum { fresh, soph, junior, senior} ClassLevel; enum { old, new } BreadStatus; BreadStatus = fresh; error can be detected

21 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

22 Composite data 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

23 String implementations

24 String operations In C, arrays and character strings are the same. Implementation: L-value(A[I]) = L- value(A[0]) + I

25 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

26 Pointer aliasing

27 Structured Types: Arrays An array is an ordered sequence of identical objects. The ordering is determined by a scalar data object (usually integer or enumeration data). This value is called the subscript or index, and written as A[I] for array A and subscript I.

28 Structured Types: Arrays Multidimensional arrays have more than one subscript. A 2-dimensional array can be modeled as the boxes on a rectangular grid. The L-value for array element A[I,J] is given by the accessing formula on the next slide

29

30 Array accessing (continued) L-value(A[0,0]) = V0, a constant. Call this constant the virtual origin (VO); It represents the address of the 0th element of the array. L-value(A[I,J]) = VO +I*d1 + J*d2 To access an array element, use a dope vector

31 Array accessing summary To create arrays: 1. Allocate total storage beginning at  : (U2-L2+1)*(U1-L1+1)*eltsize 2. d2 = eltsize d1 = (U2-L2+1)*d2 4. To access A[I,J]: Lvalue(A[I,J]) = VO + I*d1 + J*d2 This works for 1, 2 or more dimensions. May not require runtime dope vector if all values known at compile time. (e.g., in Pascal, d1, d2, and VO can be computed by compiler.) Next slide: Storage for 2-dimensional array.

32

33 Associative arrays Access information by name without having a predefined ordering or enumeration: Example: Names and grades for students in a class: NAME[I] = name of Ith student GRADE[I] = Grade for Ith student Associative array: Use Name as index: CLASS[name] will be grade. Problem: Do not know enumeration before obtaining data so dope vector method of accessing will not work. Implemented in Perl and in SNOBOL4 (as a table)

34 Perl example %ClassList = (“Michelle”, `A', “Doris”, `B', “Michael”, `D'); # % operator makes an associative array $ClassList{‘Michelle’} has the value = %ClassList # Converts ClassList to an # enum. array with index 0..5

35 Perl example $I= 0 $y[$I] = Doris $I= 1 $y[$I] = B $I= 2 $y[$I] = Michael $I= 3 $y[$I] = D $I= 4 $y[$I] = Michelle $I= 5 $y[$I] = A

36 Structs in C Representation: a sequence of objects: record { A: object; B: object; C: object }

37 Structs in C

38 Union types typedef union { int X; float Y; char Z[4];} B; B P; Similar to records, except all have overlapping (same) L-value.

39 Union types But problems can occur. What happens below? P.X = 142; printf(“%O\n”, P.Z[3]) All 3 data objects have same L-value and occupy same storage. No enforcement of type checking.  Poor language design

40 Variant records type PayType=(Salaried, Hourly); var Employee:record ID: integer; Dept: array[1..3] of char; Age: integer; case PayClass: PayType of Salaried:(MonthlyRate:real; StartDate:integer); Hourly:(HourRate:real; Reg:integer; Overtime:integer) end

41 Variant records

42 Variant records (continued) Tagged union type - Pascal variant records type whichtype = (inttype, realtype, chartype); type uniontype = record case V: whichtype of inttype: (X: integer); realtype: (Y: real); chartype: (Z: char4); string of length 4 end

43 Variant records (continued) But can still subvert tagging: var P: uniontype P.V = inttype; P.X = 142; P.V = chartype;