import java.util.*; public class Visitor implements MyParserVisitor { private HashSet types; private HashSet globals; private HashSet locals; private boolean inClass = false; private boolean inMethod = false; private boolean inMain = false; private boolean foundMain = false; public Visitor() { globals = new HashSet(); locals = new HashSet(); String arr[] = {"int", "bool", "char"}; types = new HashSet(Arrays.asList(arr)); } public void defaultVisit(SimpleNode node, Object data) throws InvalidProgramException { node.childrenAccept(this, data); return; } public void visit(SimpleNode node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(Program node, Object data) throws InvalidProgramException { defaultVisit(node, data); if (!foundMain) { throw new InvalidProgramException("No main function"); } } public void visit(ConstDecl node, Object data) throws InvalidProgramException { String type = node.jjtGetChild(0).jjtGetChild(0).toString(); System.out.print("CONSTs of type: " + type); System.out.print("\t\twith names: "); for (int i = 1; i < node.jjtGetNumChildren(); i++) { String current = node.jjtGetChild(i).toString(); if (globals.contains(current)) { throw new InvalidProgramException( "Can't define 2 variables with the same name: " + current); } globals.add(current); System.out.print(current + " "); } System.out.println(); } public void visit(EnumDecl node, Object data) throws InvalidProgramException { System.out.print("Enum of type: " + ((Ident)node.jjtGetChild(0)).name); System.out.print("\t\twith variants: "); for (int i = 1; i < node.jjtGetNumChildren(); i++) { System.out.print(((Ident)node.jjtGetChild(i)).name + " "); } System.out.println(); } public void visit(VarDecl node, Object data) throws InvalidProgramException { String type = node.jjtGetChild(0).jjtGetChild(0).toString(); System.out.print("Vars of type: " + type); System.out.print("\t\twith names: "); for (int i = 1; i < node.jjtGetNumChildren(); i++) { String current = node.jjtGetChild(i).toString(); if (inMethod) { locals.add(current); } else { globals.add(current); } System.out.print(current + " "); } System.out.println(); } public void visit(ClassDecl node, Object data) throws InvalidProgramException { String className = ((Ident)node.jjtGetChild(0)).name; types.add(className); System.out.println("Entering " + className); inClass = true; defaultVisit(node, data); inClass = false; System.out.println("Leaving " + className); } public void visit(InterfaceDecl node, Object data) throws InvalidProgramException { String interfaceName = ((Ident)node.jjtGetChild(0)).name; if (types.contains(interfaceName)) { throw new InvalidProgramException( "Can't have 2 interfaces named: " + interfaceName); } types.add(interfaceName); System.out.println("Entering " + interfaceName); inClass = true; defaultVisit(node, data); inClass = false; System.out.println("Leaving " + interfaceName); } public void visit(InterfaceMethodDecl node, Object data) throws InvalidProgramException { defaultVisit(node, data); locals.clear(); } public void visit(MethodDecl node, Object data) throws InvalidProgramException { int namePos = 0; if (node.jjtGetChild(0) instanceof Type) { namePos = 1; } String methodName = node.jjtGetChild(namePos).toString(); globals.add(methodName); if (methodName.equals("main") && !inClass) { foundMain = true; inMain = true; if (namePos == 1) { throw new InvalidProgramException("Main function must be void"); } } inMethod = true; System.out.println("Entering " + methodName); defaultVisit(node, data); inMethod = false; locals.clear(); inMain = false; System.out.println("Leaving " + methodName); } public void visit(ParamDecl node, Object data) throws InvalidProgramException { System.out.print("Params:\t\t\t\t\t"); if (inMain && node.jjtGetNumChildren() > 0) { throw new InvalidProgramException( "Main function shouldn't have any arguments"); } for (int i = 1; i < node.jjtGetNumChildren(); i += 2) { String current = node.jjtGetChild(i).toString(); if (locals.contains(current)) { throw new InvalidProgramException( "Can't define 2 variables with the same name: " + current); } locals.add(current); System.out.print(current + " "); } System.out.println(); defaultVisit(node, data); } public void visit(Statement node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(DesignatorStatement node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(Condition node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(CompareAnd node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(Comparison node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(Params node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(Expr node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(Term node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(Factor node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(Type node, Object data) throws InvalidProgramException { String name = node.jjtGetChild(0).toString(); if (!types.contains(name)) { throw new InvalidProgramException("Type not declared before use: " + name); } } public void visit(Designator node, Object data) throws InvalidProgramException { defaultVisit(node, data); } public void visit(Ident node, Object data) throws InvalidProgramException { String name = node.name; if (!globals.contains(name) && !locals.contains(name)) { throw new InvalidProgramException( "Variable not declared before use: " + name); } } }