1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.

Slides:



Advertisements
Similar presentations
Names, Bindings, Type Checking, and Scopes
Advertisements

ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
CS 355 – Programming Languages
Names, Bindings, Type Checking, and Scopes
Names, Bindings, Type Checking, and Scopes
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
Names, Bindings, Type Checking, and Scopes
ISBN Lecture 05 Variable Attributes.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
The Concept of Variables
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
Names, Bindings, Type Checking, and Scopes
CMSC331. Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 5 Chapter 5 Variables: Names, Bindings, Type Checking and Scope.
Scope.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 © 2002 by Addison Wesley Longman, Inc Names - We discuss all user-defined names here - Design issues for names: - Maximum length? - Are.
COMP4730/2003/lec5/H.Melikian Names, Bindings,Type Checking and Scopes (Chapter 5) - Design issues: - Maximum length? - Are connector characters allowed?
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
중요 용어 설명 ( 추후 설명이 됨 ). 2 명칭과 관련된 용어의 개념  Names  Variables  The Concept of Binding  Type Checking  Strong Typing  Type Compatibility  Scope  Scope.
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Names, Bindings, Type Checking, and Scopes
COME Chapter 5 Chapter 5 Variables: Names, Bindings, Type Checking and Scope.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
ISBN Chapter 5 Names, Bindings, and Scopes.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
ISBN Chapter 5 Names, Bindings, and Scopes.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
ICOM 4036: PROGRAMMING LANGUAGES Lecture ? Naming and Scopes.
CS 363 Comparative Programming Languages Names, Type Checking, and Scopes.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names and Binding In Text: Chapter 4.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
5.2 Names - We discuss all user-defined names here
5.2 Names - We discuss all user-defined names here
Names, Bindings, Type Checking, and Scopes
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Structure of Programming Languages
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
Names, Bindings, Type Checking, and Scopes
Names and Binding In Text: Chapter 5.
Names, Bindings, and Scopes
Names, Bindings, Type Checking, and scopes
Types and Related Issues
Presentation transcript:

1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible type is either –legal for the operator, or –language rules allow it to be converted to a legal type Coercion –Automatic conversion Type error –Application of an operator to an operand of incorrect type Nearly all type checking can be static for static type bindings Type checking must be dynamic for dynamic type bindings

2 Strong Typing Strongly typed programming language –PL where type errors are always detected Advantages: –Reliability –Detection of the misuses of variables that result in type errors Disadvantages: –Increased size of code –Slower development - declarations –Reduced writability –Exception handling may be move complicated

3 Strong Typing in PLs Examples: –FORTRAN 77 is not: parameters, EQUIVALENCE –Pascal is not: variant records –C and C++ are not: parameter type checking can be avoided; unions are not type checked –Ada is, almost (UNCHECKED CONVERSION is loophole) –Java is similar to Ada Coercion rules affect strong typing –can weaken it considerably (C++ versus Ada) Java has just half the assignment coercions of C++ Java’s strong typing is still less effective than in Ada

4 Name Type Compatibility Variables have compatible types if they are either –in the same declaration –or in declarations that use the same type name Easy to implement but highly restrictive –Sub-ranges of integer are not compatible with integer! –Formal parameters must be the same type as their corresponding actual parameters (Pascal)

5 Structure Type Compatibility Variables have compatible types if their types have identical structures –More flexible –But harder to implement Because of structured types Others are much simpler Questions: –Structurally the same record types with different field names? –Array types with the same base type but different subscripts? E.g. [1..10] vs. [0..9] –Enumeration types whose components are spelled differently? With structural type compatibility, you can not differentiate between types of the same structure –E.g. different units of speed, when both are floating point)

6 Type Compatibility Examples Pascal: usually structure, but in some cases name is used (formal parameters) C: structure, except for records Ada: restricted form of name –Derived types allow types with the same structure to be different –Anonymous types are all unique, even in: A, B : array (1..10) of INTEGER See Sebesta 5.7

7 Scope The range of statements where a variable is visible, i.e. can be accessed Local variables are visible in the program unit where they are declared Non-local variables are visible in a program unit but not declared there Scope rules determine how names are associated with variables

8 Static Scope Also called Lexical Scope Based on structure of program text To connect a name to a variable, one (you or the compiler) must find the declaration Search process: –Search declarations, first locally, –Then in increasingly larger outer enclosing scopes –Until declaration for the name is found Enclosing static scopes (to a specific scope) are called its static ancestors; The nearest static ancestor is called a static parent

9 Shadowed Variables Variables are shadowed (hidden) in part of the code where there is a more immediate ancestor (closer in scope) with the same name C++ and Ada allow access to hidden variables using longer names –In Ada:. –In C++: :: Common Lisp –Packages variables accessible via longer name or package imported Java – this. –.this. – super. // shadowed field in superclass

10 Scope Blocks Static scopes can be created inside program units Started with ALGOL 60! C and C++: for (int index; … ) { int x; … } Scope block –statements in between { … }, and –condition expression in preceding or following ( … )

11 Static Scoping Diagram Example: MAIN calls A and B A calls C and D B calls A and E

12 Static Scope Problems Suppose the specs are changed so that D must access data in B Solutions: –Put D in B but then C can 't call D and D can't access A's variables –Move the data from B that D needs to MAIN but then all procedures can access them Same problem for procedure access Static scoping may encourages many global variables

13 Dynamic Scope Uses calling sequences of program units, not their textual layout –I.e. temporal versus spatial References are connected to declarations by searching back through the chain of subprogram calls in execution –I.e. search through the dynamic stack for the most recent variable with the name

14 Scope Example MAIN -declaration of x SUB1 - declaration of x -... call SUB2... SUB reference to x -... call SUB1... MAIN calls Sub1 Sub1 calls Sub2 Sub2 uses x Static scoping Reference to x is to MAIN's x Dynamic scoping Reference to x is to SUB1's x

15 Scope Evaluation Advantage of dynamic scoping –convenience –flexibility Disadvantage –poor readability –reliability

16 Testing Lexical Scoping (setf fish ’(salmon tuna)) (defun ref-fish () fish) (defun test-lexical (fish) (list fish (ref-fish)))

17 Testing Dynamic Scoping (defvar birds) (setf birds ’(eagle vulture)) (defun ref-birds () birds) (defun test-dynamic (birds) (list birds (ref-birds)))

18 Scope and Lifetime Scope and lifetime are sometimes closely related, but are different concepts Consider a static variable in a C or C++ function

19 Referencing Environments Referencing environment (RE) of a statement –all names that are visible in the statement In statically-scoped language RE is –Local variables, and –All of the visible variables in all of the enclosing scopes In Dynamically-scoped languages RE is –Local variables, and –All visible variables in all active subprograms –Active execution of the subprogram has begun and is not yet terminated