jgs
Compilers
Lecture 22:
Semantic Analysis II
Dr. Javier Gonzalez-Sanchez
[email protected]
Slide 2
Slide 2 text
jgs
Previously …
Slide 3
Slide 3 text
Dr. Javier Gonzalez-Sanchez | Compilers | 3
jgs
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 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.
Slide 4
Slide 4 text
Dr. Javier Gonzalez-Sanchez | Compilers | 4
jgs
Symbol Table
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
Dr. Javier Gonzalez-Sanchez | Compilers | 7
jgs
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 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.
Slide 8
Slide 8 text
Dr. Javier Gonzalez-Sanchez | Compilers | 8
jgs
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
=
Slide 9
Slide 9 text
Dr. Javier Gonzalez-Sanchez | Compilers | 9
jgs
Type matching | Cube
fill one sheet for each
operator in the
language
cube
Slide 10
Slide 10 text
Dr. Javier Gonzalez-Sanchez | Compilers | 10
jgs
Type matching | Cube
OPERATOR int float char strin
g
boolea
n
void
int
float
char
string
boolean
void
cube
Slide 11
Slide 11 text
Dr. Javier Gonzalez-Sanchez | Compilers | 11
jgs
Type matching | 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
Slide 12
Slide 12 text
Dr. Javier Gonzalez-Sanchez | Compilers | 12
jgs
Type matching | 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
Slide 13
Slide 13 text
Dr. Javier Gonzalez-Sanchez | Compilers | 13
jgs
Type matching | 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
Slide 14
Slide 14 text
Dr. Javier Gonzalez-Sanchez | Compilers | 14
jgs
Type matching | 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
Slide 15
Slide 15 text
Dr. Javier Gonzalez-Sanchez | Compilers | 15
jgs
Type matching | 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)
Slide 16
Slide 16 text
Dr. Javier Gonzalez-Sanchez | Compilers | 16
jgs
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
=
Slide 17
Slide 17 text
Dr. Javier Gonzalez-Sanchez | Compilers | 17
jgs
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
Slide 18
Slide 18 text
Dr. Javier Gonzalez-Sanchez | Compilers | 18
jgs
Programming Assignment 3
Level 2
Reviewing Type Matching
Slide 19
Slide 19 text
Dr. Javier Gonzalez-Sanchez | Compilers | 19
jgs
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;
Dr. Javier Gonzalez-Sanchez | Compilers | 21
jgs
Assignment 2 | Code
C
1.
// except for the open parenthesis
SemanticAnalizer.pushStack(
tokens.get(currentToken).getToken()
);
Slide 22
Slide 22 text
Dr. Javier Gonzalez-Sanchez | Compilers | 22
jgs
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);
}
Slide 23
Slide 23 text
Dr. Javier Gonzalez-Sanchez | Compilers | 23
jgs
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?
Slide 24
Slide 24 text
Dr. Javier Gonzalez-Sanchez | Compilers | 24
jgs
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?
Slide 25
Slide 25 text
Dr. Javier Gonzalez-Sanchez | Compilers | 25
jgs
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?
Slide 26
Slide 26 text
Dr. Javier Gonzalez-Sanchez | Compilers | 26
jgs
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);
}
Slide 27
Slide 27 text
Dr. Javier Gonzalez-Sanchez | Compilers | 27
jgs
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
}
Slide 28
Slide 28 text
Dr. Javier Gonzalez-Sanchez | Compilers | 28
jgs
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
}
Slide 29
Slide 29 text
Dr. Javier Gonzalez-Sanchez | Compilers | 29
jgs
Assignment 2 | Code
String x = SemanticAnalizer.popStack();
String y = SemanticAnalizer.popStack();
String result = SemanticAnalizer.checkCube (x, y, “=” );
if (!result.equals(“OK”) {
error(2);
}
Slide 30
Slide 30 text
Dr. Javier Gonzalez-Sanchez | Compilers | 30
jgs
Homework
Programming
Final Version of Your Parser
Slide 31
Slide 31 text
Dr. Javier Gonzalez-Sanchez | Compilers | 31
jgs
Extra
If time allows it
start with your Semantic Analyzer
(more details/work is coming)
Slide 32
Slide 32 text
Dr. Javier Gonzalez-Sanchez | Compilers | 32
jgs
Questions
Slide 33
Slide 33 text
jgs
Compilers
Javier Gonzalez-Sanchez, Ph.D.
[email protected]
Spring 2025
Copyright. These slides can only be used as study material for the Compilers course at Universidad Panamericana.
They cannot be distributed or used for another purpose.