MIDTERM COM 1205 Winter 2003 Tuesday, February 11 Question 1: =========== Consider the following program.cd, program.beh and program.input file and the output. Find the UNKNOWNS. program.cd: =============================================================== import edu.neu.ccs.demeter.dj.*; Main = String. Aspect = "aspect" TraversalAspectName SList(Decl). Decl = "declare" Item. Item : NamedStrat | Trav. NamedStrat = "strategy" ":" StrategyName ":" Strat. Trav = "traversal" ":" MethodSignature ":" StrategyName "(" ClassName ")". MethodSignature = "void" MethodName "(" FormalArgument ")". FormalArgument = ClassName VariableName. TraversalAspectName = Ident. StrategyName = Ident. ClassName = Ident. MethodName = Ident. Strat = String. VariableName = Ident. SList(S) ~ "{" S {";" S } "}". program.beh: =============================================================== Main { {{ static ClassGraph cg; public static void main(String args[]) throws Exception { Aspect m = Aspect.parse(System.in); System.out.println("input ===================== "); m.display(); System.out.println(); System.out.println(" processing starts "); cg = new ClassGraph(true, false); cg.traverse(m, "from Aspect to ClassName", new Visitor() { void before (ClassName host) { System.out.println(host.get_ident().toString()); } }); System.out.println("done: from Aspect to ClassName"); cg.traverse(m, "from Aspect via MethodSignature to ClassName", new Visitor() { void before (ClassName host) { System.out.println(host.get_ident().toString()); } }); System.out.println("done: from Aspect via MethodSignature to ClassName"); } }} } program.input: =============================================================== // aspect { // void display() to * (DisplayVisitor); // } aspect Traversals { declare strategy: eachCommand: "from Commands to Command"; declare traversal: void interpret(CompoundFile cd): eachCommand(Interpreter) } OUTPUT: ============================================================== input ===================== : Aspect ( : TraversalAspectName ( : Ident "Traversals" ) : Decl_SList { : Nonempty_Decl_SList ( : Decl ( : NamedStrat ( : StrategyName ( : Ident "eachCommand" ) : Strat ( : String "from Commands to Command" ) ) ) : Nonempty_Decl_SList ( : Decl ( : Trav ( : MethodSignature ( : MethodName ( : Ident "interpret" ) : FormalArgument ( : ClassName ( : Ident "CompoundFile" ) : VariableName ( : Ident "cd" ) ) ) : StrategyName ( : Ident "eachCommand" ) : ClassName ( : Ident "Interpreter" ) ) ) ) ) } ) processing starts CompoundFile Interpreter done: from Aspect to ClassName CompoundFile done: from Aspect via MethodSignature to ClassName Question 2: ========================================================================= The following lines from the above *.beh file contain significant duplication. Rewrite the program so that the duplication is minimized and the behavior stays the same. cg.traverse(m, "from Aspect to ClassName", new Visitor() { void before (ClassName host) { System.out.println(host.get_ident().toString()); } }); cg.traverse(m, "from Aspect via MethodSignature to ClassName", new Visitor() { void before (ClassName host) { System.out.println(host.get_ident().toString()); } }); Question 3: ========================================================================== Is the following a legal sentence? Explain. aspect Traversals { declare strategy: eachCommand: "from Commands to Command"; declare traversal: void listAll(): eachFile(FileLister); declare traversal: void listAll(X x, Y y): eachFile(FileLister); } If it is not illegal describe how you would change the class dictionary.