Computer Science 301 - 2003


Test on Prac 22

Please answer all questions in the spaces provided (two sides). Text books may not be used. Max 20

This test is intended to check your ability to wite simple Coco/R specifications. It should only take you about 15 minutes at the most. Textbooks may not be used.

1. Oh dear. I need reminding yet again. What are your name (surname) and student number? [1]

Pat Terry 663T0844

2. Write a Cocol specification for a list of the names of people. Here is an example of such a list:

      Patrick David Terry.
      John-John Cecil Montmorency Stuart-Watkins.
      Britney Spears.  Madonna.
      Paddy O'Toole.
      Charles Philip Arthur George Windsor.
      Camilla Parker-Bowles.  Diana Spencer.

Assume that individual names will start with an upper case letter, and that some names might have hyphens or apostrophes within them (but not at the beginning or end). [7]

The trick here is to note that the surnames all end with a period (fullstop), so that we can write distinctive tokens for first names and surnames:

    COMPILER NameList
    /* A grammar to describe a simple list of people's names
       P.D. Terry, Rhodes University, 2003 */
    CHARACTERS
      ULetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" .
      LLetters = "abcdefghijklmnopqrstuvwxyz" .
    IGNORE CHR(1) .. CHR(31)
    TOKENS
      FirstName = ULetters { LLetters | "'" ULetters | "-" ULetters } .
      Surname   = ULetters { LLetters | "'" ULetters | "-" ULetters } "." .
    PRODUCTIONS
      NameList = { OneName } .
      OneName = { FirstName } Surname .
    END NameList.

Actually it is unlikely that a name with a Hyphen or Apostrophe would have only one letter after it, or that any name would have fewer than 3 letters, although some Asian names are only two letters long. So better tokens might be

    TOKENS
      FirstName = ULetters { LLetters | "'" ULetters | "-" ULetters } LLetters .
      Surname   = ULetters { LLetters | "'" ULetters | "-" ULetters } LLetters  "." .

Note that we have assumed that all letters "within" a name are lower-case. This is not always the case. Singers of Canntaireachd often have surnames like MacGregor or McKay or MacHine, and such names might even be used as internal names, such as James MacPherson Farquharson Colquhoun. We could even invent tokens for these:

    TOKENS
      FirstName =   ULetters { LLetters | "'" ULetters | "-" ULetters } LLetters
                  | ( "Mac" | "Mc" ) ULetters { LLetters | "'" ULetters | "-" ULetters } LLetters .
      Surname   =   ULetters { LLetters | "'" ULetters | "-" ULetters } LLetters  "."
                  | ( "Mac" | "Mc" ) ULetters { LLetters | "'" ULetters | "-" ULetters } LLetters  "." .

There are even English surnames that are written all in lower-case, such as one owned by a headmistress and Old Rhodian in Port Elizabeth, Daphne ffoliott, but this is unusual.

3. You will recall Prac 20 where you had to write simple PVM assembler code. Here is an example of the sort of code you might have written:

       0   DSP    2
       2   LDA    0          ; push address of X
       4   LDC    -1         ; push constant -1
           STO               ;        X := -1
       7   LDA    1          ; push address of Y
       9   LDA    0          ; push address of X
           LDV               ; dereference - value of X (-1) is now on top of stack
      12   STO               ;        Y := X
           PRNS   "finished"
           HLT

in which we draw attention to the following:

(a) only one statement appears on a line
(b) statements may have an optional number at the start
(c) some opcodes (like LDC) take an integer argument, others (like STO) do not.
(d) a line may include a comment
(e) the lines in text files are separated by a CR LF sequence. (CR = CHR(13), LF = CHR(10))

Complete a Cocol specification of this language. Limit your description to the opcodes shown above [12]

There are several ways of doing this. Here is a solution acceptable for this test. The trick here is that we cannot ignore the end-of-line.

    COMPILER PVMAsm
    /* Grammar for subset of PVM assembler language
       P.D. Terry, Rhodes University, 2003 */
    CHARACTERS
      Control     = CHR(0) .. CHR(31) .
      Printable   = ANY - Control .
      Digits      = "0123456789" .
      LF          = CHR(10) .
      CR          = CHR(13) .
    IGNORE CHR(9) + CHR(11) .. CHR(13)
    TOKENS
      Number      = [ "-" ] Digits { Digits } .
      String      = '"' { Printable } '"' .
      EOL         = LF .
      Comment     = ";" { Printable } CR .
    PRODUCTIONS
      PVMAsm      = { Statement } .
      Statement   = [ Number ] Instruction [ Comment ] EOL .
      Instruction = TwoWord | OneWord | PrintString .
      TwoWord     = ( "LDA" | "LDC" | "DSP" ) Number .
      OneWord     = "STO" | "LDV" | "HLT" .
      PrintString = "PRNS" String .
    END PVMAsm.

A better definition for String might incorporate "escape sequences", and one might be tempted to use the COMMENTS feature of Coco/R. Here's what we might come up with:

    COMPILER PVMAsm
    /* Grammar for subset of PVM assembler language
       P.D. Terry, Rhodes University, 2003 */
    CHARACTERS
      Control     = CHR(0) .. CHR(31) .
      Backslash   = CHR(92) .
      Printable   = ANY - Control .
      InString    = Printable - Backslash - '"' .
      Digits      = "0123456789" .
      LF          = CHR(10) .
      CR          = CHR(13) .
    IGNORE CHR(9) + CHR(11) .. CHR(13)
    COMMENTS FROM ";" TO CR
    TOKENS
      Number      = [ "-" ] Digits { Digits } .
      String      = '"' { InString | Backslash Printable } '"' .
      EOL         = LF .
    PRODUCTIONS
      PVMAsm      = { Statement } .
      Statement   = [ Number ] Instruction EOL .
      Instruction = TwoWord | OneWord | PrintString .
      TwoWord     = ( "LDA" | "LDC" | "DSP" ) Number .
      OneWord     = "STO" | "LDV" | "HLT" .
      PrintString = "PRNS" String .
    END PVMAsm.


Home  © P.D. Terry