Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 12, 2004 1 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)

Similar presentations


Presentation on theme: "March 12, 2004 1 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)"— Presentation transcript:

1 March 12, 2004 1 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341) Lecture #6 March 12, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT. icu.ac.kr

2 March 12, 2004 2 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Announcements Check the class homepage (BBS) for the grading policy of HW#1, and the revised HW#2 description Check the class homepage (BBS) for the grading policy of HW#1, and the revised HW#2 description Form project groups Form project groups 3-4 people per a group 3-4 people per a group Report your group formation to TA by Friday March 19th Report your group formation to TA by Friday March 19th

3 March 12, 2004 3 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Review of the Previous Lecture Attribute Grammars Attribute Grammars Dynamic Semantics Dynamic Semantics Operational Semantics Operational Semantics Axiomatic Semantics Axiomatic Semantics

4 March 12, 2004 4 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Names, Variables, Bindings, and Types int counter = 100; 100 … 0x0023FF01 Memory Variable Name (Identifier): “counter”Name (Identifier): “counter” Type: intType: int Value (R-value): 100Value (R-value): 100 Address (L-value): 0x0023FF01Address (L-value): 0x0023FF01 Binding

5 March 12, 2004 5 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Design Issues for Names Maximum length? Maximum length? If too short, they cannot be connotative If too short, they cannot be connotative If too long, the symbol table needs to be large If too long, the symbol table needs to be large e.g., FORTRAN I: max 6, COBOL: max 30, Ada and Java: no limit e.g., FORTRAN I: max 6, COBOL: max 30, Ada and Java: no limit Which characters are allowed, in what order? Which characters are allowed, in what order? Letters, digits and underscore characters are usually allowed Letters, digits and underscore characters are usually allowed The first character of a name must be a letter in FORTRAN The first character of a name must be a letter in FORTRAN Are connector characters allowed? Are connector characters allowed? Space characters in FORTRAN (e.g., No Of Things), Underscores in C (e.g., no_of_things) Space characters in FORTRAN (e.g., No Of Things), Underscores in C (e.g., no_of_things) “Camel” notation: e.g, NoOfThings “Camel” notation: e.g, NoOfThings Are names case sensitive? Are names case sensitive? Are special words reserved words or keywords? Are special words reserved words or keywords? e.g., int float = 100; e.g., int float = 100; Naming convention? Naming convention? In Java, class names start with an uppercase letter, and variable and method names start with a lowercase letter In Java, class names start with an uppercase letter, and variable and method names start with a lowercase letter

6 March 12, 2004 6 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Variables A variable is an abstraction of a memory cell A variable is an abstraction of a memory cell Variables can be characterized as a sextuple of attributes: (name, address, value, type, lifetime, and scope) Variables can be characterized as a sextuple of attributes: (name, address, value, type, lifetime, and scope) Type: determines the range of values of variables and the set of operations that are defined for values of that type Type: determines the range of values of variables and the set of operations that are defined for values of that type Address (L-value): the physical memory address Address (L-value): the physical memory address Value (R-value): the contents of the memory location Value (R-value): the contents of the memory location Aliases: multiple variables that have the same memory address Aliases: multiple variables that have the same memory address e.g., pointers and reference variables in C, C++ e.g., pointers and reference variables in C, C++ Sizes of a variable Sizes of a variable Abstract memory cell size vs. physical memory size Abstract memory cell size vs. physical memory size * AW Lecture Notes

7 March 12, 2004 7 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Bindings Binding: An association, such as between an attribute and an entity, or between an operation and a symbol Binding: An association, such as between an attribute and an entity, or between an operation and a symbol Binding Times: The times at which a binding takes place Binding Times: The times at which a binding takes place Language-design-time bindings (e.g., ‘*’ – multiplication) Language-design-time bindings (e.g., ‘*’ – multiplication) Language-implementation-time bindings (e.g., int – a range of possible values) Language-implementation-time bindings (e.g., int – a range of possible values) Compile-time bindings (e.g., a variable – a data type) Compile-time bindings (e.g., a variable – a data type) Load-time bindings (e.g., a variable – a memory cell) Load-time bindings (e.g., a variable – a memory cell) Link-time bindings (e.g., a call to a library function – subprogram codes) Link-time bindings (e.g., a call to a library function – subprogram codes) Run-time bindings (e.g., a variable – a value) Run-time bindings (e.g., a variable – a value) * AW Lecture Notes

