march 12, 2004 1 ice 1341 – programming languages (lecture #6) in-young ko programming languages...

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

Upload: anastasia-mason

Post on 13-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

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

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

Programming LanguagesProgramming Languages(ICE 1341)(ICE 1341)

Lecture #6Lecture #6 March 12, 2004

In-Young Koiko .AT. icu.ac.kr

Information and Communications University (ICU)

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

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

AnnouncementsAnnouncements

Check the class homepage (BBS) for the Check the class homepage (BBS) for the grading policy of HW#1grading policy of HW#1, and the , and the revised HW#2 revised HW#2 descriptiondescription

Form project groupsForm project groups 3-4 people per a group3-4 people per a group Report your group formation to TA by Friday March Report your group formation to TA by Friday March

19th19th

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

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

Review of the Previous LectureReview of the Previous Lecture

Attribute GrammarsAttribute Grammars Dynamic SemanticsDynamic Semantics

Operational SemanticsOperational Semantics Axiomatic SemanticsAxiomatic Semantics

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

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

Names, Variables, Bindings, and Names, Variables, Bindings, and TypesTypes

int counter = 100;int counter = 100;

100100

……

0x0023FF010x0023FF01

MemoryMemory

VariableVariable

• Name (Identifier): Name (Identifier): “counter”“counter”

• Type: Type: intint

• Value (R-value): Value (R-value): 100100

• Address (L-value):Address (L-value): 0x0023FF01 0x0023FF01

BindingBinding

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

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

Design Issues for NamesDesign Issues for Names

Maximum Maximum lengthlength?? If too short, they cannot be connotativeIf too short, they cannot be connotative If too long, the symbol table needs to be largeIf too long, the symbol table needs to be large e.g., e.g., FORTRAN I: max 6FORTRAN I: max 6, , COBOL: max 30COBOL: max 30, , Ada and Java: no limitAda and Java: no limit

Which Which characterscharacters are allowed, in what order? are allowed, in what order? Letters, digits and underscore characters are usually allowedLetters, digits and underscore characters are usually allowed The first character of a name must be a letter in FORTRANThe first character of a name must be a letter in FORTRAN

Are Are connector charactersconnector characters allowed? allowed? Space characters in FORTRAN (e.g., No Of Things), Underscores in C Space characters in FORTRAN (e.g., No Of Things), Underscores in C

(e.g., no_of_things)(e.g., no_of_things) ““Camel” notationCamel” notation: e.g, NoOfThings: e.g, NoOfThings

Are names Are names case sensitivecase sensitive?? Are special words Are special words reserved wordsreserved words or or keywordskeywords??

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 In Java, class names start with an uppercase letter, and variable and method names start with a lowercase lettermethod names start with a lowercase letter

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

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

VariablesVariables

A A variablevariable is an is an abstraction of a memory cellabstraction of a memory cell Variables can be characterized as a sextuple of Variables can be characterized as a sextuple of

attributes:attributes: ((namename, , addressaddress, , valuevalue, , typetype, , lifetimelifetime, and , and scopescope)) TypeType:: determines the determines the range of valuesrange of values of variables and the of variables and the set set

of operationsof operations that are defined for values of that type that are defined for values of that type Address (L-value)Address (L-value): the physical memory address: the physical memory address ValueValue (R-value)(R-value):: the the contentscontents of the of the memory memory locationlocation

AliasesAliases: multiple variables that have the same memory : multiple variables that have the same memory addressaddress e.g., pointers and reference variables in C, C++e.g., pointers and reference variables in C, C++

Sizes of a variableSizes of a variable Abstract memory cell size vs. physical memory sizeAbstract memory cell size vs. physical memory size

* AW Lecture Notes

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

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

BindingsBindings

BBindinginding:: AAn association, such as between an attribute n association, such as between an attribute and an entity, or between an operation and a symboland an entity, or between an operation and a symbol

Binding Binding TTimeimes:s: TThe timehe timess at which a binding takes at which a binding takes placeplace Language-design-time bindingsLanguage-design-time bindings (e.g., ‘*’ – multiplication) (e.g., ‘*’ – multiplication) Language-implementation-time bindingsLanguage-implementation-time bindings (e.g., int – a range of (e.g., int – a range of

possible values)possible values) Compile-time bindingsCompile-time bindings (e.g., a variable – a data type) (e.g., a variable – a data type) Load-time bindingsLoad-time bindings (e.g., a variable – a memory cell) (e.g., a variable – a memory cell) Link-time bindingsLink-time bindings (e.g., a call to a library function – subprogram (e.g., a call to a library function – subprogram

codes)codes) Run-time bindingsRun-time bindings (e.g., a variable – a value) (e.g., a variable – a value)

* AW Lecture Notes

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

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

Type BindingsType Bindings

Static Type BindingsStatic Type Bindings: : A binding occurs before run time and A binding occurs before run time and remains unchanged throughout program executionremains unchanged throughout program execution EExplicit xplicit Type DType Declarationeclaration:: DDeclaring the types of variableseclaring the types of variables by using a by using a

program statement (e.g., Pascal, C, Java)program statement (e.g., Pascal, C, Java) IImplicit mplicit Type DType Declarationeclaration:: SSpecifying types of variablespecifying types of variables based on based on

default conventionsdefault conventions Based on tBased on the he first appearancefirst appearance of the variable of the variable name name in the program in the program Based on the Based on the first letterfirst letter (symbol) of a variable name (e.g., FORTRAN, Perl) (symbol) of a variable name (e.g., FORTRAN, Perl)

Type DeclarationsType Declarations (memory allocation) vs. (memory allocation) vs. Type DefinitionsType Definitions

Dynamic Type BindingsDynamic Type Bindings:: S Specifpecifying/changing a data type ying/changing a data type through an through an

assignment statementassignment statement ( (e.g., JavaScripte.g., JavaScript)e.g., list = [2, 4.33, 6, 8]; list = 17.3;

Type InferenceType Inference:: Types are inferred from the type of constants Types are inferred from the type of constants (e.g., ML, (e.g., ML, Miranda, and HaskellMiranda, and Haskell))

e.g., fun circumf(r) = 3.14159 * r * r;

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

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

Binding LifetimesBinding Lifetimes

StaticStatic: Storage bindings: Storage bindings remain remain throughout executionthroughout executione.g. all FORTRAN 77 variables, e.g. all FORTRAN 77 variables, JavaJava static variables static variables

AdvantagesAdvantages: efficiency (direct addressing): efficiency (direct addressing);; history-sensitivehistory-sensitive subprogram supportsubprogram support

DisadvantageDisadvantage: lack of flexibility (no recursion): lack of flexibility (no recursion)

Stack-Stack-DDynamicynamic: : Storage bindings are created when Storage bindings are created when their declaration statements are elaboratedtheir declaration statements are elaborated e.g. local variables in C subprograms and Java methodse.g. local variables in C subprograms and Java methods Advantage: allows recursion; conserves storageAdvantage: allows recursion; conserves storage Disadvantages: Disadvantages: ooverhead of allocation and deallocationverhead of allocation and deallocation; ;

subprograms cannot besubprograms cannot be history sensitive history sensitive; slower accesses; slower accesses (indirect addressing)(indirect addressing)

* AW Lecture Notes

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

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

Binding Lifetimes Binding Lifetimes (cont’d)(cont’d)

Explicit Explicit HHeap-eap-DDynamicynamic: Storages are a: Storages are allocated and deallocated llocated and deallocated by explicit directives, specified by the programmer, which take by explicit directives, specified by the programmer, which take effect during executioneffect during execution

e.g. dynamic objects in C++ (via new and delete)e.g. dynamic objects in C++ (via new and delete)

all objects in Javaall objects in Java (new int[100]) (new int[100]) AdvantageAdvantage: provides for dynamic storage management: provides for dynamic storage management DisadvantageDisadvantage: inefficient and unreliable: inefficient and unreliable

Implicit Implicit HHeap-eap-DDynamicynamic: Storages are a: Storages are allocation and deallocation llocation and deallocation caused by assignment statementscaused by assignment statements

e.g. all variables in APL; all strings and arrays in Perl and JavaScripte.g. all variables in APL; all strings and arrays in Perl and JavaScript AdvantageAdvantage: flexibility: flexibility DisadvantagesDisadvantages: : iinefficient, because all attributes are dynamicnefficient, because all attributes are dynamic; l; loss of oss of

error detectionerror detection by the compiler by the compiler

* AW Lecture Notes

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

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

Type CheckingType Checking

Type Type CCheckinghecking:: the activity of ensuring that the the activity of ensuring that the operands of an operator are of compatible operands of an operator are of compatible typestypes

CCompatible ompatible TTypeype:: a typea type that is either legal for that is either legal for anan operator, or is allowed under language rules operator, or is allowed under language rules to be implicitly convertedto be implicitly converted to a legal typeto a legal type ((coercioncoercion))

TType ype EErrorrror:: the application of an operator to an the application of an operator to an operand of an inappropriate typeoperand of an inappropriate type

A programming language is A programming language is strongly typedstrongly typed if if type errors are always detectedtype errors are always detected

* AW Lecture Notes

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

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

Type CompatibilityType Compatibility

Name Name TType ype CCompatibilityompatibility: : two variables have two variables have compatible types if they are in either the same compatible types if they are in either the same declaration or in declarations that use the same type declaration or in declarations that use the same type namename Easy to implement but Easy to implement but highly restrictivehighly restrictive

Subranges of integer types are not compatible with integer typesSubranges of integer types are not compatible with integer typese.g.,e.g., type Indextype is 1..100;type Indextype is 1..100;

count: Integer;count: Integer;

index: Indextype;index: Indextype; Formal parameters must be the same type as their corresponding Formal parameters must be the same type as their corresponding

actual parameters (Pascal)actual parameters (Pascal)

Structure Structure TType ype CCompatibilityompatibility: : two variables have two variables have compatible types if their types have identical structurescompatible types if their types have identical structures More flexible, but harder to implementMore flexible, but harder to implement

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

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

Type Compatibility Type Compatibility (cont’d)(cont’d)

Derived TypeDerived Type: a new type that is based on some : a new type that is based on some previously defined typepreviously defined type Derive types inherit all the properties of their parent typesDerive types inherit all the properties of their parent types Derived types allow types with the same structure to be differentDerived types allow types with the same structure to be different

e.g., e.g., typetype celsius celsius is newis new Float; Float;

typetype fahrenheit fahrenheit is newis new Float; Float;

SubtypeSubtype: a : a range-constrainedrange-constrained version of an existing type version of an existing type A subtype is compatible with its parent typeA subtype is compatible with its parent type

e.g., e.g., subtypesubtype Small_type Small_type isis Integer Integer rangerange 0..99; 0..99;

Anonymous TypeAnonymous Type: Unnamed type: Unnamed type Anonymous types are all unique, even inAnonymous types are all unique, even in

e.g., C, D : e.g., C, D : arrayarray (1..10) (1..10) ofof Integer; Integer;