RHODES UNIVERSITY


Computer Science 301 - 2006 - Programming Language Translation

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


How to spend a Special Sunday

From now until about 22h30 tonight, Computer Science 3 students have exclusive use of the Hamilton Laboratory. You are encouraged to throw out anyone else who tries to use it, but we hope that this does not need to happen. At about 22h30 Chris and I will have to move some computers around and prepare things for Monday. If there is still a high demand we shall try to leave some computers for use until later, but by then we hope you will have a good idea of what is involved.

This year the format of the examination is somewhat different from previous years, so as to counter what has happened recently where, essentially, one solution was prepared by quite a large group and then passed around to many others, who had probably not contributed to it in any real way. The problem set below is only part of the story. At 14h30 you will receive further hints as to how this problem should be solved (by then you might have worked this out for yourselves, of course). You will be encouraged to study the problem further, and the solution in detail, because during the examination tomorrow you will be set further questions relating to the system - for example, asked to extend the solution in certain ways. It is my hope that if you have really understood the material today, these questions will have solutions that come readily to mind, but of course you will have to answer these on your own!

My "24 hour exam" problems have all been designed so that everyone should have been able to produce at least the basic solution, with scope given for top students to demonstrate their understanding of the subtler points of parsing, scanning and compiling. Each year I have been astounded at the quality of some of the solutions received, and I trust that this year will be no exception.

Please note that there will be no obligation to produce a machine-readable solution in the examination (in fact doing so is quite a risky thing, if things go wrong for you, or if you cannot type quickly). The tools will be provided before the examination so that you can experiment in detail. If you feel confident, then you are free to produce a complete working solution to Section B during the examination. If you feel more confident writing out a neat detailed solution or extensive summary of the important parts of the solution, then that is quite acceptable. Many of the best solutions over the last few years have taken that form.

During the first few hours you will be able to find the following files in the usual places, or by following links from the course web page:

At 14h30 new versions of the exam kit will be posted (FREE2J.ZIP and FREE2C.ZIP), and a further handout issued, with extra information.

Most of the machines in the labs will work in exactly the way they have done this semester. Some are set up to show you the configuration you can expect tomorrow. It is suggested that you work at one of those machines for a short time to make sure you know what to expect. To work on these machines you proceed as follows

From here on things should be familiar. You could, for example, log onto the D: or J: drive, use UltraEdit use CMAKE and CRUN ... generally have hours of fun. The C# system will work only on the D: drive, however.

But note that the exam set up has no connection with the outside world - no ftp client, telnet client, shared directories - not even a printer!

Today you may use the files and either the "usual" or the "exam" systems in any way that you wish subject to the following restrictions: Please observe these in the interests of everyone else in the class.

(a) When you have finished working, please delete any files from the D: drive, so that others are not tempted to come and snoop around to steal ideas from you.

(b) You are permitted to discuss the problem with one another, and with anybody not on the "prohibited" list.

(c) You are also free to consult books in the library. If you cannot find a book that you are looking for, it may well be the case that there is a copy in the Department. Feel free to ask.

(d) Please do not try to write any files onto the C: directory, for example to C:\TEMP