8 March 12, 2004 8 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Type Bindings Static Type Bindings: A binding occurs before run time and remains unchanged throughout program execution Static Type Bindings: A binding occurs before run time and remains unchanged throughout program execution Explicit Type Declaration: Declaring the types of variables by using a program statement (e.g., Pascal, C, Java) Explicit Type Declaration: Declaring the types of variables by using a program statement (e.g., Pascal, C, Java) Implicit Type Declaration: Specifying types of variables based on default conventions Implicit Type Declaration: Specifying types of variables based on default conventions Based on the first appearance of the variable name in the program Based on the first appearance of the variable name in the program Based on the first letter (symbol) of a variable name (e.g., FORTRAN, Perl) Based on the first letter (symbol) of a variable name (e.g., FORTRAN, Perl)  Type Declarations (memory allocation) vs. Type Definitions Dynamic Type Bindings: S pecifying/changing a data type through an assignment statement (e.g., JavaScript Dynamic Type Bindings: S pecifying/changing a data type through an assignment statement (e.g., JavaScript ) e.g., list = [2, 4.33, 6, 8]; list = 17.3; Type Inference: Types are inferred from the type of constants (e.g., ML, Miranda, and Haskell) Type Inference: Types are inferred from the type of constants (e.g., ML, Miranda, and Haskell) e.g., fun circumf(r) = 3.14159 * r * r;

9 March 12, 2004 9 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Binding Lifetimes Static: Storage bindings remain throughout execution Static: Storage bindings remain throughout execution e.g. all FORTRAN 77 variables, Java static variables Advantages: efficiency (direct addressing); history-sensitive subprogram support Advantages: efficiency (direct addressing); history-sensitive subprogram support Disadvantage: lack of flexibility (no recursion) Disadvantage: lack of flexibility (no recursion) Stack-Dynamic: Storage bindings are created when their declaration statements are elaborated Stack-Dynamic: Storage bindings are created when their declaration statements are elaborated e.g. local variables in C subprograms and Java methods e.g. local variables in C subprograms and Java methods Advantage: allows recursion; conserves storage Advantage: allows recursion; conserves storage Disadvantages: overhead of allocation and deallocation; subprograms cannot be history sensitive; slower accesses (indirect addressing) Disadvantages: overhead of allocation and deallocation; subprograms cannot be history sensitive; slower accesses (indirect addressing) * AW Lecture Notes

10 March 12, 2004 10 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Binding Lifetimes (cont’d) Explicit Heap-Dynamic: Storages are allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution Explicit Heap-Dynamic: Storages are allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution e.g. dynamic objects in C++ (via new and delete) all objects in Java (new int[100]) all objects in Java (new int[100]) Advantage: provides for dynamic storage management Advantage: provides for dynamic storage management Disadvantage: inefficient and unreliable Disadvantage: inefficient and unreliable Implicit Heap-Dynamic: Storages are allocation and deallocation caused by assignment statements Implicit Heap-Dynamic: Storages are allocation and deallocation caused by assignment statements e.g. all variables in APL; all strings and arrays in Perl and JavaScript Advantage: flexibility Advantage: flexibility Disadvantages: inefficient, because all attributes are dynamic; loss of error detection by the compiler Disadvantages: inefficient, because all attributes are dynamic; loss of error detection by the compiler * AW Lecture Notes

11 March 12, 2004 11 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Type Checking Type Checking: the activity of ensuring that the operands of an operator are of compatible types Type Checking: the activity of ensuring that the operands of an operator are of compatible types Compatible Type: a type that is either legal for an operator, or is allowed under language rules to be implicitly converted to a legal type (coercion) Compatible Type: a type that is either legal for an operator, or is allowed under language rules to be implicitly converted to a legal type (coercion) Type Error: the application of an operator to an operand of an inappropriate type Type Error: the application of an operator to an operand of an inappropriate type A programming language is strongly typed if type errors are always detected A programming language is strongly typed if type errors are always detected * AW Lecture Notes

12 March 12, 2004 12 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Type Compatibility Name Type Compatibility: two variables have compatible types if they are in either the same declaration or in declarations that use the same type name Name Type Compatibility: two variables have compatible types if they are in either the same declaration or in declarations that use the same type name Easy to implement but highly restrictive Easy to implement but highly restrictive Subranges of integer types are not compatible with integer types Subranges of integer types are not compatible with integer types e.g.,type Indextype is 1..100; count: Integer; index: Indextype; Formal parameters must be the same type as their corresponding actual parameters (Pascal) Formal parameters must be the same type as their corresponding actual parameters (Pascal) Structure Type Compatibility: two variables have compatible types if their types have identical structures Structure Type Compatibility: two variables have compatible types if their types have identical structures More flexible, but harder to implement More flexible, but harder to implement

13 March 12, 2004 13 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Type Compatibility (cont’d) Derived Type: a new type that is based on some previously defined type Derived Type: a new type that is based on some previously defined type Derive types inherit all the properties of their parent types Derive types inherit all the properties of their parent types Derived types allow types with the same structure to be different Derived types allow types with the same structure to be different e.g., type celsius is new Float; type fahrenheit is new Float; type fahrenheit is new Float; Subtype: a range-constrained version of an existing type Subtype: a range-constrained version of an existing type A subtype is compatible with its parent type A subtype is compatible with its parent type e.g., subtype Small_type is Integer range 0..99; Anonymous Type: Unnamed type Anonymous Type: Unnamed type Anonymous types are all unique, even in Anonymous types are all unique, even in e.g., C, D : array (1..10) of Integer;


Download ppt "March 12, 2004 1 ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)"

Similar presentations


Ads by Google