Semantic Analysis.svg

Parsing answers one fundamental question:

Is this program structurally correct according to the grammar?

Semantic analysis answers a different one:

Does this program actually make sense?

A program can be grammatically valid and still be meaningless. For example:

var x = 10;
var x = 15;  // Parser: seems okay to me...

However, the above code redeclares the same variable in the same scope, which violates the language rules. This is where semantic analysis comes into the picture.

var x = 10;
var x = 15;  // ERROR: x has already been declared!

In this phase, we operate on the AST produced by the parser and perform checks related to names, scopes, and declarations.

What This Phase Checks

To keep the compiler simple and focused, this implementation performs only name and scope related semantic checks for now. Specifically: