Computer Science 301 - 2005

Extra tutorials for Swot Week (2) - Attributing grammars

(1) The grammar below attempts to describe your hard-working lifestyle over the last few weeks.

    COMPILER Prac $CN /* Describe progress of a compiler practical */

    CHARACTERS
      letter  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
    TOKENS
      name = letter { letter } .
    IGNORE CHR(0) .. CHR(31)

    PRODUCTIONS
      Prac         = Handouts { Task } "Submit" .
      Handouts     = "PracSheet" { "HintSheet" } .
      Task         = "Attempt" { ConsultTutor  "Attempt" }  .
      ConsultTutor = name .
    END Prac.

As you have complained, you sometimes have trouble finding tutors. As you may not know, we are going to have problem paying them, because they have recently told me that they want to be paid a fee for each occasion they are consulted. So what I need now is a list of tutors with a count of such occasions for each one. What must I add to the grammar to produce me such a list?

Typical output might be as follows (it does not have to be in alphabetic order):

       Kevin      12
       Ingrid      5
       Nyasha      8
       Pat        45

(2) The Cocol grammar below describes a sequence of Regular Expressions (written one to a line) using the conventions discussed during the course.

    COMPILER RE  $CN  /* Regular expression grammar */

    CHARACTERS
      control  = CHR(0) .. CHR(31) .
      noquote1 = ANY - control - "'" .
      noquote2 = ANY - control - '"' .
      meta     = "()*|[]-?+" .
      lf       = CHR(10) .
      simple   = ANY - control - "'" - '"' - meta .
    TOKENS
      atomic  = simple .
      escaped = "'" noquote1 "'" | '"' noquote2 '"' .
      EOL     = lf .

    IGNORE  control - lf

    PRODUCTIONS
      RE         = { Expression EOL } EOF .
      Expression = Term { "|" Term } .
      Term       = Factor { Factor } .
      Factor     = Element [ "*" | "?" | "+" ] .
      Element    = Atom | Range | "(" Expression ")" .
      Range      = "[" OneRange { OneRange } "]" .
      OneRange   = Atom [ "-" Atom ] .
      Atom       = atomic | escaped .
    END RE.

How would you add appropriate actions and attributes so that you could generate a program that would parse a sequence of regular expressions and report on the alphabets used in each one. Input like

            a | b c d | ( x y z )*
            [a-g A-G] [x - z]?
            a? "'" z+

the output should be something like

            Alphabet = a b c d x y z
            Alphabet = A B C D E F G a b c d e f g x y z
            Alphabet = ' a z



  import

  COMPILER Prac $CN
  /* Determine how often each tutor has been consulted during a practical */

  static class Tutor {  // add features as needed
    public String name;













  }

  static ArrayList list = new ArrayList();              // global for simplicity here!

  static int position(String name) {
  // Finds position of entry with search key name in list, or -1 if not found








  }


  CHARACTERS
    letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .

  TOKENS
    name   = letter { letter } .

  IGNORE CHR(0) .. CHR(31)

  PRODUCTIONS
    Prac
    = Handouts
      { Task
      } "Submit"




    .

    Handouts
    = "PracSheet"
      { "HintSheet"
      }
    .

    Task
    = "Attempt"
      { ConsultTutor
        "Attempt"
      }
    .

    ConsultTutor
    =
    name






    .

  END Prac.


COMPILER RE $CN /* Regular expression grammar */ CHARACTERS control = CHR(0) .. CHR(31) . noquote1 = ANY - control - "'" . noquote2 = ANY - control - '"' . meta = "()*|[]-?+" . lf = CHR(10) . simple = ANY - control - "'" - '"' - meta . TOKENS atomic = simple . escaped = "'" noquote1 "'" | '"' noquote2 '"' . EOL = lf . IGNORE control - lf PRODUCTIONS RE = { Expression EOL } EOF . Expression = Term { "|" Term } . Term = Factor { Factor } . Factor = Element [ "*" | "?" | "+" ] . Element = Atom | Range | "(" Expression ")" . Range = "[" OneRange { OneRange } "]" . OneRange = Atom [ "-" Atom ] . Atom = atomic | escaped . END RE.


Home  © P.D. Terry