Download presentation
Presentation is loading. Please wait.
Published byTrevor Manning Modified over 9 years ago
1
Visitor-Based HMM Israel Perez, Fayz Rahman, Chinedu Egboh
2
Changes to HMM Edited AbstractSyntaxTree class – All “node” classes now have accept methods. Added VisitorIF interface – Defines an interface for a Visitor. Has visit methods for all “node” classes in AST. Not all are used, but they are there if needed in the future. Edited Hmm.java – Edited to utilize our new additions.
3
Changes to HMM Added PrinterVisitor – A Visitor that prints out the Abstract Syntax Tree. Added StaticTypeCheckVisitor – A Visitor that performs static-type checking on the Abstract Syntax Tree. Added InterpreterVisitor – A Visitor that interprets the Abstract Syntax Tree and “runs” the program.
4
Changes to HMM Added Souvenir class – Some AST nodes return different things in different contexts. This class holds returns of type boolean, Value, and Type. Used as a unified return type for each method in the visitor interface. Added additional test programs
5
Why the Visitor Pattern? We are separating Functionality from Structure. We can add a new operation to an existing structure of objects without having to alter the classes of those objects. – In our project, we added PrintVisitor, StaticTypeCheckVisitor, and InterpreterVisitor. All use the same AST, but do very different things.
6
Interpretation in HMM Old HMM Evaluates global variables Finds main method, gets its body, calls runStatement(body) – Goes through list of if/elseif instanceof blocks, and do whatever it needs to for that node. Possibly recursive. – If node is an Expression, call runExpression go through more if/elseif instanceof blocks. Possibly recursive. Visitor-Based HMM Evaluates global variables Finds main method, gets its body, creates new Visitor, calls runStatement(Visitor) Visitor finds correct visitor for specific node type, and does whatever it needs to for that node. Possibly recursive.
7
Demo Time?
8
Refresher: Features of HMM List Comprehension Support for Tuples and Lists Support for Strings Built-in functions: – print – println int main() { list test = createTest(); list select = [(name1, name2) | (name1, name2) <- test, name1 == “Fayz” ]; println("Group Members: ”, test); println("Member: “, select); } list createTest() { return[("Fayz", "Rahman"), ("Nick", "Egboh"), ("Israel", "Perez")]; }
9
Refresher: Features of HMM Lambda Expressions For-each loop int main() { list count = [1,2,3]; for (n <- count) { int doubleFunc = application(n, (\x -> x*2) ); println("(\x -> (2*x)) applied to ", n, " = ", doubleFunc); } int application(object x, (object, object) a) { int result = a(x); return result; }
10
Refresher: Features of HMM Static Scoping Static Binding (variables in lambdas) int main() { list count = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]; int x = 10; list output = selector(count, lambdaFilter()); println("Count: ", count); println("Filtered: ", output); } (object, bool) lambdaFilter() { int x = 30; return (\y -> y < x); } list selector(list nums, (object, bool) filter) { return [ x | x <- nums, filter(x) ]; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.