Semantic Analysis
. Check semantics
|

|
. Error reporting
|
. Disambiguate overloaded operators |
. Type coercion
|
. Static checking
|
- Type checking
|
-Control flow checking
|
- Uniqueness checking
|
- Name checks
|
Assume that the program has been verified to be syntactically correct and converted into some kind of intermediate representation (a parse tree). One now has parse tree available.
The next phase will be semantic analysis of the generated parse tree. Semantic analysis also includes error reporting in case any semantic error is found out.
Semantic analysis is a pass by a compiler that adds semantic information to the parse tree and performs certain checks based on this information. It logically follows the parsing phase, in which the parse tree is generated, and logically precedes the code generation phase, in which (intermediate/target) code is generated. (In a compiler implementation, it may be possible to fold different phases into one pass.) Typical examples of semantic information that is added and checked is typing information ( type checking ) and the binding of variables and function names to their definitions ( object binding ). Sometimes also some early code optimization is done in this phase.
For this phase the compiler usually maintains symbol tables in which it stores what each symbol (variable names, function names, etc.) refers to.
Following things are done in Semantic Analysis:
. Disambiguate Overloaded operators : If an operator is overloaded, one would like to specify the meaning of that particular operator because from one will go into code generation phase next.
. Type checking : The process of verifying and enforcing the constraints of types is called type checking. This may occur either at compile-time (a static check) or run-time (a dynamic check). Static type checking is a primary task of the semantic analysis carried out by a compiler. If type rules are enforced strongly (that is, generally allowing only those automatic type conversions which do not lose information), the process is called strongly typed, if not, weakly typed.
. Uniqueness checking : Whether a variable name is unique or not, in the its scope.
. Type coercion : If some kind of mixing of types is allowed. Done in languages which are not strongly typed. This can be done dynamically as well as statically.
. Name Checks : Check whether any variable has a name which is not allowed. Ex. Name is same as an identifier( Ex. int in java).
|