Hamilton Madhouse
e-mail: g.hedge-bette@ru.ac.za

November 9, 2003

Dear Computer Science 3 Student

Thank you so much for offering to produce a more acceptable Parva compiler for the students to use in my first year examination tomorrow. As you know, the new compiler will have to be very reliable. Here are some test programs that should help you understand what I need. You'll find these all in the kit that our nice Professor Terry has put together for the occasion.

The first program shows some basic declarations and operations that must allowed for simple characters

  void main () {
  // sample01.pav
  // Very simple allowable operations on characters
    write('z');

    char c = 'a';
    write(c);

    read(c);
    write(c);
  }

The second one is a bit more ambitious and shows the use of an array of characters:

  void main () {
  // sample02.pav
  // Read a sentence and write it backwards
    char[] sentence = new char[1000];
    int i = 0;
    char ch;
    read(ch);
    while (ch != '.') {  // input loop
      sentence[i] = ch;
      i = i + 1;
      read(ch);
    }
    while (i > 0) {      // output loop
      i = i - 1;
      write(sentence[i]);
    }
  }

The third one shows how we can compare characters and control simple loops to do exciting things like print out the alphabet in either direction:

  void main () {
  // sample03.pav
  // Characters can control loops and can be incremented
    char c = 'a';
    while (c <= 'z') {
      write(c);
      c++;
    }
    for c = 'Z' downto 'A' do write(c);
  }

Characters have a limited range, however, and we had better check for overflow somehow:

  void main () {
  // sample04.pav
  // Characters must not be allowed to go out of range
    int i;
    char c = '~';
    for i = 1 to 200 do {
      write(i, ' ', c);
      if (i % 8 == 0) write('\n');
      c++;
    }
  }

Characters are a bit like integers, but sometimes you have to be able to cast integers to characters:

  void main () {
  // sample05.pav
  // Simple type casting for characters
    int i;
    do {
      read("\nSupply ASCII number (65 stops) ", i);
      write(i, '\t', (char) i, (int) i);
    } while (( char ) i != 'A');
  }

This next one is quite cute. Don't show that nice professor man though, as he hates "magic numbers"!! So do we all!

  void main () {
  // sample06.pav
  // Write out the alphabet (with a cast)
    char c;
    for c = (char) 65 to (char) 90 do write(c);
  }

Casting is a bit tricky, as sometimes we might have misconceptions when we try to mix characters and integers:

  void main () {
  // sample07.pav
  // Some mismatched types or type casts
    char c;
    int i;
    c += 'a';
    c += 7;
    i += c;
    c += (char) i;
    i += 'a';
    i += (char) 'a';
    c = - c;
    c = + c;
    c = 1 + 2;
    c = true;
    char [] string = new int[12];
    i = (int) string;
    c = (char) string;
    c = null;
    i = null;
    i++;
    c--;
    for c = 'a' to 90 do write(c);
    for c = 'a' to (int) (char) 90 do write(c);
    for c = 'a' to (char) (int) 'z' do write(c);
  }

As you know, I thought the word const was a cool modifier that one could apply to variable declarations:

  void main () {
  // sample08.pav
  // Variables declared constant can be examined
    const int meaningOfLife = 6 * 7;
    write(meaningofLife);
  }

You have to be careful of these const modifiers though. Here are some funnies to ponder. Isn't this all such fun?

  void main () {
  // sample09.pav
  // Rather meaningless program to illustrate variable declarations
  // that incorporate a const modifier
    const int i = 10;               // acceptable
    const char terminator;          // unacceptable
    const int[] list = new int[i];  // acceptable
    int j = i;
    while (j < 100) {
      const int k = j;              // acceptable
      read(i);                      // unacceptable
      i = j;                        // unacceptable
      list[4] = 5;                  // acceptable
      j = j + list[4];              // acceptable
      list = new int[12];           // unacceptable
    }
  }

Here is that really silly program I was telling you about. It's such an easy mistake to make, and it would really be nice if the compiler could stop this sort of thing for me. Please, please try!

  void main () {
  // sample10.pav
  // Print a set of multiplication tables
    int i, j;
    for i = 1 to 10 do { // <----------------- should be j not i
      for i = 1 to 10 do write (i * j, "\t");
      write("\n");
    }
  }

For loops have their own special problems.

  void main () {
  // sample11.pav
  // Control variables cannot be declared constant
    const int i;
    for i = 1 to 10 do write(i);
  // const variables must not be modifiable after a bad loop ends
    i = 10;
  // for loops must not threaten control variables
    int loop;
    for loop = 10 to 100 do {
      read(loop);
      write(loop);
      loop++;
      write(loop);
      loop += 90;
      write(loop);
      loop = loop + 1;
    }
  }

And here is something you might not have thought of. I didn't, at first.

  void main () {
  // sample12.pav
  // Control variables can be modified once the loop is completed
    int i;
    for i = 1 to 10 do write(i);
    i = 10;
  }

Finally, please make sure that you can run the program I really wanted them to be able to write.

  void main () {
  // sample13.pav
  // Print a set of multiplication tables
    int i, j;
    for i = 1 to 10 do {
      for j = 1 to 10 do write (i * j, "\t");
      write("\n");
    }
  }

Many thanks again.

Yours sincerely

Gambol Hedge-Bette, PhD
Lecturer, Computer Science

P.S. That old meanie!! Professor Rat has just told me that you can try running any of these programs by giving commands like

       PATPARVA  SAMPLE01.PAV 
       PATPARVA  SAMPLE04.PAV -L