/** * 用于建立符号表的类 */ package minijava.visitor; import java.util.Enumeration; import minijava.symboltable.*; import minijava.syntaxtree.*; import minijava.typecheck.PrintError; public class BuildSymbolTableVisitor extends GJDepthFirst { // // Auto class visitors--probably don't need to be overridden. // public MType visit(NodeList n, MType argu) { MType _ret=null; int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { e.nextElement().accept(this,argu); _count++; } return _ret; } public MType visit(NodeListOptional n, MType argu) { if ( n.present() ) { MType _ret=null; int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { e.nextElement().accept(this,argu); _count++; } return _ret; } else return null; } public MType visit(NodeOptional n, MType argu) { if ( n.present() ) return n.node.accept(this,argu); else return null; } public MType visit(NodeSequence n, MType argu) { MType _ret=null; int _count=0; for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { e.nextElement().accept(this,argu); _count++; } return _ret; } public MType visit(NodeToken n, MType argu) { return null; } // // User-generated visitor methods below // /** * f0 -> MainClass() * f1 -> ( TypeDeclaration() )* * f2 -> */ public MType visit(Goal n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); return _ret; } /** * f0 -> "class" * f1 -> Identifier() * f2 -> "{" * f3 -> "public" * f4 -> "static" * f5 -> "void" * f6 -> "main" * f7 -> "(" * f8 -> "String" * f9 -> "[" * f10 -> "]" * f11 -> Identifier() * f12 -> ")" * f13 -> "{" * f14 -> PrintStatement() * f15 -> "}" * f16 -> "}" */ public MType visit(MainClass n, MType argu) { MType _ret=null; MClass m_class; String class_name; String error_msg; n.f0.accept(this, argu); // 处理类定义的标识符Identifier() // 获得名字 class_name = ((MIdentifier) n.f1.accept(this, argu)).getName(); // 在符号表中插入该类,如果出错,打印出错信息 m_class = new MClass(class_name, (MClasses) argu, n.f1.f0.beginLine, n.f1.f0.beginColumn); error_msg = ((MClasses) argu).InsertClass(m_class); if (error_msg != null) PrintError.print(m_class.getLine(), m_class.getColumn(), error_msg); n.f2.accept(this, argu); n.f3.accept(this, argu); n.f4.accept(this, argu); n.f5.accept(this, argu); n.f6.accept(this, argu); n.f7.accept(this, argu); n.f8.accept(this, argu); n.f9.accept(this, argu); n.f10.accept(this, argu); // main的参数,待处理 n.f11.accept(this, argu); n.f12.accept(this, argu); n.f13.accept(this, argu); // printStatement n.f14.accept(this, argu); n.f15.accept(this, argu); n.f16.accept(this, argu); return _ret; } /** * f0 -> ClassDeclaration() * | ClassExtendsDeclaration() */ public MType visit(TypeDeclaration n, MType argu) { MType _ret=null; n.f0.accept(this, argu); return _ret; } /** * f0 -> "class" * f1 -> Identifier() * f2 -> "{" * f3 -> ( VarDeclaration() )* * f4 -> ( MethodDeclaration() )* * f5 -> "}" */ public MType visit(ClassDeclaration n, MType argu) { MType _ret = null; MClass m_class; String class_name; String error_msg; n.f0.accept(this, argu); // 处理类定义的标识符Identifier() // 获得名字 class_name = ((MIdentifier) n.f1.accept(this, argu)).getName(); // 在符号表中插入该类,如果出错,打印出错信息 m_class = new MClass(class_name, (MClasses) argu, n.f1.f0.beginLine, n.f1.f0.beginColumn); error_msg = ((MClasses) argu).InsertClass(m_class); if (error_msg != null) PrintError.print(m_class.getLine(), m_class.getColumn(), error_msg); n.f2.accept(this, argu); n.f3.accept(this, m_class); n.f4.accept(this, m_class); n.f5.accept(this, argu); return _ret; } /** * f0 -> "class" * f1 -> Identifier() * f2 -> "extends" * f3 -> Identifier() * f4 -> "{" * f5 -> ( VarDeclaration() )* * f6 -> ( MethodDeclaration() )* * f7 -> "}" */ public MType visit(ClassExtendsDeclaration n, MType argu) { MType _ret=null; MClass m_class; String class_name; String error_msg; n.f0.accept(this, argu); // 处理子类定义,并加入符号表 class_name = ((MIdentifier) n.f1.accept(this, argu)).getName(); m_class = new MClass(class_name, (MClasses) argu, n.f1.f0.beginLine, n.f1.f0.beginColumn); error_msg = ((MClasses) argu).InsertClass(m_class); if (error_msg != null) PrintError.print(m_class.getLine(), m_class.getColumn(), error_msg); n.f2.accept(this, argu); // 将父类名称加入类属性 m_class.extend_class_name = ((MIdentifier) n.f3.accept(this, argu)).getName(); n.f4.accept(this, argu); n.f5.accept(this, m_class); n.f6.accept(this, m_class); n.f7.accept(this, argu); return _ret; } /** * f0 -> Type() * f1 -> Identifier() * f2 -> ";" */ public MType visit(VarDeclaration n, MType argu) { MType _ret=null; MLocalVarType m_class = (MLocalVarType)argu; // m_class可能是类或方法 String var_name = n.f1.accept(this, argu).getName(); String type_name = n.f0.accept(this, argu).getName(); // debug System.out.println("Variable: \'" + var_name + "\' from class/method " + m_class.getName()); System.out.println("It has type: " + type_name); // 建立并插入变量 MVariable var = new MVariable(var_name, type_name, n.f1.f0.beginLine, n.f1.f0.beginColumn); String _err = m_class.insertVar(var); if (_err!=null) { // 重复定义,则输出错误信息 PrintError.print(var.getLine(), var.getColumn(), _err); } n.f2.accept(this, argu); return _ret; } /** * f0 -> "public" * f1 -> Type() * f2 -> Identifier() * f3 -> "(" * f4 -> ( FormalParameterList() )? * f5 -> ")" * f6 -> "{" * f7 -> ( VarDeclaration() )* * f8 -> ( Statement() )* * f9 -> "return" * f10 -> Expression() * f11 -> ";" * f12 -> "}" */ public MType visit(MethodDeclaration n, MType argu) { MType _ret = null; MClass m_class = (MClass)argu; String ret_type = n.f1.accept(this, argu).getName(); String method_name = n.f2.accept(this, argu).getName(); MMethod m_method = new MMethod(method_name, ret_type, n.f2.f0.beginLine, n.f2.f0.beginColumn); // 将定义的方法插入类中,如果出错则打印错误信息 String _err = m_class.insertMethod(m_method); if (_err!=null) { PrintError.print(n.f2.f0.beginLine, n.f2.f0.beginColumn, _err); } // 处理参数表 n.f4.accept(this, m_method); // 函数局部变量处理 n.f7.accept(this, m_method); n.f8.accept(this, argu); n.f9.accept(this, argu); n.f10.accept(this, argu); n.f11.accept(this, argu); n.f12.accept(this, argu); return _ret; } /** * f0 -> FormalParameter() * f1 -> ( FormalParameterRest() )* */ public MType visit(FormalParameterList n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); return _ret; } /** * f0 -> Type() * f1 -> Identifier() */ public MType visit(FormalParameter n, MType argu) { MType _ret=null; MMethod m_method = (MMethod)argu; String p_type = n.f0.accept(this, argu).getName(); String p_name = n.f1.accept(this, argu).getName(); MVariable param = new MVariable(p_name, p_type, n.f1.f0.beginLine, n.f1.f0.beginColumn); // 插入方法的参数表 String _err = m_method.addParam(param); if (_err!=null) { PrintError.print(n.f1.f0.beginLine, n.f1.f0.beginColumn, _err); } return _ret; } /** * f0 -> "," * f1 -> FormalParameter() */ public MType visit(FormalParameterRest n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); return _ret; } /** * f0 -> ArrayType() * | BooleanType() * | IntegerType() * | Identifier() */ public MType visit(Type n, MType argu) { return n.f0.accept(this, argu); } /** * f0 -> "int" * f1 -> "[" * f2 -> "]" */ public MType visit(ArrayType n, MType argu) { MType _ret = new MIdentifier(MIdentifier.arrType, n.f0.beginLine, n.f0.beginColumn); return _ret; } /** * f0 -> "boolean" */ public MType visit(BooleanType n, MType argu) { MType _ret = new MIdentifier(MIdentifier.boolType, n.f0.beginLine, n.f0.beginColumn); return _ret; } /** * f0 -> "int" */ public MType visit(IntegerType n, MType argu) { MType _ret = new MIdentifier(MIdentifier.intType, n.f0.beginLine, n.f0.beginColumn); return _ret; } /** * f0 -> Block() * | AssignmentStatement() * | ArrayAssignmentStatement() * | IfStatement() * | WhileStatement() * | PrintStatement() */ public MType visit(Statement n, MType argu) { MType _ret=null; n.f0.accept(this, argu); return _ret; } /** * f0 -> "{" * f1 -> ( Statement() )* * f2 -> "}" */ public MType visit(Block n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); return _ret; } /** * f0 -> Identifier() * f1 -> "=" * f2 -> Expression() * f3 -> ";" */ public MType visit(AssignmentStatement n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, argu); return _ret; } /** * f0 -> Identifier() * f1 -> "[" * f2 -> Expression() * f3 -> "]" * f4 -> "=" * f5 -> Expression() * f6 -> ";" */ public MType visit(ArrayAssignmentStatement n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, argu); n.f4.accept(this, argu); n.f5.accept(this, argu); n.f6.accept(this, argu); return _ret; } /** * f0 -> "if" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> Statement() * f5 -> "else" * f6 -> Statement() */ public MType visit(IfStatement n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, argu); n.f4.accept(this, argu); n.f5.accept(this, argu); n.f6.accept(this, argu); return _ret; } /** * f0 -> "while" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> Statement() */ public MType visit(WhileStatement n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, argu); n.f4.accept(this, argu); return _ret; } /** * f0 -> "System.out.println" * f1 -> "(" * f2 -> Expression() * f3 -> ")" * f4 -> ";" */ public MType visit(PrintStatement n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, argu); n.f4.accept(this, argu); return _ret; } /** * f0 -> AndExpression() * | CompareExpression() * | PlusExpression() * | MinusExpression() * | TimesExpression() * | ArrayLookup() * | ArrayLength() * | MessageSend() * | PrimaryExpression() */ public MType visit(Expression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "&&" * f2 -> PrimaryExpression() */ public MType visit(AndExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "<" * f2 -> PrimaryExpression() */ public MType visit(CompareExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "+" * f2 -> PrimaryExpression() */ public MType visit(PlusExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "-" * f2 -> PrimaryExpression() */ public MType visit(MinusExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "*" * f2 -> PrimaryExpression() */ public MType visit(TimesExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "[" * f2 -> PrimaryExpression() * f3 -> "]" */ public MType visit(ArrayLookup n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, argu); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "." * f2 -> "length" */ public MType visit(ArrayLength n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); return _ret; } /** * f0 -> PrimaryExpression() * f1 -> "." * f2 -> Identifier() * f3 -> "(" * f4 -> ( ExpressionList() )? * f5 -> ")" */ public MType visit(MessageSend n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, argu); n.f4.accept(this, argu); n.f5.accept(this, argu); return _ret; } /** * f0 -> Expression() * f1 -> ( ExpressionRest() )* */ public MType visit(ExpressionList n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); return _ret; } /** * f0 -> "," * f1 -> Expression() */ public MType visit(ExpressionRest n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); return _ret; } /** * f0 -> IntegerLiteral() * | TrueLiteral() * | FalseLiteral() * | Identifier() * | ThisExpression() * | ArrayAllocationExpression() * | AllocationExpression() * | NotExpression() * | BracketExpression() */ public MType visit(PrimaryExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); return _ret; } /** * f0 -> */ public MType visit(IntegerLiteral n, MType argu) { MType _ret=null; n.f0.accept(this, argu); return _ret; } /** * f0 -> "true" */ public MType visit(TrueLiteral n, MType argu) { MType _ret=null; n.f0.accept(this, argu); return _ret; } /** * f0 -> "false" */ public MType visit(FalseLiteral n, MType argu) { MType _ret=null; n.f0.accept(this, argu); return _ret; } /** * f0 -> */ public MType visit(Identifier n, MType argu) { String identifier_name = n.f0.toString(); MType _ret = null; _ret = new MIdentifier(identifier_name, n.f0.beginLine, n.f0.beginColumn); return _ret; } /** * f0 -> "this" */ public MType visit(ThisExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); return _ret; } /** * f0 -> "new" * f1 -> "int" * f2 -> "[" * f3 -> Expression() * f4 -> "]" */ public MType visit(ArrayAllocationExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, argu); n.f4.accept(this, argu); return _ret; } /** * f0 -> "new" * f1 -> Identifier() * f2 -> "(" * f3 -> ")" */ public MType visit(AllocationExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); n.f3.accept(this, argu); return _ret; } /** * f0 -> "!" * f1 -> Expression() */ public MType visit(NotExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); return _ret; } /** * f0 -> "(" * f1 -> Expression() * f2 -> ")" */ public MType visit(BracketExpression n, MType argu) { MType _ret=null; n.f0.accept(this, argu); n.f1.accept(this, argu); n.f2.accept(this, argu); return _ret; } }