1. Declaration and Unicity. Review for uniqueness and that the variable has been declared before its usage. 2. Types. Review that the types of variables match the values assigned to them. 3. Array’s indexes. Review that the indexes are integers. 4. Conditions. Review that all expressions on the conditions return a boolean value. 5. Return type. Review that the value returned by a method matches the type of the method. 6. Parameters. Review that the parameters in a method match the type and number with the declaration of the method.
int i; char j; int m; void method(int n, char c) { int n; short l; i = j; i = m; } void method() { int i = 5; int j = i + i; } int k; int method(int i) { if (i>5) { ++i; } while (i + 1) { ++i; } do {++i; } while (i); for (i = 0; m; ++i) { k++; } } name type scope value i int global j char global m int global method-int-char void function n int method-int-char l short method-int-char method void function i int method j int method k int global method-int int function i Int method-int
1. Declaration and Unicity. Review for uniqueness and that the variable has been declared before its usage. 2. Types. Review that the types of variables match the values assigned to them. 3. Array’s indexes. Review that the indexes are integers. 4. Conditions. Review that all expressions on the conditions return a boolean value. 5. Return type. Review that the value returned by a method matches the type of the method. 6. Parameters. Review that the parameters in a method match the type and number with the declaration of the method.
| Cube OPERATOR + int float char strin g boolea n void int int float X string X X float float float X string X X char X X X string X X string string string string string string X boolean X X X string X X void X X X X X X cube
| Cube OPERATOR & int float char strin g boolea n void int X X X X X X float X X X X X X char X X X X X X string X X X X X X boolean X X X X boolean X void X X X X X X cube
| Cube OPERATOR < int float char string boolean void int boolea n boolea n X X X X float boolea n boolea n X X X X char X X X X X X string X X X X X X boolean X X X X X X void X X X X X X cube
| Cube OPERATOR = int float char strin g boolea n void int OK X X X X X float OK OK X X X X char X X OK X X X string X X X OK X X boolean X X X X OK X void X X X X X OK cube
| Cube OPERATOR - int float char strin g boolea n void int float X X X X cube (- unary) OPERATOR + int float char strin g boolea n void int int float X string X X float float float X string X X char X X X string X X string string string string string string X boolean X X X string X X void X X X X X X cube (- binary)
/ / Definition of the cube of types and operators int cube [][][]; public static int OP_PLUS = 1; public static int OP_MINUS = 2; public static int OP_MULT = 3; / / … public static int INTEGER = 1; public static int FLOAT = 2; / / … cube [OP_PLUS][INTEGER][INTEGER] = INTEGER; cube [OP_PLUS][FLOAT][INTEGER] = FLOAT; cube [OP_PLUS][INTEGER][FLOAT] = FLOAT;
Store in a flag (operatorWasUSed): Did the operator ‘-’ exist? 2. if (operatorWasUsed) String x = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, “-” ); SemanticAnalizer.pushStack(result); }
Store in a flag (twiceHere): Did we pass this point twice? 3. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, operator ); SemanticAnalizer.pushStack(result); twiceHere = false; / / reset the flag } 2. Store the operator that creates the loop?
Store in a flag (twiceHere): Did we pass this point twice? 3. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, operator ); SemanticAnalizer.pushStack(result); twiceHere = false; / / reset the flag } 2. Store the operator that creates the loop?
Store in a flag (twiceHere): Did we pass this point twice? 3. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, operator ); SemanticAnalizer.pushStack(result); twiceHere = false; / / reset the flag } 2. Store the operator that creates the loop?
Store in a flag (operatorWasUSed): Did the operator ‘-’ exist? 2. if (operatorWasUsed) String x = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, “!” ); SemanticAnalizer.pushStack(result); }
Store in a flag (twiceHere): Did we pass this point twice? 2. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, “&” ); SemanticAnalizer.pushStack(result); twiceHere = false; / / reset the flag }
Store in a flag (twiceHere): Did we pass this point twice? 2. if (twiceHere) String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, “&” ); SemanticAnalizer.pushStack(result); twiceHere = false; / / reset the flag }
| Code String x = SemanticAnalizer.popStack(); String y = SemanticAnalizer.popStack(); String result = SemanticAnalizer.checkCube (x, y, “=” ); if (!result.equals(“OK”) { error(2); }
slides can only be used as study material for the Compilers course at Universidad Panamericana. They cannot be distributed or used for another purpose.