Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISBN 0-321-33025-0 Chapter 5 Names, Bindings, Type Checking, and Scopes.

Similar presentations


Presentation on theme: "ISBN 0-321-33025-0 Chapter 5 Names, Bindings, Type Checking, and Scopes."— Presentation transcript:

1 ISBN 0-321-33025-0 Chapter 5 Names, Bindings, Type Checking, and Scopes

2 Dr. Muhammed Al-MulhemICS 410 – Programming Languages2 Names Design issues for names: –Maximum length? –Are connector characters allowed? –Are names case sensitive? –Are special words reserved words or keywords?

3 Dr. Muhammed Al-MulhemICS 410 – Programming Languages3 Names (Cont’d) Length –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

4 Dr. Muhammed Al-MulhemICS 410 – Programming Languages4 Names (Cont’d) Connectors –Pascal, Modula-2, and FORTRAN 77 don't allow –Others do

5 Dr. Muhammed Al-MulhemICS 410 – Programming Languages5 Names (Cont’d) Case sensitivity –C, C++, and Java names are case sensitive. –Other languages are not. –Disadvantage: readability (names that look alike are different) –Worse in C++ and Java because predefined names are mixed case (e.g. IndexOutOfBoundsException )

6 Dr. Muhammed Al-MulhemICS 410 – Programming Languages6 Names (Cont’d) Special words –An aid to readability; used to delimit or separate statement clauses –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.

7 Dr. Muhammed Al-MulhemICS 410 – Programming Languages7 Variables A variable is an abstraction of a memory cell Variables can be characterized by: Name Address Value Type Lifetime Scope

8 Dr. Muhammed Al-MulhemICS 410 – Programming Languages8 Variables Attributes Name – discussed before. Address - the memory address with which it is associated –A variable may have different addresses at different times during execution –A variable may have different addresses at different places in a program –If two variable names can be used to access the same memory location, they are called aliases

9 Dr. Muhammed Al-MulhemICS 410 – Programming Languages9 Variables Attributes (Cont’d) Type - determines the range of values of variables and the set of operations that are defined for values of that type; in the case of floating point, type also determines the precision. Value - the contents of the location with which the variable is associated.

10 Dr. Muhammed Al-MulhemICS 410 – Programming Languages10 The Concept of Binding A binding is an association, such as between an attribute and an entity, or between an operation and a symbol Binding time is the time at which a binding takes place.

11 Dr. Muhammed Al-MulhemICS 410 – Programming Languages11 Possible Binding Times Language design time -- bind operator symbols to operations ( Symbol (*) bound to multiplication) Language implementation time-- bind int to a range of possible values. Compile time -- bind a variable to a type

12 Dr. Muhammed Al-MulhemICS 410 – Programming Languages12 Static and Dynamic Binding A binding is static if it first occurs before run time and remains unchanged throughout program execution. A binding is dynamic if it first occurs during execution or can change during execution of the program.

13 Dr. Muhammed Al-MulhemICS 410 – Programming Languages13 Type Binding Before a variable can be referenced in a program, it must be bound to a data type. Types can be specified statically, dynamically, or by type inference.

14 Dr. Muhammed Al-MulhemICS 410 – Programming Languages14 Static type binding Static type binding can be done by either an explicit or an implicit declaration. 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 FORTRAN, PL/I, and BASIC provide implicit declarations –Advantage: writability –Disadvantage: reliability

15 Dr. Muhammed Al-MulhemICS 410 – Programming Languages15 Dynamic Type Binding With dynamic type binding, the type of a variable is not specified by a declaration statement, nor implicitly. It is Specified through an assignment statement e.g., JavaScript list = [2, 4.33, 6, 8]; list = 17; –Advantage: flexibility (generic program units) –Disadvantages: High cost Type error detection by the compiler is difficult (reliability).

16 Dr. Muhammed Al-MulhemICS 410 – Programming Languages16 Variable Attributes (Cont’d) Type Inferencing (ML, Miranda, and Haskell) –Rather than by assignment statement, types are determined from the context of the reference. For example, the function declaration fun circumf ( r )= 3.14159 * r * r; –The type of both the parameter and the return value are inferred from the type of the constant in the expression (real) For example, the function declaration fun square ( x )= x * x; –The type of both the parameter and the return value are inferred from * operator (arithmetic and the default is int).

