Semantic Analysis
What Is Semantic Analysis?
After syntax analysis confirms that the structure of your code is valid, semantic analysis checks that the code also has meaning.
Example:
int x = "hello"; // Syntax is fine, but semantically invalid
Here, the parser is okay with the structure, but the semantic analyzer will raise an error because you’re assigning a string to an integer.
What Does It Check?
- Type Checking
- Are values used with the correct data types?
- Example:
int x = 5 + true;
→ invalid in most languages
- Scope Resolution
- Are variables declared before use?
- Is a variable accessed within its valid scope?
- Function Signatures and Calls
- Are functions called with the right number and type of arguments?
- Variable Declarations and Usage
- Prevents using undeclared variables
- Checks for duplicate declarations
Using the AST
Semantic analysis works with the AST, adding details like variable types or scopes. The AST is a simplified tree from syntax analysis, acting as an intermediate representation (IR) for this phase.
Symbol Table
Semantic analysis heavily uses a symbol table, which keeps track of:
- Variable names
- Types
- Scopes
- Function definitions
It’s like the compiler’s cheatsheet for the code.
Conclusion
- Semantic analysis ensures code is meaningful, using the AST.
- It uses a symbol table to track declarations, scopes, and types
- Errors at this stage are not structural but meaning-related