1. CSE340 - Principles of
Programming Languages
Lecture 18:
Semantic Analysis II
Javier Gonzalez-Sanchez
BYENG M1-38
Office Hours: By appointment
2. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2
Semantic Analysis
1. Declaration and Unicity. Review for uniqueness and that the variable
has been declared before its usage.
2. Type Matching. 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.
4. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4
Type matching | Example 1
int x, y, z;
char p, q, r;
float a, b, c;
boolean foo;
void method() {
x = a * c + p;
}
x
*
+
a c p
=
5. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5
Type matching | Cube
fill one sheet for
each operator in
the language
cube
6. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6
Type matching | Cube
OPERATOR int
float
char
string
boolean
void
int
float
char
string
boolean
void
cube
7. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7
Type matching | Cube
OPERATOR
+
int
float
char
string
boolean
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
8. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8
Type matching | Cube
OPERATOR
&
int
float
char
string
boolean
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
9. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9
Type matching | Cube
OPERATOR
<
int
float
char
string
boolean
void
int
boolean
boolean
X
X
X
X
float
boolean
boolean
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
10. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10
Type matching | Cube
OPERATOR
=
int
float
char
string
boolean
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
11. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11
Type matching | Cube
OPERATOR
-
int
float
char
string
boolean
void
int
float
X
X
X
X
cube (- unary)
OPERATOR
+
int
float
char
string
boolean
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)
12. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12
Type matching | Example 1
int x, y, z;
char p, q, r;
float a, b, c;
boolean foo;
void method() {
x = a * c + p;
}
x
*
+
a c p
=
13. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13
Type matching | Example 2
int a;
int c (int b) {
return b * 3 * 2 * 1 ;
}
void main () {
a = 1;
boolean a= c(14)/2 > 1;
}
cube for
matching types
symbol table
stack
14. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14
Programming Assignment 3
Level 2
Reviewing Type Matching
15. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15
Handwritten Notes
// 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;
17. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17
Assignment 2 | Code
C
1.
// except for the open parenthesis
SemanticAnalizer.pushStack(
tokens.get(currentToken).getToken()
);
18. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18
Parser
1. Store in a flag (operatorWasUSed):
Did the operator ‘-’ exist?
2.
if (operatorWasUsed)
String x = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, “-” );
SemanticAnalizer.pushStack(result);
}
19. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19
Parser
1. 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?
20. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 20
Parser
1. 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?
21. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 21
Parser
1. 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?
22. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 22
Parser
1. Store in a flag (operatorWasUSed):
Did the operator ‘-’ exist?
2.
if (operatorWasUsed)
String x = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, “!” );
SemanticAnalizer.pushStack(result);
}
23. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 23
Parser
1. 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
}
24. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 24
Parser
1. 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
}
25. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 25
Assignment 2 | Code
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, y, “=” );
if (!result.equals(“OK”) {
error(2);
}
27. 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.