Type Bindings. Static Type Binding

Similar documents
9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Chapter 5 Names, Binding, Type Checking and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5. Names, Bindings, and Scopes

Chapter 5. Variables. Topics. Imperative Paradigm. Von Neumann Architecture

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

Programming Languages, Summary CSC419; Odelia Schwartz

Imperative Programming

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Names, Bindings, Scopes

CSC 533: Organization of Programming Languages. Spring 2005

Binding and Variables

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

NOTE: Answer ANY FOUR of the following 6 sections:

Lecture 4 Memory Management

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Types. What is a type?

Chapter 6. Data Types ISBN

11/29/17. Outline. Subprograms. Subroutine. Subroutine. Parameters. Characteristics of Subroutines/ Subprograms

Name, Scope, and Binding. Outline [1]

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Concepts of Programming Languages

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Chapter 9 Subprograms

Concepts Introduced in Chapter 7

Semantic Analysis. Compiler Architecture

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Names, Scopes, and Bindings II. Hwansoo Han

Fundamentals of Programming Languages

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Lecture 16: Static Semantics Overview 1

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

COSC252: Programming Languages: Basic Semantics: Data Types. Jeremy Bolton, PhD Asst Teaching Professor

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Chapter 10. Implementing Subprograms ISBN

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Properties of an identifier (and the object it represents) may be set at

LECTURE 14. Names, Scopes, and Bindings: Scopes

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Programming Languages: Lecture 11

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Expressions & Assignment Statements

Types and Type Inference

The role of semantic analysis in a compiler

Chapter 7. Expressions and Assignment Statements

IEEE LANGUAGE REFERENCE MANUAL Std P1076a /D3

Software II: Principles of Programming Languages. Why Expressions?

Chapter 9. Subprograms

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Programmiersprachen (Programming Languages)

Informatica 3 Syntax and Semantics

Topic 9: Type Checking

Topic 9: Type Checking

Programming Languages Third Edition. Chapter 7 Basic Semantics

TYPES, VALUES AND DECLARATIONS

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Organization of Programming Languages CS3200 / 5200N. Lecture 06

CS 314 Principles of Programming Languages

Chapter 5 Names, Bindings, Type Checking, and Scopes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Chapter 9. Subprograms

Chapter 7. Expressions and Assignment Statements ISBN

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

MIDTERM EXAM (Solutions)

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Implementing Subprograms

Chapter 7 Expressions and Assignment statements

CSE 431S Type Checking. Washington University Spring 2013

CSCE 314 Programming Languages. Type System

Programming Languages: Lecture 12

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Implementing Subprograms

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

MIDTERM EXAMINATION - CS130 - Spring 2005

Chapter 10 Implementing Subprograms

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Concepts of Programming Languages (750321)

Types and Type Inference

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

G Programming Languages - Fall 2012

9. Subprograms. 9.2 Fundamentals of Subprograms

CSCI312 Principles of Programming Languages!

Lecture 12: Data Types (and Some Leftover ML)


Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Expression Evaluation and Control Flow. Outline

Final-Term Papers Solved MCQS with Reference

COS 140: Foundations of Computer Science

Programming Methodology

Transcription:

Type Bindings Two key issues in binding (or associating) a type to an identifier: How is type binding specified? When does the type binding take place? N. Meng, S. Arthur 1 Static Type Binding An explicit declaration is a program statement that lists variable names and specifies their types var x: int Advantage: safer, cheaper Disadvantage: less flexible N. Meng, S. Arthur 2 1

Static Type Binding An implicit declaration is a means of associating variables with types through default conventions, rather than declaration statements First use of variable: X := 1.2; X is a float and will not change afterwards N. Meng, S. Arthur 3 Static Type Binding Default rules In Fortran, if an undeclared identifier begins with one of the letters I, J, K, L, M, or N, or their lower case versions, it is implicitly declared to be Integer type Advantage: convenience Disadvantage: reliability N. Meng, S. Arthur 4 2

Dynamic Type Binding The type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name Instead, the variable is bound to a type when it is assigned a value in an assignment statement E.g., list = [10. 2, 3.5]; (JavaScript) Regardless of its previous type, list has the new type of single-dimension array of length 2 N. Meng, S. Arthur 5 Dynamic Type Binding Advantage flexibility (can change type dynamically) Disadvantage Type error detection by the compiler is difficult High cost Type checking must be done at runtime Every variable must have a runtime descriptor to maintain the current type The storage used for the value of a variable must be of varying size N. Meng, S. Arthur 6 3

Dynamic Type Binding Type Inference (ML, Miranda, and Haskell) Rather than by assignment statement, types are determined from the context of the reference N. Meng, S. Arthur 7 Type Checking Type checking is the activity of ensuring that the operands of an operator are of compatible types The definition can be generalized to include Subprograms (argument types, return type), and Assignments N. Meng, S. Arthur 8 4

Type Checking A compatible type is one that is legal for the operator, or is allowed under language rules to be implicitly converted to a legal type The automatic conversion is called (implicit) coercion Mixed mode arithmetic (2 + 3.5) N. Meng, S. Arthur 9 Type Error A type error is the application of an operator to an operand of an inappropriate type 1.5 + Just say NO! to UVA N. Meng, S. Arthur 10 5

