/** * You must implement the visitor. The symbol table is extra credit [10 pts]
ID: 3651666 • Letter: #
Question
/*** You must implement the visitor. The symbol table is extra credit [10 pts] for
* those who wish to implement their own. Your symbol table must be very efficient
* for the full 10 points.
*
* A reference symbol table visitor is included to start you off.
*
* For this project, you need to process variable, class, field, and method DECLARATIONS. For
* REFERENCES, you need only concern yourself with ordinary variables. Class, field, and method
* references are not part of the assignment. (But do not confuse them with declarations.)
*
* Your goal is to familiarize yourself with the SymbolTable and the concept of
* processing ASTs using visitors.
*/
public class SymtabVisitor extends NodeVisitor {
/** This method lets us keep Main as-is before and after your implementation.
* Suggestion: develop as follows.
* Step 1: replace the return below with
* return new SymtabVisitor(new ClassBuildSymtab());
* This will use your visitor but the class symbol table implementation
* Step 2: [ not necessary but if you attempt the extra credit] replace the return below with
* return new SymtabVisitor(new BuildSymtab());
* This will use your visitor and your symbol table implementation
*/
public static ReflectiveVisitor getVisitor() {
return new CourseSymtabVisitor(new CourseBuildSymtab());
}
public static java_cup.runtime.lr_parser getParser(String[] args) {
return new CourseParser(new CourseScanner(args.length == 0 ? new OpenFile("") : new OpenFile(args[0])));
}
private SymtabInterface sti;
public SymtabVisitor(SymtabInterface sti) { this.sti = sti; }
/** This method will cause the whole AST to be visited, but nothing will happen.
* It's good default behavior, but you'll need methods to handle various node types.
*/
public void defaultVisit(Object o) {
AbstractNode n = (AbstractNode) o;
visitChildren(n);
}
}
/** [+10] Extra Credit: Your symbol table implementation */
class BuildSymtab extends Symtab implements SymtabInterface {
public void incrNestLevel() { out("Nest level now " + getCurrentNestLevel()); }
public void decrNestLevel() { out("Nest level now " + getCurrentNestLevel()); }
public int getCurrentNestLevel() { return 0; }
public SymInfo lookup(String id) { return null; }
public void enter(String id, SymInfo s) { }
}
Explanation / Answer
import ___*; /** * You must implement the visitor and the symbol table */ public class SymtabVisitor extends NodeVisitor { /** This method lets us keep Main as-is before and after your implementation. * Suggestion: develop as follows. * Step 1: replace the return below with * return new SymtabVisitor(new ClassBuildSymtab()); * This will use your visitor but the class symbol table implementation * Step 2: replace the return below with * return new SymtabVisitor(new BuildSymtab()); * This will use your visitor and your symbol table implementation */ public static ReflectiveVisitor getVisitor() { return new CourseSymtabVisitor(new CourseBuildSymtab()); } private SymtabInterface sti; public SymtabVisitor(SymtabInterface sti) { this.sti = sti; } /** This method will cause the whole AST to be visited, but nothing will happen. * It's good default behavior, but you'll need methods to handle various node types. */ public void defaultVisit(Object o) { AbstractNode n = (AbstractNode) o; visitChildren(n); } } /** Your symbol table implementation */ class BuildSymtab extends Symtab implements SymtabInterface { public void incrNestLevel() { out("Nest level now " + getCurrentNestLevel()); } public void decrNestLevel() { out("Nest level now " + getCurrentNestLevel()); } public int getCurrentNestLevel() { return 0; } public SymInfo lookup(String id) { return null; } public void enter(String id, SymInfo s) { } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.