Computer Science 3 - 2004 - Test on Prac 19

This should take no longer than 20 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).

1. You should get this right! Mine is Pat Terry, 63T0844. What is yours? [1]

Pat Terry, 63T0844. Student name and student number!

2. And with a little intelligence you should be close to solving the problem posed in yesterday's lecture - when was I born? [1]

The leading "63" is the year I came to Rhodes. I was 18, so I was born in 1945. I was amused to see that many students have never discovered the significance of the first part of the student number. The extra 6 that sometimes appears at the beginning signifies the "Grahamstown Campus" (as opposed to 8 which signified the "East London" one that we also had until recently - no, I don't know why they used 6 and 8 for that purpose!)

3. What happens in the front end of a compiler, and what happens in the back end of a compiler? [2]

The front end performs "analysis" - lexical, syntactic, semantic. It generally results in the production of an implicit or explicit decorated tree representation of the program. The back end uses performs "synthesis" - it uses this representation to generate object code for the program. See pages 9 - 14 of the text.

4. Translators can be classified in various ways - for example, compilers, assemblers, decompilers, disassemblers, high level compilers, interpretive compilers, pretty printers. Complete the following table, which classifies various of the tools you (should have!) used in Practical 19. The first row has been done for you. [10]

Considering that you were supposed to have used these tools and that the classification effectively appeared on the prac sheet, this was very badly done. Several people were clearly simply guessing, or writing the first thing that came to mind. What I wanted was something like this:

  .-------------------------------------------------------------------------------.
  | name   | translator class |     source code      |     object code            |
  |--------+------------------+----------------------+----------------------------|
  | javac  |     compiler     |     Java source      |     Java class files       |
  |--------+------------------+----------------------+----------------------------|
  | bcc    |     compiler     |   C or C++ source    |     Pentium executable     |
  |--------+------------------+----------------------+----------------------------|
  | jad    |    decompiler    |   Java class file    |     Java source            |
  |--------+------------------+----------------------+----------------------------|
  | ilasm  |    assembler     | CIL assembler source |     .NET Assembly          |
  |--------+------------------+----------------------+----------------------------|
  | oolong |    assembler     |   JVM assembler      |     Java class file        |
  |--------+------------------+----------------------+----------------------------|
  | parva  |   interpretive   |   Parva source       |     PVM pseudo code        |
  |        |     compiler     |                      |                            |
  |--------+------------------+----------------------+----------------------------|
  | fpc    |     compiler     |   Pascal source      |     Pentium executable     |
  |--------+------------------+----------------------+----------------------------|
  | gnoloo |   disassembler   |   Java class file    |     JVM assembler source   |
  |--------+------------------+----------------------+----------------------------|
  | jikes  |     compiler     |   Java source        |     Java class files       |
  |--------+------------------+----------------------+----------------------------|
  | ildasm |  disassembler    |   .NET assembly      |     CIL assembler source   |
  |--------+------------------+----------------------+----------------------------|
  | xc     | high level       |   Modula-2 source    |     C source files                       |
  |        | compiler         |                      |                            |
  `-------------------------------------------------------------------------------'

5. Explain very simply what you understand by lexical analysis and what you understand by syntax analysis. [2]

Lexical analysis is the phase responsible for grouping the characters of the source text into tokens. Syntax analysis is the phase of discerning syntactic structure from these tokens. Many people seemed to want to confuse lexical analysis with semantic or constraint analysis, but they are completely different in what they set out to achieve. See pages 9-10 of the text. Most answers here were very confused and suggested that students had simply not read the text at all.

6. What is the significance of the number 16411? [2]

16411 is the smallest prime number for which the first multiple (3282) is outside the range of the 16 bit arithmetic used in Free Pascal (-32768 - 32767). Hence it appears to be the largest prime number that the 16-bit Sieve program can handle. Note that this is not the same as "the largest array" that the 16-bit compiler can handle. In fact one can do better than this - see the prac solution.

7. A programmer has been set the following task: Read a text file of sentences (one per line) and determine which of them are palindromes (palindromes are sentences that read the same from either end, like "otto sees otto").

The first hack has got as far as this, and the deadline looms.

  // Check a file for lines that might contain palindromes
  // Ima Boff, Rhodes University, 2004

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

  class PalinCheck {

    public static void main(String[] args) {

    // first check that commandline arguments have been supplied
      if (args.length != 2) {
        System.out.println("Usage: PalinCheck inputfile outputfile");
        System.exit(1);
      }

    // attempt to open data file
    // still have to do this

    // attempt to open results file
    // still have to do this

    // read and process data file
      while (true) {
        // must find out how to read a string
        String s =
        results.write(s);   // reflect on output
        // not sure about this if statement - wonder who can fix it?
        if (I did not find a string) break;
        String r = Reverse(s.toLowerCase()); // should ignore case
        if r.Equals(s) results.writeLine(" is a palindrome")
      }
    }

    static String Reverse(String s) {
    // returns a string formed by reversing s
    // for example if s = "hello world", returns "dlrow olleh"
      StringBuffer sb = new StringBuffer();
    // Must steal this code from somebody soon!!

      return sb.toString();
    }

  }

Show how to complete the program using the routines you should have become familiar with this week. Simply add the missing code - don't write it all out again. [12]

This was not very well done, which was disappointing - it indicated that many people had not studied the sample file handling code given to you, or knew how to make use of the Library routines.

Points to note:

  // Check a file for lines that might contain palindromes
  // Ima Boff, Rhodes University, 2004

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

  class PalinCheck {

    public static void main(String[] args) {

    // first check that commandline arguments have been supplied
      if (args.length != 2) {
        System.out.println("Usage: PalinCheck inputfile outputfile");
        System.exit(1);
      }

    // attempt to open data file
    InFile data = new InFile(args[0]);
    if (data.openError()) {
      System.out.println("cannot open " + args[0]);
      System.exit(1);
    }

    // attempt to open results file
    OutFile results = new OutFile(args[1]);
    if (results.openError()) {
      System.out.println("cannot open " + args[1]);
      System.exit(1);
    }

    // read and process data file
      while (true) {
        String s = data.readLine();
        results.write(s);       // reflect on output
        if (data.noMoreData()) break;
        s = s.toLowerCase();    // should ignore case
        String r = reverse(s);
        if (r.equals(s)) results.writeLine(" is a palindrome");
        else results.writeLine();
      }
    }

    static String reverse(String s) {
    // returns a string formed by reversing s
    // for example if s = "hello world", returns "dlrow olleh"

/* This is the sort of code I was looking for

      StringBuffer sb = new StringBuffer();
      for (int i = s.length() - 1 ; i >= 0; i--)
        sb.append(s.charAt(i));
      return sb.toString();

*/

    // This is the simplest way to do it, making use of a method
    // that was not mentioned on the web page summary

      StringBuffer sb = new StringBuffer(s);
      return sb.reverse().toString();

    }

  }


Home  © P.D. Terry