Q
Quinnie
Hi,
I have a homework assignment that I'm so confused and really need help
with. Here's the description, any help would be appreciated. Thanks!
Assume we have a statically-scoped language with nested procedures.
That is, a procedure
(or function) can contain local procedures (and functions). Procedures
can be nested arbitrarily
deep. The scoping rules for procedure names (i.e., the ability to call
a procedure from
a particular point in a program) is really the same as the scoping
rules for local variables:
A name is visible in the block in which it is declared and in all
enclosed blocks (procedures)
unless shadowed by a declaration of the same name. So at any given
point in a program the
visible names are those declared in all enclosing blocks (except for
those that are hidden).
Instead of implementing this language using static links to access
non-local variables, we
can first transform programs into ones that contain no nesting of
procedures, i.e., a C-like
structure with just a main program and a (flat) set of procedure
declarations. The key idea
behind this transformation is adding appropriate parameters to
procedures so that they are
passed (by reference) all of the (previously non-local) variables that
they need in addition
to the globals and their locals. Give an algorithm for this
transformation.
To provide a starting framework, assume that a program is given as
input data with the
following operations supported:
children(P) - returns all the procedures declared local to procedure P
body(P) - returns the body of procedure P
called(P) - returns the set of procedures called in the body of P.
You may or may not find these definitions useful. You should define
additional ones to
make your algorithm clearer.
This problem is a subtle one and care should be taken to ensure that
you've considered
all possible cases. For example, simply adding all the non-local
variables in a procedure P
to P's parameter list is not always sufficient. A proper solution
requires you to analyze the
program and the "flow" of information (variables) through the program.
You will need to
iteratively collect this information until you are sure you have it
all.
This assignment requires you to do some thinking about a new kind of
problem and
come up with a solution based on some new ideas. (But no proofs are
involved!) Feel free
to discuss this among yourselves and with me.
Ambitious students may also try to reason why their proposed algorithm
gives a correct
transformation.
note that my professor doesn't like the idea of redefining all nested
procedures as global, and then add to the parameter list of each
nested procedure an object representing the variables of the parent
procedure
any thoughts? Thanks!
I have a homework assignment that I'm so confused and really need help
with. Here's the description, any help would be appreciated. Thanks!
Assume we have a statically-scoped language with nested procedures.
That is, a procedure
(or function) can contain local procedures (and functions). Procedures
can be nested arbitrarily
deep. The scoping rules for procedure names (i.e., the ability to call
a procedure from
a particular point in a program) is really the same as the scoping
rules for local variables:
A name is visible in the block in which it is declared and in all
enclosed blocks (procedures)
unless shadowed by a declaration of the same name. So at any given
point in a program the
visible names are those declared in all enclosing blocks (except for
those that are hidden).
Instead of implementing this language using static links to access
non-local variables, we
can first transform programs into ones that contain no nesting of
procedures, i.e., a C-like
structure with just a main program and a (flat) set of procedure
declarations. The key idea
behind this transformation is adding appropriate parameters to
procedures so that they are
passed (by reference) all of the (previously non-local) variables that
they need in addition
to the globals and their locals. Give an algorithm for this
transformation.
To provide a starting framework, assume that a program is given as
input data with the
following operations supported:
children(P) - returns all the procedures declared local to procedure P
body(P) - returns the body of procedure P
called(P) - returns the set of procedures called in the body of P.
You may or may not find these definitions useful. You should define
additional ones to
make your algorithm clearer.
This problem is a subtle one and care should be taken to ensure that
you've considered
all possible cases. For example, simply adding all the non-local
variables in a procedure P
to P's parameter list is not always sufficient. A proper solution
requires you to analyze the
program and the "flow" of information (variables) through the program.
You will need to
iteratively collect this information until you are sure you have it
all.
This assignment requires you to do some thinking about a new kind of
problem and
come up with a solution based on some new ideas. (But no proofs are
involved!) Feel free
to discuss this among yourselves and with me.
Ambitious students may also try to reason why their proposed algorithm
gives a correct
transformation.
note that my professor doesn't like the idea of redefining all nested
procedures as global, and then add to the parameter list of each
nested procedure an object representing the variables of the parent
procedure
any thoughts? Thanks!