24.12.2014 Views

Names, Bindings, Type Checking and Scopes

Names, Bindings, Type Checking and Scopes

Names, Bindings, Type Checking and Scopes

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

SE 2002<br />

PROGRAMMING LANGUAGES<br />

Assist. Prof. Dr. M. Alper TUNGA<br />

(alper.tunga@bahcesehir.edu.tr)<br />

(http://akademik.bahcesehir.edu.tr/~alper)


CHAPTER 3<br />

NAMES, BINDINGS,<br />

TYPE CHECKING <strong>and</strong> SCOPES


INTRODUCTION<br />

▶ Imperative languages are abstractions of von<br />

Neumann architecture<br />

◆ Memory<br />

◆ Processor<br />

▶ The abstractions in a language for the memory<br />

cells of the machine are variables.<br />

3


NAMES<br />

▶ A name is a string of characters used to identify<br />

some entity in a program.<br />

▶ Primary design issues for names:<br />

◆ Are names case sensitive<br />

◆ Are special words reserved words or keywords<br />

4


NAMES<br />

▶ Length<br />

◆ If too short, they cannot be connotative<br />

◆ Language examples:<br />

✔ FORTRAN I: maximum 6<br />

✔ COBOL: maximum 30<br />

✔ FORTRAN 90 <strong>and</strong> C89: maximum 31<br />

✔ C99: maximum 63<br />

✔ C#, Ada, <strong>and</strong> Java: no limit, <strong>and</strong> all are significant<br />

✔ C++: no limit, but implementers often impose one<br />

5


NAMES<br />

▶ Legal names<br />

◆ <strong>Names</strong> in most programming languages have the<br />

same form:<br />

✔ a letter followed by a string consisting of letters,<br />

digits, <strong>and</strong> underscore characters (_).<br />

6


NAMES<br />

▶ Case sensitivity<br />

◆ <strong>Names</strong> in the C-based languages are case sensitive<br />

◆ <strong>Names</strong> in others are not<br />

◆ Disadvantage: readability (names that look alike are<br />

different)<br />

✔ Worse in C++, Java, <strong>and</strong> C# because predefined<br />

names are mixed case<br />

7


NAMES<br />

▶ Special words<br />

◆ Special words in programming languages are used<br />

to make programs more readable by naming actions<br />

to be performed.<br />

◆ Used to separate the syntactic entities of programs<br />

◆ In most languages, special words are classified as<br />

reserved words, but in some they are only<br />

keywords.<br />

✔ A keyword is a word that is special only in certain<br />

contexts,<br />

✔ A reserved word is a special word that cannot be used<br />

as a user-defined name<br />

8


VARIABLES<br />

▶ A variable is an abstraction of a computer<br />

memory cell or collection of cells.<br />

▶ Variables can be characterized as a sextuple of<br />

attributes:<br />

◆ Name<br />

◆ Address<br />

◆ Value<br />

◆ <strong>Type</strong><br />

◆ Lifetime<br />

◆ Scope<br />

9


VARIABLES<br />

▶ Name<br />

◆ Most variables have names.<br />

◆ <strong>Names</strong> in most programming languages have the<br />

same form:<br />

✔ a letter followed by a string consisting of letters,<br />

digits, <strong>and</strong> underscore characters (_).<br />

▶ Address<br />

◆ The address of a variable is the machine memory<br />

address with which it is associated<br />

10


VARIABLES<br />

▶ <strong>Type</strong><br />

◆ The type of a variable determines the range of<br />

values of variables <strong>and</strong> the set of operations that<br />

are defined for values of that type; in the case of<br />

floating point, type also determines the precision<br />

11


VARIABLES<br />

▶ Value<br />

◆ The value of a variable is the contents of the<br />

location (memory cell) with which the variable is<br />

associated.<br />

◆ Abstract memory cell - the physical cell or collection<br />

of cells associated with a variable<br />

◆ The l-value of a variable is its address<br />

◆ The r-value of a variable is its value<br />

12


THE CONCEPT OF BINDING<br />

▶ A binding is an association, such as between an<br />

attribute <strong>and</strong> an entity, or between an operation<br />

<strong>and</strong> a symbol<br />

▶ Binding time is the time at which a binding takes<br />

place.<br />

13


THE CONCEPT OF BINDING<br />

▶ Possible Binding Times<br />

◆ Language design time<br />

◆ Language implementation time<br />

◆ Compile time<br />

◆ Load time<br />

◆ Link time or run time<br />

14


THE CONCEPT OF BINDING<br />

▶ Static <strong>and</strong> Dynamic Binding<br />

◆ A binding is static if it first occurs before run time<br />

<strong>and</strong> remains unchanged throughout program<br />

execution.<br />

◆ A binding is dynamic if it first occurs during<br />

execution or can change during execution of the<br />

program.<br />

15


THE CONCEPT OF BINDING<br />

▶ <strong>Type</strong> Binding<br />

◆ Before a variable can be referenced in a program, it<br />

must be bound to a data type.<br />

◆ The two aspects of this binding are<br />

✔ how the type is specified,<br />

✔ when the binding takes place.<br />

16


THE CONCEPT OF BINDING<br />

▶ Static <strong>Type</strong> Binding<br />

◆ <strong>Type</strong>s can be specified statically through some form<br />

of explicit or implicit declaration.<br />

◆ An explicit declaration is a program statement<br />

used for declaring the types of variables<br />

◆ An implicit declaration is a default mechanism for<br />

specifying types of variables (the first appearance of<br />

the variable in the program)<br />

17


THE CONCEPT OF BINDING<br />

▶ Dynamic <strong>Type</strong> Binding<br />

◆ With dynamic binding, the type of a variable is not<br />

specified by a decIaration statement, nor can it be<br />

determined by the spelling of its name.<br />

◆ Instead, the variable is bound to a type when it is<br />

assigned a value in an assignment statement.<br />

◆ When the assignment statement is executed, the<br />

variable being assigned is bound to the type of the<br />

value of the expression on the right side of the<br />

assignment.<br />

18


THE CONCEPT OF BINDING<br />

▶ <strong>Type</strong> Inference<br />

◆ Rather than by assignment statement, types are<br />

determined (by the compiler) from the context of<br />

the reference<br />

◆ <strong>Type</strong> inference is used in ML, Mir<strong>and</strong>a, <strong>and</strong> Haskell<br />

languages.<br />

◆ Example: Consider the following ML function.<br />

✔ fun square(x)=x*x;<br />

»<br />

* is an arithmetic operator <strong>and</strong> the default numeric type<br />

is int.<br />

✔ fun circumf(r)=3.14159*r*r;<br />

»<br />

takes a floating-point argument <strong>and</strong> produces a floatingpoint<br />

result.<br />

✔ fun square(x):real=x*x;<br />

19


THE CONCEPT OF BINDING<br />

▶ Storage <strong>Bindings</strong> & Lifetime<br />

◆ Allocation - getting a cell, to which a variable can<br />

be bound, from some pool of available cells<br />

◆ Deallocation - putting a cell back into the pool<br />

◆ The lifetime of a variable is the time during which it<br />

is bound to a particular memory cell<br />

20


THE CONCEPT OF BINDING<br />

▶ Storage <strong>Bindings</strong> & Lifetime (cont.)<br />

◆ To investigate storage bindings of variables, it is<br />

convenient to separate scalar variables into four<br />

categories, according to their lifetimes:<br />

✔ static<br />

✔ stack–dynamic<br />

✔ explicit heap–dynamic<br />

✔ implicit heap–dynamic<br />

21


THE CONCEPT OF BINDING<br />

▶ Storage <strong>Bindings</strong> & Lifetime (cont.)<br />

◆ Static Variables<br />

✔ Static variables are those that are bound to memory<br />

cells before program execution begins <strong>and</strong> remain<br />

bound to those same memory cells until program<br />

execution terminates.<br />

22


THE CONCEPT OF BINDING<br />

▶ Storage <strong>Bindings</strong> & Lifetime (cont.)<br />

◆ Stack–Dynamic Variables<br />

✔ Stack-dynamic variables are those whose storage<br />

bindings are created when their declaration<br />

statements are elaborated.<br />

23


THE CONCEPT OF BINDING<br />

▶ Storage <strong>Bindings</strong> & Lifetime (cont.)<br />

◆ Explicit heap–dynamic variables<br />

✔ These variables are nameless (abstract) memory cells<br />

that are allocated <strong>and</strong> deallocated by explicit<br />

directives, specified by the programmer, which take<br />

effect during execution.<br />

24


THE CONCEPT OF BINDING<br />

▶ Storage <strong>Bindings</strong> & Lifetime (cont.)<br />

◆ Implicit heap–dynamic variables<br />

✔ These variables are bound to heap storage only when<br />

they are assigned values.<br />

25


TYPE CHECKING<br />

▶ <strong>Type</strong> checking is the activity of ensuring that the<br />

oper<strong>and</strong>s of an operator are of compatible types<br />

◆ A compatible type is one that is either legal for the<br />

operator, or is allowed under language rules to be<br />

implicitly converted, by compiler- generated code,<br />

to a legal type.<br />

✔ This automatic conversion is called a coercion.<br />

26


TYPE CHECKING<br />

▶ A type error is the application of an operator to<br />

an oper<strong>and</strong> of an inappropriate type.<br />

▶ A programming language is strongly typed if<br />

type errors are always detected.<br />

27


TYPE CHECKING<br />

▶ If all type bindings are static, nearly all type<br />

checking can be static.<br />

▶ If type bindings are dynamic, type checking must<br />

be dynamic.<br />

◆ Requires type checking at run time<br />

◆ Javascript <strong>and</strong> PHP<br />

▶ It is better to detect errors at compile time than at<br />

run time, because the earlier correction is usually<br />

less costly.<br />

▶ The penalty for static checking is reduced<br />

programmer flexibility.<br />

28


STRONG TYPING<br />

▶ A programming language is strongly typed if<br />

type errors are always detected.<br />

▶ This requires that the types of all oper<strong>and</strong>s can be<br />

determined, either at compile time or at run time.<br />

▶ The importance of strong typing lies in its ability to<br />

detect all misuses of variables that result in type<br />

errors.<br />

29


SCOPE<br />

▶ The scope of a variable is the range of statements<br />

over which it is visible.<br />

▶ A variable is visible in a statement if it can be<br />

referenced in that statement.<br />

▶ The nonlocal variables of a program unit are<br />

those that are visible within the program unit but<br />

are not declared there.<br />

▶ The scope rules of a language determine how<br />

references to names are associated with variables<br />

30


SCOPE<br />

▶ Static Scope<br />

◆ The method of binding names to nonlocal variables.<br />

◆ To connect a name reference to a variable, you (or<br />

the compiler) must find the declaration<br />

✔ This permits a human program reader to determine<br />

the type of every variable in the program.<br />

◆ Search process: search declarations, first locally,<br />

then in increasingly larger enclosing scopes, until<br />

one is found for the given name<br />

◆ Enclosing static scopes (to a specific scope) are<br />

called its static ancestors; the nearest static<br />

ancestor is called a static parent<br />

31


SCOPE<br />

▶ Dynamic Scope<br />

◆ Based on calling sequences of program units, not<br />

their textual layout (temporal versus spatial)<br />

✔ Thus the scope can be determined only at run time.<br />

◆ References to variables are connected to<br />

declarations by searching back through the chain of<br />

subprogram calls that forced execution to this point<br />

32

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!