Chapter 5 Names, Bindings, Type Checking, and Scopes.

Slides:



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

Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Names and Bindings.
Chapter 5 Names, Bindings, and Scopes
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Names, Bindings, Type Checking, and Scopes
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
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.
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 and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Names, Bindings, and Scopes
Names, Bindings, and Scopes
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.
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 Chapter 5 Names Bindings Type Checking Scope. 2 High-Level Programming Languages Two main goals:Two main goals: –Machine independence –Ease of programming.
1 CS Programming Languages Class 07 September 14, 2000.
Names Variables Type Checking Strong Typing Type Compatibility 1.
Names, Bindings, Type Checking, and Scopes
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
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.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
ISBN Chapter 5 Names, Bindings, and Scopes.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
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
Names, Bindings, 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
Presentation transcript:

Chapter 5 Names, Bindings, Type Checking, and Scopes

contents Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence Scope Scope and Lifetime Referencing Environments Named Constants

Introduction Imperative programming languages are abstractions of von Neumann computer architecture Memory: stores both instruction and data Processor: provides operations modifying the contents of the memory. Variables are the abstraction of memory cells Variables are characterized by attributes To design a type, must consider scope, lifetime, type checking, initialization, and type compatibility

Relation with special character and reserved words Names A Name is a string of characters used to indentify some entity in a program. Length Length Case Sensitivity Relation with special character and reserved words (Ex. variables, formal paramètres, methodes, etc.) Meaning: number of characters in the string. Types of length: (1) limited such as (a) COBOL: maximum 30 (b) FORTRAN 90 : maximum 31 (2) Unlimited such as Ada, C#, and JAVA

Relation with special character and reserved words Names Length Case Sensitivity Relation with special character and reserved words Meaning: distinguish between capital letters and small letters. Types: some languages are (1) case sensitive such as C, and Java 2) not case sensitive such as Ada, and Pascal. Disadvantage of case sensitivity: readability (names that look alike are different)

Relation with special character and reserved words Names Length Case Sensitivity Relation with special character and reserved words Reserved Word (RW) Meaning: A reserved word is a special word that cannot be used as a user- defined name. Example: RW in Java: if, for, while, return,….. Disadvantage: if the language contains large number of RW then many collisions occur. (e.g., COBOL has 300 reserved words!) Special Characters(SC) Meaning: Other than letters and digits. Example: In some languages, we use the SC in the name. (1) PHP: all variable names must begin with $. (2) Perl: all variable names begin with special characters, which specify the variable’s type.

Relation with special character and reserved words Names Length Case Sensitivity Relation with special character and reserved words Keywords(KW) Meaning: A keyword is a word that is special only in certain contexts, Example: In Fortran, Real VarName (Real is a data type followed with a name, therefore Real is a keyword). Real = 3.4 (Real is a variable).

Variables Meaning: A variable is a named location in memory that is used to hold a value that can be modified by the program. Attributes: Name Address Value Lifetime Type Scope Name : not all variables have them.

The same variable can be associated with Attributes: Name Address Type Value Lifetime Scope Address Meaning: the memory address with which it is associated The same name may be associated with different addresses at different places and at different times in the program The same variable can be associated with different addresses at different times during program execution

Example: integer type in Java Attributes: Name Address Type Value Lifetime Scope Type Meaning: determines the range of values of variables and the set of operations that are defined for values of that type. Example: integer type in Java Range of values :-2,147,483,648 .. 2,147,483,647 Set of Operations: +, -, *, Mod, Div int n; 703 704 705 706 707 708 n

The l-value of a variable is its address Attributes: Name Address Type Value Lifetime Scope Value Meaning: the contents of the location with which the variable is associated. The l-value of a variable is its address The r-value of a variable is its value

The Concept of Binding Meaning: A binding is the association of data/code with an identifier or an association between an attribute and an entity, or between an operation and a symbol Binding time is the time at which a binding takes place. Language design time : bind operator symbols to operations (ex. * is bound to multiplication) Language implementation time: bind floating point type to a representation (in java float represents as 4 bytes according to IEEE-754 Compile time: bind a variable to a type in C or Java (ex. int n  n is bound integer type at compilation ) 4. Load time: bind a C or C++ static variable to a memory cell) (i.e. allocate in memory) 5. Runtime: bind a non-static local variable to a memory cell. (ex. object in Java; a value is bound to variable at runtime) Time of Binding

