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; } Case 2: int i, j; void method() { int i = 5; int j = i + i; int i = i + i; } Case 3: int i, m, k; boolean j; void main() { if (i>5) { ++i; } while (i + 1) { ++i; } do {++i; } while (i); for (i = 0; m; ++i) { k++; } } Case 4: int a; int b; int c, d; char c1, c2; int test1(int x, int y) { return x+y; } void main() { int i; i = a++; i = test1(a, b); i = test1(c1, c2); i = test1(a, c1); } } Case 5: int i, m; boolean j; public void main() { int m; int a[]; a = new int[j]; } Case 6: int i; void main(int m) { i++; return i; } Study Cases
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
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
names bindings i j m method-int-char n l method k method-int int global int method char global int method int global void function void function int function int method-int-char short method-int-char int global int Method-int
a, b; char c, d; float e,f; void foo1(int a) { // float a = 1; float w = a; } void foo2(char b) { int a = c + d; } int foo3() { int i = a + b; } Create the symbol table
SemanticAnalyzer.java public class SemanticAnalizer { private Hashtable<String, Vector<SymbolTableItem>> symbolTable; public static void CheckVariable(string type, string id) { // A. Search the id in the symbol table // B. if !exist then insert type, scope=global, value={0, false, "", ’’} // C. else error(1): “variable id is already defined.” }
slides can only be used as study material for the Compilers course at Universidad Panamericana. They cannot be distributed or used for another purpose.