RHODES UNIVERSITY


Computer Science 301 - 2007 - Programming Language Translation

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


Section B [ 80 marks ]

Free information and initial problem to be solved:

It was in 1988, at the second of a series of conferences held by a group charged with developing a rigorous standard for the definition of Modula-2, that one Susan Eisenbach made a telling observation: "Modula-2 would be the perfect language if we could add just one more feature. The difficulty is that nobody can decide what that one feature should be". She was right. Language designers cannot resist the temptation to add extension after extension to their brainchild.

So it is with Parva.

The Parva language that you have got to know so well currently has three basic data types - integer, boolean and character. Although it makes provision for the use of string constants in read and write statement lists:

    void main() { $C+  // eg00.pav
      int i;
      read("Supply a value for i" , i);
      write("The value you supplied was ", i);
    }

it does not provide for the declaration and use of string variables:

    void main() { $C+  // eg02.pav
      string yourName, myName;
      myName = "Pat";
      read("What is your name? ", yourName);
      write(myName, " is pleased to meet ", yourName);
    }

As the first step in this examination you are invited to extend a version of the Parva compiler to incorporate a string type.

Implementations of languages that support a string type usually come with a complete support library of useful functions. Within the time constraints of the examination it will not be possible to provide a full implementation of the string type, but your system should be able to do the following at least:

(a) Declare variables and arrays of type string
(b) Assign string literals to such variables, and values of string variables to other string variables
(c) Read and write values for strings
(d) Compare two strings for equality or inequality
(e) Provide a function for converting a string to UPPERCASE
(f) Perform any necessary semantic and constraint checking on strings.

Here is a variation on the program given earlier, showing the use of some of these features:

    void main() { $C+  // eg06.pav
      string yourName, myName, theBoff;
      myName = "Pat";
      theBoff = myName;
      read("What is your name? ", yourName);
      write(myName, " is pleased to meet ", yourName);
      if (myName != yourName)
        writeLine(" (Our names are different)");
      if (upper(theBoff) == upper(yourName))
        writeLine("(Our uppercased names are the same)");
    }


Hints:

(a) The examination kit includes all the files needed to build a working Parva compiler similar to the one used in your last practical exercise (it does not have all the extensions you were asked to make in that practical, and you need not bother to try to add all of those extensions again). As usual, there is a Java version and an equivalent C# version. You may use either version.

(b) It also includes various simple test programs of the sort outlined above (in files named egXX.pav)

(c) The original Parva compiler stores string constants character by character in high memory, as you should remember. However, it is suggested that you do not try to extend that idea. Rather create a "string pool" dynamically, using the ArrayList class. Store string values in this pool, and use the index into the pool as the "value" of a string. A synopsis of the ArrayList class is given below.

(d) Later in the day - at 16h00 - we shall release more information, to help those of you who may not have completed the exercise to do so. Section B of the examination tomorrow will include a set of unseen questions probing your understanding of the system.

(e) Rest assured that you will not be expected to reproduce a complete Parva compiler from memory under examination conditions, but you may be asked to make some additions or improvements to the system developed today. You will not be asked to make the exact extensions of the last practical again.

(f) Remember Einstein's Advice: "Keep it as simple as you can but no simpler" and Terry's Corollary: "For every apparently complex programming problem there is an elegant solution waiting to be discovered".


Simple list handling in Java

The following is the specification of useful members of a Java generic list handling class useful in developing translators as discussed in this course.

  import java.util.*;

  class ArrayList
  // Class for constructing a list of objects

    public ArrayList <type> ()
    // Empty list constructor

    public void add(Object o)
    // Appends o to end of list

    public void add(int index, Object o)
    // Inserts o at position index

    public Object get(int index)
    // Retrieves an object from position index

    public Object set(int index, Object o)
    // Stores an object o at position index

    public void clear()
    // Clears all elements from list

    public int size()
    // Returns number of elements in list

    public boolean isEmpty()
    // Returns true if list is empty

    public boolean contains(Object o)
    // Returns true if o is in the list

    public boolean indexOf(Object o)
    // Returns position of o in the list

    public Object remove(int index)
    // Removes the object at position index

  } // ArrayList

Here is a simple Java program using this class:

  import java.util.*;
  import Library.*;

  class ListDemo {

    public static void main(String[] args) {
      ArrayList<String> strList = new ArrayList<String> ();
      for (int i = 0; i < 10; i++)
        strList.add( IO.readWord() );
      IO.writeLine(strList.size() + " items in the list");
      for (int j = strList.size() - 1; j >= 0; j--) {
        IO.write(strList.get(j).toUpperCase() );
        if ( strList.get(j).equals("Pat") ) IO.write(" the rotter");
        IO.writeLine();
      }
    }
  }


Simple list handling in C#

The following is the specification of useful members of a C# list handling class.

  using System.Collections.Generic;

  class List  // Note the change of name from ArrayList in the non-generic form
  // Class for constructing a list of objects

    public List <type>()
    // Empty list constructor

    public int Add(object o)
    // Appends o to end of list

    public object this [int index] {set; get; }
    // Inserts or retrieves an object in position index
    // list[index] = object;  object = list[index]

    public void Clear()
    // Clears all elements from list

    public int Count { get; }
    // Returns number of elements in list

    public boolean Contains(object o)
    // Returns true if o is in the list

    public boolean IndexOf(object o)
    // Returns position of o in the list

    public void Remove(object o)
    // Removes object o from list

    public void RemoveAt(int index)
    // Removes the object at position index

  } // ArrayList

Here is the equivalent simple C# program to the one given above:

  using System.Collections.Generic;
  using Library;

  class ListDemo {

    public static void Main(string[] args) {
      List<string>  strList = new List<string> ();
      for (int i = 0; i < 10; i++)
        strList.Add( IO.ReadWord() );
      IO.WriteLine(strList.Count + " items in the list");
      for (int j = strList.Count - 1; j >= 0; j--) {
        IO.Write( (strList[j]).ToUpper() );
        if (strList[j].Equals("Pat") ) IO.Write(" the rotter");
        IO.WriteLine();
      }
    }
  }


Home  © P.D. Terry