473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

alternative to nested procedures

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!
Nov 13 '05 #1
2 2328
Quinnie wrote:

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. any thoughts? Thanks!


Seems to me, more like a topic for
news:comp.programming

--
pete
Nov 13 '05 #2
In article <8f**************************@posting.google.com >
Quinnie <qn****@psu.edu> writes:
I have a homework assignment that I'm so confused and really need help
with. ...
The problem has nothing to do with C per se; comp.programming is
certainly a much more appropriate group.

[Problem description snipped; my summary: produce an algorithm
for transforming lexically-nested procedures into parameterized
unnested procedures, given a limited set of operations on the
procedures and their variables. The algorithm you will need is
quite common and is called "transitive closure".]
... For example, simply adding all the non-local
variables in a procedure P
to P's parameter list is not always sufficient.
In particular, consider what happens if you have:

procedure outermost() {
variables t, u, v, w, and z all defined here
...
procedure P(var x) {
procedure Q(var y) {
touch variable z
}
touch variable w
}
}

Procedure P here uses "w", which is not local to P, but it also
calls Q, which uses "z", which is *also* not local to P. If Q is
moved to the "outermost" level, and P calls Q, outermost() will
need to pass *both* w *and* z to P, so that P can pass z on to Q.
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


This method works if done right -- instead of adding "the" parent's
locals, you must add ALL the parents' locals, so that both P *and*
Q get *both* "w" and "z" -- but is overkill. In this case it would
mean that P and Q also get t, u, and v. It also requires renaming
steps so that if P has its own local t, you do not attempt to have
two variables named "t" in the outer-ized P.

To bring this all back to C, this kind of problem does come up now
and then, especially with "callback functions". My preferred method
is not to add one parameter per pass-through variable, but rather
to collect up the passed-through state in a "context" structure.
The (no-longer-nested) "inner" procedures like P then read:

struct P_context {
int i; /* if there is an "i" */
double w; /* the "w" seen above in the nested code */
/* and so on -- a copy of "z" might be in here too */
};

void P(any regular args, struct P_context *ctx) {
... code ...
ctx->w = value; /* as needed */
... more code ...
}

Actual situations in which P has a "pass-through" parameter for Q
are quite rare (indeed, I think I have never come across one myself),
and I would tend to go for ad-hoc solutions for those. (For instance,
Q might just take a single "pointer to z" parameter, and P_context
might have "Z_TYPE *zp;" as a member.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Forgone Conclusion | last post by:
Hi, I have a View that groups and sums up totals. This View is then nested within in another View and used (it needs to be done like this). What i need to do is to be able to vary the records...
3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
3
by: Bruce | last post by:
Since DBlib is no longer the suggested method for connecting back to sql server from an Extended Stored Procedure, has anyone built any extended stored procedures that use other connection methods...
5
by: ahokdac-sql | last post by:
Hi, I'm adapting access queries to sql server and I have difficulties with the following pattern : query1 : SELECT * FROM Query2 WHERE A=@param1 query 2: SELECT * FROM Table2 WHERE B=@param2 ...
7
by: Anthony Robinson | last post by:
Have been encountering an odd issue. Every now and again, certain packages of stored procedures just become invalid. I'm aware that dropping or altering an underlying table would render a package...
5
by: Neil Zanella | last post by:
Hello, I am curious as to why the designers of C did not include support for Pascal-like nested procedures. I guess that it's because nested procedures in C would not buy you much other than...
2
by: Thilaka | last post by:
Hi guys, I want to know if there's an alternative to stored procedures. Ie, when updating certain tables of SQL server, i would like to update a few other tables with vaules retrieving from...
1
by: sasachi sachi sachi | last post by:
Hi there, I have a data manipulation process written in a Nested Stored procedure that have four levels deeper. When I run these individual procedures individually they all seems to be fine....
0
by: Aravindkumar | last post by:
Hi My name is Aravind. I would like to know how VARRAY and NESTED TABLES can be used in Oracle Stored Procedures / Packages. Please treat this as urgent Aravind
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.