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.
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
Scope.
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.
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.
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.
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
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
Presentation transcript:

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

Introduction High-level languages provide an abstration level that separates language features from the hardware Imperative languages are abstractions of von Neumann architecture A machine consists of Memory - stores both data and instructions Processor - can modify the contents of memory Mention abstract memory cells Copyright © 2007 Addison-Wesley. All rights reserved.

Names A name is a character string used to represent something else need a way to refer to variables, functions, user- defined types, labeled statements, … Design issues for names: Maximum length? What characters are allowed? Are names case sensitive? C, C++, and Java names are case sensitive this is not true of other languages Are special words reserved words or keywords? case sensitivity => readability issues functions provide another level of abstraction Copyright © 2007 Addison-Wesley. All rights reserved.

Length of Names If too short, they cannot be connotative Language examples: FORTRAN I: maximum 6 COBOL: maximum 30 FORTRAN 90 and ANSI C: maximum 31 Ada and Java: no limit, and all are significant C++: no limit, but implementers often impose one Copyright © 2007 Addison-Wesley. All rights reserved.

Keywords vs Reserved Words Words that have a special meaning in the language A keyword is a word that is special only in certain contexts, e.g., 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) A reserved word is a special word that cannot be used as a user-defined name most reserved words are also keywords keywords separate syntactic entities; aid readability Copyright © 2007 Addison-Wesley. All rights reserved.

Variables A variable is an abstraction of a memory cell For primitive types correspondence is direct For structured types (objects, arrays) things are more complicated Variables can be characterized as a sextuple of attributes: Name Address Value Type Lifetime Scope Copyright © 2007 Addison-Wesley. All rights reserved.

Variable Attributes Name - not all variables have them Address - the memory address with which it is associated (l-value)‏ Type - allowed range of values of variables and the set of defined operations Value - the contents of the location with which the variable is associated (r- value) Stack variables have different addresses at different times Two names used to access the same variable are aliases - Created using pointers, reference variables, unions, EQUIVALENCE - harmful to readability Copyright © 2007 Addison-Wesley. All rights reserved.

The Concept of Binding A binding is an association between a name and the "object" it is bound to e.g. between an attribute and an entity, or between an operation and a symbol entity could be a variable or a function or even a class Binding time is the time at which a binding takes place. Copyright © 2007 Addison-Wesley. All rights reserved.

Possible Binding Times Language design time -- bind operator symbols to operations Language implementation time-- bind floating point type to a representation, I/O coupling Program-writing time -- Compile time -- bind a variable to a type in C or Java Link time -- Load time -- bind a FORTRAN 77 variable to a memory cell (or a C static variable)‏ Runtime -- bind a nonstatic local variable to a memory cell binding primitive type to representation happens at language design time in Java scripting languages bind type at run time Copyright © 2007 Addison-Wesley. All rights reserved.

Static and Dynamic Binding A binding is static if it first occurs before run time and remains unchanged throughout program execution. early binding is more efficient A binding is dynamic if it first occurs during execution or can change during execution of the program late binding provides flexibility Give some examples of static and dynamic bindings in Java compiled languages usually have static type binding Copyright © 2007 Addison-Wesley. All rights reserved.

Type Binding How is a type specified? An explicit declaration is a program statement used for declaring the types of variables An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program)‏ When does the binding take place? If static, the type may be specified by either an explicit or an implicit declaration Java uses explicit declarations scripting languages use implicit declaration (Scheme, ruby)‏ mention FORTRAN - name implies type Perl - $, @, % imply scalar, array, hash Type inference: ML, Haskell, Miranda Copyright © 2007 Addison-Wesley. All rights reserved.

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 insures that a program obeys type-compatibility rules 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 A type error is the application of an operator to an operand of an inappropriate type Copyright © 2007 Addison-Wesley. All rights reserved.

When is type checking done? If all type bindings are static, nearly all type checking can be static (done at compile time)‏ If type bindings are dynamic, type checking must be dynamic (done at run time)‏ A programming language is strongly typed if type errors are always detected Advantage: allows the detection of misuse of variables that result in type errors Copyright © 2007 Addison-Wesley. All rights reserved.

How strongly typed? FORTRAN 77 is not: parameters, EQUIVALENCE Pascal has variant records C and C++ parameter type checking can be avoided unions are not type checked Ada is almost UNCHECKED CONVERSION is loophole Java is similar Copyright © 2007 Addison-Wesley. All rights reserved.

