Computer Science 301 - 2014 - Test 6 - Attributed Grammars

You may use your textbook, or refer to material on the website if you like, but not your solutions to prac 24. Keep your solutions as simple as possible! Answer on the question sheet (4 sides)

1. I have really lost it this week. I cannot even remember the question! But what is the answer?

2. Deja vu all over again. Remember those awful trains for which you struggled to find an LL(1) grammar that imposed safety constraints (no fuel truck just behind a loco, or immediately in front of a coach)? Well, here is a much simpler grammar that does not impose safety syntactically - but with appropriate semantic actions the checking is easily performed. Show how this can be done. Use the spread-out grammar on page 4, keep it simple, and note that there is no need to pass parameters between methods.

    COMPILER Train $CN
    /* Grammar to describe a simple railway train */

    IGNORE CHR(0) .. CHR(31)

    PRODUCTIONS
      Train     = LocoPart [ [ GoodsPart ] HumanPart ] SYNC "." .
      LocoPart  = "loco" { "loco" } .
      GoodsPart = Truck { Truck } .
      HumanPart = "brake" | "coach" { "coach" } .
      Truck     = ( "closed" | "open" | "fuel" ) .
    END Train.

3. And deja vu once more! By now you are well acquainted with my family, having seen a grammar to describe families. Slightly simplified (not bothering about possessions or funny names with hyphens or quotes) it looked like this.

    COMPILER Family $CN
    /* Describe a family */

    CHARACTERS
      uLetter    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" .
      lLetter    = "abcdefghijklmnopqrstuvwxyz" .

    TOKENS
      name       = uLetter { lLetter } .

    IGNORE CHR(0) .. CHR(31) .

    PRODUCTIONS
      Family      = { Generation } Surname { Generation } "." .
      Surname     = "Surname" ":" name { name } .
      Generation  = ( "Parents" | "Grandparents" | "Children" | "Grandchildren" ) ":" NameList .
      NameList    = OnePerson { "," OnePerson } .
      OnePerson   = name { name } [ "(" "deceased" ")" ] { Description } [ Spouse ] .
      Spouse      = "=" name { name } .
      Description = "[" Relative "of" OnePerson "]" .
      Relative    =   "son"  | "daughter" | "mother"  | "father" | "sister"
                    | "brother" | "wife" | "husband"  | "partner" | "mistress" .
    END Family.

As you probably know, families tend to perpetuate first names (politically correctly nowadays called given names) - my father was William David, my grandfather was William, and so was his father. My second name is David and my son is Kenneth David.

How would you add actions to this grammar to (a) record the given names of the members and the number of times that given name had appeared anywhere in the description, printing this information at the end of parsing it (b) check that each Generation has appeared at most once.

Once again - keep it simple. There is no need to pass parameters here either - static fields will work well enough.

For your convenience these grammars have been spread out on the following pages.

4. Can you think of a simple way of telling a newcomer to Coco/R under what conditions it will be necessary to add parameters to methods, and when global (static) fields in the parser would suffice?

    import library.*

    COMPILER Train $CN
    /* Grammar to describe a simple railway train */

    IGNORE CHR(0) .. CHR(31)

    PRODUCTIONS
      Train
      = LocoPart [ [ GoodsPart ] HumanPart ] "." .

      LocoPart
      = "loco"
        { "loco"
        }
      .

      GoodsPart
      = Truck
        { Truck
        }
      .

      HumanPart
      =   "brake"
        | "coach"
          { "coach"
          }
      .

      Truck
      =  (  "closed"
          | "open"
          | "fuel"
         )
      .

    END Train.
    import library.*;    // Don't stress if you prefer C#!
    import java.util.*;

    COMPILER Family $CN
    /* Describe a family */

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



    }









    static ArrayList<GivenName> names = new ArrayList<GivenName>(); // global for simplicity
























    CHARACTERS
      uLetter    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" .
      lLetter    = "abcdefghijklmnopqrstuvwxyz" .

    TOKENS
      name       = uLetter { lLetter } .

    IGNORE CHR(0) .. CHR(31) .







    PRODUCTIONS
      Family
      = { Generation
        }
        Surname
        { Generation
        } "."
      .


      Surname
      = "Surname" ":"
          name
        { name
        }
      .

      Generation
      = (  "Parents"
         | "Grandparents"
         | "Children"
         | "Grandchildren"
        ) ":" NameList .

      NameList
      = OnePerson { "," OnePerson } .

      OnePerson
      =   name
        { name
        }
        [ "(" "deceased" ")" ]
        { Description }
        [ Spouse ] .

      Spouse
      = "="
          name
        { name
        }
      .

      Description
      = "[" Relative "of" OnePerson "]" .

      Relative
      =    "son"  | "daughter" | "mother" | "father" | "sister"
        | "brother" | "wife" | "husband"  | "partner" | "mistress" .

    END Family.


Home  © P.D. Terry