Semantic Analysis
Info
With semantic analysis (contextual analysis), we aim to reject programs that satisfy CFG but invalid. We also want to compute information that will be needed by the compiler to generate machine code.
E.g every variable needs to be declared. In a context-sensitive analysis, we can enforce this, while we can't in context-free analysis.
Not that this is the same idea as syntax vs semantics in logic, where the syntax does not have any inherent meaning unless the semantics are defined.
For LACS:
-
Resolve names (map each use of a name to declaration of variable or procedure), build symbol table for each scope (procedure)
Potential errors:
- Undeclared name
- Duplicate declaration
-
Compute types of (sub)expressions