17 Dr. Muhammed Al-MulhemICS 410 – Programming Languages17 Variable Attributes (Cont’d) Storage binding of a variable is the process of binding a variable to a memory cell. This is done by: –Allocation - getting a cell from some pool of available cells –Deallocation - putting a cell back into the pool Lifetime of a variable is the time during which it is bound to a particular memory cell. Variables are categorized according to their lifetime into four categories: –Static –Stack-dynamic –Explicit heap-dynamic –Implicit heap-dynamic

18 Dr. Muhammed Al-MulhemICS 410 – Programming Languages18 Categories of Variables by Lifetimes Static variables– are those bound to memory cells before execution begins and remains bound to the same memory cells until program execution terminates. Static variables have several applications: –Globally accessible variables are often used thought the execution of the program, thus making it necessary to have them bound to the same storage during that execution. –To are variables that are declared in subprograms be history-sensitive; that is, have them retain values between separate execution of the subprogram.

19 Dr. Muhammed Al-MulhemICS 410 – Programming Languages19 Static variables Advantages of static variables –Efficiency - all addressing of static variables can be direct; other kind of variables often require indirect addressing, which is slower. –No run-time overhead for allocation and deallocation of static variables. Disadvantages of static variables –lack of flexibility – recursive subprogram cannot be supported. –Storage cannot be shared among variables. For example, a program with two subprograms, both require large arrays. Suppose that the two subprograms are never active at the same time. If the arrays are static, they cannot share the same storage for their arrays.

20 Dr. Muhammed Al-MulhemICS 410 – Programming Languages20 Static Variables All FORTRAN 77 variables are static variables. C and C++ allow programmers to include the static specifier in a functions, making the variables it defines static variables.

21 Dr. Muhammed Al-MulhemICS 410 – Programming Languages21 Stack-Dynamic Stack-dynamic--Storage bindings are created for variables when their declaration statements are elaborated. Elaboration of declaration refers to the storage allocation, which takes place when execution reaches the declaration code. It occurs during run-time For example, the variable declarations that appear at the beginning of a Java method are elaborated when the method is called and the variables defined by those declaration are deallocated when the method completes its execution.

22 Dr. Muhammed Al-MulhemICS 410 – Programming Languages22 Stack-Dynamic Advantage: –Allows recursion. –Conserves storage. Disadvantages: –Overhead of allocation and deallocation. –Subprograms cannot be history sensitive. –Inefficient references (indirect addressing).

23 Dr. Muhammed Al-MulhemICS 410 – Programming Languages23 Explicit heap-dynamic Explicit heap-dynamic variables are memory cells that are allocated and deallocated by explicit directives, specified by the programmer. These variables are allocated and deallocated to the heap. Referenced only through pointers or references, e.g. dynamic objects in C++ (via new and delete), all objects in Java. The heap is a collection of storage cells whose organization is highly disorganized because of the unpredictability of its use.

24 Dr. Muhammed Al-MulhemICS 410 – Programming Languages24 Explicit heap-dynamic For example, in C++, the allocation operator (new) uses a type name as an operand. –When executed, an explicit heap-dynamic variable of the operand type is created and a pointer to it is returned. –Because an Explicit heap-dynamic variable is bound to a type at compile time, that binding is static. –The variable is bound to storage at the time they are created, which is during run time. –Consider the C++ code segment int *node;//create a pointer node = new int;//create the heap dynamic variable. delete node;//deallocate the variable

25 Dr. Muhammed Al-MulhemICS 410 – Programming Languages25 Explicit heap-dynamic In Java, all data except the primitive scalars are objects. Java objects are explicitly heap-dynamic and are accessed through reference variables (using new). Java has no way of explicitly destroying a heap-dynamic variable—it uses implicit garbage collection. Advantage: –Used for dynamic data structures, such as linked lists and trees, that need to grow and shrink during execution. Disadvantage: –The difficulty of using pointer and reference variables correctley. –The complexity of the storage management implementation.

26 Dr. Muhammed Al-MulhemICS 410 – Programming Languages26 Implicit heap-dynamic Implicit heap-dynamic variables are bound to heap storage only when they are assigned values through assignment statements. They are just names that adapt to whatever value they are assigned. For example, consider the JavaScript code list = [10, 3]; list = 50;

