Sei sulla pagina 1di 22

Copyright 2006 The McGraw-Hill Companies, Inc.

Programming Languages
2nd edition
Tucker and Noonan

Chapter 3
Names
(This chapter corresponds to chapter 4 of your textbook)

The first step toward wisdom is calling things by their right names.
Anon. Chinese Proverb


Copyright 2006 The McGraw-Hill Companies, Inc.

Names are used in programs to denote many different
entities like : variables, types, functions, classes
etcetera.

Another term for a name is Identifier.

C family identifiers must be a series of letters and
digits, beginning with a letter.

This is a common theme for creating names in most
programming languages.


Copyright 2006 The McGraw-Hill Companies, Inc.

The term binding is an association between an entity (such
as a variable) and a property (such as its value).

A binding is static if the association occurs before run-time.

A binding is dynamic if the association occurs at run-time.

Name bindings play a fundamental role.

The lifetime of a variable name refers to the time interval
during which memory is allocated.

Copyright 2006 The McGraw-Hill Companies, Inc.
Syntactic Issues
Lexical rules for names.
Collection of reserved words or keywords.

Case sensitivity: alpha, Alpha, AlPHa,ALPHA are
different names if a language is case-sensitive.

C-like: yes
Early languages: no
PHP: partly yes, partly no

Copyright 2006 The McGraw-Hill Companies, Inc.
Reserved Words
Most languages have a predefined collection of names
called reserved words or keywords that carry meaning
and cannot be used as Identifiers.

Reserverd words include names that are useful in parsing
a program because they identify major constructs
major constructs like int, if, while, switch ...

Predefined identifiers: e.g., library routines

Copyright 2006 The McGraw-Hill Companies, Inc.
Variables
An important use of naming is the naming of variables, which are
fundamental in imperative and object-oriented programming.

In such langauges a variable is name for a
memory location (or block of locations.)

A variable is a binding of a name to memory address. In
addition, a variable has a type, a value and a lifetime.
VARIABLE MEMORY LOCATION
A program variable has four basic bindings:
Name, Address, Type, Value.

Copyright 2006 The McGraw-Hill Companies, Inc.

L-value - use of a variable name to denote its address.
Ex: x =

R-value - use of a variable name to denote its value.
Ex: = x

Some languages support/require explicit dereferencing.
e.g. In C/C++ Unary * is used for dereference a pointer
variable.


Copyright 2006 The McGraw-Hill Companies, Inc.

// Pointer example:
(Unary * is used for dereference a pointer variable.)
A pointer is a variable (memory location)
which can only store an offset address.


int x=3, y=5:
int *p;
p =&y; // & is address of operator
x =*p; // assigns to the l-value of x to the value of the
memory address referenced by p, that is the R-
value of the R-value of p. (i.e. Dereferencing)
x 3 5 1A2D
y 5 1A2E
.
.
.
.

p 1A2E 43C1

Copyright 2006 The McGraw-Hill Companies, Inc.
Scope
The scope of a name is the collection of statements
which can access the name binding.

In static scoping, a name is bound to a collection of
statements according to its position in the source
program.

Most modern languages use static (or lexical) scoping.
Because static scoping is based on the grammatical
structure of a program, it is sometimes called
lexical scoping.

Copyright 2006 The McGraw-Hill Companies, Inc.

Two different scopes are either nested or disjoint.

In disjoint scopes, same name can be bound to different
entities without interference.

What constitutes a scope?
Algol C J ava Ada
Class n/a n/a nested yes
Function nested yes yes nested
Block nested nested nested nested
For Loop no no (in C++ yes) yes automatic


