Computer Science 3 - 2015 - Test 1 - Parva and some other tools

This should take no longer than 45 minutes and is simply intended to probe whether you have understood the material in the prac questions and the early part of the course. Answer on the question sheet (3 sides).

"The purpose of computing is insight, not numbers" (Richard Hamming)

1. You should get this right! Mine is Pat Terry, 63T0844. What is yours - in the same format? [2]

2. In the practical sessions you (should) have used the Parva2 translator. This was developed at Rhodes by using a compiler generator program called CoCo to produce its C# source code from a so-called "Attribute Grammar" written in a specification language called Cocol, which you will meet in a few weeks' time. This C# code was then compiled with the C# compiler CSC.exe available for the PC. Draw T-diagrams that describe this development. [6]

      ┌──────────────────────────┐          ┌──────────────────────────┐         ┌─────────────────────────┐
      │                          │          │                          │         │                         │
      │        ──────────>       │          │        ──────────>       │         │        ────────>        │
      │                          │          │                          │         │                         │
      └───────┐          ┌───────┴──────────┴───────┐          ┌───────┴─────────┴───────┐         ┌───────┘
              │          │         Coco.exe         │          │                         │         │
              │   Cocol  │ Cocol   ────────>        │          │        ────────>        │         │
              │          │                          │          │                         │         │
              └──────────┴───────┐          ┌───────┴──────────┴───────┐         ┌───────┴─────────┘
                                 │          │                          │         │
                                 │          │                          │         │
                                 ├──────────┤                          ├─────────┤
                                 │          │                          │         │
                                 │          │                          │         │
                                 └──────────┘                          └─────────┘

3. Here is a simple Parva program (assume it is stored in the file Silly.pav and that the compiler can recognise the % operator):

    void Main() {
    // A rather simple Parva program - but what does it calculate?
      int a, b;
      read("Supply a and b (0 stops) ", a, b);  /*  first pair of data */
      while (a != 0) {
        write(a, b, a / b, a % b);
        read(a, b);                             // read the next data pair
      }
    } // Main

(a) What does it attempt to calculate and print? [2]

(b) If you were to translate this program to C# using the Parva2ToCSharp program, it would produce the following code (Silly.cs):

    using Library;

    class Silly {

      static public void Main(string[] args) {
        int a, b;
        { IO.Write("Supply a and b (0 stops) "); a = IO.ReadInt(); b = IO.ReadInt(); }
        while (a != 0) {
          { IO.Write(a); IO.Write(b); IO.Write(a / b); IO.Write(a % b); }
          { a = IO.ReadInt(); b = IO.ReadInt(); }
        }
      } // Main

    } // Silly

This seems to have lost something (the comments) and gained something (extra, apparently unnecessary, braces { } ) - it might be preferable to have generated code like this:

    using Library;

    class Silly {

      static public void Main(string[] args) {
      // A rather simple Parva program - but what does it calculate?
        int a, b;
        IO.Write("Supply a and b (0 stops) ");
        a = IO.readInt(); b = IO.readInt();     /*  first pair of data */
        while (a != 0) {
          IO.Write(a); IO.Write(b); IO.Write(a / b); IO.Write(a % b);
          a = IO.ReadInt(); b = IO.ReadInt();    // read the next data pair
        }
      } // Main

    } // Silly

(c) Is the loss of the comments critical? Why, or why not? [2]

(d) Might the extra { braces }, in fact, be necessary in such translations? Explain! [4]

4. Question 2 mentions four systems programs. Classify each one as being either (a) a native code compiler (b) an interpretive compiler (c) an interpreter (d) a high-level compiler (e) an assembler or (f) a decompiler. [4]

(a) Parva2ToCSharp

(b) Parva

(c) CSC

(d) CoCo

5. A reminder of the methods in the IntSet class appears below.

    public class IntSet {
      public IntSet()
      public IntSet(BitSet s)
      public IntSet(params int[] members) {
      public IntSet(int ... members)
      public void Incl(int i)
      public void Excl(int i)
      public bool Contains(int i)
      public bool IsEmpty()
      public int Members()
      public IntSet Union(IntSet otherSet)
      public IntSet Intersection(IntSet otherSet)
      public IntSet Difference(IntSet otherSet)
      public void Write()
      public string ToString()
    } // IntSet

The code below shows a C# version of a Sieve program for counting the number of primes below a limit read in as data. Show how it could be modified to make use of the IntSet class, in place of the Boolean array it currently uses. It will suffice to mark the changes on the code; there is no need to write out all the code again. [5]


     using Library;

     class Sieve {

       public static void Main (String [] args) {
         IO.write("Supply largest number to be tested ");
         int n = IO.ReadInt();
         bool [] Uncrossed = new bool [n];
         int primes = 0;
         for (int i = 2; i <= n; i++) {
           Uncrossed[i] = true;
         }
         for (int i = 2; i <= n; i++) {
           if (Uncrossed[i]) {
             primes++;
             int k = i;
             do {
               Uncrossed[k] = false;
               k += i;
             } while (k <= n);
           }
         }
         IO.Write(primes + " primes between 1 and " + n);
       } // Main
     } // Sieve


Home  © P.D. Terry