RHODES UNIVERSITY


Computer Science 301 - 2015 - Programming Language Translation

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


Preliminary to the examination

As by now you are well aware, the Parva language and its compiler, as found in the textbook, were designed to serve two roles - firstly, to provide a system in which small, but non-trivial, programs could be written and, secondly, and more importantly, to act as a system which could be extended in numerous ways to allow students to learn some of the techniques of language design and implementation.

The only data types found in the basic version of Parva were integers, booleans, and one-dimensional arrays of elements of those types. During the course you may have seen how the character data type can be added to this quite easily, as well as a few new statement forms.

Over the years, Parva has developed quite a cult following, especially amongst teachers who admire its simplicity. Inevitably some of these keep asking for new features ("it would be perfect", they say, "if you could just add this last little thing", unaware of the recursive nature of their suggestion!).

But one overwhelming request has just come in, a little more attractive than most. Beginners are often taught some programming logic using "Turtle Graphics", and as students at Rhodes University you must have encountered this concept at least once. The request from my contacts is for Turtle Graphics to be added to Parva - and for this to be done within the next 24 hours! Five minutes of brainstorming suggests they would be delighted if they could write little programs like:

     // Simple Graphics Example - Rotating square - eg04

     void Main() {
       InitGraphics(600, 400);
       int i = 1;
       while (i < 20) {
         Forward(100);
         TurnLeft(90); Forward(100);
         TurnLeft(90); Forward(100);
         TurnLeft(90); Forward(100);
         TurnLeft(108);
         i = i + 1;
       }
     } // Main

Here's your chance to show off what you have learned in this course - and to get closer to getting that most prized possession - a degree from Rhodes University.


Where do you go from here?

Take a little time to read through the rest of this document carefully and make sure you understand it before you leap in and start to hack at a "solution".

In adopting the approach suggested below you can (and should) make use of the files provided in the examination kit free1.zip, which you will find on the course website. In particular, you will find, after unzipping this, a structure like that below - and it is recommended that you retain this directory structure

  work\CMake.bat               Script to generate and compile the Parva compiler
  work\CSharp.bat              Script to compile the C# graphics examples

  work\CocoManual.pdf          Documentation
  work\Library.pdf

  work\Coco.exe                Coco compiler and basic frame files
  work\Scanner.frame
  work\Parser.frame
  work\Driver.frame
  work\Parva.atg               Parva grammar
  work\Parva.frame             Parva frame file

  work\Library.cs              Rhodes C# library

  work\Parva\CodeGen.cs        Auxiliary routines for Parva compiler
  work\Parva\PVM.cs
  work\Parva\Table.cs

  work\Parva\TurtleLib.cs      Auxiliary routines for graphics
  work\Parva\GraphicsLib.cs

  work\examples\eg*.cs         Simple C# example turtle graphics programs
  work\examples\eg*.pav        Equivalent Parva example turtle graphics programs

That is, you have been provided with

Firstly, develop the Turtle class (TurtleLib.cs) so that it can be used in a standalone C# program to draw some simple patterns and graphs as exemplified by the various small C# programs in the kit that match their equivalent Parva programs, for example:

     // Simple Graphics Example - Rotating square - eg04

     using System;

     class Program {

       static void Main(string[] args) {
         Turtle.InitGraphics(600, 400);
         int i = 1;
         while (i < 60) {
           Turtle.Forward(100);
           Turtle.TurnLeft(90); Turtle.Forward(100);
           Turtle.TurnLeft(90); Turtle.Forward(100);
           Turtle.TurnLeft(90); Turtle.Forward(100);
           Turtle.TurnLeft(108);
           i = i + 1;
         }

         Console.Write("\nHit Enter to Exit!");
         Console.ReadLine();

       } // Main

     } // Program

Of course, the Turtle class should make use of the supplied GraphicsWindow and GWindow classes. Remember to "keep it as simple as you can, but no simpler". A skeleton file appears on the last page of this handout - restrict yourself to implementing only the features suggested there.