Copyright 2006 The McGraw-Hill Companies, Inc.
in C
for (int i = 0; i < 10; i++) {
.......................
in Java (and also in C++)
for (int i = 0; i < 10; i++) {
System.out.println(i);
...
}
... i ... // invalid reference to i

For loop creates
scope
For loop cannot
create scope

Copyright 2006 The McGraw-Hill Companies, Inc.

C-like languages follow the algol tradition of allowing a
compound statement(like a block) to contain new
declarations, and hence to introduce a new scope.
Functions comstitute a scope, but cannot be nested.
Blocks do constitute a scope and dan ve nested.

The scope in which a name is defined or declared is called its
defining scope.

A reference to a name is nonlocal if it occurs in a nested
scope of the defining scope; otherwise, it is local.


Copyright 2006 The McGraw-Hill Companies, Inc.

1 void sort (float a[ ], int size) {
2 int i, j;
3 for (i = 0; i < size; i++) // i, size local
4 for (j = i + 1; j < size; j++)
5 if (a[j] < a[i]) { // a, i, j local
6 float t;
7 t = a[i]; // t local; a, i nonlocal
8 a[i] = a[j];
9 a[j] = t;
10 }
11 }
Scope of local variable t:
statements on lines 6 9

Scope of a and size:
statements on lines 2 10




Copyright 2006 The McGraw-Hill Companies, Inc.
Symbol Table
One of the tasks of the semantic analysis phase of a translator
is to construct a data structure called symbol table to keep
track of each declared name and its binding.

Assume that each name is unique within its local scope. That
is, the only other declarations of the same name occur in
either a disjoint scope or a nested scope.

The data structure can be any implementation of a dictionary,
where the name is the key.

Copyright 2006 The McGraw-Hill Companies, Inc.

1. Each time a scope is entered, push a new dictionary onto
the stack.
2. Each time a scope is exited, pop a dictionary off the top
of the stack.
3. For each name declared, generate an appropriate binding
and enter the name-binding pair into the dictionary on
the top of the stack.
4. Given a name reference, search the dictionary on top of
the stack:
a) If found, return the binding.
b) Otherwise, repeat the process on the next dictionary down in
the stack.
c) If the name is not found in any dictionary, report an error.

Copyright 2006 The McGraw-Hill Companies, Inc.

C program in Fig. 4.1, stack of dictionaries at line 7:
<t, 6>
<j, 4><i, 3><size,1><a, 1>
<sort, 1>
At line 4 and 11:
<j, 4><i, 3><size,1><a, 1>
<sort, 1>

Copyright 2006 The McGraw-Hill Companies, Inc.
Resolving References
For static scoping, the referencing environment for a
name is its defining scope and all nested subscopes.

The referencing environment defines the set of
statements which can validly reference a name.

Copyright 2006 The McGraw-Hill Companies, Inc.
Visibility
A name is visible if its referencing environment
includes the reference and the name is not reclared
in an inner scope.
A name redeclared in an inner scope effectively hides
the outer declaration.
Some languages provide a mechanism for referencing
a hidden name; e.g.: this.x in C++/J ava.

Copyright 2006 The McGraw-Hill Companies, Inc.

1 public class Student {
2 private String name;
3 public Student (String name, ...) {
4 this.name =name;
5 ...
6 }
7 }


Copyright 2006 The McGraw-Hill Companies, Inc.
Overloading
Overloading uses the number or type of parameters to
distinguish among identical function names or
operators.
Examples:
+, -, *, / can be float or int
+can be float or int addition or string
concatenation in J ava
System.out.print(x) in J ava

Copyright 2006 The McGraw-Hill Companies, Inc.

public class PrintStream extends
FilterOutputStream {
...
public void print(boolean b);
public void print(char c);
public void print(int i);
public void print(long l);
public void print(float f);
public void print(double d);
public void print(char[ ] s);
public void print(String s);
public void print(Object obj);
}

Copyright 2006 The McGraw-Hill Companies, Inc.
Lifetime
The lifetime of a variable is the time interval during
which the variable has been allocated a block of
memory.

Earliest languages used static allocation.

Algol introduced the notion that memory should be
allocated/deallocated at scope entry/exit.

Potrebbero piacerti anche