Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 5 Names, Bindings, Type Checking, and Scopes."— Presentation transcript:

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

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

3 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 abstractions of memory cells Variables are characterized by attributes To design a type, must consider scope, lifetime, type checking, initialization, and type compatibility

4 Relation between special characters and reserved words
Names A Name is a string of characters used to indentify some entity in a program. Length Length Case Sensitivity Relation between special characters and reserved words (Ex. variables, formal parameters, methods, 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

5 Relation between special characters and reserved words
Names Length Case Sensitivity Relation between special characters 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)

6 Relation between special characters and reserved words
Names Length Case Sensitivity Relation between special characters 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.

7 Relation between special characters and reserved words
Names Length Case Sensitivity Relation between special characters 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 = (Real is a variable).

8 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. Example: The result of applying “my.username” is assigned to “p”. Code: The result of applying “my.username” is not assigned to any variable. Code:

9 Meaning: the memory address with which it is associated
Attributes: Name Address Type Value Lifetime Scope Address Meaning: the memory address with which it is associated A variable may have different addresses at different times during execution (ex. pointer) different places in a program. 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, ,147,483,647 Set of Operations: +, -, *, Mod, Div int n; 703 704 705 706 707 708 n

10 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

11 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

12 Example: int count; count = count + 5; The type of count is bound at compile time. The set of possible values of count is bound at compiler design time. The meaning of the operator symbol + is bound at compile time, when the types of its operands have been determined. The internal representation of the literal 5 is bound at compiler design time. The value of count is bound at execution time with this statement.

13 Type of Binding 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

14 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 How is a type specified? When does the binding take place? Static Type Binding Dynamic Type Binding

15 done through explicit or implicit declaration.
How is a type specified? Static Type Binding 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. ) Ex. Sum =0; (FORTRAN has implicit dec.) listLn; Advantage: writability Disadvantage: reliability (less trouble with Perl)

16 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 implemented in interpretation, because the overhead of type checking is not the bottleneck. Type error detection by the compiler is difficult.

17 Storage Bindings and Lifetime
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. The lifetime of a variable is the time during which it is bound to a particular memory cell.

18 Name Address Type Value Lifetime Scope
Attributes: Name Address Type Value Lifetime Scope Meaning: The lifetime of a variable is the time during which it is bound to a particular memory cell. Storage binding and lifetime The logical organization of the memory that is used by a running problem: 3 main areas for variables: - the global or static area - the stack (contains "activation records") - the heap | static area | | stack | | | | | v | | | | ^ | | heap |

19 Categories of Variables by Lifetimes
static stack-dynamic explicit heap-dynamic variables implicit heap-dynamic variables

20 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. e.g., C and C++ static variables Advantages: # efficiency (direct addressing), # history-sensitive subprogram support int f(int x) { static int a = 0; a += 2; printf("%d\n",a); } f(2) --> f(4) ---> 4 Disadvantage: lack of flexibility (no recursion). lifetime = entire program execution.

21 Categories of Variables by Lifetimes
(2) Stack-dynamic variables: 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 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 f(int x) { int a = 0; ... f(y+a); … }

22 Categories of Variables by Lifetimes
(3)Explicit heap-dynamic variables: are nameless (abstract) memory cells that are allocated and deallocated by explicit instructions, 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 *intnode; // Create a pointer intnode = new int; // Create the heap-dynamic variable . . . delete intnode; // Deallocate the heap-dynamic variable // to which intnode points Advantage: high flexibility (provides for dynamic storage management) Disadvantage: inefficient and unreliable. lifetime = from explicit allocation to explicit deallocation

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

24 Type Checking (section 6.12 from chapter 6)
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

25 If all type bindings are static, nearly all type checking can be static
If type bindings are dynamic, type checking must be dynamic

26 Strong Typing (section 6.13 from chapter 6)
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: 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 ML is strongly typed, even though the types of some function parameters may not be known at compile time. F# is strongly typed.

27 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 ML and F#

28 Variable Attributes: Scope
The scope of a variable is the range of statements in which it is visible. A variable is visible in a statement if it can be referenced in that statement. A variable is local in a program unit or block if it is declared there. The nonlocal variables of a program unit or block are those that are visible within the program unit or block but are not declared there. The scope rules of a language determine how references to names are associated with variables

29 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)

30 Static Scope Example Consider the following JavaScript function, big, in which the two functions sub1 and sub2 are nested: function big() { function sub1() { var x = 7; sub2(); } function sub2() { var y = x; var x = 3; sub1();

31 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

32 Blocks void sub() { int count; . . . while (. . .) { count++; }
A method of creating static scopes inside program units--from ALGOL 60 Example: Consider the following skeletal C function: void sub() { int count; . . . while (. . .) { count++; }

33 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

34 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

35 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 nonlocals

36 Dynamic Scope Based on the calling sequence of subprograms, not on their spatial relationship to each other References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point

37 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 … function big() { function sub1() { var x = 7; sub2(); } function sub2() { var y = x; var x = 3; sub1();

38 Scope Example Static scoping Dynamic scoping
Reference to X is to Big's X Dynamic scoping Reference to X is to Sub1's X

39 Evaluation of Dynamic Scoping
Disadvantages: Subprograms are always executed in the environment of all previously called subprograms that have not yet completed their executions (less reliability) The inability to type check references to nonlocals statically Poor readability Accesses to nonlocal variables in dynamic-scoped languages take far longer than accesses to nonlocals when static scoping is used Advantages: No parameters need to be passed from caller to subprogram

40 Referencing Environments
The referencing environment of a statement is the collection of all variables 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

41 Named Constants A named constant is a variable that is bound to a value only once 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 (allow dynamic binding of values to named constants) 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

42 Variable Initialization
The binding of a variable to a value at the time it is bound to storage is called initialization If the variable is statically bound to storage, binding and initialization occur before run time. In these cases, the initial value must be specified as a literal or an expression whose only nonliteral operands are named constants that have already been defined. If the storage binding is dynamic, initialization is also dynamic and the initial values can be any expression. Initialization is often done on the declaration statement, e.g., in Java int sum = 0;

43 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 Variables are categorized according to their lifetime as: static, stack dynamic, explicit heap dynamic, implicit heap dynamic Strong typing means detecting all type errors


Download ppt "Chapter 5 Names, Bindings, Type Checking, and Scopes."

Similar presentations


Ads by Google