Computer Science 301 - 2017


Test on Prac 5 - Week beginning 21 August 2017

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

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

2. Consider the following Cocol grammar, a solution to one of last week's exercises:

  COMPILER Bool $CN
  /* Boolean expression parser
     P.D. Terry, Rhodes University, 2017 */

  CHARACTERS
    letter     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  TOKENS
    variable   = letter .

  IGNORE CHR(0) .. CHR(31)

  PRODUCTIONS
    Bool       = { Expression "=" } .
    Expression = Term { Or Term } .
    Term       = Factor { [ And ] Factor } .
    Factor     = "NOT" Factor | Primary { "'" } .
    Primary    = True | False | variable | "(" Expression ")" .
    True       = "TRUE" | "1" .
    False      = "FALSE" | "0" .
    And        = "AND" | "&" | "." .
    Or         = "OR" | "|" | "+" .
  END Bool.

Develop a scanner and parser for this specification on the lines of this week's exercises.

If you wish, and feel confident, you cqan download the file test5.zip from the usual sites, unpack it, and then complete the code in the file test.cs, finally printing off your solution. Test data is in a file bool.txt and you compile with a provided csharp.bat script as usual.

But there is a 40 minute time restriction - you may find it quicker simply to write in the code in the spaces provided.

    // +++++++++++++++++++++++  token kinds enumeration +++++++++++++++++++++++++

    const int
      noSym     =  0,  orSym      =  1,  andSym     =  2,  notSym  =  3,
      quoteSym  =  4,  lparentSym =  5,  rparentSym =  6,  trueSym =  7,
      falseSym  =  8,  varSym     =  9,  assignSym  = 10,  EOFSym  = 11;

    static char ch;        // look ahead character for scanner

    static void GetChar(); // assume this is provided; it is in the online version

    // note the following possible representations +++++++++++++++++++

    // True       = "TRUE"  | "1" .
    // False      = "FALSE" | "0" .
    // And        = "AND"   | "&" | "." .
    // Or         = "OR"    | "|" | "+" .

    // +++++++++++++++++++++++++++++++ Scanner ++++++++++++++++++++++++++++++++++

    static Token sym;  // look ahead token (has fields sym.kind and sym.val)

    static void GetSym() {
    // Scans for next sym from input
      while (ch > EOF && ch <= ' ') GetChar();

      StringBuilder symLex = new StringBuilder();
      int symKind = noSym;



















































      sym = new Token(symKind, symLex.ToString());
    } // GetSym
    // +++++++++++++++++++++++++++++++ Parser +++++++++++++++++++++++++++++++++++

    static IntSet // you may like to define and use these

      FirstExp         = new IntSet(  ),


      FirstMoreFactors = new IntSet(  );



    static void Accept(int wantedSym, string errorMessage) {
    // Gets next token if the current token matches wantedSym
      if (sym.kind == wantedSym) GetSym(); else Abort(errorMessage);
    } // Accept

    static void Accept(IntSet allowedSet, string errorMessage) {
    // Gets next token if the current token matches one of allowedSet
      if (allowedSet.Contains(sym.kind)) GetSym(); else Abort(errorMessage);
    } // Accept

    static void Bool() {
    // Bool = { Expression "=" }







    } // Bool

    static void Expression() {
    // Expression = Term { Or Term } .









    } // Expression

    static void Term() {
    // Term = Factor { [ And ] Factor } .













    } // Term

    static void Factor() {
    // Factor = "NOT" Factor | Primary { "'" } .

















    } // Factor

    static void Primary() {
    // Primary = True | False | variable | "(" Expression ")" .




















    } // Primary


Home  © P.D. Terry