Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 (ICU)

2 Spring 2005 2 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Binding Lifetimes (cont.) Binding Lifetimes (cont.) WWW Concepts WWW Concepts WWW Languages WWW Languages XML (Extended Markup Language) XML (Extended Markup Language) Last Lecture

3 Spring 2005 3 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University This Lecture Scope Rules Scope Rules 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

4 Spring 2005 4 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University An XML-based Programming Language <program> 0 0 n 1 n 1 n n <assignment> val val n n n 1 n 1 </assignment> </function> result 5 result 5 </program>

5 Spring 2005 5 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University An Example class MyClass { int myVar1 = 100, myVar2 = 200; void myMethod1() { System.out.println(myVar1); System.out.println(myVar1); { int myVar1 = 200; { int myVar1 = 200; System.out.println(myVar1); System.out.println(myVar1); System.out.println(this.myVar1); System.out.println(this.myVar1); } System.out.println(myVar1); System.out.println(myVar1);} class InnerClass { int myVar1 = 300; int myVar1 = 300; void myMethod2() { void myMethod2() { System.out.println(myVar1); System.out.println(myVar1); System.out.println(myVar2); System.out.println(MyClass.this.myVar1); }}} How many different variables are defined in the program? How many different variables are defined in the program? In which program units the variables are visible? In which program units the variables are visible? What are the current values of the variables? What are the current values of the variables?

6 Spring 2005 6 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Scope Scope of a Variable: the range of statements over which it is visible Scope of a Variable: the range of statements over which it is visible Visibility: a variable is visible in a statement if it can be referenced in that statement Visibility: a variable is visible in a statement if it can be referenced in that statement Scope Rules of a Language: determine how references to names are associated with variables Scope Rules of a Language: determine how references to names are associated with variables Non-local Variables: variables that are visible but not declared in a program unit (block) Non-local Variables: variables that are visible but not declared in a program unit (block) Static Scope: the scope of a variable that can be determined prior to execution Static Scope: the scope of a variable that can be determined prior to execution

7 Spring 2005 7 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University An Example of Nested Static Scopes class MyClass { int myVar1 = 100, myVar2 = 200; void myMethod1() { System.out.println(myVar1); System.out.println(myVar1); { int myVar1 = 200; { int myVar1 = 200; System.out.println(myVar1); System.out.println(myVar1); System.out.println(this.myVar1); System.out.println(this.myVar1); } System.out.println(myVar1); System.out.println(myVar1);} class InnerClass { int myVar1 = 300; int myVar1 = 300; void myMethod2() { void myMethod2() { System.out.println(myVar1); System.out.println(myVar1); System.out.println(myVar2); System.out.println(MyClass.this.myVar1); }}} The scope of myVar1 and myVar2 The scope of the local variable, myVar1 A block for defining the local variable, myVar1 myVar2 is a non-local variable in this block InnerClass is the static parent of myVar2InnerClass is the static parent of myVar2 MyClass is the static ancestor of myVar2MyClass is the static ancestor of myVar2 Accessing the hidden variable, myVar1

8 Spring 2005 8 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Program Blocks A method of creating static scopes inside program units A method of creating static scopes inside program units Introduced in ALGOL 60 Introduced in ALGOL 60 Block-structured Languages Block-structured Languages Examples: Examples: C and C++: for (...) { C and C++: for (...) { int index; int index;...... } Ada: declare LCL : FLOAT; Ada: declare LCL : FLOAT; begin begin...... end end * AW Lecture Notes

9 Spring 2005 9 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Problems in Static Scoping (1) Required procedure access: MAIN calls A and B MAIN calls A and B A calls C and D A calls C and D B calls A and E B calls A and E Program Structure Desirable Calls Potential Calls

10 Spring 2005 10 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Problems in Static Scoping (2) Suppose the spec is changed so that D must now access some data in B Suppose the spec is changed so that D must now access some data in B Solutions: Solutions: Put D in B (but then C can no longer call it and D cannot access A's variables) Put D in B (but then C can no longer call it and D cannot access A's variables) Move the data from B that D needs to MAIN (but then all procedures can access them) Move the data from B that D needs to MAIN (but then all procedures can access them) Same problem for procedure access Same problem for procedure access Overall: static scoping often encourages many globals Overall: static scoping often encourages many globals * AW Lecture Notes

11 Spring 2005 11 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Dynamic Scope The scope of a variable is determined based on calling sequences of program units, not their textual layout (temporal versus spatial) The scope of a variable is determined based on calling sequences of program units, not their textual layout (temporal versus spatial) * AW Lecture Notes Advantage: convenience Advantage: convenience Disadvantage: poor readability Disadvantage: poor readability Reference to x is to SUB1's x MAIN - declaration of x SUB1 SUB1 - declaration of x - - declaration of x -...... call SUB2 call SUB2...... SUB2 SUB2...... - reference to x - - reference to x -...... call SUB1 call SUB1 … MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x e.g.,

12 Spring 2005 12 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Other Scoping Issues Scope and Lifetime Scope and Lifetime In Java, the scope of a variable is spatial (textual), and the lifetime of the variable is temporal In Java, the scope of a variable is spatial (textual), and the lifetime of the variable is temporal In Java, a static variable in a method is statically bound to its scope, and is also statically bound to storage In Java, a static variable in a method is statically bound to its scope, and is also statically bound to storage Reference Environments Reference Environments The referencing environment of a statement is the collection of all names that are visible in the statement The referencing environment of a statement is the collection of all names that are visible in the statement In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms * AW Lecture Notes

13 Spring 2005 13 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Other Scoping Issues Named constant: a variable that is bound to a value only when it is bound to storage Named constant: a variable that is bound to a value only when it is bound to storage Advantages: readability and modifiability Advantages: readability and modifiability Used to parameterize programs Used to parameterize programs The binding of values to named constants can be either static (called manifest constants) or dynamic The binding of values to named constants can be either static (called manifest constants) or dynamic Pascal: literals only Pascal: literals only FORTRAN 90: constant-valued expressions FORTRAN 90: constant-valued expressions Ada, C++, and Java: expressions of any kind Ada, C++, and Java: expressions of any kind Variable Initialization: the binding of a variable to a value at the time it is bound to storage Variable Initialization: the binding of a variable to a value at the time it is bound to storage If the storage binding of a variable is dynamic, initialization is also dynamic If the storage binding of a variable is dynamic, initialization is also dynamic * AW Lecture Notes

14 Spring 2005 14 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Chapter 6

15 Spring 2005 15 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Data Types A data type defines a collection of data objects and a set of predefined operations on those objects A data type defines a collection of data objects and a set of predefined operations on those objects Primitive Data Types: data types that are not defined in terms of other types (e.g., int, float, char) Primitive Data Types: data types that are not defined in terms of other types (e.g., int, float, char) Structured Data Types: non-scalar data types that specify structured organizations of data of other types (e.g., arrays, records) Structured Data Types: non-scalar data types that specify structured organizations of data of other types (e.g., arrays, records) Design issues for all data types: Design issues for all data types: What is the syntax of references to variables? What is the syntax of references to variables? What operations are defined and how are they specified? What operations are defined and how are they specified?

16 Spring 2005 16 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Primitive Data Types – Numeric Types (1) Integer Integer Its representation usually reflects the hardware (size, notation – ones or twos complement) Its representation usually reflects the hardware (size, notation – ones or twos complement)twos complementtwos complement e.g., Java – byte, short, int, long; C – unsigned int e.g., Java – byte, short, int, long; C – unsigned intJava CJava C Floating-point Floating-point Model real numbers, but only as approximations Model real numbers, but only as approximations IEEE Floating-Point Standard 754 format IEEE Floating-Point Standard 754 format IEEE Floating-Point Standard 754 IEEE Floating-Point Standard 754 e.g., Java – float, double e.g., Java – float, double Precision: the accuracy of the fractional part of a value Precision: the accuracy of the fractional part of a value Range: a combination of the ranges of fractions and exponents Range: a combination of the ranges of fractions and exponents

17 Spring 2005 17 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Primitive Data Types – Numeric Types (2) Decimal Decimal Store a fixed number of decimal digits (coded), with decimal point at a fixed position Store a fixed number of decimal digits (coded), with decimal point at a fixed position BCD (Binary Coded Decimal) BCD (Binary Coded Decimal) BCD (Binary Coded Decimal) BCD (Binary Coded Decimal) Advantage: accuracy (e.g., 0.1 represented in BCD vs. Floating-point) Advantage: accuracy (e.g., 0.1 represented in BCD vs. Floating-point) Disadvantages: limited range, wastes memory Disadvantages: limited range, wastes memory Boolean Boolean Could be implemented as bits, but often as bytes Could be implemented as bits, but often as bytes Advantage: readability Advantage: readability Character Character Stored as numeric codings (e.g., ASCII, Unicode) Stored as numeric codings (e.g., ASCII, Unicode)ASCIIUnicodeASCIIUnicode * AW Lecture Notes

18 Spring 2005 18 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Character String Types Character String: a sequence of characters Character String: a sequence of characters Design issues: Design issues: Is it a primitive type or just a special kind of array? Is it a primitive type or just a special kind of array? e.g., Java – the String class, C – character arrays (char *str) Is the length of strings static or dynamic? Is the length of strings static or dynamic? Static length strings – e.g., Java, Fortran Static length strings – e.g., Java, Fortran Limited dynamic length strings – e.g., C (sizeof vs. strlen) Limited dynamic length strings – e.g., C (sizeof vs. strlen) Dynamic length strings – e.g., JavaScript, Perl Dynamic length strings – e.g., JavaScript, Perl Operations: Operations: Assignment, Comparison (e.g., strcmp, >), Catenation (e.g., strcat, +), Substring reference, Pattern matching (e.g., indexOf, matches) Assignment, Comparison (e.g., strcmp, >), Catenation (e.g., strcat, +), Substring reference, Pattern matching (e.g., indexOf, matches)matches * AW Lecture Notes

19 Spring 2005 19 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Implementation of String Types Static length - compile-time descriptor Static length - compile-time descriptor Limited dynamic length - may need a run-time descriptor for length (C and C++ use ‘\0’ to indicate the end of the string) Limited dynamic length - may need a run-time descriptor for length (C and C++ use ‘\0’ to indicate the end of the string) Dynamic length - need run-time descriptor; allocation/deallocation is the biggest implementation problem Dynamic length - need run-time descriptor; allocation/deallocation is the biggest implementation problem * AW Lecture Notes Compile-time descriptor for static strings Run-time descriptor for limited dynamic strings

20 Spring 2005 20 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University User-Defined Ordinal Types An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers e.g., Java – int, char, boolean Enumeration Types: user-defined ordinal types in which named constants (enumeration constants) are provided in the definition Enumeration Types: user-defined ordinal types in which named constants (enumeration constants) are provided in the definition e.g., enum days { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; days myDay = Sat; days myDay = Sat; char *str = names[Tue]; char *str = names[Tue]; C++, C#, Pascal, Ada support enumeration types C++, C#, Pascal, Ada support enumeration types Improve readability and reliability (restricting ranges) Improve readability and reliability (restricting ranges)

21 Spring 2005 21 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Design Issues of User-Defined Ordinal Types Reuse of constants (overloaded literals) Reuse of constants (overloaded literals) e.g., enum weekendDays { Sat, Sun }; ? e.g., enum weekendDays { Sat, Sun }; ? The type of an occurrence of a constant needs to be determined based on its context The type of an occurrence of a constant needs to be determined based on its context Ada supports overloaded literals Ada supports overloaded literals Coercion to integer Coercion to integer e.g., myDay++; ? e.g., myDay++; ? myDay = 4; ? myDate = (days)4; ? myDay = 4; ? myDate = (days)4; ? C++ enumeration types are coerced to integer C++ enumeration types are coerced to integer

22 Spring 2005 22 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Subrange Types (Subtypes) Contiguous sequences of an ordinal type Contiguous sequences of an ordinal type e.g., subtype Index is Integer range 1..100; type Days is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun); type Days is ( Mon, Tue, Wed, Thu, Fri, Sat, Sun); subtype Weekdays is Days range Mon..Fri; subtype Weekdays is Days range Mon..Fri; Pascal and Ada support subrange types Pascal and Ada support subrange types Subtypes are not new types, just constrained existing types (so they are compatible); c.f., Derived Types Subtypes are not new types, just constrained existing types (so they are compatible); c.f., Derived Types e.g., type Derived_Int is new Integer range 1..100; subtype Subrange_Int is Integer range 1..100; subtype Subrange_Int is Integer range 1..100; Enhance readability and reliability Enhance readability and reliability

23 Spring 2005 23 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Array Types Array: an aggregate of homogeneous data elements in which an individual element is identified by its position (subscript or index) Array: an aggregate of homogeneous data elements in which an individual element is identified by its position (subscript or index) Indexing: a mapping from indices to elements Indexing: a mapping from indices to elements e.g., A(2), A[2], *(A + 2) e.g., A(2), A[2], *(A + 2) Subscript Types: Subscript Types: FORTRAN, C, Java – integer only FORTRAN, C, Java – integer only Pascal, Ada – any ordinal type (integer, boolean, char, enum) Pascal, Ada – any ordinal type (integer, boolean, char, enum) … 01n-1n An element Subscripts (Indices) A:

24 Spring 2005 24 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 … 01n-1n A Fixed heap-dynamic (Fortran 90, Java) … 011819 A Fixed stack-dynamic (Ada) … 01n-1x A Heap-dynamic (Perl, JavaScript)

25 Spring 2005 25 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

26 Spring 2005 26 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

27 Spring 2005 27 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

28 Spring 2005 28 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Homework #4 Write a simple Java program that includes: Write a simple Java program that includes: At least two non-local variables defined with initialization At least two non-local variables defined with initialization At least two static variables defined in different scopes At least two static variables defined in different scopes At least two overwriting variables defined in different scopes At least two overwriting variables defined in different scopes At least two accesses of hidden variables At least two accesses of hidden variables Two named constants, one with the static value binding, and the other with the dynamic value binding Two named constants, one with the static value binding, and the other with the dynamic value binding Add comments to indicate where those variables are Add comments to indicate where those variables are Explain the static scopes of the variables Explain the static scopes of the variables Explain the lifetimes of the variables Explain the lifetimes of the variables Explain the reference environments at three different locations in the program Explain the reference environments at three different locations in the program Due by March 31st


Download ppt "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."

Similar presentations


Ads by Google