QUESTION 10 Visitor Pattern [Total marks: 10] [10 Marks] With the aid of example code, describe the Visitor pattern. Why is it particularly suited to "walking an abstract syntax tree"? [End Question 10]
Added by Mary A.
Close
Step 1
It achieves this by separating the algorithm from the object structure. The algorithm is encapsulated in a "visitor" class, which visits each element in the structure and performs the desired operation. Show more…
Show all steps
Your feedback will help us improve your experience
Supreeta N and 94 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Assume that you are a professional software developer, working on the next-generation compiler for a programming language. Now, it is summertime, and you are supervising a group of summer interns. You want these interns to implement the following feature using your compiler, but you do not want them to modify or even read your proprietary code that implements the core of the compiler. The new feature is to count the total number of instructions in a program written in the programming language. You have decided to use the Visitor design pattern. You want the summer interns to write as much code as possible. The question is, which of the following code should NOT be left for the summer interns to write? Part A: interface Code { void accept(CodeVisitor visitor); } Part B: interface CodeVisitor { void visit(Program p); void visit(Function f); void visit(Statement s); } Part C: class Program implements Code { public void accept(CodeVisitor v) { v.visit(this); } } class Function implements Code { public void accept(CodeVisitor v) { v.visit(this); } } class Statement implements Code { public void accept(CodeVisitor v) { v.visit(this); } } Part D: class CodeCountingVisitor implements CodeVisitor { public void visit(Program p) { ... } public void visit(Function f) { ... } public void visit(Statement s) { ... } } Part E: public class Main { public static void main(...) { Program p = new Program(); p.accept(new CodeCountingVisitor()); } } Questions: Should "Part A" be left for the summer interns to write? Should "Part B" be left for the summer interns to write? Should "Part C" be left for the summer interns to write? Should "Part D" be left for the summer interns to write? Should "Part E" be left for the summer interns to write?
Supreeta N.
The attached Java code (ast.zip) is an interpreter for C. The code takes as input a Clang-generated Abstract Syntax Tree (AST) in a text file (out.txt in the ast2 directory) and traverses the tree while interpreting the nodes. The code only "works" for simple expressions and with integer variables. You are to: a) modify the code to handle more complex expressions, "if statements", "while loops" and "for loops". b) Java classes have an outline gen() method which shows how the code could be used as a compiler to generate ARM assembly. Outline how you would generate code for if statements. We will use Clang to generate an AST for a given simple C function. We use the -ast-dump command line argument to generate a textual representation of the AST, which is stored in a file. For example: clang-check -ast-dump for2.c --extra-arg="-fno-color-diagnostics" > out.txt The attached code reads in this text file and constructs a tree with a Java object for each node in the Clang AST. For example, the VarDecl class models a C declaration of a variable. When the interpreter sees a VarDecl AST node, it creates a corresponding Java variable to model it. If the C variable is used in an expression in t1.c, the AST will have a corresponding DeclRefExpr node to model that use. When the interpreter sees the DeclRefExpr node in the AST, it uses the current value of the corresponding Java variable when evaluating the expression.
Akash M.
PROBLEM 3 (10 points) Consider the following code in C syntax (use static scoping). #include<stdio.h> int bar(int i) { i = 10; return i + 4; } int foo(int e, int y) { int a; int i; a = e + y; e = e + 2; i = y; y = i - 1; return bar(e); } int main() { int a = 0; int e[3] = {0, 1, 2}; int x; x = foo(a, e[a]); printf("%d-%d-%d-%d-%d ", a, e[0], e[1], e[2], x); return 0; } 5 points: If parameters are passed by value, the output of this program is (Explain by showing the call arguments to each function). 5 points: If parameters are passed by reference, the output of this program is explained with box and circle.
Aarya B.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD