Q5Compiler Design
Question
2 marks
What is Semantic Analysis?
Answer
Semantic Analysis is the compiler phase that checks the source program for semantic correctness (type compatibility, scope rules, declarations) beyond what grammar rules can express.
Semantic Analysis is the third major phase of a compiler. It operates on the parse tree/AST produced by the syntax analyzer and verifies that the program is semantically correct — that it conforms to the rules of the language that cannot be captured by context-free grammars.
- Type Checking: Ensures that operators are applied to compatible types (e.g., no integer + string without conversion). Every expression has a type.
- Declaration Checking: Verifies that all identifiers are declared before use and that there are no multiple declarations in the same scope.
- Scope Resolution: Determines which declaration an identifier refers to based on scoping rules.
- Function Call Checking: Verifies correct number and types of arguments in function calls.
- Return Type Checking: Ensures function return values match the declared return type.