Computer Science 3 - 2008 - Test on Prac 19

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

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

Pat Terry, 63T0844. Student name and student number! And it is genuine. Note that your student number normally just has two digits (63) for the year of first registration, the initial letter of the surname, and a four digit number for the number of applications that had been received when yours was processed. the number is often quoted with a further leading 6 (as in 663T0844), but this is a hangover from the days when we had campus 6 (Grahamstown) and campus 8 (East London). No, I don't know, so don't ask!

2. Very briefly - 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 performs "synthesis" - it uses this representation to generate object code for the program. See pages 18 - 21 of the text.

3. 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         |                      |                            |
  `-------------------------------------------------------------------------------'

4. Explain very simply what you understand by the terms syntax, static semantics, and dynamic semantics, and what distinguishes one from another. [6]

Syntax rules describe the arrangement or form that program source, statements and expressions can take or in which they can be combined (for example, in Java source code a while statement requires a parenthesized expression and another statement to follow the parentheses). Static semantic rules relate to the way in which types, expressions identifiers and so on interact (for example, in a while statement the parenthesized expression must be one composed of operands and operators that not only satisfy syntactic rules, but which also evaluates to a Boolean result). Dynamic semantics describe what happens when the program (or statement) executes (for example, in the case of a while statement the subsidiary statement will be executed, possibly repeatedly, for as long as a re-evaluation of the expression yields a Boolean value of true).

5. Consider the silly Modula program below, along with its translation into C performed using the translator X2C. (Even if you are not expert in Modula the intent of this piece of code should be pretty obvious.)

Why do you suppose the X2C translation makes so much of the assignment statement List[I] := I + 10 ? [2]

There are two points - firstly it is doing "range checking" to ensure that the value assigned to List[I-5] is correctly within the range 5 to 15. Those of you who have grown up in the unprotected world of C++ have never learnt how useful this sort of check can be when debugging programs, unfortunately. Secondly, it has had to map the subscripts 5 ... 15 to 0 ... 10.

Why do you suppose the X2C translation has introduced such long-winded variable names? [2]

X2C has done this so as to try to ensure that "name clashes" (maybe even with key words, which differ in the two languages) do not arise in the C++ code that is produced. Each name has been synthesized from the "simple" name and the "module name" - this may make for long identifiers but helps programmers and compilers identify whence they have come (quickly - if I gave you a piece of C++ code with a function call to a function called offsetof, which library does it come from?).

  MODULE Silly;
    TYPE
      SUBSCRIPTS = CARDINAL [5 .. 15];
    VAR
      I, J : SUBSCRIPTS;
      List : ARRAY SUBSCRIPTS OF SUBSCRIPTS;
    BEGIN
      FOR I := 5 TO J DO List[I] := I + 10 END
    END Silly.


  #include "X2C.h"

  typedef X2C_CARD16 Silly_SUBSCRIPTS;
  static Silly_SUBSCRIPTS Silly_I;
  static Silly_SUBSCRIPTS Silly_J;
  typedef Silly_SUBSCRIPTS Silly_ANONYM1[11];
  static Silly_ANONYM1 Silly_List;

  int main(int argc, char **argv)
  {
    X2C_BEGIN(argc,argv);
    { /* FOR */
      static Silly_SUBSCRIPTS Silly_V1;
      Silly_I = 5U;
      Silly_V1 = Silly_J;
      if (Silly_I <= Silly_V1)
      {
        for (;;)
        {
          Silly_List[(X2C_CARD16) Silly_I - 5U] = (X2C_CARD16) X2C_CHKU((Silly_I + 10U),5U,15U);
          if (Silly_I == Silly_V1) break;
          ++Silly_I;
        }
      }
    } /* END FOR */
    return 0;
  }

  /* END Silly. */

6. 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, 2008

  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, 2008

  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