RHODES UNIVERSITY


Computer Science 301 - 2010 - Programming Language Translation

Well, here you are. Here is the free information you have all been waiting for, with some extra bits of advice:


How to spend a Special Sunday

From now until about 22h30 tonight, Computer Science 3 students have exclusive use of the Hamilton Laboratory. You are encouraged to throw out anyone else who tries to use it, but we hope that this does not need to happen. At about 22h30 Chris, Dameon, Jill and I may have to move some computers around and prepare things for Monday. If there is still a high demand we shall try to leave some computers for use until later, but by then we hope you will have a good idea of what is involved.

Once again, this year the format of the examination is somewhat different from years gone by, so as to counter what had happened where, essentially, one solution was prepared by quite a large group and then passed around to many others, who had probably not contributed to it in any real way. As in recent years (since 2007), the problem set below is only part of the story. At 16h30 you will receive further hints as to how this problem should be solved (by then you might have worked this out for yourselves, of course). You will be encouraged to study the problem further, and the hints in detail, because during the examination tomorrow you will be set further questions relating to the system - for example, asked to extend the solution you have worked on in certain ways. It is my hope that if you have really understood the material today, these questions will have solutions that come readily to mind, but of course you will have to answer these on your own!

My "24 hour exam" problems have all been designed so that everyone should have been able to produce at least the basic solution, with scope given for top students to demonstrate their understanding of the subtler points of parsing, scanning and compiling. Each year I have been astounded at the quality of some of the solutions received, and I trust that this year will be no exception.

Please note that there will be no obligation to produce a machine-readable solution in the examination (in fact doing so is quite a risky thing, if things go wrong for you, or if you cannot type quickly). The tools will be provided before the examination so that you can experiment in detail. If you feel confident, then you are free to produce a complete working solution to Section B during the examination. If you feel more confident writing out a neat detailed solution or extensive summary of the important parts of the solution, then that is quite acceptable. Many of the best solutions over the last few years have taken that form.

During the first few hours you will be able to find the following files in the usual places, or by following links from the course web page:

At 16h30 new versions of the exam kit will be posted (FREE2J.ZIP and FREE2C.ZIP), and a further handout issued, with extra information.

Most of the machines in the labs will work in exactly the way they have done this semester. Some are set up to show you the configuration you can expect tomorrow. It is suggested that you work at one of those machines for a short time to make sure you know what to expect. To work on these machines you proceed as follows

From here on things should be familiar. You could, for example, log onto the D: or J: drive, use UltraEdit use CMAKE and CRUN ... generally have hours of fun. The C# system will work only on the D: drive, however.

But note that the exam set up has no connection with the outside world - no Google, FaceBook, ftp client, telnet client, shared directories - not even a printer!

Today you may use the files and either the "usual" or the "exam" systems in any way that you wish, subject to the following restrictions: Please observe these in the interests of everyone else in the class.

(a) When you have finished working, please delete any files from the D: drive, so that others are not tempted to come and snoop around to steal ideas from you.

(b) You are permitted to discuss the problem with one another, and with anybody not on the "prohibited" list.

(c) You are also free to consult books in the library. If you cannot find a book that you are looking for, it may well be the case that there is a copy in the Department. Feel free to ask.

(d) Please do not try to write any files onto the C: directory, for example to C:\TEMP