Example: int count; count = count + 5; - set of possible types for count: bound at language design time - type of count: bound at compile time - set of possible values of count: bound at language implementation time -- value of count: bound at execution time - set of possible meaning for the operator +: bound at language design time - Meaning of the operator symbol '+': bound at compile time - Internal representation of literal 5: bound at language implementation

Binding of Attributes to Variables Static: A binding is static if it first occurs before run time and remains unchanged throughout program execution. Dynamic: A binding is dynamic if it first occurs during execution or can change during execution of the program

Type Binding Before a variable can be referenced in a program it must be bound to a type. We have two key issues in binding a type to an identifier •A variable must be bound to a data type before it can be referenced •When does the binding take place? –static or dynamic binding Static Type Binding How is a type specified? When does the binding take place? Dynamic Type Binding

done through explicit or implicit declaration. Static Type Binding How is a type specified? done through explicit or implicit declaration. An explicit declaration is a program statement used for declaring the types of variables (a statement in a program that lists variable names and specifies their types). Ex. int sum=0; (most language require explicit dec.) An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program). (means of associating variables with types through default conventions. ) •An identifier not explicitly declared is implicitly declared according to the following convention : –Identifiers beginning with I, J, K, L, M, or N are implicitly declared to be Integer type – Otherwise, they are Real type. Example of : Implicit Declaration in Fortran

Implicit Declaration in Perl Static Type Binding Example of : Implicit Declaration in Perl Implicit Declaration in Perl •Any name begins with $ is a scalar, which can store either a string or a numeric value •A name begins with @ is an array •A name begins with a % is a hash structure Advantage: writability Disadvantage: reliability (less trouble with Perl)

Ex. for dynamic type binding in JavaScript list = [2, 4.33, 6, 8]; A variable is bound to a type when it is assigned a value in an assignment statement (at run time, take the type of the value on the right-hand side) Ex. for dynamic type binding in JavaScript list = [2, 4.33, 6, 8]; list = 17.3; Advantage: flexibility (generic program units) Disadvantages: High cost (dynamic type checking and interpretation) i.e. dynamically typed languages are often implement in interpretation, because the overhead of type checking is not the bottleneck. Type error detection by the compiler is difficult.

When does the binding take place? (How does one associate memory locations to variables? ) Allocation - getting a cell from some pool of available cells. i.e. the process of binging a variable to a memory cell that is taken from a pool of available memory. Deallocation - putting a cell back into the pool. I.e. the process of unbinding a variable and placing its memory cell back in the pool of available memory.

Categories of Variables by Lifetimes Attributes: Name Address Type Value Lifetime Scope The lifetime of a variable is the time during which it is bound to a particular memory cell. . Categories of Variables by Lifetimes

Categories of Variables by Lifetimes (1) Static variables: bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. int f(int x) { static int a = 0; a += 2; printf("%d\n",a); } f(2) --> 2 f(4) ---> 4

Static Variables Advantages: efficiency (direct addressing), •variables bound to memory cells before execution begins and remains bound to the same memory cell throughout execution Advantages: efficiency (direct addressing), history-sensitive subprogram support int f(int x) { static int a = 0 a += 2; printf("%d\n",a); } f(2) --> 2 f(4) --->4 Disadvantage: lack of flexibility (no recursion). lifetime = entire program execution. e.g., C and C++ static variables #include <stdio.h> int sum; int fun1(int a, float b) { static int cnt; int i, j; … }

lifetime = while the subprogram is active (2) Stack-dynamic: Storage bindings are created for variables when their declaration statements are elaborated. I.e. A declaration is elaborated when the executable code associated with it is executed) e.g. local variables in C subprograms and Java methods lifetime = while the subprogram is active Advantage: allows recursion; conserves storage Disadvantages: Overhead of allocation and deallocation Subprograms cannot be history sensitive Inefficient references (indirect addressing) int sum; int fun1(int a, float b) { static int cnt; int i, j; … }

Categories of Variables by Lifetimes (3)Explicit heap-dynamic variables: are nameless (abstract) memory cells that are allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution. Referenced only through pointers or references, e.g. dynamic objects in C++ (via new and delete), all objects in Java. C: int *intptr; intptr = (int *) malloc(1 * sizeof(int)); free(intptr); Advantage:high flexibility (provides for dynamic storage management) Disadvantage: inefficient and unreliable. lifetime = from explicit allocation to explicit deallocation

