Semantic Analysis is the third phase of Compiler. Semantic Analysis makes sure that declarations and statements of program are semantically correct. It is a collection of procedures which is called by parser as and when required by grammar. Both syntax tree of previous phase and symbol table are used to check the consistency of the given code. Type checking is an important part of semantic analysis where compiler makes sure that each operator has matching operands.
It uses syntax tree and symbol table to check whether the given program is semantically consistent with language definition. It gathers type information and stores it in either syntax tree or symbol table. This type information is subsequently used by compiler during intermediate-code generation.
Errors recognized by semantic analyzer are as follows:
- Type mismatch
- Undeclared variables
- Reserved identifier misuse
Functions of Semantic Analysis:
- Type Checking –
Ensures that data types are used in a way consistent with their definition.
- Label Checking –
A program should contain labels references.
- Flow Control Check –
Keeps a check that control structures are used in a proper manner.(example: no break statement outside a loop)
float x = 10.1; float y = x*30;
In the above example integer 30 will be typecasted to float 30.0 before multiplication, by semantic analyzer.
Static and Dynamic Semantics:
- Static Semantics –
It is named so because of the fact that these are checked at compile time. The static semantics and meaning of program during execution, are indirectly related.
- Dynamic Semantic Analysis –
It defines the meaning of different units of program like expressions and statements. These are checked at runtime unlike static semantics.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Introduction to Syntax Analysis in Compiler Design
- Data flow analysis in Compiler
- Introduction of Compiler Design
- Why FIRST and FOLLOW in Compiler Design?
- Bootstrapping in Compiler Design
- Peephole Optimization in Compiler Design
- Input Buffering in Compiler Design
- Runtime Environments in Compiler Design
- Loop Optimization in Compiler Design
- Basic Blocks in Compiler Design
- Code Optimization in Compiler Design
- Labeling Algorithm in Compiler Design
- Types of Parsers in Compiler Design
- Error Handling in Compiler Design
- Syntax Directed Translation in Compiler Design
- Intermediate Code Generation in Compiler Design
- Introduction of Object Code in Compiler Design
- Transition diagram for Identifiers in Compiler Design
- Compiler Design | Syntax Directed Definition
- Compiler Design | Detection of a Loop in Three Address Code
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sachiniyermalkapur