(e) If you take the exam kit to a private machine you will need to have Java installed (or the .NET framework or equivalent to use the C# version).

I suggest that you do spend some of the next 24 hours in discussion with one another, and some of the time in actually trying out your ideas. You have plenty of time in which to prepare and test your ideas - go for it, and good luck. Remember that tomorrow you may not bring anything into the room other than your student card and writing utensils, and especially not listings, diskettes, memory sticks, text books or cell phones.

If you cannot unpack the file, or have trouble getting the familiar tools to work (unlikely!), you may ask me for help. You may also ask for explanation of any points in the question that you do not understand, in the same way that you are allowed to ask before the start of an ordinary examination. You are no longer allowed to ask me questions about any other part of the course. Sorry; you had your chance earlier, and I cannot allow this without risking the chance of sneak questions and suggestions being called for.

If you cannot solve the problem completely, don't panic. It has been designed so that I can recognize that students have reached varying degrees of sophistication and understanding.


How you will spend a Merry Monday

Before the start of the formal examinations the laboratory will be unavailable. During that time

At the start of the examination session:


Cessation of Hostilities Party

As previously mentioned, Sally and I would like to invite you to an informal end-of-course party at our house on 15 November. There's another copy of the map below to help you find your way there. It would help if you could let me know whether you are coming so that we can borrow enough glasses, plates etc. Please post the reply slip into the hand-in box during the course of the day. Time: from 18h30 onwards. Dress: Casual

                       PDT(8) \      <==================================== here!
                               |\
        \     \               /---\
          \   Bedford       /       \  Constitution St
   Cradock  \     \       /           \
              \     \   /               \      ^Hospital
                \    Pear Lane            \    |
                  |          Worcester St   \  |
          -------------------------------------|
           DSG    |                            |
                  | St Andrew's                |Milner St
                  |                            |
                  |                            |
         Somerset |               African St   |
         Street   |------------------------------------------
                  |        |                      |
                  |        |  The Mall            |
                  |        |                      |
                  |   Rat  |             New St   |
          --------+-------------------------------+----------
      Hamilton    |      |                        |
      Hell Hole   |      |                        | Hill St
                  |      |                  Gino's|
        Rhodes    |      |     High St            |
                ----------------------------------- Cathedral


Section B [ 70 marks ]


Extending Parva to allow for enumeration types

Most computer languages provide simple, familiar, notations for handling arithmetic, character and Boolean types of data. Variables, structures and arrays can be declared of these basic types; they may be passed from one routine to another as parameters, and so on.

Some languages, notably Pascal, Modula-2, C, C++, and Ada, allow programmers the flexibility to define what are often known as enumeration types, or simply enumerations. Here are some examples to illustrate this idea:

     TYPE  (* Pascal or Modula-2 *)
       COLOURS = ( Red, Orange, Yellow, Green, Blue, Indigo, Violet );
       INSTRUMENTS = ( Drum, Bass, Guitar, Trumpet, Trombone, Saxophone, Bagpipe );
     VAR
       Walls, Ceiling, Roof : COLOURS;
       JazzBand : ARRAY [0 .. 40] OF INSTRUMENTS;

or the equivalent

     typedef  /* C or C++ */
       enum { Red, Orange, Yellow, Green, Blue, Indigo, Violet } COLOURS;
     typedef
       enum { Drum, Bass, Guitar, Trumpet, Trombone, Saxophone, Bagpipe } INSTRUMENTS;
     COLOURS      Walls, Ceiling, Roof;
     INSTRUMENTS  JazzBand[41];

Sometimes the variables are declared directly in terms of the enumerations:

     VAR  (* Pascal or Modula-2 *)
       CarHireFleet : ARRAY [1 .. 100] OF ( Golf, Tazz, Sierra, BMW316 );

     enum CARS { Golf, Tazz, Sierra, BMW316 } CarHireFleet[101];  /* C or C++ */

Java got into the act rather later; the original version of Java did not provide the facility that is now manifest in Java and C#, where we might declare:

     enum COLOURS { Red, Orange, Yellow, Green, Blue, Indigo, Violet };
     enum INSTRUMENTS { Drum, Bass, Guitar, Trumpet, Trombone, Saxophone, Bagpipe };
     COLOURS      Walls, Ceiling, Roof;
     INSTRUMENTS[] JazzBand = new INSTRUMENTS[41];

The big idea here is to introduce a distinct, usually rather small, set of values which a variable can legitimately be assigned. Internally these values are represented by small integers - in the case of the CarHireFleet example the "value" of Golf would be 0, the value of Tazz would be 1, the value of Sierra would be 2, and so on.

In the C/C++ development of this idea the enumeration, in fact, results in nothing more than the creation of an implicit list of const int declarations. Thus the code

        enum CARS { Golf, Tazz, Sierra, BMW316 } CarHireFleet[101]; 

is semantically completely equivalent to

      const int Golf = 0; const int Tazz = 1; 
      const int Sierra = 2, const int BMW316 = 3; 
      int CarHireFleet[101]; 

and to all intents and purposes this gains very little, other than possible readability - an assignment like

      CarHireFleet[N] = Tazz; 

might, of course, convey more to a reader than the semantically identical

      CarHireFleet[N] = 1; 

In the much more rigorous Pascal and Modula-2 approach one would not be allowed this freedom; one would be forced to write

      CarHireFleet[N] := Tazz; 

Furthermore, whereas in C/C++ one could write code with rather dubious meaning like

      CarHireFleet[4] = 45;             /* Even though 45 does not correspond to any known car! */ 
      CarHireFleet[1] = Tazz / Sierra;  /* Oh come, come! */ 
      Walls = Sierra;                   /* Whatever turns you on is allowed in C++ */ 

in Pascal, Modula-2, Java and C# one cannot perform arithmetic on variables of these types directly, or assign values of one type to variables of an explicitly different type. In short, the idea is to promote "safe" programming - if variables can meaningfully only assume one of a small set of values, the compiler (and/or run-time system) should prevent the programmer from writing (or executing) meaningless statements.

Clearly there are some operations that could have sensible meaning. Looping and comparison statements like

      if (Walls == Indigo) Redecorate(Blue); 

or

      for (Roof = Red; Roof <= Violet; Roof++) DiscussWithNeighbours(Roof); 

or

      if (JazzBand[N] >= Saxophone) Shoot(JazzBand[N]); 

might reasonably be thought to make perfect sense - and would be easy to "implement" in terms of the underlying integer values.

In fact, the idea of a limited enumeration is already embodied in the standard character (and, in some languages, Boolean) types - type Boolean is, in a sense the enumeration of the values {0, 1} identified as {false, true}, although this type is so common that the programmer is not required to declare the type explicitly. Similarly, the character type is really an enumeration of a sequence of (typically) ASCII codes, and so on.

Although languages that support enumeration types forbid programmers from abusing variables and constants of any enumeration types that they might declare, the idea of "casting" allows programmers to bypass the security where necessary. The reader will be familiar with the (rather strange) notation in the C family of languages that allows code like

      char uc = (char) 65;                 // 'A' 
      char lc = (char) ( (int) uc + 32 );  // 'a' 

In Pascal and Modula-2 a standard function ORD(x) can be applied to a value of an enumeration type to do little more than cheat the compiler into extracting the underlying integral value. This, and the inverse operation of cheating the compiler into thinking that it is dealing with a user-defined value when you want to map it from an integer, are exemplified by Modula-2 code like

      IF (ORD(Bagpipe) > 4) THEN ..... 
      Roof := VAL(COLOURS, I + 5)); 

Rather annoyingly, in Pascal and Modula-2 one cannot read and write values of enumeration types directly - one has to use these casting functions and switching statements to achieve the desired effects.

Enumerations are a "luxury" - clearly they are not really needed, as all they provide is a slightly safer way of programming with small integers. Not surprisingly, therefore, they are not found in languages like Oberon (simplified from Modula-2).

In recent times you have studied and extended a compiler for a small language, Parva, in the implementation of which we have repeatedly stressed the ideas and merits of safe programming.

How would you add the ability to define enumeration types in Parva programs and to implement these types, at the same time providing safeguards to ensure that they could not be abused? Initially, strive to develop a system that will allow

You may wish to read up a little more on enumeration types as they are used in languages like Modula-2 and Pascal. Enumeration types in Java and C# are rather more complex in their full ramifications, however.


Hints: