Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICE1341 Programming Languages Spring 2005 Lecture #11 Lecture #11 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.

Similar presentations


Presentation on theme: "ICE1341 Programming Languages Spring 2005 Lecture #11 Lecture #11 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."— Presentation transcript:

1 ICE1341 Programming Languages Spring 2005 Lecture #11 Lecture #11 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University (ICU)

2 Spring 2005 2 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Announcements Midterm Exam Midterm Exam Time and Location: Monday April 4, 2005 4PM- 6PM, L401 Time and Location: Monday April 4, 2005 4PM- 6PM, L401 Coverage Coverage Chapter 1, Chapter 3 (except 3.5.3), Chapter 5, Chapter 6 (except 6.9.9) Chapter 1, Chapter 3 (except 3.5.3), Chapter 5, Chapter 6 (except 6.9.9) WWW Concepts and Languages WWW Concepts and Languages Closed book and note Closed book and note Midterm Project Report Due & Presentation Midterm Project Report Due & Presentation Thursday April 14 th Thursday April 14 th

3 Spring 2005 3 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Data Types Data Types Primitive Types Primitive Types Character String Data Types Character String Data Types User-defined Data Types User-defined Data Types Arrays Arrays Last Lecture

4 Spring 2005 4 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University This Lecture Data Types Data Types More on Arrays More on Arrays Records Records Unions Unions Pointers Pointers

5 Spring 2005 5 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Categories of Arrays Based on subscript binding and binding to storage Stack Area (Local variables) Static Area (Program code & non-local, static variables) Heap Area (Dynamically allocated/deallocated blocks) A program space in the memory ……011819 A Static (Fortran 77) … 01n-1n A Stack-dynamic (Ada, C) … 011819 A Fixed heap-dynamic (Fortran 90, C, Java) … 011819 A Fixed stack-dynamic (C) … 01n-1x A Heap-dynamic (Perl, JavaScript)

6 Spring 2005 6 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Other Design Issues of Arrays Number of subscripts (Dimension) Number of subscripts (Dimension) FORTRAN I allowed up to three FORTRAN I allowed up to three FORTRAN 77 allows up to seven FORTRAN 77 allows up to seven Others - no limit Others - no limit Array Initialization Array Initialization A list of values – e.g. (C), int stuff [] = {2, 4, 6, 8}; A list of values – e.g. (C), int stuff [] = {2, 4, 6, 8}; Positioned values – e.g. (Ada), Positioned values – e.g. (Ada), SCORE : array (1..14, 1..2) := (1 => (24, 10), 2 => (10, 7), 3 =>(12, 30), others => (0, 0)); Array Operations Array Operations Assignment, Concatenation, Elemental ops. (+), etc. Assignment, Concatenation, Elemental ops. (+), etc. Intrinsic (library) operations (e.g., matrix ops) Intrinsic (library) operations (e.g., matrix ops) APL supports many array operations APL supports many array operations

7 Spring 2005 7 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Multi-dimensional Arrays Rectangular Array Jagged Array Slices: Substructures of an array e.g., FORTRAN 90 INTEGER MAT (1:4, 1:4) MAT(1:4, 1) - the first column MAT(2, 1:4) - the second row

8 Spring 2005 8 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Implementation of Array Types Access Function: maps subscript expressions to an address in the array Access Function: maps subscript expressions to an address in the array addr(A[k]) = addr(A[lbound]) + ((k - lbound) * ele_size); Row major order vs. column major order Row major order vs. column major order Compile-time descriptors: Compile-time descriptors: Single- dimensioned array Multi- dimensional array

9 Spring 2005 9 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Associative Arrays Associative array: an unordered collection of data elements that are indexed by an equal number of values called keys Associative array: an unordered collection of data elements that are indexed by an equal number of values called keys 1:1 correspondence between keys and values 1:1 correspondence between keys and values Keys need to be stored in the array structure Keys need to be stored in the array structure e.g., Java’s Map interface, Perl’s hashes e.g., Java’s Map interface, Perl’s hashes Design Issues: Design Issues: 1. What is the form of references to elements? 2. Is the size static or dynamic? Key e.g., an email address Value e.g., a student name Associative Array

10 Spring 2005 10 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Hashtable: An Implementation of an Associative Array Hong, Kildong hkd@icu.ac.kr KeysValues HashingFunction A Bucket

11 Spring 2005 11 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Record Types Record: a possibly heterogeneous aggregate of data elements in which the individual elements are identified by names Record: a possibly heterogeneous aggregate of data elements in which the individual elements are identified by names Design Issues: Design Issues: 1. What is the form of references? 2. What unit operations are defined? * AW Lecture Notes Employee = record Name : Employ_Name; Sex : (Female, Male); Hourly_Rate : Real; end; var Clerk : Employee; type Employee_Name = record First : String; First : String; Middle : String; Middle : String; Last : String; Last : String; end;

12 Spring 2005 12 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Record Field References COBOL; field_name OF record_name_1 OF... OF record_name_n field_name OF record_name_1 OF... OF record_name_n Others (dot notation); record_name_1.record_name_2.... record_name_n.field_name record_name_1.record_name_2.... record_name_n.field_name Fully qualified references: include all record names Fully qualified references: include all record names e.g., Clerk.Name.Last Elliptical references: allow leaving out record names as long as the reference is unambiguous Elliptical references: allow leaving out record names as long as the reference is unambiguous Pascal provides a with clause to abbreviate references Pascal provides a with clause to abbreviate references with Clerk do begin write(‘The wage of the clerk, ’); write(‘The wage of the clerk, ’); write(Name.First); write(Name.Middle); write(Name.Last); write(Name.First); write(Name.Middle); write(Name.Last); write(‘ is $.’, Hourly_Rate :7:2); write(‘ is $.’, Hourly_Rate :7:2);end * AW Lecture Notes

13 Spring 2005 13 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Record Operations Assignment Assignment e.g., Clerk := Janitor e.g., Clerk := Janitor Pascal, Ada, and C allow it if the types are identical Pascal, Ada, and C allow it if the types are identical Initialization Initialization e.g., Clerk = { { “Tom”, “J.”, “Lee” }, Male, 28.50 }; e.g., Clerk = { { “Tom”, “J.”, “Lee” }, Male, 28.50 }; Allowed in Ada, using an aggregate constant Allowed in Ada, using an aggregate constant Comparison Comparison e.g., if Clerk = Janitor then … e.g., if Clerk = Janitor then … In Ada, = and /=; one operand can be an aggregate constant In Ada, = and /=; one operand can be an aggregate constant MOVE CORRESPONDING MOVE CORRESPONDING In COBOL - it moves all fields in the source record to fields with the same names in the destination record In COBOL - it moves all fields in the source record to fields with the same names in the destination record See the example in the textbook (pp. 267) See the example in the textbook (pp. 267) * AW Lecture Notes

14 Spring 2005 14 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University A Compile-time Descriptor for a Record * AW Lecture Notes

15 Spring 2005 15 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Union (Variant Record) Types Union: a type whose variables are allowed to store different type values at different times during execution Union: a type whose variables are allowed to store different type values at different times during execution Design Issues for unions: Design Issues for unions: 1. What kind of type checking, if any, must be done? 2. Should unions be integrated with records? type Node = record case Tag : Boolean of True : (Count : Integer); True : (Count : Integer); False : (Sum : Real); False : (Sum : Real); end; var Uval : Node; union Node { int count; int count; float sum; } uval; CPascal

16 Spring 2005 16 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Union (Variant Record) Types type Node = record case Tag : Boolean of True : (Count : Integer); True : (Count : Integer); False : (Sum : Real); False : (Sum : Real); end; var Uval : Node; union Node { int count; int count; float sum; } uval; int utype; CPascalCont’d Free Union No language support for type checking Discriminated Union A union construct includes a type indicator (tag or discriminant) uval: count sum uval: Count Sumtag Allocate a big enough space to hold the “widest” member

17 Spring 2005 17 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Union (Variant Record) Types type Node = record case Tag : Boolean of True : (Count : Integer); True : (Count : Integer); False : (Sum : Real); False : (Sum : Real); end; var Uval : Node; union Node { int count; int count; float sum; } uval; int utype; CPascalCont’d Free Union Discriminated Union if (utype == INT) printf(“%d”, uval.count); else if (utype == FLOAT) printf(“%f”, uval.sum); Uval.Tag := True; Uval.Count := 1341; Uval.Tag := False; var X := Uval.Sum; { ??? }  Unions are potentially unsafe constructs In Ada, all assignments to the union must include the tag value

18 Spring 2005 18 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Pointer Types Pointer Type: a type in which the variables have a range of values that consists of memory addresses and a special value, nil (null) Pointer Type: a type in which the variables have a range of values that consists of memory addresses and a special value, nil (null) A pointer can be used to access a heap-dynamic variable A pointer can be used to access a heap-dynamic variable int n = 11; int *p = 22; int *q, *r, *h; q = &n; r = NULL; h = (int *)malloc(sizeof(int)); *h = 33; int m = *h; 11 n 22 p NULL q r … 33h Dereferencing (Indirect reference)

19 Spring 2005 19 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Pointer Problems – Dangling Pointers int n = 11; int *p = 22; int *q, *r, *h; q = &n; r = NULL; h = (int *)malloc(sizeof(int)); *h = 33; r = h; free(h); 11 n 22 p NULL q r … 33 h A dangling pointer NULL

20 Spring 2005 20 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Pointer Problems – Lost Heap-dynamic Variables int n = 11; int *p = 22; int *q, *r, *h; q = &n; r = NULL; h = (int *)malloc(sizeof(int)); *h = 33; h = NULL; 11 n 22 p NULL q r … 33 h NULL A lost heap-dynamic variable Garbage, Memory Leakage

21 Spring 2005 21 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Reference Types Reference Type Variable: a constant pointer that is always implicitly dereferenced (aliasing) Reference Type Variable: a constant pointer that is always implicitly dereferenced (aliasing) C++ supports reference types C++ supports reference types Improve readability in a function that uses bi- directional variables Improve readability in a function that uses bi- directional variables int result = 100; f(result); void f(int *input) { *input += 200; } int result = 100; f(result); void f(int &input) { input += 200; }

22 Spring 2005 22 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Pointers in Java? No pointers, only references to objects (which are all on the heap) No pointers, only references to objects (which are all on the heap) No explicit deallocator (garbage collection is used) No explicit deallocator (garbage collection is used) There can be no dangling references There can be no dangling references Dereferencing is always implicit Dereferencing is always implicit class DataClass { int number = 100; String name = “ICU”; } class MainClass { void aMethod(DataClass data) { data.number += 200; data.number += 200; data.name = “Go ”+data.name; data.name = “Go ”+data.name;} void mainMethod() { DataClass input=new DataClass(); DataClass input=new DataClass(); aMethod(input); aMethod(input); System.out.println(“”+input.number); System.out.println(“”+input.number); System.out.println(input.name); System.out.println(input.name); }} * AW Lecture Notes

23 Spring 2005 23 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Reading Assignment #2 Read Section 6.9.9 of the textbook Read Section 6.9.9 of the textbook Solutions to the dangling pointer problem Solutions to the dangling pointer problem Heap management Heap management Garbage collection Garbage collection

24 Spring 2005 24 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Course Review for the Midterm Exam

25 Spring 2005 25 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Review of Chapter 1 Programming Domains Programming Domains Programming Paradigms Programming Paradigms Language Evaluation Criteria Language Evaluation Criteria Readability Factors Readability Factors Writability Factors Writability Factors Reliability Factors Reliability Factors Imperative Languages Imperative Languages Overview of Language Processors Overview of Language Processors

26 Spring 2005 26 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Review of Chapter 3 - Syntax Syntax vs. Semantics Syntax vs. Semantics Language Recognizers vs. Language Generators Language Recognizers vs. Language Generators Formal ways to define language syntax Formal ways to define language syntax Context-free Grammar Context-free Grammar Chomsky Hierarchy Chomsky Hierarchy BNF (Backus Naur Form), EBNF BNF (Backus Naur Form), EBNF Derivations Derivations Parse Trees Parse Trees Ambiguity in Grammars Ambiguity in Grammars Operator Precedence Operator Precedence Associativity of Operators Associativity of Operators Attribute Grammars Attribute Grammars

27 Spring 2005 27 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Review of Chapter 3 - Semantics Operational Semantics: states of a machine Operational Semantics: states of a machine Axiomatic Semantics: preconditions, postconditions, weakest preconditions Axiomatic Semantics: preconditions, postconditions, weakest preconditions Axiom of Assignment Axiom of Assignment Rules of Consequence Rules of Consequence Rules of Composition Rules of Composition Rules of Selection Rules of Selection Rules of Iteration Rules of Iteration

28 Spring 2005 28 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Review of Chapter 5 – Names and Bindings Design Issues for Names Design Issues for Names Sextuple of Attributes for Names: (name, address, value, type, lifetime, and scope) Sextuple of Attributes for Names: (name, address, value, type, lifetime, and scope) Binding Times Binding Times Type Bindings: Static Type Bindings, Dynamic Type Bindings, Type Inference Type Bindings: Static Type Bindings, Dynamic Type Bindings, Type Inference Binding Lifetimes: Static, Stack-Dynamic, Explicity Heap-Dynamic, Implicit Heap-Dynamic Binding Lifetimes: Static, Stack-Dynamic, Explicity Heap-Dynamic, Implicit Heap-Dynamic Type Compatibility: Name Type Compatibility, Structure Type Compatibility, Derived Types, Subtypes, Anonymous Types Type Compatibility: Name Type Compatibility, Structure Type Compatibility, Derived Types, Subtypes, Anonymous Types

29 Spring 2005 29 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Review of Chapter 5 – Scope Rules Variable Visibility Variable Visibility Nested Static Scopes Nested Static Scopes Program Blocks Program Blocks Problems in Static Scoping Problems in Static Scoping Dynamic Scopes Dynamic Scopes Scope and Lifetime Scope and Lifetime Reference Environments: a set of visible names Reference Environments: a set of visible names Named Constants: static (manifest) or dynamic constants Named Constants: static (manifest) or dynamic constants Variable Initialization Variable Initialization

30 Spring 2005 30 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Review of Web-based Languages WWW Concepts: Universal Readership, Hypertext, Client-Server Model, Searching, Format Negotiation WWW Concepts: Universal Readership, Hypertext, Client-Server Model, Searching, Format Negotiation Extended Markup Language (XML) Extended Markup Language (XML) Basic language components Basic language components XML Processors: XML Parsers, Document Type Declaration (DTD), XML Schema (XSD), eXtensible Stylesheet Language (XSL) XML Processors: XML Parsers, Document Type Declaration (DTD), XML Schema (XSD), eXtensible Stylesheet Language (XSL) Document Object Model (DOM) Document Object Model (DOM)


Download ppt "ICE1341 Programming Languages Spring 2005 Lecture #11 Lecture #11 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."

Similar presentations


Ads by Google