CS471 Programming Languages I
Fall 1996

Assignment: Imperative languages

This assignment compares the scope systems of the languages C and Pascal.

The Summary

First, summarize the scope rules of the two languages by examining the contstructs available for partitioning the program text, and giving appropriate examples:

The Task

To show that the scope system of C is adequate to reproduce the scope rules in Pascal. To do this, carry out these steps:

1. Show how the following nested Pascal structure can be implemented in C so that the calls F1 to F2, F1 to F3, F3 to F2 are valid, but F2 to F3 is invalid. You will need to use declaration ordering and prototypes to do this:


function F1() : integer; function F2() : integer; begin { F2 } ... end; { F2 } function F3() : integer; begin { F3 } ... end; { F3 } begin { F1 } ... end; { F1 }

2. Add a local function F4 to function F2, and show how, again, the calling structure can be implemented. Valid are F2, F3, and F4 calling F1; F1 and F4 calling F2; F1 calling F3; F2 and F3 calling F1, F2 calling F4. Invalid are F4 calling F3, F3 calling F4, and F2 calling F3. You will need to use multiple files, extern and static to do this.

3. Finally, show that adding a local function, F5, to F3 can be implemented in C in a way that makes all the calls allowed by the rules of scope valid, and all the calls disallowed invalid. i.e. use multiple files, extern and static to allow the appropriate calls between functions and to disallow unwanted potential calls between functions. Note that there are many solutions to this; just find one of them.

Due date

Hand in your completed assignment on Tuesday, October 15th.