2. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4
Semantic Analysis
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 conditons return a
boolean value.
5. Return type. Review that the value returned by a method match the
type of the method.
6. Parameters. Review that the parameters in a method match in type
and number with the declaration of the method.
3. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5
Errors to be reported
! Line <line>: Variable <variable> not found
! Line <line>: Variable <variable> is already defined
! Line <line>: Incompatible types: type mismatch
! Line <line>: Incompatible types: expected boolean
7. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9
Test Case 1
1. {
2. boolean boo; boolean foo;
3. int boo; int j;
4. int i; int minusfour; int k;
5. boolean k;
6. i = 4;
7. j = 3;
8. minusfour = boo;
9. while(i > 0) {
10. j = hello + 100 / 50;
11. while( j > 0) {
12. k = i*j;
13. print (k);
14. j = j-1;
15. }
16. i = i-1;
17. }
18. if (boo + minusfour) {
19. print (i);
20. }
21.
22. if (i < minusfour) {
23. print (j);
24. }
25. while (!i > minusfour) {
26. print (i);
27. i = i - 1;
28. }
29.
30. i = i + foo;
31. j = j + 1;
32. print (i);
33. print (j);
34. j = 100 > 90;
35. }
8. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10
Test Case 1
1. Line 2: variable <boo> is already defined
2. Line 4: variable <k> is already defined
3. Line 8: incompatible types: type mismatch
4. Line 10: variable <hello> not found
5. Line 10: incompatible types: type mismatch
6. Line 18: boolean expression expected
7. Line 30: incompatible types: type mismatch
8. Line 34: incompatible types: type mismatch
10. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12
Example
{
int a; int b;
boolean c;
a = 4;
b = a + 1;
if (a > b) {
print (a);
}
}
cube for
matching types
symbol table
stack
13. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15
Conditions
WHILE
String x = SemanticAnalizer.popStack();
if (!x.equals(“boolean”) {
error(3);
}
14. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16
Conditions
IF
String x = SemanticAnalizer.popStack();
if (!x.equals(“boolean”) {
error(3);
}
15. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17
Semantic Analysis
# Declaration and Unicity. Review for uniqueness and that the variable
has been declared before its usage.
# Type Matching. Review that the types of variables match the values
assigned to them.
$ Array’s indexes. Review that the indexes are integers.
# Conditions. Review that all expressions on the conditons return a
boolean value.
$ Return type. Review that the value returned by a method match the
type of the method.
$ Parameters. Review that the parameters in a method match in type
and number with the declaration of the method.
16. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18
Assignment #3
public class SemanticAnalyzer {
private static final Hashtable<String, Vector<SymbolTableItem>> symbolTable;
private static final Stack stack;
// create here a data structure for the cube of types
public static Hashtable<String, Vector<SymbolTableItem>> getSymbolTable()
public static void checkVariable(String type, String id)
public static String isBoolean()
public static String isTypeMatching()
public static void stackPush (String type)
public static String stackPop()
private String calculateType(String type, String operator)
private String calculateType(String type1, String type2, String operator)
private static void error(Gui gui, int error, int line, String info)
}
19. CSE340 - Principles of Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2015
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.