Categories of Variables by Lifetimes (4) Implicit heap-dynamic: Allocation and deallocation caused by assignment statements. all strings and arrays in Perl, JavaScript, and PHP Advantage: flexibility (generic code) Disadvantages: Inefficient, because all attributes are dynamic Loss of error detection lifetime = from implicit allocation to implicit deallocation

Type Checking Generalize the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the operands of an operator are of compatible types A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler- generated code, to a legal type This automatic conversion is called a coercion. A type error is the application of an operator to an operand of an inappropriate type

If all type bindings are static, nearly all type checking can be static If type bindings are dynamic, type checking must be dynamic A programming language is strongly typed if type errors are always detected Advantage of strong typing: allows the detection of the misuses of variables that result in type errors

Strong Typing Language examples: FORTRAN 95 is not: parameters, EQUIVALENCE C and C++ are not: parameter type checking can be avoided; unions are not type checked Ada is, almost (UNCHECKED CONVERSION is loophole) (Java and C# are similar to Ada)

Coercion rules strongly affect strong typing--they can weaken it considerably (C++ versus Ada) Although Java has just half the assignment coercions of C++, its strong typing is still far less effective than that of Ada

Variable Attributes: Scope The scope of a variable is the range of statements over which it is visible The nonlocal variables of a program unit are those that are visible but not declared there The scope rules of a language determine how references to names are associated with variables

Static Scope Based on program text To connect a name reference to a variable, you (or the compiler) must find the declaration Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent Some languages allow nested subprogram definitions, which create nested static scopes (e.g., Ada, JavaScript, and PHP)

Scope (continued) Variables can be hidden from a unit by having a "closer" variable with the same name C++ and Ada allow access to these "hidden" variables In Ada: unit.name In C++: class_name::name

Blocks A method of creating static scopes inside program units- -from ALGOL 60 Examples: C-based languages: while (...) { int index; ... } Ada: declare Temp : Float; begin end

Evaluation of Static Scoping Assume MAIN calls A and B A calls C and D B calls A and E MAIN A C D B E E The tree structure the program The structure of a program

Static Scope Example The graph of the desirable calls in the program MAIN MAIN A B A B C D E C D E The graph of the desirable calls in the program The potential call graph of the program

Suppose the spec is changed so that D must now access some data in B Solutions: 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) Same problem for procedure access Overall: static scoping often encourages many global

Dynamic Scope Based on calling sequences of program units, not their textual layout (temporal versus spatial) References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point

Scope Example Big calls Sub1; Sub1 calls Sub2; Sub2 uses X Big - declaration of X Sub1 - declaration of X - ... call Sub2 Sub2 - reference to X - call Sub1 … Procedure Big is X : integer; Procedure Sub1 is begin -- of Sub1 - - end; - - of Sub1 Procedure Sub2 is begin -- of Sub2 …. X … end; - - of Sub 2 begin - - of Big - - - end; - - of Big

Scope Example Static scoping Dynamic scoping Reference to X is to Big's X Dynamic scoping Reference to X is to Sub1's X Evaluation of Dynamic Scoping: Advantage: convenience (called subprogram is executed in the context of the caller) Disadvantage: poor readability

Referencing Environments 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 A subprogram is active if its execution has begun but has not yet terminated In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms

Named Constants A named constant is a variable that is bound to a value only when it is bound to storage Advantages: readability and modifiability Used to parameterize programs The binding of values to named constants can be either static (called manifest constants) or dynamic Languages: FORTRAN 95: constant-valued expressions Ada, C++, and Java: expressions of any kind C# has two kinds, readonly and const - the values of const named constants are bound at compile time - The values of readonly named constants are dynamically bound

Variable Initialization The binding of a variable to a value at the time it is bound to storage is called initialization Initialization is often done on the declaration statement, e.g., in Java int sum = 0;

Summary Case sensitivity and the relationship of names to special words represent design issues of names Variables are characterized by the sextuples: name, address, value, type, lifetime, scope Binding is the association of attributes with program entities Scalar variables are categorized as: static, stack dynamic, explicit heap dynamic, implicit heap dynamic Strong typing means detecting all type errors