Remember, from an earlier prac, that a call to GraphicsLib.GraphicsWindow(width, height) opens a window in which other drawing functions can take effect. This window is measured in "pixel" units, with an origin (0, 0) at the top left, a horizontal X axis pointing to the right spanning the range (0 ... width) and a vertical Y axis pointing downwards, spanning the range (0 ... height). While in a later development one would want to scale these axes, initially just limit your graphs to work in pixel coordinates - convenient values for width and height would be in the range 400 to 800 (this depends on your screen resolution).

Compiling the simple example C# programs is best done by using the supplied csharp.bat script - for example, from the command line:

           CSharp  Eg01         (compile)
           Eg01                 (execute)

You may have to move the command window clear of the graphics window to be able to see them unobscured.

To close the program and the graphics window you may have to click your mouse on the command window again and then hit the <enter> key. Slightly crude, but this suffices at this stage, as this is only an intermediate step.

Secondly, develop the Parva grammar (Parva.atg) further so that the Parva compiler can parse the various Parva versions of the graphics programs given in the kit. To start with, concentrate on getting the syntax correct, and ignore code generation and execution. Follow the syntactic development with static semantic checking.

When you are ready to attempt code generation, decide on what new PVM codes must be introduced to allow the PVM to access the various routines implemented in your Turtle class. Go on to develop the code generator and the PVM to deal with these codes. Once you are in a position to execute your compiler, try to compile and run a sample of the supplied Parva programs, for example:

           Parva  Eg01.pav         (compile and interpret)

To allow you to test your compiler on programs that are not meant to be executed (merely compiled so as to check the .COD file, syntax or the program listing), remember that the Parva compiler recognizes a -n command line flag, as in

           Parva  voter.pav  -l  -d  -n

which won't call on the PVM to interpret the code after it has been generated. It now also recognizes a -g command line flag, as in

           Parva  Eg03.pav  -l  -d  -g

which will follow a successful compilation with an immediate interpretation without the usual annoying questions.

When you have done all this satisfactorily you should be able to compile and run simple complete correct graphics programs in Parva (and also reject incorrect ones). You will also have a Parva compiler unlike any developed before! If you have time, you might like to explore adding further graphics facilities to the system.

CAUTION: As with all of the "24 hour exam" problems devised over many years, this is a non-trivial exercise, and you will have to think clearly to be able to solve it. By way of assistance, at about 16h30 there will be a further handout. You will need to study all of this material carefully, as the formal examination tomorrow will require you to demonstrate understanding of what you do today. Good luck!

  // TurtleGraphics Library for use with Parva and the PVM (C#)
  // P.D. Terry, Rhodes University, 2015
  // As supplied for the free information phase of the 2015 examination.
  // Feel free to complete, modify and extend this as you like, without
  // modifying the GraphicsLib class.  Notice that all members are static.
  // The exercise and exam will not require more than one turtle object.
  //
  // 2015/08/13

  using GraphicsLib;    // interface onto basic graphics library
  using System;

    public class Turtle {

      static private
        GraphicsWindow w = null;

        // Add variables for storing the state of the (single) turtle
        //  - suit yourself


      // TurtleGraphics ===============================================

      public static void InitGraphics(int width, int height) {
        w = new GraphicsWindow(width, height);

        // .... add further code as necessary

      } // Turtle.InitGraphics

      public static void CloseGraphics() {

        // .... add further parameters and code as necessary

      } // Turtle.CloseGraphics

      public static void Home() {

        // .... add further parameters and code as necessary

      } // Turtle.Home()

      public static void PenUp() {

        // .... add further parameters and code as necessary

      } // Turtle.PenUp

      public static void PenDown() {

        // .... add further parameters and code as necessary

      } // Turtle.PenDown

      public static void TurnLeft() {

        // .... add further parameters and code as necessary

      } // Turtle.TurnLeft

      public static void Forward() {

        // .... add further parameters and code as necessary

      } // Turtle.Forward

      // Line Drawing ===============================================

      public static void Line(int x1, int y1, int x2, int y2) {

        w.DrawLine(x1, y1, x2, y2);

      } // Turtle.Line

    } // class Turtle


Home  © P.D. Terry