Consider the following class dictionary called TraversalPP: TraversalPP = "traversal-pp" ClassName FunctionName Args Body. Body = "{" Initialize Traversal VisitorMethods "}". Initialize = "initialize" JavaCode. Traversal : Simple | Compound. Simple = [ From ] Constraints To. Compound = Op Traversals. Op : Join. Join = "join". From = "from" ClassName. To = "to" ClassName. Constraint = [ Bypassing] Through. Bypassing = "bypassing" Edges. Through = "through" Edges. Edge = "->" ClassName LabelName ClassName. Arg = JavaTypeName Variable. VisitorMethod : Before | After *common* ClassNames JavaCode. Before = "before". After = "after". // Terminal-Buffer rule ClassName = Ident. JavaTypeName = Ident. Variable = Ident. LabelName = Ident. FunctionName = Ident. JavaCode = Text. // Repetition-Buffer rule Args = "(" [ CList(Arg) ] ")". ClassNames = "{" CList(ClassName) "}". VisitorMethods = NList(VisitorMethod) . Traversals = "(" CList(Traversal) ")". Edges = NList(Edge). Constraints = List(Constraint). // Repetition classes CList(S) ~ S {"," S}. NList(S) ~ S {S}. List(S) ~ {S}. Main = . // Visitors PrintingVisitor = . The behavior file program.beh is: TraversalPP { (@ void g_print() { PrintingVisitor pv = new PrintingVisitor(); this.t(pv); } @) traversal t(PrintingVisitor pv) {to *;} } Main { (@ static public void main(String args[]) throws Exception { TraversalPP a = TraversalPP.parse(System.in); a.g_print(); System.out.println(" "); } @) } PrintingVisitor{ before TraversalPP (@ System.out.println(" traversal-pp "); @) after TraversalPP (@ System.out.println(" "); @) before Body (@ System.out.println(" { "); @) after Body (@ System.out.println(" } "); @) before Initialize (@ System.out.println(" initialize "); @) before Join (@ System.out.println(" join "); @) before From (@ System.out.println(" from "); @) before To (@ System.out.println(" to "); @) before Bypassing (@ System.out.println(" bypassing "); @) before Through (@ System.out.println(" through "); @) before Edge (@ System.out.println(" -> "); @) before Before (@ System.out.println(" before "); @) before After (@ System.out.println(" after "); @) before ClassName (@ System.out.println (" " + host.get_n() + " "); @) before JavaTypeName (@ System.out.println (" " + host.get_name() + " "); @) before Variable (@ System.out.println (" " + host.get_name() + " "); @) before LabelName (@ System.out.println (" " + host.get_name() + " "); @) before FunctionName (@ System.out.println (" " + host.get_name() + " "); @) before JavaCode (@ System.out.println (" " + "(" + "@" + host.get_code() + "@" + ")" + " "); @) before Args (@ System.out.println(" ( "); @) after Args (@ System.out.println(" ) "); @) before ClassNames (@ System.out.println(" { "); @) after ClassNames (@ System.out.println(" } "); @) before Traversals (@ System.out.println(" ( "); @) after Traversals (@ System.out.println(" ) "); @) } The input program.input is: traversal-pp int sum_salaries(X x, Y y) { initialize (@ 0 @) bypassing -> A b B -> X y Y through -> K l L -> M n N to Salary before {Company, X} (@ @) before {Salary, Y} (@ @) } What is the output of the program? java Main < program.input traversal-pp int sum_salaries ( X x Y y ) { initialize (@ 0 @) bypassing -> A b B -> X y Y through -> K l L -> M n N to Salary before { Company X } (@ @) before { Salary Y } (@ @) } The traversal was changed to LabelName TraversalPP { (@ void g_print() { PrintingVisitor pv = new PrintingVisitor(); this.t(pv); } @) traversal t(PrintingVisitor pv) {to LabelName;} // traversal allJoin(PrintingVisitor pV) { to Join;} } Main { (@ static public void main(String args[]) throws Exception { TraversalPP a = TraversalPP.parse(System.in); a.g_print(); System.out.println(" "); } @) } PrintingVisitor{ before TraversalPP (@ System.out.println(" traversal-pp "); @) after TraversalPP (@ System.out.println(" "); @) before Body (@ System.out.println(" { "); @) after Body (@ System.out.println(" } "); @) before Initialize (@ System.out.println(" initialize "); @) before Join (@ System.out.println(" join "); @) before From (@ System.out.println(" from "); @) before To (@ System.out.println(" to "); @) before Bypassing (@ System.out.println(" bypassing "); @) before Through (@ System.out.println(" through "); @) before Edge (@ System.out.println(" -> "); @) before Before (@ System.out.println(" before "); @) before After (@ System.out.println(" after "); @) before ClassName (@ System.out.println (" " + host.get_n() + " "); @) before JavaTypeName (@ System.out.println (" " + host.get_name() + " "); @) before Variable (@ System.out.println (" " + host.get_name() + " "); @) before LabelName (@ System.out.println (" " + host.get_name() + " "); @) before FunctionName (@ System.out.println (" " + host.get_name() + " "); @) before JavaCode (@ System.out.println (" " + "(" + "@" + host.get_code() + "@" + ")" + " "); @) before Args (@ System.out.println(" ( "); @) after Args (@ System.out.println(" ) "); @) before ClassNames (@ System.out.println(" { "); @) after ClassNames (@ System.out.println(" } "); @) before Traversals (@ System.out.println(" ( "); @) after Traversals (@ System.out.println(" ) "); @) } traversal-pp R find(X x) { initialize (@ new R(); @) through -> K l L -> M n N bypassing -> A b B -> X y Y through -> S t T -> P q Q to R before {R} (@ return_val = this; @) } traversal-pp { through -> l -> n bypassing -> b -> y through -> t -> q } Question 4: This question is about programming with visitors. We reuse the class dictionary from question 1 and add the following classes: SelectiveVisitor = PrintingVisitor SelectorVisitor. SelectorVisitor = Integer. The SelectiveVisitor is used in combination with a traversal t to target C. When the traversal arrives at C, the SelectiveVisitor will interrogate the selector visitor soV to check whether this C-object should be processed. If the answer is positive, the C-object is processed by a traversal using the visitor in doingV. The reuse information for the SelectiveVisitor is as follows: Parameters: ---------------------------------------------------- t: A traversal. Where: A class which is the target of traversal t. t2: a traversal to process Where-objects. WhatVisitor: What needs to be done. A visitor class for traversing Where-objects. IfVisitor: Decides if something needs to be done. A visitor class for traversal t. ---------------------------------------------------- Visitor classes: SelectiveVisitor = // used with t WhatVisitor // used with t2 IfVisitor. // work done before entering Where-objects IfVisitor = Integer. Example: t = from TraversalPP to JavaCode t2 = from JavaCode to * Where = JavaCode WhatVisitor = PrintingVisitor IfVisitor = SelectorVisitor Define a traversal t(WhatVisitor,IfVisitor) with Target(t) = Where. SelectiveVisitor has a before method at Where: SelectiveVisitor{ before Where (@ if (soV.get_selected().intValue() == 1) { host.t2(doingV); soV.set_selected(new Integer(0)); } @) } The IfVisitor makes the selection decision and stores it in selected. Since SelectiveVisitor has a before method at Where, the IfVisitor must have made its decision before the Where-object is entered. Now we apply the SelectiveVisitor to find all JavaCode-objects contained in a Before-object: TraversalPP { (@ void selective_visit() { // a method using three visitors: // SelectiveVisitor: finds objects of class JavaCode // SelectorVisitor: determines which objects of // class JavaCode should be processed depending on whether // they are contained in some class Before. // assumes JavaCode-objects are contained in Before-objects. // PrintingVisitor: determines how selected objects // should be processed SelectorVisitor sov = new SelectorVisitor(new Integer(0)); SelectiveVisitor siv = new SelectiveVisitor(new PrintingVisitor(), sov); this.all(sov,siv); } @) traversal all(SelectorVisitor sov, SelectiveVisitor siv) { to JavaCode;} } JavaCode { traversal all_j(PrintingVisitor doingV) { to *;} } Main { (@ static public void main(String args[]) throws Exception { TraversalPP a = TraversalPP.parse(System.in); a.selective_visit(); System.out.println(" "); } @) } SelectiveVisitor{ before JavaCode (@ if (soV.get_selected().intValue() == 1) { host.all_j(doingV); soV.set_selected(new Integer(0)); } @) } SelectorVisitor { before Before (@ set_selected(new Integer(1)); @) } traversal-pp int sum_salaries(X x, Y y) { initialize (@ 0 @) to Salary before {Company, X} (@ // before @) after {Salary, Y} (@ // after @) before {Y} (@ // before2 @) } output: (@ // before @) (@ // before2 @)