Type Checking If all bindings of variables to types are static in a language, then type checking can nearly always be done statically Dynamic type binding requires type checking at runtime, which is called dynamic type checking Dynamic type binding only allows dynamic type checking N. Meng, S. Arthur 11 Type Checking Type checking is complicated when a language allows a memory cell to store values of different types at different times during execution E.g., C and C++ unions In such cases, type checking must be dynamic Even though all variables are statically bound to types, not all type errors can be detected by static type checking N. Meng, S. Arthur 12 6

Type Checking It is better to detect errors at compile time than at runtime The earlier correction is usually less costly Penalty for static checking Reduced programmer flexibility Fewer shortcuts and tricks are possible N. Meng, S. Arthur 13 Strong Typing A programming language is strongly typed if type errors are always detected Advantages of strong typing Ability to detect all misuses of variables that result in type errors N. Meng, S. Arthur 14 7

Language Comparison for Strong Typing FORTRAN 95 is not strongly typed the use of Equivalence between variables of different types allows a variable of one type to refer to a value of a different type C and C++ are not strongly typed Both include union types, which are not type checked N. Meng, S. Arthur 15 Language Comparison for Strong Typing Ada, Java, and C# are almost strongly typed It allows programmers to breach the typechecking rules by specially requesting that type checking be suspended for a particular type conversion ML is strongly typed N. Meng, S. Arthur 16 8

Coercion Rules Coercion rules can weaken strong typing E.g., int a = 3, b = 5; float d = 4.5; If a developer meant to type a + b, but mistakenly typed a + d, the error would not be detected by the compiler due to coercion Languages with more coercion are less reliable than those with little coercion Reliability comparison Fortran/C/C++ < Ada C++ < Java/C# N. Meng, S. Arthur 17 Type Compatibility The rules dictate the type of operands that are acceptable for each operator and thereby specify the possible type errors of the language Type rules are called compatibility because in some cases, the type of an operand can be implicitly converted by the compiler or runtime system to make it acceptable to the operator N. Meng, S. Arthur 18 9

Type Equivalence A strict form of type compatibility compatibility without coercion Two approaches to defining type equivalence Name type equivalence (Type equivalence by name) Structure type equivalence (Type equivalence by structure) N. Meng, S. Arthur 19 Name Type Equivalence Two variables have equivalent types if they are defined in the same declaration or in declarations using the same type name Ex. 1, int a, b; Ex. 2, int a; int b; N. Meng, S. Arthur 20 10

