CS453 Colorado State University ============================================== PA4, Semantic Analysis, and Types ============================================== ------------------------- Today Discuss partner suggestions - Multiple people wanted to see more in person meetings with partner. - There was some concern about knowing how much various aspects of the project would take and planning accordingly. Discuss PA4 Semantic analysis Computing lines and position information to enable type checking Type checking for ... -Meggy method calls -while and if statements -integer, byte, color, and boolean expressions ------------------------- PA4 Goals Tricky bits to figure out with partner ahead of time Aspects to divvy up - having the parser generate an AST instead of generating code - writing a code generator visitor that handles all PA3 features - testing the code generation for PA3 - extending the lexer for PA4 - extending the parser for PA4 - writing test cases for PA4 including one for each of the possible type errors - write parts of the type checking visitor Ways to consider breaking things up - have one partner write half the test cases and the other partner write the parts of the visitors that make those test cases work ------------------------- Type Checking Examples -> have students suggest single type error examples and run it with reference compiler -> show AST for this example -> show lines and positions ast -> show error message --------------------------------- Lines and positions for AST nodes The problem is that not all nodes in the AST have lines and positions associated with them. Use a right-to-left, or reverse, depth first visit to assign line and position numbers to nodes. Store the mapping of nodes to line and position data in a data structure that one concrete visitor creates and other visitors use as input. ----------------- Semantic Analysis (used as a reference http://www.cs.ucsd.edu/classes/wi07/cse131b/) Goal: figure out if the syntactically correct program makes sense in terms of the program semantics. - Another way to think of it is what errors can the parser not catch, but are still possible to catch at compile-time? Examples - class name is same as the file name - redeclaration of a variable - make sure that a break or continue appears in a loop? - how could we check this with a parser? - check that operations and functions are passed the correct types Examples of what we can not check with semantic analysis, IOW can't be completely checked statically (can check somewhat, but static check might be overly conservative) - divide by 0 - arithmetic overflow - grabbing an object reference out of a container and then casting to a subclass type - array bounds overflow ---------------- What are types? Each type has a - representation - interpretation - possible operations that can be performed on that type, we are focusing on this piece right now Example: Integer - represented with some number of contiguous bits (typically 32) - we "interpret" the representation as follows: value(Int) = sum_i Int[i]*2^i - the integer type has certain behavior/operations/methods associated with it, could depend on ISA - in general need to know the type to generate code: - which instruction in the ISA to use, addint: add r22, r18 // r22 = r22 + r18 adc r23, r19 // add with carry, // r23 = r23 + r19 + C versus addbyte: add r22, r18 // r22 = r22 + r18 - which routine to call, overloaded methods void foo(int p) void foo(byte p) Example: Color - representation? - interpretation? - operations? Suggested exercise: Determine the representation, interpretation, and operations for all of the types in the MeggyJava language. -------------------- Representing types For PA4 only need the following types: You can represent that set of types with an enumerated type in Java. -------------------- Type checking (spend at least 15 minutes on this) Can be done with a post-order traversal of the AST. - check that children nodes have expected types - map current node to its type Example: public void outAndExp(AndExp node) { if(mExprToType.get(node.getLExp()) != Type.BOOL) { throw new SemanticException( "Invalid left operand type for operator &&", mLines.getLine(node.getLExp()), mLines.getPos(node.getLExp())); } if(mExprToType.get(node.getRExp()) != Type.BOOL) { throw new SemanticException( "Invalid right operand type for operator &&", mLines.getLine(node.getRExp()), mLines.getPos(node.getRExp())); } mExprToType.put(node, Type.BOOL); } -> See slides for which errors are associated with which AST nodes. ------------------------ mstrout@cs.colostate.edu, 3/21/10