Computer Science 301 - 2014


Test on Prac 5 - Week beginning 5 May 2014

Please answer all questions in the space provided or on the back sides of the two pages. Max 25

This test is intended to check your ability to write simple scanners and parsers. It should only take you about 30 minutes at the most. Textbooks and computers and notes may not be used.

1. Your spot came up - lucky you! What are your name (surname) and student number? [1]

2. Suppose we have a grammar with the PRODUCTIONS (possibly among others which might make reference to them):

     PRODUCTIONS
        A = { C [ B] } [ B ] .
        B = number | identifier .
        C = '(' string ')' .

Assuming that you have an accept routine like the one you used last week, and a scanner that can recognise tokens that might be described by the enumeration below, complete the parser routines [6]

numSym, identSym, stringSym, lParenSym, rParenSym, EOFSym, noSym

    static void A () {
    //  A = { C [ B ] } [ B ] .











    }

    static void B () {
    //  B = number | identifier .






    }

    static void C () {
    //  C = '(' string ')' .





    }

This is not an LL(1) grammar. Does this totally invalidate the parser? Explain why or why not - perhaps give an input string that might cause trouble. [3]

2. Suppose we wanted to write a scanner to match a specification expressed in Cocol as follows. Only three kinds of tokens are valid - read the specification carefully.

       CHARACTERS
         digit = "0123456789" .
         eol   = CHR(10) .

       COMMENTS FROM ";" TO eol

       IGNORE CHR(1) .. CHR(32)  /* character handler returns CHR(0) as EOF */

       TOKENS
         float   =   digit { digit } '.' { digit }
                   | "." digit { digit } .
         integer = digit { digit } .
         dotdot  = ".." .

The routine must assign either floatSym, intSym or dotdotSym to sym.kind before leaving the routine (or assign the value of noSym if it comes across an invalid token, or eofSym at the end of file). Complete the skeleton code below to show how this might be done. [15]

      static void getSym() {  // Scans for next sym
        StringBuilder symLex = new StringBuilder();
        int symKind = noSym;
        while (ch > EOF && ch <= ' ') getChar();






























Home  © P.D. Terry