Question 1: =========== The answer in the object below starts in the left-most column and attributes the syntax to specific locations in the object as determined by the class definitions. The answer without embedding it in the object is: A = B. B : C | D *common* "." [Y] ":". X ~ R {R}. (see /proj/asl/lieber/java/java-envs/j-mine-syntax/c-plus-plus) What is the sentence corresponding to this object: : Program ( < classgraph > : ClassGraph ( < classes > : ClassDef_DList { : ClassDef ( < paramclassname > : ParamClassName ( < classname > : ClassName ( A < name > : Ident "A" ) ) = < classparts > : ConstructionClass ( < parts > : PartOrSyntax_List { : Part ( < classspec > : ClassSpec ( < classname > : ClassName ( B < name > : Ident "B" ) ) ) } . < parents > : ClassParents ( ) ) ) , : ClassDef ( < paramclassname > : ParamClassName ( < classname > : ClassName ( B < name > : Ident "B" ) ) : < classparts > : AlternationClass ( < subclasses > : Subclass_Barlist { : Subclass ( < classspec > : ClassSpec ( < classname > : ClassName ( C < name > : Ident "C" ) ) ) , | : Subclass ( < classspec > : ClassSpec ( < classname > : ClassName ( D < name > : Ident "D" ) ) ) } *common* < common > : Common ( ) < parts > : PartOrSyntax_List { : PlainSyntax ( "." < string > : String "." ) , [ : OptionalPart ( < part > : Part_Sandwich ( < first > : Syntax_List { } < inner > : Part ( < classspec > : ClassSpec ( < classname > : ClassName ( Y] < name > : Ident "Y" ) ) ) < second > : Syntax_List { } ) ) , : PlainSyntax ( ":" < string > : String ":" ) } . < parents > : ClassParents ( ) ) ) , : ClassDef ( < paramclassname > : ParamClassName ( < classname > : ClassName ( X < name > : Ident "X" ) ) ~ < classparts > : RepetitionClass ( < sandwiched > : RepeatedPart_Sandwich ( < first > : Syntax_List { } < inner > : RepeatedPart ( < nonempty > : ClassSpec ( < classname > : ClassName ( R < name > : Ident "R" ) ) { < repeated > : ClassSpec_Sandwich ( < first > : Syntax_List { } < inner > : ClassSpec ( < classname > : ClassName ( R < name > : Ident "R" ) ) < second > : Syntax_List { } ) ) }. < second > : Syntax_List { } ) ) ) } ) ) A = B . B : C | D * common * "." [ Y ] ":" . X ~ R { R } . Question 2 ========== (see /proj/asl/lieber/java/java-envs/j-mine and /proj/asl/lieber/java/java-envs/j-mine2-clean) ANSWER: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Put the CountingVisitor into PrintConsClassesVisitor. PrintConsClassesVisitor = ClassNameTranspVisitor CountingVisitor. 2. Create new PrintConsClassesVisitor-object: ClassNameTranspVisitor cntv = new ClassNameTranspVisitor(); CountingVisitor cv = new CountingVisitor( new Integer(0)); PrintConsClassesVisitor pcv = new PrintConsClassesVisitor(cntv,cv); 3. rearrange visitors: CountingVisitor before ClassNameTranspVisitor or initialize CountingVisitor.total with 1. traversal toAllConstructionClasses( ClassNameTranspVisitor cntv, CountingVisitor cv, PrintConsClassesVisitor pcv ) { to ConstructionClass; } 4. Update PrintConsClassesVisitor before ConstructionClass (@ System.out.println( this.get_cv().get_total().intValue() + "." + // <- new "construction class " + this.dig_out().get_name()) ; @) } The difference between the two files is: diff demjava.beh ../j-mine/demjava.beh 14a16 > PrintConsClassesVisitor pcv = new PrintConsClassesVisitor(cntv); 17,19c19 < PrintConsClassesVisitor pcv = new PrintConsClassesVisitor(cntv,cv); < this.toAllConstructionClasses(cntv,cv,pcv); } @) < // this.toAllConstructionClasses(pcv,cntv,cv); } --- > this.toAllConstructionClasses(pcv,cntv,cv); } @) 21c21 < // PrintConsClassesVisitor pcv, --- > PrintConsClassesVisitor pcv, 23,24c23 < CountingVisitor cv, < PrintConsClassesVisitor pcv --- > CountingVisitor cv 53,54c52 < (@ System.out.println(this.get_cv().get_total().intValue() < + "." +" construction class " + --- > (@ System.out.println("construction class " + Entire program is below. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ALTERNATIVE ANSWER 1. Change order of visitor parameters in traversal toAllConstructionClasses CountingVisitor must be before PrintConsClassesVisitor 2. Change code in CountingVisitor: CountingVisitor { before ConstructionClass (@ this.set_total(new Integer(total.intValue() + 1)); System.out.print(this.get_total().intValue() + "."); //<- new @) (makes CountingVisitor less reusable) Program { (@ /** Prints all construction class names in a given class graph. A program to show second order visitors. Requires a visitor to traverse a visitor. */ public static void main(String args[]) throws Exception { Program p = parse(System.in); p.print_and_count_cons_classes(); } @) (@ public void print_and_count_cons_classes() { ClassNameTranspVisitor cntv = new ClassNameTranspVisitor(); CountingVisitor cv = new CountingVisitor( new Integer(0)); PrintConsClassesVisitor pcv = new PrintConsClassesVisitor(cntv,cv); this.toAllConstructionClasses(cntv,cv,pcv); } @) // this.toAllConstructionClasses(pcv,cntv,cv); } traversal toAllConstructionClasses( // PrintConsClassesVisitor pcv, ClassNameTranspVisitor cntv, CountingVisitor cv, PrintConsClassesVisitor pcv ) { to ConstructionClass; } } CountingVisitor { before ConstructionClass (@ this.set_total(new Integer(total.intValue() + 1)); @) after Program (@ System.out.println(this.get_total().intValue() + " = number of construction classes"); @) } ClassNameTranspVisitor { before ClassDef (@ this.set_pcn(host.get_paramclassname()); @)} ClassNameRetVisitor { before ClassName (@ this.set_cn(host); @)} PrintConsClassesVisitor { traversal toClassName( ClassNameRetVisitor cnrv) { bypassing -> *,parameters,* to ClassName;} (@ /** Retrieve the class name from the visitor. Requires a traversal from the visitor class which itself needs a visitor. */ public ClassName dig_out() { ClassNameRetVisitor cnrv = new ClassNameRetVisitor(); this.toClassName(cnrv); return cnrv.get_cn(); } @) before ConstructionClass (@ System.out.println(this.get_cv().get_total().intValue() + "." +" construction class " + this.dig_out().get_name()) ; @) } // this.get_cntv().get_pcn().get_classname().get_name()) ; @) } In this question you are given an adaptive program and you are asked to modify the behavior. The program below prints out the names of all construction classes and the total number of construction classes. The program uses the following visitor classes: CountingVisitor = Integer. ClassNameRetVisitor = ClassName. ClassNameTranspVisitor = ParamClassName. PrintConsClassesVisitor = ClassNameTranspVisitor. For example, for input //================================================== Computer = List(Equipment). List(X) ~ { X }. Equipment : Card | Drive | FloppyDisk | CompositeEquipment *common* Currency "discount" Currency. Card = "card" . Drive = "drive" . FloppyDisk = "floppy" . CompositeEquipment : Cabinet | Chassis | Bus *common* "(" List(Equipment) ")". Cabinet = "cabinet" . Chassis = "chassis" . Bus = "bus" . PricingVisitor = Currency. Currency = "$" Integer. InventoryVisitor = Inventory. Inventory = List(Equipment). Main = . Test(S) = S. //================================================== the program produces the following output: construction class Computer construction class Card construction class Drive construction class FloppyDisk construction class Cabinet construction class Chassis construction class Bus construction class PricingVisitor construction class Currency construction class InventoryVisitor construction class Inventory construction class Main construction class Test 13 = number of construction classes ======================================== Describe how to change the program so that it produces the following output, i.e., the program needs to number the classes: 1. construction class Computer 2. construction class Card 3. construction class Drive 4. construction class FloppyDisk 5. construction class Cabinet 6. construction class Chassis 7. construction class Bus 8. construction class PricingVisitor 9. construction class Currency 10. construction class InventoryVisitor 11. construction class Inventory 12. construction class Main 13. construction class Test 13 = number of construction classes Describe how to modify demjava.beh or demjava.java. Below we give demjava.beh and the relevant parts of demjava.java. demjava.beh Program { (@ /** Prints all construction class names in a given class graph. A program to show second order visitors. Requires a visitor to traverse a visitor. Also counts the total number of construction classes. */ public static void main(String args[]) throws Exception { Program p = parse(System.in); p.print_and_count_cons_classes(); } @) (@ public void print_and_count_cons_classes() { ClassNameTranspVisitor cntv = new ClassNameTranspVisitor(); PrintConsClassesVisitor pcv = new PrintConsClassesVisitor(cntv); CountingVisitor cv = new CountingVisitor( new Integer(0)); this.toAllConstructionClasses(pcv,cntv,cv); } @) traversal toAllConstructionClasses( PrintConsClassesVisitor pcv, ClassNameTranspVisitor cntv, CountingVisitor cv ) { to ConstructionClass; } } CountingVisitor { before ConstructionClass (@ this.set_total(new Integer(total.intValue() + 1)); @) after Program (@ System.out.println(this.get_total().intValue() + " = number of construction classes"); @) } ClassNameTranspVisitor { before ClassDef (@ this.set_pcn(host.get_paramclassname()); @)} ClassNameRetVisitor { before ClassName (@ this.set_cn(host); @)} PrintConsClassesVisitor { traversal toClassName( ClassNameRetVisitor cnrv) { bypassing -> *,parameters,* to ClassName;} (@ /** Retrieve the class name from the visitor. Requires a traversal from the visitor class which itself needs a visitor. */ public ClassName dig_out() { ClassNameRetVisitor cnrv = new ClassNameRetVisitor(); this.toClassName(cnrv); return cnrv.get_cn(); } @) before ConstructionClass (@ System.out.println("construction class " + this.dig_out().get_name()) ; @) } // this.get_cntv().get_pcn().get_classname().get_name()) ; @) } Question 3: (see /proj/asl/lieber/java/java-envs/j-mine-both) ANSWER: No change to class dictionary. .beh changes: 1. Replace the first two occurrences of ConstructionClass by {ConstructionClass, AlternationClass} The last occurrence of ConstructionClass is left unchanged and we add: 2. before AlternationClass (@ System.out.println("alternation class " + this.dig_out().get_name()) ; @) (Alternatively, and more elegantly, add a visitor class ClassDistinctionVisitor to avoid code duplication.) 3. Also delete "construction" in " = number of construction classes". The difference beween the two behavior files is: diff demjava.beh ../j-mine/demjava.beh 24c24 < ) { to {ConstructionClass, AlternationClass}; } --- > ) { to ConstructionClass; } 27c27 < before {ConstructionClass,AlternationClass} --- > before ConstructionClass 31c31 < " = number of classes"); @) } --- > " = number of construction classes"); @) } 53,55d52 < this.dig_out().get_name()) ; @) < before AlternationClass < (@ System.out.println("alternation class " + The complete program is: Program { (@ /** Prints all construction class names in a given class graph. A program to show second order visitors. Requires a visitor to traverse a visitor. Also counts the total number of construction classes. */ public static void main(String args[]) throws Exception { Program p = parse(System.in); p.print_and_count_cons_classes(); } @) (@ public void print_and_count_cons_classes() { ClassNameTranspVisitor cntv = new ClassNameTranspVisitor(); PrintConsClassesVisitor pcv = new PrintConsClassesVisitor(cntv); CountingVisitor cv = new CountingVisitor( new Integer(0)); this.toAllConstructionClasses(pcv,cntv,cv); } @) traversal toAllConstructionClasses( PrintConsClassesVisitor pcv, ClassNameTranspVisitor cntv, CountingVisitor cv ) { to {ConstructionClass, AlternationClass}; } } CountingVisitor { before {ConstructionClass,AlternationClass} (@ this.set_total(new Integer(total.intValue() + 1)); @) after Program (@ System.out.println(this.get_total().intValue() + " = number of classes"); @) } ClassNameTranspVisitor { before ClassDef (@ this.set_pcn(host.get_paramclassname()); @)} ClassNameRetVisitor { before ClassName (@ this.set_cn(host); @)} PrintConsClassesVisitor { traversal toClassName( ClassNameRetVisitor cnrv) { bypassing -> *,parameters,* to ClassName;} (@ /** Retrieve the class name from the visitor. Requires a traversal from the visitor class which itself needs a visitor. */ public ClassName dig_out() { ClassNameRetVisitor cnrv = new ClassNameRetVisitor(); this.toClassName(cnrv); return cnrv.get_cn(); } @) before ConstructionClass (@ System.out.println("construction class " + this.dig_out().get_name()) ; @) before AlternationClass (@ System.out.println("alternation class " + this.dig_out().get_name()) ; @) } // this.get_cntv().get_pcn().get_classname().get_name()) ; @) } Modify the program of question 2, so that it prints both construction and alternation classes and the total number of classes: The output, after modification of the program, should be: construction class Computer alternation class Equipment construction class Card construction class Drive construction class FloppyDisk alternation class CompositeEquipment construction class Cabinet construction class Chassis construction class Bus construction class PricingVisitor construction class Currency construction class InventoryVisitor construction class Inventory construction class Main construction class Test 15 = number of classes Question 4: =========== (from /proj/asl/lieber/java/java-envs/j-mine-syntax) There are two questions, one about output and the other about the classes which get a traversal function. Consider the following program: Program { (@ public static void main(String args[]) throws Exception { Program p = parse(System.in); p.toAllPlainSyntax(new PrintVisitor()); } @) traversal toAllPlainSyntax(PrintVisitor v) { to PlainSyntax; } } PrintVisitor { before PlainSyntax (@ System.out.println(host.get_string() ); @) } Question 4.1: ============= What is the output produced for the following input? ANSWER: Print all tokens (appear between " and " in class dictionary) discount card drive floppy ( ) cabinet chassis bus $ // computer.cd -- class dictionary for Computer example Computer = List(Equipment). List(X) ~ { X }. Equipment : Card | Drive | FloppyDisk | CompositeEquipment *common* Currency "discount" Currency. Card = "card" . Drive = "drive" . FloppyDisk = "floppy" . CompositeEquipment : Cabinet | Chassis | Bus *common* "(" List(Equipment) ")". Cabinet = "cabinet" . Chassis = "chassis" . Bus = "bus" . PricingVisitor = Currency. Currency = "$" Integer. InventoryVisitor = Inventory. Inventory = List(Equipment). Main = . Test(S) = S. Question 4.2: ============= For which classes does the above program define a traversal function? Give the class names in alphabetical order. It is best to start in the UML class diagram and then go to the textual form. The textual form tells us about the repetition classes. I take here file simple-1.trv produced by the C++ system in directory inter-pps for file simple.pp: *operation* void f() *traverse* *from* Program *to* PlainSyntax *wrapper* PlainSyntax (@ @) Traversal directive: *from* { Program } *to* { PlainSyntax } Propagation graph for traversal: *source* { Program } *target* { PlainSyntax } *paths* Program = < classgraph > ClassGraph . ClassGraph = < classes > ClassDef_DList . ClassDef = < classparts > ClassParts . ClassParts : ConstOrAltClass | RepetitionClass *common* . ConstOrAltClass : ConstructionClass | AlternationClass *common* < parts > PartOrSyntax_List . PartOrSyntax : OptionalPart | Syntax *common* . OptionalPart = < part > Part_Sandwich . ConstructionClass = . AlternationClass = . RepetitionClass = < sandwiched > RepeatedPart_Sandwich . RepeatedPart = < repeated > ClassSpec_Sandwich . Syntax : PlainSyntax *common* . PlainSyntax = . ClassDef_DList ~ ClassDef { ClassDef } . PartOrSyntax_List ~ { PartOrSyntax } . Part_Sandwich = < first > Syntax_List < second > Syntax_List . RepeatedPart_Sandwich = < first > Syntax_List < inner > RepeatedPart < second > Syntax_List . ClassSpec_Sandwich = < first > Syntax_List < second > Syntax_List . Syntax_List ~ { Syntax } . ///////////////////////////////////////////////////////////////// // There are 80 classes in total // There are 19 classes in the propagation graph // There are 61 classes not in the propagation graph After sorting: Answer only requires class names. AlternationClass = . ClassDef = < classparts > ClassParts . ClassDef_DList ~ ClassDef { ClassDef } . ClassGraph = < classes > ClassDef_DList . ClassParts : ConstOrAltClass | RepetitionClass *common* . ClassSpec_Sandwich = < first > Syntax_List < second > Syntax_List . ConstOrAltClass : ConstructionClass | AlternationClass *common* < parts > PartOrSyntax_List . ConstructionClass = . OptionalPart = < part > Part_Sandwich . PartOrSyntax : OptionalPart | Syntax *common* . PartOrSyntax_List ~ { PartOrSyntax } . Part_Sandwich = < first > Syntax_List < second > Syntax_List . PlainSyntax = . Program = < classgraph > ClassGraph . RepeatedPart = < repeated > ClassSpec_Sandwich . RepeatedPart_Sandwich = < first > Syntax_List < inner > RepeatedPart < second > Syntax_List . RepetitionClass = < sandwiched > RepeatedPart_Sandwich . Syntax : PlainSyntax *common* . Syntax_List ~ { Syntax } . // There are 61 classes not in the propagation graph // There are 19 classes in the propagation graph // There are 80 classes in total /////////////////////////////////////////////////////////////////