(e) If you take the exam kit to a private machine you will need to have Java installed (or the .NET framework or equivalent to use the C# version).

I suggest that you do spend some of the next 24 hours in discussion with one another, and some of the time in actually trying out your ideas. You have plenty of time in which to prepare and test your ideas - go for it, and good luck. Remember that tomorrow you may not bring anything into the room other than your student card and writing utensils, and especially not listings, diskettes, memory sticks, text books or cell phones.

If you cannot unpack the file, or have trouble getting the familiar tools to work (unlikely!), you may ask me for help. You may also ask for explanation of any points in the question that you do not understand, in the same way that you are allowed to ask before the start of an ordinary examination. You are no longer allowed to ask me questions about any other part of the course. Sorry; you had your chance earlier, and I cannot allow this without risking the chance of sneak questions and suggestions being called for.

If you cannot solve the problem completely, don't panic. It has been designed so that I can recognize that students have reached varying degrees of sophistication and understanding.


How you will spend a Merry Monday

Before the start of the formal examinations the laboratory will be unavailable. During that time

At the start of the examination session:


Cessation of Hostilities Party

As previously mentioned, Sally and I would like to invite you to an informal end-of-course party at our house on 13 November. There's a map below to help you find your way there. It would help if you could let me know whether you are coming so that we can borrow enough glasses, plates etc. Please post the reply slip into the hand-in box during the course of the day. Time: from 18h30 onwards. Dress: Casual

                            \
                       PDT(8) \      <==================================== here!
                               |\
        \     \               /---\
          \   Bedford       /       \  Constitution St
   Cradock  \     \       /           \
              \     \   /               \      ^Hospital
                \    Pear Lane            \    |
                  |          Worcester St   \  |
          -------------------------------------|
           DSG    |                            |
                  | St Andrew's                |Milner St
                  |                            |
                  |                            |
         Somerset |               African St   |
         Street   |------------------------------------------
                  |        |                      |
                  |        |  The Mall            |
                  |        |                      |
                  |   Rat  |             New St   |
          --------+-------------------------------+----------
      Hamilton    |      |                        |
      Hell Hole   |      |                        | Hill St
                  |      |                  Gino's|
        Rhodes    |      |     High St            |
                ----------------------------------- Cathedral


Section B [ 90 marks ]

As you should know by now, having seen an example in an earlier practical, a pretty-printer is a form of translator that takes source code and translates this into object code that is expressed in the same language as the source. This probably does not sound very useful! However, the main aim is to format the "object code" neatly and consistently, according to some simple conventions, making it far easier for humans to understand.

For example, a system that will read a set of EBNF productions, rather badly laid out as

    Goal={One}.
    One
    = Two "plus" Four ".".

    Four =Five {"is" Five|(Six Seven)}.
    Five = Six [Six       ].
    Six=  Two| Three    |"(*" Four "*)"|'(' Four ')'.

and produce the much neater output

    Goal
      = { One } .

    One
      = Two "plus" Four "." .

    Four
      = Five { "is" Five | ( Six Seven ) } .

    Five
      = Six  [ Six ] .

    Six
      = Two | Three | "(*" Four "*)" | '(' Four ')' .

is easily developed by using the following Cocol grammar

  import Library.*;

  COMPILER EBNF $CN
  /* Pretty-print a set of EBNF productions
     P.D. Terry, Rhodes University, 2006 */

    static int indentation = 0;

    public static void append(String s) {
    // Append string s to results file
      IO.write(s);
    }

    public static void newLine() {
    // Write line mark to results file but leave indentation as before
      int i = 1;
      IO.writeLine();
      while (i <= indentation) { IO.write(' '); i++; }
    }

    public static void indentNewLine() {
    // Write line mark to results file and prepare to indent further lines
    // by two spaces more than before
      indentation += 2;
      newLine();
    }


    public static void exdentNewLine() {
    // Write line mark to results file and prepare to indent further lines
    // by two spaces less
      indentation -= 2;
      newLine();
    }

    public static void indent() {
    // Increment indentation level by 2
      indentation += 2;
    }

    public static void exdent() {
    // Decrement indentation level by 2
      indentation -= 2;
    }

  CHARACTERS
    letter   = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
    lowline  = "_" .
    control  = CHR(0) .. CHR(31) .
    digit    = "0123456789" .
    noquote1 = ANY - "'" - control .
    noquote2 = ANY - '"' - control .

  TOKENS
    nonterminal = letter { letter | lowline | digit } .
    terminal    = "'" noquote1 { noquote1 } "'" | '"' noquote2 { noquote2 } '"' .

  COMMENTS FROM "(*" TO "*)"  NESTED

  IGNORE control

  PRODUCTIONS
    EBNF
    = { Production                        (. newLine(); .)
      } EOF .

    Production                            (. String name; .)
    = NT<out name>                        (. append(name); .)
    "="                                   (. indentNewLine(); append("= "); .)
    Expression  "."                       (. append(" ."); exdentNewLine(); .)
    .

    Expression
    = Term { "|"                          (. append(" | "); .)
      Term } .

    Term
    = Factor {                            (. append(" "); .)
      Factor } .

    Factor                                (. String name; .)
    =   NT<out name>                      (. append(name); .)
      | terminal                          (. append(token.val); .)
      | "["                               (. append("[ "); .)
        Expression "]"                    (. append(" ]"); .)
      | "("                               (. append("( "); .)
        Expression ")"                    (. append(" )"); .)
      | "{"                               (. append("{ "); .)
        Expression "}"                    (. append(" }"); .)
    .

    NT<out String name>
    =  nonterminal                        (. name = token.val; .)
    .

  END EBNF.

The "code generator" is embodied in the simple methods append, indent, exdent and so on, which for simplicity have simply been incorporated at the start of the grammar. A more sophisticated system might direct the output to a named file in the manner familiar to you from earlier practicals.

As the first step in the examination you are invited to experiment with the above system, code for which is included in the examination kit, and then to go on to develop a pretty-printer for programs expressed in the version of Parva described in the following grammar. A version of this grammar - spread out to make the addition of actions easy - can also be found in the examination kit, along with the necessary frame files.

  COMPILER Parva $NC
  /* Parva level 1 grammar  - Coco/R for C# or Java */

  CHARACTERS
    lf         = CHR(10) .
    backslash  = CHR(92) .
    control    = CHR(0) .. CHR(31) .
    letter     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
    digit      = "0123456789" .
    stringCh   = ANY - '"' - control - backslash .
    charCh     = ANY - "'" - control - backslash .
    printable  = ANY - control .
  TOKENS
    identifier = letter { letter | digit | "_" } .
    number     = digit { digit } .
    stringLit  = '"' { stringCh | backslash printable } '"' .
    charLit    = "'" ( charCh   | backslash printable ) "'" .

  COMMENTS FROM "//" TO lf
  COMMENTS FROM "/*" TO "*/"
  IGNORE CHR(9) .. CHR(13)

  PRODUCTIONS
    Parva = "void" Ident "(" ")" "{" { Statement } "}" .
    Statement =   Block | ";" | Assignment | ConstDeclarations | VarDeclarations
                | IfStatement | WhileStatement | DoWhileStatement | ForStatement
                | BreakStatement | ContinueStatement | HaltStatement | ReturnStatement
                | ReadStatement | WriteStatement .
    Block = "{" { Statement } "}" .
    ConstDeclarations = "const" OneConst { "," OneConst } ";" .
    OneConst  = Ident "=" Constant .
    Constant = IntConst | CharConst | "true" | "false" | "null" .
    VarDeclarations = Type OneVar { "," OneVar } ";" .
    Type = BasicType [ "[]" ] .
    BasicType = "int" | "bool" | "char" .
    OneVar = Ident [ "=" Expression ] .
    Assignment = Designator ( "=" Expression | "++" | "--" ) ";" .
    Designator = Ident [  "[" Expression "]" ] .
    IfStatement = "if" "(" Condition ")" Statement [ "else" Statement ] .
    WhileStatement = "while" "(" Condition ")" Statement .
    DoWhileStatement = "do" Statement "while" "(" Condition ")" ";" .
    ForStatement = "for" Ident "=" Expression ( "to" | "downto" ) Expression Statement .
    BreakStatement = "break" ";" .
    ContinueStatement = "continue" ";" .
    HaltStatement = "halt" ";" .
    ReturnStatement = "return" ";" .
    ReadStatement = "read" "(" ReadElement { "," ReadElement } ")" ";" .
    ReadElement = ( StringConst | Designator ) .
    WriteStatement = "write" "(" WriteElement { "," WriteElement } ")" ";" .
    WriteElement = ( StringConst | Expression ) .
    Condition = Expression .
    Expression = AndExp { "||" AndExp } .
    AndExp = EqlExp { "&&" EqlExp } .
    EqlExp = RelExp { EqualOp RelExp } .
    RelExp = AddExp [ RelOp AddExp ] .
    AddExp = MultExp { AddOp MultExp } .
    MultExp = Factor { MulOp Factor } .
    Factor = Primary | "+" Factor | "-" Factor | "!" Factor .
    Primary =   Designator | Constant | "new" BasicType "[" Expression "]"
              | "(" ( "char" ")" Factor | "int" ")" Factor | Expression ")" ) .
    AddOp = "+" | "-" .
    MulOp = "*" | "/" | "%" .
    EqualOp = "==" | "!=" .
    RelOp = "<" | "<=" | ">" | ">=" .
    Ident = identifier .
    StringConst = stringLit .
    CharConst = charLit .
    IntConst = number .
  END Parva.

You should find that this is quite easy to do, leading to a system that might be able to convert a "messy" little Parva program like

     void main(){int b,c,d;
       b=        10;
       while (b>0) { write("b = ", b); b--;
       if (b == 4) write(" hit four!\n")
       ;       else write(" on we go\n");}
       }

into the rather more readable code

     void main() {
       int b, c, d;
       b = 10;
       while (b > 0) {
         write("b = ", b);
         b--;
         if (b == 4)
           write(" hit four!\n");
         else
           write(" on we go\n");
       }
     }

Hints: (a) Do not change the grammar to include any of the other extensions you made in Prac 25. (b) One test for correctness is to take the output from the pretty-printer and pretty-print that again (ie take the output file from one run and use it as the input file for another run). You should, of course, just get the same result again.


Home  © P.D. Terry