27 Dr. Muhammed Al-MulhemICS 410 – Programming Languages27 Implicit heap-dynamic Advantage: –Highest degree of flexibility, allowing generic code to be written. Disadvantages: –Inefficient — run-time overhead of maintaining all the dynamic attributes, such as array subscript types and ranges. –Loss of error detection by the compiler.

28 Dr. Muhammed Al-MulhemICS 410 – Programming Languages28 Type Checking 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

29 Dr. Muhammed Al-MulhemICS 410 – Programming Languages29 Strong Typing 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. Language examples: –FORTRAN 95 is not: EQUIVALENCE-allows a variable of one type to refer to a value of another type. –Pascal is not: variant records. –C and C++ are not: unions types are not type checked. –Java is not: Types can be explicitly cast.

30 Dr. Muhammed Al-MulhemICS 410 – Programming Languages30 Type Compatibility There are two types of compatibility methods: 1.Name type compatibility 2.Structure type compatibility

31 Dr. Muhammed Al-MulhemICS 410 – Programming Languages31 Name Type Compatibility Name type compatibility means the two variables have compatible types if they are in either the same declaration or in declarations that use the same type name. Easy to implement but highly restrictive: –subranges of integer types are not compatible with integer types. Example: Assume Ada used name compatibility, consider the following Ada declaration type Indextype is 1..100; count : Integer; index : Indextype; The variables count and index would not be compatible; count could not be assigned to index or vice versa.

32 Dr. Muhammed Al-MulhemICS 410 – Programming Languages32 Structure Type Compatibility Structure type compatibility means that two variables have compatible types if their types have identical structures. More flexible, but harder to implement: –Under name type compatibility, only two type names must be compared to determine compatibility –Under structure type compatibility, the entire structure of the two types must be compared.

33 Dr. Muhammed Al-MulhemICS 410 – Programming Languages33 Type Compatibility (Cont’d) Consider the problem of two structured types: –Are two record types compatible if they are structurally the same but use different field names? –Are two array types compatible if they are the same except that the subscripts are different? (e.g. [1..10] and [0..9]) –Are two enumeration types compatible if their components are spelled differently?

34 Dr. Muhammed Al-MulhemICS 410 – Programming Languages34 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 variables declared outside the currently executing subprogram or block are associated with their declaration.

35 Dr. Muhammed Al-MulhemICS 410 – Programming Languages35 Static Scope Based on program text, can be determined at compile time. 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 There are two categories of static scoped languages: –Nested subprograms –Blocks

36 Dr. Muhammed Al-MulhemICS 410 – Programming Languages36 Static Scope Ada, JavaScript, and PHP allow nested subprogram. Consider the following Ada procedure: Procedure Big is X : Integer; Procedure Sub1 is Begin -- of Sub1 … X … End; -- of Sub1 Procedure Sub2 is X : Integer; Begin -- of Sub2 … End; -- of Sub2 Begin -- of Big … End; -- of Big

37 Dr. Muhammed Al-MulhemICS 410 – Programming Languages37 Blocks –A method of creating static scopes inside program units—introduced by ALGOL 60 –Allows a block of code to have its own local vaiables. –Examples: C allow any compound statement ( surrounded by {} ) to have declarations and thus define a new scope. if (list[i] < list[j]) { int temp; temp = list[i]; list[i] = list[j]; list[j] = temp; }

38 Dr. Muhammed Al-MulhemICS 410 – Programming Languages38 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

39 Dr. Muhammed Al-MulhemICS 410 – Programming Languages39 Scope Example 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

40 Dr. Muhammed Al-MulhemICS 410 – Programming Languages40 Scope Example Static scoping –Reference to x is to MAIN's x Dynamic scoping –Reference to x is to SUB1's x

41 Dr. Muhammed Al-MulhemICS 410 – Programming Languages41 Scope and Lifetime Scope and lifetime are sometimes closely related, but are different concepts For example, consider a variable that is declared in a Java method that contains no method calls. The scope of such a variable is from its declaration to the end of the method. The lifetime of that variable is the period of time beginning when method is entered (after being called) and ending when execution of the method terminates.

42 Dr. Muhammed Al-MulhemICS 410 – Programming Languages42 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 (subprograms that are executing but have not yet terminated).


Download ppt "ISBN 0-321-33025-0 Chapter 5 Names, Bindings, Type Checking, and Scopes."

Similar presentations


Ads by Google