Coercion The automatic conversion between types is called coercion. Coercion rules they can weaken typing considerably C and C++ allow both widening and narrowing coercions Java allows only widening coercions Java's strong typing is still far less effective than that of Ada Copyright © 2007 Addison-Wesley. All rights reserved.

Dynamic Type Binding Dynamic Type Binding (Perl, JavaScript and PHP, Scheme)‏ Specified through an assignment statement e.g., JavaScript list = [2, 4.33, 6, 8]; list = 17.3; This provides a lot of flexibility But … How do you do any type checking? Most functional languages and scripting languages; interpreters bind everything dynamically Advantage: flexibility (generic program units)‏ Disadvantages: High cost (dynamic type checking and interpretation)‏ Type error detection by the compiler is difficult Copyright © 2007 Addison-Wesley. All rights reserved.

Storage Bindings & Lifetime The lifetime of a variable is the time during which it is bound to a particular memory cell Allocation - getting a cell from some pool of available cells Deallocation - putting a cell back into the pool The object a variable is bound to may have a different lifetime than the variable itself Depending on the language, allocation can be either controlled by the programmer or done automatically Copyright © 2007 Addison-Wesley. All rights reserved.

Categories of Variables by Lifetimes Static -- lifetime is same as that of the program Stack -- lifetime is duration of subprogram more efficient use of memory than static Heap -- lifetime is less predictable Explicit allocation and deallocation specified by the programmer Implicit -- Allocation and deallocation managed by run-time system 1: Fortran, C static; efficient; no recursion 2: Local variables in C, Java; efficient memory usage, recursion possible; overhead for allocation, deallocation, indirect addressing 3: Pointer variables in C, objects in Java; allows programmer control; error prone 4: Strings and arrays in Perl, JavaScript; flexible; inefficient Copyright © 2007 Addison-Wesley. All rights reserved.

Variable 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 Scope and lifetime are sometimes closely related, but are different concepts Class and instance variables in Java Copyright © 2007 Addison-Wesley. All rights reserved.

Approaches to Scope Only allow one global scope (Basic)‏ Allow global and local Scope (Fortran)‏ Allow nested scopes (Ada, Pascal)‏ Copyright © 2007 Addison-Wesley. All rights reserved.

The current referencing environment is the set of currently active bindings at a particular point in the program Copyright © 2007 Addison-Wesley. All rights reserved.

Static Scope The scope of a variable can be determined from the 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 Block scope Copyright © 2007 Addison-Wesley. All rights reserved.

Scope and Shadowed Variables Variables can be hidden from a unit by having a "closer" variable with the same name Sometimes say there is a hole in the scope scope resolution operator (in some languages)‏ C++, Java and Ada allow access to some of these "hidden" variables In Ada: unit.name In C++: class_name::name or ::name for globals In Java: this.name for variables declared at class level Namespaces Copyright © 2007 Addison-Wesley. All rights reserved.

Static Scoping and Nested Functions Assume MAIN calls A and B A calls C and D B calls A and E MAIN E A C D B Copyright © 2007 Addison-Wesley. All rights reserved.

Nested Functions and Maintainability 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 Copyright © 2007 Addison-Wesley. All rights reserved.

Comments Using nested functions with static scoping often encourages many globals not considered to be good programming practice Current thinking is that we can accomplish the same thing better with modules (classes)‏ Static scoping is still preferred to the alternative Most current languages use static scoping at the block level even if they don't allow nested functions Copyright © 2007 Addison-Wesley. All rights reserved.

Nested Functions in Practice Scheme, JavaScript, Ada and PHP allow you to nest functions Java allows you to nest classes scoping rules are similar to those of nested functions Copyright © 2007 Addison-Wesley. All rights reserved.

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 This is easy to implement but it makes programs hard to follow Used in APL, SNOBOL, early versions of LISP Perl allows you to use dynamic scope for selected variables Copyright © 2007 Addison-Wesley. All rights reserved.

Scope Example MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x MAIN - declaration of x SUB1 - declaration of x - ... call SUB2 SUB2 - reference to x - call SUB1 … MAIN calls SUB1 SUB1 calls SUB2 SUB2 uses x Copyright © 2007 Addison-Wesley. All rights reserved.

Scope Example Static scoping Dynamic scoping Reference to x is to MAIN's x Dynamic scoping Reference to x is to SUB1's x Evaluation of Dynamic Scoping: Advantage: convenience Disadvantage: poor readability Copyright © 2007 Addison-Wesley. All rights reserved.

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 In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms A subprogram is active if its execution has begun but has not yet terminated Copyright © 2007 Addison-Wesley. All rights reserved.