2. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2
Semantic Analysis
Understanding the meaning
i.e.,
Interpreting expressions in their context
3. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3
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.
4. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4
Case 1:
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
5. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5
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.
7. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7
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
8. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8
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
9. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9
Symbol Table
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
10. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10
Exercise
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
11. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11
Programming Assignment 3
Level 1
Reviewing Declaration and Unicity
16. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16
A3 :: 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”
}
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.