Final Exam CSU 670 Spring 2004 April 22, 2004 Karl Lieberherr Open books and open notes ========================================================= Use the provided answer form. Question 1: 103 Question 2: 36 Question 3: 20 Question 4: 80 Question 5: 30 --- 269 Question 1: =========== 1-33 1 point each = 33 34-47 5 points each = 70 103 total Consider the following class dictionary SyntaxAspect, program, input and output. Find the UNKNOWNS. Notice that the class dictionary for class dictionaries used below is different than the standard one. The changes are clearly marked. Output has been deleted at two marked places to make the questions smaller. // CLASS DICTIONARY SyntaxAspect // generate.cd -- class dictionary for DemeterJ code generator // modified for syntax commands 2004/04/19 import edu.neu.ccs.demeter.dj.*; import java.util.*; import java.io.*; Main = . Input = ClassGraphh SyntaxEnhancement. ClassGraphh = DList(Definition). Definition : ClassDef. public ClassDef = ParamClassName *s ClassParts "." . ParamClassName = ClassName ["(" Commalist(ClassName) ")"]. ClassParts : ConstOrAltClass | RepetitionClass. ConstOrAltClass : ConstructionClass | AlternationClass *common* + // The following: // List(ClassElement) // changed to: List(ClassElementWithSyntax) -. // The following: // ClassElement : Part | OptionalPart | Syntax. // changed to: ClassElement : Part | OptionalPart . ClassElementWithSyntax : ClassElement | SandwichedClassElement . SandwichedClassElement = PSandwich(ClassElement). PSandwich(S) = "{" Sandwich(S) "}". Part = *l [ "<" PartName ">" *s ] ClassSpec. OptionalPart = *l "[" Sandwich(Part) "]". ConstructionClass = "=". AlternationClass = ":" + [ Barlist(ClassSpec) ] - "common". RepetitionClass = "~" Sandwich(RepeatedPart). RepeatedPart = [ ClassSpec ] "{" Sandwich(ClassSpec) *s "}". Sandwich(S) = List(Syntax) *s S List(Syntax) . ClassSpec = ClassName ["(" Commalist(ClassSpec) ")" ] . Syntax : SyntaxToken | PrintCommand. SyntaxToken = String. PrintCommand : PrintIndent | PrintUnindent | PrintSkip | PrintSpace. PrintIndent = "+" . PrintUnindent = "-" . PrintSkip = "*l" . PrintSpace = "*s" . // Terminal buffer classes. ClassName = Ident. PartName = Ident. // Parameterized class definitions. List(S) ~ {*s S}. NList(S) ~ S {*s S}. SList(S) ~ S { *l S } . DList(S) ~ S { *l *l S } *l . Commalist(S) ~ S {"," *s S}. Barlist(S) ~ S { *l "|" *s S}. ////////////////////// //syntax enhancement// ////////////////////// SyntaxEnhancement = List(Enhancement) EOF. Enhancement = *l Where EnhancementBody. EnhancementBody : ClassBody | PartCoordinate | OptionalPartCoordinate | CollectionCoordinate common ":" SyntaxToken . // also applies outside of optional parts PartCoordinate = "part" SyntaxClassName "." SyntaxPartName. // only for optional parts OptionalPartCoordinate = "inside" "optional" "part" SyntaxClassName "." SyntaxPartName. ClassBody = "class" SyntaxClassName. CollectionCoordinate = "collection" SyntaxClassName CurlyBracket. CurlyBracket : Left | Right. Left = "{". Right = "}". SyntaxClassName = Ident. SyntaxPartName = Ident. Where : Before | After. Before = "before". After = "after". //populates hashtable with classgraph information ClassGraphVisitor = extends Visitor. //provides behavior for different types of syntax commands CommandVisitor = extends Visitor. //three types ClassCommandVisitor = extends Visitor. CollectionCommandVisitor = extends Visitor. PartCommandVisitor = extends Visitor. PROGRAM SAspectProc ========================================== Main { {{ public static void main(String args[]) throws Exception { Input input = Input.parse(System.in); input.print(); // input.trace(); // input.display(); ClassGraph cg = new ClassGraph(new ClassGraph(true, false), "from Input bypassing -> *, tail, * to *"); ClassGraphh cgRootObj = input.get_classgraphh(); System.out.println(); System.out.println(); System.out.println(" ClassGraphh-object ============ "); cgRootObj.trace(); SyntaxEnhancement seRootObj = input.get_syntaxenhancement(); System.out.println(); System.out.println(); System.out.println(" SyntaxEnhancement-object ============ "); seRootObj.trace(); //populate hashtable with class and part name key/value pairs System.out.println(); System.out.println(" Populate and print hashtable"); ClassGraphVisitor cgv = new ClassGraphVisitor(); cgv.setNameTable(new Hashtable()); cgv.setCG(cg); cg.traverse(cgRootObj,"from ClassGraphh to ClassDef", cgv); Hashtable nameTable = cgv.getNameTable(); System.out.println(); System.out.println(nameTable); //setup syntax command traversals System.out.println(); System.out.println("SyntaxEnhancement-object processing"); TraversalGraph tgSyntax = new TraversalGraph( "from Enhancement to EnhancementBody", cg); System.out.println(); System.out.println("Traversal Graph for:" + "from Enhancement to EnhancementBody"); System.out.println(); System.out.println(tgSyntax); System.out.println(); System.out.println("SyntaxEnhancement-object processing continued"); CommandVisitor cv = new CommandVisitor(); cv.setNameTable(nameTable); cv.setCG(cg); //iterate through each syntax command Enhancement_List sentences = seRootObj.get_enhancement_list(); while(sentences.hasMoreElements()) { Enhancement sent = (Enhancement)sentences.nextElement(); cv.setEnhancement(sent); tgSyntax.traverse(sent, cv); } System.out.println(); cgRootObj.print(); } }} } Input { void print() to * (PrintVisitor); void trace() to * (TraceVisitor); void display() to * (DisplayVisitor); } ClassGraphh { void print() to * (PrintVisitor); void trace() to * (TraceVisitor); void display() to * (DisplayVisitor); } SyntaxEnhancement { void print() to * (PrintVisitor); void trace() to * (TraceVisitor); void display() to * (DisplayVisitor); } ClassGraphVisitor { {{ private Hashtable nameTable; public void setNameTable(Hashtable nameTable) { this.nameTable = nameTable; } public Hashtable getNameTable() { return nameTable; } private ClassGraph cg; public void setCG(ClassGraph cg) { this.cg = cg; } public ClassGraph getCG() { return cg; } void before(ClassDef classdef) { nameTable.put(classdef.get_paramclassname(). get_classname().get_ident(), classdef.get_classparts()); List partnames = cg.gather(classdef, "from ClassDef through Part to edu.neu.ccs.demeter.Ident"); Iterator iter = partnames.iterator(); while(iter.hasNext()) { Ident oid = (Ident)iter.next(); nameTable.put( oid, classdef ); } } }} } CommandVisitor { {{ private Hashtable nameTable; public void setNameTable(Hashtable nameTable) { this.nameTable = nameTable; } public Hashtable getNameTable() { return nameTable; } private ClassGraph cg; public void setCG(ClassGraph cg) { this.cg= cg; } public ClassGraph getCG() { return cg; } private Enhancement eh; public void setEnhancement(Enhancement eh) { this.eh = eh; } public Enhancement getEnhancement() { return eh; } void before (ClassBody classbody) { System.out.println("visiting ClassBody " + classbody); ConstOrAltClass coa = (ConstOrAltClass)nameTable. get((Ident)cg.fetch(classbody, "from ClassBody through SyntaxClassName to edu.neu.ccs.demeter.Ident")); } void before(CollectionCoordinate collection) { System.out.println("visiting CollectionCoordinate " + collection); } void before(PartCoordinate partc) { System.out.println("visiting PartCoordinate " + partc); //grab part list from hashtable Ident syntaxpartname = (Ident)cg.fetch(partc, "from PartCoordinate through SyntaxPartName to edu.neu.ccs.demeter.Ident"); ClassDef cdef = (ClassDef)nameTable.get(syntaxpartname); System.out.println("SyntaxPartName " + syntaxpartname); } }} } INPUT ============================================= LastName = Ident. A = B C {"bd" D "ed"} {"be" ["x" E] "ee"} [ F]. B : C | D common X. C ~ "x" Y { "," Y } ")". before part LastName.ident : "last: " before inside optional part A.e : "last: " before class LastName : "last: " before collection C { : "last: " OUTPUT ================================================== Running the test... LastName = Ident. A = B C {"bd" D "ed"} {"be" ["x" E]"ee"} [ F]. B : C | D X common. C ~"x" Y{"," Y }")". before part LastName.ident:"last: " before inside optional part A.e:"last: " before class LastName:"last: " before collection C{:"last: " ClassGraphh-object ============ // NOTE: UNKNOWN48 is out of order before UNKNOWN1 before -> UNKNOWN2, UNKNOWN48, UNKNOWN3 before UNKNOWN4 before -> Definition_DList, first, Nonempty_Definition_DList before Nonempty_Definition_DList before -> Nonempty_Definition_DList, it, Definition before UNKNOWN5 before UNKNOWN6 before -> UNKNOWN7, UNKNOWN8, UNKNOWN9 before UNKNOWN10 before -> UNKNOWN11, UNKNOWN12, UNKNOWN13 before UNKNOWN14 before -> UNKNOWN15, UNKNOWN16, UNKNOWN17 // output part deleted after UNKNOWN18 after -> UNKNOWN19, UNKNOWN20, UNKNOWN21 after UNKNOWN22 SyntaxEnhancement-object ============ before UNKNOWN23 before -> UNKNOWN24, UNKNOWN25, UNKNOWN26 before UNKNOWN27 before -> Enhancement_List, first, Nonempty_Enhancement_List before Nonempty_Enhancement_List before -> Nonempty_Enhancement_List, it, Enhancement before UNKNOWN28 before -> UNKNOWN29, UNKNOWN30, UNKNOWN31 before UNKNOWN32 before UNKNOWN33 // output part deleted Populate and print hashtable {D=ClassDef@cfec48, C=RepetitionClass@a17083, B=AlternationClass@e1d5ea, X=ClassDef@a31e1b, A=ConstructionClass@10da5eb, Ident=ClassDef@1081d2e, f=ClassDef@cfec48, e=ClassDef@cfec48, d=ClassDef@cfec48, LastName=ConstructionClass@1b3f829, c=ClassDef@cfec48, b=ClassDef@cfec48, x=ClassDef@a31e1b, F=ClassDef@cfec48, E=ClassDef@cfec48} SyntaxEnhancement-object processing Traversal Graph for:from Enhancement to EnhancementBody Start set: [Enhancement: {0}] Copy 0: Nodes: UNKNOWN34 UNKNOWN35 PartCoordinate OptionalPartCoordinate ClassBody CollectionCoordinate Edges: -> UNKNOWN36,UNKNOWN37,UNKNOWN38 => EnhancementBody,PartCoordinate :> PartCoordinate,EnhancementBody => EnhancementBody,OptionalPartCoordinate :> OptionalPartCoordinate,EnhancementBody => EnhancementBody,ClassBody :> ClassBody,EnhancementBody => EnhancementBody,CollectionCoordinate :> CollectionCoordinate,EnhancementBody Edges to other copies: Finish set: [EnhancementBody: {0}] SyntaxEnhancement-object processing continued visiting UNKNOWN39 UNKNOWN40@7c4c51 UNKNOWN41 UNKNOWN42 UNKNOWN43 UNKNOWN44 UNKNOWN45@148bd3 visiting UNKNOWN46 UNKNOWN47@e80842 LastName = Ident. A = B C {"bd" D "ed"} {"be" ["x" E]"ee"} [ F]. B : C | D X common. C ~"x" Y{"," Y }")". Question 2: =========== 36 points Consider again the class dictionary SyntaxAspect from question 1. Consider the traversal strategy "from Input to Part". List in alphabetical order all the classes that are involved in the traversal (similar to the project). An example of such a class is ClassElement_PSandwich. Use the class names of the normalized cd where parameterized classes are expanded (like in the project). I count 20 such classes. Put the answer in UNKNOWN1. Question 3: =========== 20 points Consider the following two inputs: A = { B } "x" { C}. A = [ X] "y" [ Y]. and the class dictionary SyntaxAspect of question 1. Both inputs are NOT legal inputs for class ClassGraphh. Give for both sentences the first token that is wrong. Put your answer in UNKNOWN1. Question 4: ================================= 20 UNKNOWNs, 4 points each: 80 points Consider the following sentences and design a class dictionary for them. The purpose of the language is to define the pretty printing aspect of a class dictionary. We use the same 4 pretty printing commands as in DemeterJ. Fill in the UNKNOWNs below. before part LastName.x : + + after part LastName.x : - - before inside optional part LastName.x : + + after inside optional part LastName.x : - - before class LastName : *s before class FirstName : *l after class FirstName : *s + + *l after collection List 1. sandwich : + + before collection List 2. sandwich : - - before collection List 1. sandwich : - - after collection List 1. sandwich : - - after collection List 1. sandwich: *l after collection List 2. sandwich: *l // Terminal buffer classes. ClassName = Ident. PartName = Ident. // Parameterized class definitions. List(S) ~ {*s S}. PrintCommand : PrintIndent | PrintUnindent | PrintSkip | PrintSpace. PrintIndent = UNKNOWN1 . PrintUnindent = UNKNOWN2 . PrintSkip = UNKNOWN3 . PrintSpace = UNKNOWN4 . ////////////////////// //printing enhancement// ////////////////////// PrintingAspect = List(UNKNOWN5) EOF. PrintEnhancement = UNKNOWN6 PrintEnhancementBody. UNKNOWN7 : ClassBody | PartCoordinate | OptionalPartCoordinate | CollectionCoordinate common ":" UNKNOWN8 . // also applies outside of optional parts UNKNOWN9 = "part" ClassName "." PartName. // only for optional parts UNKNOWN10 = UNKNOWN11 "optional" "part" UNKNOWN12 "." PartName. UNKNOWN13 = "class" ClassName. CollectionCoordinate = "UNKNOWN14" ClassName Level. UNKNOWN15 : Outer | Inner. UNKNOWN16 = "1." "UNKNOWN17". Inner = "2." "UNKNOWN18". Where : UNKNOWN19 | After. Before = "UNKNOWN20". After = "after". Question 5: =========== Consider the program SAspectProc from question 1. Discuss violations of the Law of Demeter "Talk only to your friends". Specify classes and methods and statements where the violations occur. Put your answer into UNKNOWN1 in the form: Class Method Statement Explanation X foo() d.bar() d is not a preferred supplier because ...