Semantic Analysis

(meaning, logic of program) Semantic analysis is dependent on language

Typical Semantic Errors

→ Type mismatch

→ Undeclared Variable

→ Reserved identifier misuse

→ Multiple declarations of variables in a scope

→ Accessing an out-of-scope variable

→ Actual and formal parameter mismatch.

Scoping

→ Match identifiers’ declaration with uses.

→ Visibility of an entity

→ Binding between declaration and uses

→ The scope of an identifier is the portion of a program in which the identifier is accessible

→ The same identifier may refer to different things in different parts of the program: Different scopes for the same name don’t overlap

→ An identifier may have a restricted scope.

Static Scoping

const int b = 5;
int foo()
{
	int a = b + 5;
	return a;
}
int bar()
{
	int b = 2;
	return foo();
}
int main()
{
	foo(); //return 10
	bar(); //return 10
	return 0;
}

Dynamic Scoping

const int b = 5;
int foo()
{
	int a = b + 5;
	return a;
}
int bar()
{
	int b = 2;
	return foo();
}
int main()
{
	foo(); //return 10
	bar(); //return 7
	return 0;
}