CS453 Intro and PA1 1CS453 Lecture Semantic Analysis 1Plan for Today MiniJava types and type rules– representation and interpretation will be discussed while doing IRTgeneration– how type information is represented with SymTable and Type datastructures– what type errors can occur in some of the AST nodesCS453 Lecture Semantic Analysis 2Type implementation in the MiniJava compiler Only one instance of the type object per atomic type and class type– to determine if types are equal just compare references– does the Type class know about inheritance?public class Type { public static final Type ARRAY = new Type(); public static final Type BOOL = new Type(); public static final Type INT = new Type(); // class type map (key: class name, value: type) private static final HashMap<String, Type> classTypes = new HashMap<String, Type>();CS453 Lecture Semantic Analysis 3MiniJava Types for ExampleCS453 Lecture Semantic Analysis 4Implementing type checking for MiniJava (Slide 1) Visitor over AST will check for type errors at each AST nodeid = Exp ; statement = {assign} id exp[LINENUM,POSNUM] Undeclared variable VARNAME[LINENUM,POSNUM] Invalid expression type assigned to variable VARNAMESyntax AST production AST nodeErrorsid [Exp] = Exp ; statement = {array_assign} id [index]:exp exp[LINENUM,POSNUM] Undeclared variable VARNAME[LINENUM,POSNUM] Array reference to non-array type[LINENUM,POSNUM] Invalid index expression type for array reference[LINENUM,POSNUM] Invalid expression type assigned into arrayExp op Exp exp = {op} [l_exp]:exp [r_exp]:exp[LINENUM,POSNUM] Invalid left operand type for operator OP[LINENUM,POSNUM] Invalid right operand type for operator OPCS453 Intro and PA1 2CS453 Lecture Semantic Analysis 5Implementing type checking for MiniJava (Slide 2)! Exp exp = {not} exp[LINENUM,POSNUM] Invalid operand type for operator !Syntax AST production AST nodeErrorsnew int [ Exp ] exp = {new_array} exp[LINENUM,POSNUM] Invalid operand type for new array operatorExp [ Exp ] exp = {array} exp [index]:exp[LINENUM,POSNUM] Array reference to non-array type[LINENUM,POSNUM] Invalid index expression type for array referenceExp . length exp = {length} exp[LINENUM,POSNUM] Operator length called on non-array typeCS453 Lecture Semantic Analysis 6Implementing type checking for MiniJava (Slide 3)new id ( ) exp = {new} id[LINENUM,POSNUM] Class CLASSNAME does not existSyntax AST production AST nodeErrorsExp . id ( ExpList ) exp = {call} exp id [args]:exp*[LINENUM,POSNUM] Receiver of method call must be a class type[LINENUM,POSNUM] Method METHODNAME does not exist in class typeCLASSNAME[LINENUM,POSNUM] Method METHODNAME requires exactly NUM arguments[LINENUM,POSNUM] Invalid argument type for method METHODNAMECS453 Lecture Semantic Analysis 7Implementation Plan Test-driven approach– Write test cases for– one AST node at a time– one type check at a time– one possible type at a time (start with atomic types)– Set up a regression testing script– capture your compiler output on test case to a temp file– compare output to a handwritten output for test case– Implement– one AST node at a time– one type check at a time– one possible type at a time (start with atomic types) Advantages– turn in your program at any point to get partial credit– separate two most difficult pieces: understanding MiniJava typing andimplementing the typecheck with the provided data
View Full Document