LARA

Java Example for Semantic Analysis

What does this program compute?

class Example {
    boolean x;
    int y;
    int z;
 
    int compute(int x, int y) {
	int z = 3;
	return x + y + z;
    }
    public void main() {
	int res;
	x = true;
	y = 10;
	z = 17;
	res = compute(z, z+1);
	System.out.println(res);
    }
}

In the program above, what are the

identifiers

variables

Scope of a variable = part of program where the variable is visible

What is the scope of each variable?

Compilers and interpreters use environments (symbol tables) to keep track of variables in current scope

  • mathematically, these are just partial functions
  • we call them maps