Name Type Equivalence Easy to implement but is more restrictive In Ada type Indextype is 1..100; count : Integer; index: IndexType; The type of count is a subrange of the integers, which is not equivalent to the integer type The two variables cannot be assigned to each other N. Meng, S. Arthur 21 Name Type Equivalence In Pascal Type X: array[1..5] of integer Y: X; Procedure K(J: array[1..5] of integer K(Y) /* Y incompatible with J */ Although J and X have the same type structure, they are considered as two types Y cannot be passed as a valid parameter to call K N. Meng, S. Arthur 22 11

Structure Type Equivalence Two variables have equivalent types if their types have identical structures Ex 1., type celsius = float; fahrenheit = float; The two types are considered equivalent N. Meng, S. Arthur 23 Structure Type Equivalence More flexible, but harder to implement The entire structures of two types must be compared Developers are not allowed to differentiate between types with the same structure N. Meng, S. Arthur 24 12

Scope The scope of a variable is the range of statements over which its declaration is visible A variable is visible in a statement if it can be referenced in that statement The nonlocal variables of a program unit or block are those that are visible but not declared in the unit Global versus nonlocal N. Meng, S. Arthur 25 Scope The scope rules of a language determine how a particular occurrence of a name is associated with a variable They determine how references to variables declared outside the currently executing subprogram or block are associated with their declarations Two types of scope Static/lexical scope Dynamic scope N. Meng, S. Arthur 26 13

Static Scope The scope of a variable can be statically determined, that is, prior to execution Two categories of static-scoped languages Languages allowing nested subprograms: Ada, JavaScript, and PHP Languages which does not allow subprograms: C-based languages N. Meng, S. Arthur 27 Static Scope To connect a name reference to a variable, you must find the appropriate declaration Search process 1. search the declaration locally 2. If not found, search the next-larger enclosing unit 3. Loop over step 2 until a declaration is found or an undeclared variable error is detected N. Meng, S. Arthur 28 14

Static Scope Given a subprogram Sub1, the subprogram that declared Sub1 is called its static parent the static parent of Sub1, its static parent, and so forth up to and including the largest enclosing subprogram, are called static ancestors of Sub1 N. Meng, S. Arthur 29 An Example (Ada) 1. procedure Big is 2. X : Integer; 3. procedure Sub1 is 4. X: Integer; 5. begin -- of Sub1 6. 7. end; -- of Sub1 8. procedure Sub2 is 9. begin -- of Sub2 10. X 11. end; -- of Sub2 12. begin -- of Big 13. 14. end; -- of Big Which declaration does X in line 10 refer to? N. Meng, S. Arthur 30 15

Variable Hiding Variables can be hidden from a unit by having a closer variable with the same name Closer means more immediate enclosing scope C++ and Ada allow access to the hidden variables (using fully qualified names) scope.name Blocks can be used to create new static scopes inside subprograms N. Meng, S. Arthur 31 Dynamic Scope Dynamic scoping is based on the calling sequence of subprograms, not on their spatial relationship to each other Dynamic scope can be determined only at runtime Always used in interpreted languages, which usually does not have type checking at compile time N. Meng, S. Arthur 32 16

An Example (Common Lisp) [2] (setq x 3) ; declare lexical scoping with setq (defun foo () x) (let ((x 4)) (foo)) ; returns 3 (defvar x 3) ; declare dynamic scoping with defvar (defun foo () x) (let ((x 4)) (foo)) ; returns 4 When foo goes to find the value of x, it initially finds the lexical value defined at the top level ( setq x 3 or defvar x 3 ) it checks if the variable is dynamic If it is, then foo looks to the calling environment, and uses 4 as x value N. Meng, S. Arthur 33 Advantages Static vs. Dynamic Scoping Static scoping 1. Readability 2. Locality of reasoning 3. Less runtime overhead Dynamic scoping Some extra convenience (minimal parameter passing) Disadvantages Less flexibility 1. Loss of readability 2. Unpredictable behavior 3. More runtime N. Meng, S. Arthur overhead 34 17

Scope and Lifetime The scope and lifetime of a variable appear to be related The scope defines how a name is associated with a variable The lifetime of a variable is the time during which the variable is bound to a specific memory location N. Meng, S. Arthur 35 Scope and Lifetime Consider a variable v declared in a Java method that contains no method calls The scope of v is from its declaration to the end of the method The lifetime of v begins when the method is entered and ends when execution of the method terminates The scope and lifetime seem to be related N. Meng, S. Arthur 36 18

Scope and Lifetime In C and C++, a variable is declared in a function using the specifier static The scope is static and local to the function The lifetime extends over the entire execution of the program of which it is a part Static scope is a textual and spatial concept, while lifetime is a temporal concept N. Meng, S. Arthur 37 Another Example void printheader() { } void compute() { } int sum; printheader(); What is the static scope of sum? What is the lifetime of sum? N. Meng, S. Arthur 38 19

Referencing Environments Referencing environments of a statement is the collection of all variables that are visible in the statement N. Meng, S. Arthur 39 Referencing environments in staticscoped languages The variables declared in the local scope plus the collection of all variables of its ancestor scopes that are visible, excluding variables in nonlocal scopes that are hidden by declarations in nearer procedures N. Meng, S. Arthur 40 20

An Example 1. procedure Example is 2. A, B : Integer; 3. ß -------------------------1 4. procedure Sub1 is 5. X, Y: Integer; 6. begin -- of Sub1 7. ß ----------------2 8. end; -- of Sub1 9. procedure Sub2 is 10. X: Integer; 11. begin -- of Sub2 12. ß ----------------3 13. end; -- of Sub2 14. begin -- of Example 15. ß -----------------------4 16. end; -- of Example What are the referencing environments of the indicated program points? Point RE 1. A and B of Example 2. A and B of Example, X and Y of Sub1 3. 4. N. Meng, S. Arthur 41 Referencing environments in dynamic-scoped languages A subprogram is active if its execution has begun but has not yet terminated The referencing environments of a statement in a dynamically scoped language is the locally declared variables, plus the variables of all other subprograms that are currently active Some variables in active previous subprograms can be hidden by variables with the same names in recent ones N. Meng, S. Arthur 42 21

1. void sub1() { 2. int a, b; 3. ß -------------------------1 4. } /* end of sub1 */ 5. void sub2() { 6. int b, c; 7. ß -------------------2 8. sub1(); 9. } /* end of sub2 */ 10.void main() { 11. int c, d; 12. ß ----------------3 13. sub2(); 14.} /* end of main */ An Example What are the referencing environments of the indicated program points? N. Meng, S. Arthur 43 The meaning of names within a scope Within a scope, Two or more names that refer to the same object at the same program point are called aliases E.g., int a =3; int* p = &a, q = &a; A name that can refer to more than one object at a given point is considered overloaded E.g., print_num(){ }, print_num(int n){ } E.g., complex + complex, complex + float N. Meng, S. Arthur 44 22

Reference [1] Robert W. Sebesta, Concepts of Programming Languages, 8 th edition, pg. 201-240 [2] Dynamic and Lexical variables in Common Lisp, http://stackoverflow.com/questions/ 463463/dynamic-and-lexical-variables-incommon-lisp N. Meng, S. Arthur 45 Can be a test question program foo; var x: integer; procedure f; begin print(x); end f; procedure g; var x: integer; begin x := 2; f; end g; begin x := 1; g; end foo. What value is printed? Evaluate with static scoping: x = 1 Evaluate with dynamic scoping: x = 2 N. Meng, S. Arthur 46 23