What is Compiler?
A compiler is a program which is usually known as a translator and it is used to turn the high-level language into machine language so the computer can execute the human-written code. These days developers only use high-level languages which are not machine friendly and machine and computer do not have a direct approach to these kinds of languages. For that, we use a translator so the machine could understand how that particular code should be executed.
How does a compiler work?
A compiler read source code line by line, at once and show appropriate error after reading the code, and it works on various phases and each phase depends on the previous phase. A compiler includes 6 phases which are:
- Lexical analysis
- Syntax analysis
- Semantic analysis
- Intermediate code generator
- Code Optimizer
- Code generator
Lexical analysis: This phase of compiler scans the code and divide the code into groups of tokens.
Syntax analysis: This phase of compiler checks the syntax of the program and assure whether the code is written according to the syntax or not.
Semantic Analysis: This phase of compiler checks the meaning of the program, it checks whether the program code following a meaningful approach or not.
Intermediate Code Generation: This phase of compiler commences with the generation of Intermediate code; this intermediate code is the middle language of high-level and machine language.
Code Optimization: This phase of compiler deal with the unwanted code written in the program and remove them. Code optimization creates a sequence of intermediate code so the execution of the code becomes easy.
Code Generation: This is the most important phase of the compiler which accepts the sequence of code form the optimization phase and converts it into object code.
The compiler just not convert the high-level code to the object code but also check its legitimacy. As the working of the compiler is divided into 6 phases, which state that it is a collection of subprograms. Programming language like C++, Java, C and Perl use the compiler as a translator.
People Also Read: What is Arrays?