The
static/dynamic distinction
Among
the most important issues that we face when designing a compiler for a language
is what decisions can the compiler make about a program. If a language uses a
policy that allows the compiler to decide an issue, then we say that the
language uses a static policy
or that the issue can be decided at compile time. On the other hand, a policy that only allows
a decision to be made when we execute the program is said to be a dynamic policy or to require a
decision at run time.
Environments
and states
Another
important distinction we must make when discussing programming languages is whether
changes occurring as the program runs affect the values of data elements or
affect the interpretation of names for that data. For example, the execution of
an assignment such as x = y +1 changes the
value denoted by the name x. More
specifically, the assignment changes the value in whatever location is denoted
by x.
Static
scope and block structure
Most
languages, including C and its family, use static scope. The scope rules for C
are based on program structure; the
scope of a declaration is determined implicitly by where the declaration
appears in the program. Later languages, such as C + + , Java, and C # , also provide
explicit control over scopes through the use of keywords like public,
private, and protected.
In this section we consider static-scope rules for a language with
blocks, where a block is a grouping
of declarations and statements. C uses braces {and} to delimit a block; the alternative
use of b e g in and end
for the same purpose dates back to Algol.
Explicit
access control
Classes
and structures introduce a new scope for their members. If p
is an object of a class with a field (member) x,
then the use of x in
p.x refers to field x
in the class definition. In analogy with block structure, the
scope of a member declaration x in
a class C extends to any subclass
C, except if C has
a local declaration of the same name x.
Dynamic
scope
Technically,
any scoping policy is dynamic if it is based on factor(s) that can be known
only when the program executes. The term dynamic scope, however,
usually refers to the following policy: a use of a name x
refers to the declaration of x in
the most recently called procedure with such a declaration. Dynamic scoping of
this type appears only in special situations.
Parameter
passing mechanisms
All
programming languages have a notion of a procedure, but they can differ in how
these procedures get their arguments. In this section, we shall consider how
the actual parameters (the
parameters used in the call of a procedure) are associated with the formal
parameters (those used in the procedure definition).
Which mechanism is used determines how the calling-sequence code treats
parameters. The great majority of languages use either
"call-byvalue," or "call-by-reference," or both. We shall
explain these terms, and another method known as "call-by-name," that
is primarily of historical interest.
Aliasing
There
is an interesting consequence of call-by-reference parameter passing or its
simulation, as in Java, where references to objects are passed by value. It is
possible that two formal parameters can refer to the same location; such
variables are said to be aliases of
one another. As a result, any two variables, which may appear to take their
values from two distinct formal parameters, can become aliases of each other,
as well.
0 comments