Javier Gonzalez-Sanchez
BYENG M1-38
Office Hours: By appointment
CSE340 - Principles of
Programming Languages
Lecture 17:
Semantic Analysis I
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2
Semantic Analysis
Understanding the meaning
i.e.,
Interpreting expressions in their context
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.
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
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.
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6
1.  Variable Declaration and Unicity
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
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
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
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
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11
Programming Assignment 3
Level 1
Reviewing Declaration and Unicity
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12
Symbol Table
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13
Symbol Table
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14
Grammar
<PROGRAM> à '{' <BODY> '}’
<BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'}
<ASSIGNMENT> à identifier '=' <EXPRESSION>
<VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier
<WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM>
<IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>]
<RETURN> à 'return'
<PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’
<EXPRESSION> à <X> {'|' <X>}
<X> à <Y> {'&' <Y>}
<Y> à ['!'] <R>
<R> à <E> {('>'|'<'|'=='|'!=') <E>}
<E> à <A> {(’+'|'-’) <A>}
<A> à <B> {('*'|'/') <B>}
<B> à ['-'] <C>
<C> à integer | octal | hexadecimal | binary | true | false |
string | char | float | identifier|'(' <EXPRESSION> ')'
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15
A3 :: Parser Update
VARIABLE
void rule_variable( ) {
. . .
if (tokens.get(currentToken1).getType().equals(“identifier”)) {
SemanticAnalizer.CheckVariable(
tokens.get(currentToken-1).getWord(),
tokens.get(currentToken).getWord()
);
currentToken++;
} else {
error (6);
}
. . .
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”
}
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17
A3 :: Review
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18
Homework
Programming Assignment 3
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.

201506 CSE340 Lecture 17

  • 1.
    Javier Gonzalez-Sanchez BYENG M1-38 OfficeHours: By appointment CSE340 - Principles of Programming Languages Lecture 17: Semantic Analysis I
  • 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.
  • 6.
    Javier Gonzalez-Sanchez |CSE340 | Summer 2015 | 6 1.  Variable Declaration and Unicity
  • 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
  • 12.
    Javier Gonzalez-Sanchez |CSE340 | Summer 2015 | 12 Symbol Table
  • 13.
    Javier Gonzalez-Sanchez |CSE340 | Summer 2015 | 13 Symbol Table
  • 14.
    Javier Gonzalez-Sanchez |CSE340 | Summer 2015 | 14 Grammar <PROGRAM> à '{' <BODY> '}’ <BODY> à {<PRINT>';'|<ASSIGNMENT>';'|<VARIABLE>';’|<WHILE>|<IF>|<RETURN>';'} <ASSIGNMENT> à identifier '=' <EXPRESSION> <VARIABLE> à ('int'|'float'|'boolean'|'char’|'string'|'void')identifier <WHILE> à 'while' '(' <EXPRESSION> ')' <PROGRAM> <IF> à 'if' '(' <EXPRESSION> ')' <PROGRAM> ['else' <PROGRAM>] <RETURN> à 'return' <PRINT> à ’print’ ‘(‘ <EXPRESSION> ‘)’ <EXPRESSION> à <X> {'|' <X>} <X> à <Y> {'&' <Y>} <Y> à ['!'] <R> <R> à <E> {('>'|'<'|'=='|'!=') <E>} <E> à <A> {(’+'|'-’) <A>} <A> à <B> {('*'|'/') <B>} <B> à ['-'] <C> <C> à integer | octal | hexadecimal | binary | true | false | string | char | float | identifier|'(' <EXPRESSION> ')'
  • 15.
    Javier Gonzalez-Sanchez |CSE340 | Summer 2015 | 15 A3 :: Parser Update VARIABLE void rule_variable( ) { . . . if (tokens.get(currentToken1).getType().equals(“identifier”)) { SemanticAnalizer.CheckVariable( tokens.get(currentToken-1).getWord(), tokens.get(currentToken).getWord() ); currentToken++; } else { error (6); } . . .
  • 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” }
  • 17.
    Javier Gonzalez-Sanchez |CSE340 | Summer 2015 | 17 A3 :: Review
  • 18.
    Javier Gonzalez-Sanchez |CSE340 | Summer 2015 | 18 Homework Programming Assignment 3
  • 19.
    CSE340 - Principlesof 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.