Computer Science 3 - 2016 - 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 (4 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. Here is a simple C# statement sequence

if (s != 0 && t/s > 8) ; else IO.WriteLine("wrong");

Is this sequence syntactically correct? If not, why not? [2]

Assuming it is syntactically correct, is this sequence necessarily statically semantically correct? If not why not? [2]

Assuming correctness so far, is it possible for its dynamic semantics to be in error? Explain. [2]

3. What distinguishes a compiler from an assembler? [2]

4. What distinguishes a self-resident compiler from a cross-compiler? [2]

5. Suppose you submit the following simple program to the ParvaToCSharp system.

        const first = 75, second = 60;

        void Main() {
        // Simple example of Parva program to be converted to C#
          int class = 3;
          int mark, firsts;
          read("Supply mark earned", mark);
          if (mark >= first)  { class = 1; firsts = firsts + 1; };
          if (mark >= second) class = 2;
          write(mark, " falls in class ", class);
        } // Main

It would produce the following output. Criticize the translation. What does this tell you about using a tool like this? Do you suppose the tool could have been improved? How? [8]

        using Library;

        class demo {
          static const int first = 75;
          const int second = 60;

          static public void Main(string[] args) {
            int class = 3;
            int mark, firsts;
            { IO.Write("Supply mark earned"); mark = IO.ReadInt(); }
            if (mark >= first) {
              class = 1;
              firsts = firsts + 1;
            };
            if (mark >= second)
              class = 2;
            { IO.Write(mark); IO.Write(" falls in class "); IO.Write(class); }
          } // Main

        } // demo

6. Suppose in your first job as a professional compiler writer you are provided with (a) a compiler for the C language that can run on the PC and (b) the source code of this compiler, also written in C. Draw T diagrams to represent these tools:

      ┌──────────────────────────┐          ┌──────────────────────────┐
      │                          │          │                          │
      │        ──────────>       │          │        ──────────>       │
      │                          │          │                          │
      └───────┐          ┌───────┘          └───────┐          ┌───────┘
              │          │                          │          │
              │          │                          │          │
              │          │                          │          │
              └──────────┘                          └──────────┘

You are asked to write a compiler for the popular new language D (similar to C) to run on the PC and compile D programs to run on the PC. Draw T diagrams showing what you decide to do:

      ┌──────────────────────────┐          ┌──────────────────────────┐
      │                          │          │                          │
      │        ──────────>       │          │        ──────────>       │
      │                          │          │                          │
      └───────┐          ┌───────┴──────────┴───────┐          ┌───────┘
              │          │                          │          │
              │          │         ────────>        │          │
              │          │                          │          │
              └──────────┴───────┐          ┌───────┴──────────┘
                                 │          │
                                 │          │
                                 ├──────────┤
                                 │          │
                                 │          │
                                 └──────────┘

Your supervisor is most impressed at what you produce. So she should be, as you are a survivor of CSC 301. She now sets you another task - write a D compiler for the new HVZ computers that are the latest craze and will replace the PCs next year. Continue to impress her - draw and annotate T diagrams to show how you intend to proceed.

      ┌──────────────────────────┐          ┌──────────────────────────┐
      │                          │          │                          │
      │        ──────────>       │          │        ──────────>       │
      │                          │          │                          │
      └───────┐          ┌───────┴──────────┴───────┐          ┌───────┘
              │          │                          │          │
              │          │         ────────>        │          │
              │          │                          │          │
              └──────────┴───────┐          ┌───────┴──────────┘
                                 │          │
                                 │          │
                                 ├──────────┤
                                 │          │
                                 │          │
                                 └──────────┘



      ┌──────────────────────────┐          ┌──────────────────────────┐
      │                          │          │                          │
      │        ──────────>       │          │        ──────────>       │
      │                          │          │                          │
      └───────┐          ┌───────┴──────────┴───────┐          ┌───────┘
              │          │                          │          │
              │          │         ────────>        │          │
              │          │                          │          │
              └──────────┴───────┐          ┌───────┴──────────┘
                                 │          │
                                 │          │
                                 ├──────────┤
                                 │          │
                                 │          │
                                 └──────────┘



[12]

7. 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 program below should be easy enough to understand!

    // A class tries to guess their lecturer's age
    // P.D. Terry,  Rhodes University, 2016

    using Library;

    class AgeGuess {

      public static void Main (string [] args) {
        int total = 0, guesses = 0;
        IO.Write();
        int guess = IO.ReadInt("Supply first guess (0 stops) ");
        while (guess > 0) {
          total = total + guess;
          guesses++;
          IO.Write("Supply next guess (0 stops) ");
          guess = IO.ReadInt("Supply next guess (0 stops) ");
        }
        int average = total / guesses;
        int actualAge = IO.ReadInt("Supply lecturer's actual age ");
        if (average <= actualAge)
          IO.WriteLine("You flatter me");
        else
          IO.WriteLine("Some DPs will have to be withdrawn");
      } // Main

    } // AgeGuess

Without changing the order of any of the code given here, show how the use of a set can give a very simple way of detecting and reporting (a) whether any students actually guessed the average age (b) whether any students correctly guessed the lecturer's age (c) how many different guesses were made. (Simply add the few necessary extra statements into their correct places.) [8]


Home  © P.D. Terry