โ Compiler Design learning pathCompiler Design ยท Module 6 of 10
Semantic analysis and type checking
Checking meaning, scopes and types.
๐ Key ideas
- Semantic analysis checks rules grammar cannot express: declarations before use, type compatibility, correct number of arguments.
- Type checking can be static (at compile time, as in C and Java) or dynamic (at run time, as in Python).
- Type coercion converts types automatically, such as int to float in 2 + 3.5.
- Scope rules decide which declaration a name refers to; symbol tables are often nested by scope.
- Errors here include undeclared variables, type mismatch and calling a function with wrong arguments.
๐ก Real-life example: Grammar checks a sentence is well formed; semantic analysis checks it makes sense, like rejecting "the table drank the river".
Type checkingVerifying operands have compatible types
Static typingTypes checked at compile time
CoercionAutomatic type conversion
ScopeRegion of code where a name is visible