The process of compilation is segregated into six different phases of compiler. Every phase takes input from the previous phase to convert the high-level language into low-level language.
In this article, we will be discussing the different phases of compiler and how it works.
Table of Contents
What is a compiler?
A compiler is a computer program that translates a source program written in high-level programming language into machine code for computer architecture. It ensures that the source code is accurate and properly formulated and the program is easy to understand. It can be used for several purposes, such as optimization, error detection, bug detection, and optimization of the generated code for performance. A compiler implements formal transformation from high-level source program to low-level target program.
Explore programming courses
Designs of compiler define an end-to-end solution and they tackle a defined subset that interfaces with compilation tools. Design requirements include a rigorously defined interface internally among compiler components and externally between supporting toolsets. If a compiler is written for a relatively simple language, it would be considered a monolithic piece of software. As the complexity of the source language increases, the design can be split into several interdependent phases.
Explore networking and hardware courses
Importance of Compiler
The requirement for compilers arises due to the way in which traditional processors execute object code. Processors utilize logic gates for routing signals to circuit board. They manipulate binary high and low signals to work the arithmetic logic unit. Since the initial high-level code contains commands, variables, calls, functions, and methods in a combination of lexical and arithmetic syntax.
Types of Compiler
There are different types of compilers in use:
- Cross compiler: It is a technology that allows developers to compile and run codes on multiple platforms. Such a compiler is useful while working on multiple versions of the code to ensure that all platforms in use are being supported. This is also useful while working on a new platform to verify if the code works on this platform.
- Just-in-time (JIT) compiler: It is a compiler that is designed to compile programs as they are executed. The compiler is much faster than a traditional compiler that has to compile a program from scratch whenever it needs to be run. These help in reducing the program size by eliminating the redundant code. This makes the program smaller and more efficient, which helps in improving performance.
- Source-to-source compiler: It is a software tool that is used for translating the source code into an executable code. This compiler is used for translating source code written in various programming languages. The process of translation can be completed both manually and automatically. It is performed by compiler that translates the source code into machine code which is then executed by target machine.
- Bytecode compiler: It is a compiler that is used for translating high-level language into machine code that is executable on the target machine. These compilers allow developers to write codes in high-level language and compile them to machine code. Through bytecode compilers, developers can write concise and easy to understand codes. However, these compilers need to be written in high-level language and are not suitable for developing low-level code.
- Binary compiler: It is a type of compiler that translates source code files into binary format. This format stores information of the program in a compact form which can be easily read by the computer. Developers use such compilers for database administration, network programming and web development.
- Hardware compiler: These compilers compile source code into machine code to transform the source code into machine code. After that, the computer executes this code. Such a compiler is used in embedded systems, operating systems and computer games. Assembler is an example of a hardware compiler.
Explore operating system courses
Phases of Compiler
In a compiler, there are the following six phases:
- Lexical analysis: This is the first phase of compiler which converts high level input programs into sequence of tokens. These tokens are sequence of characters that are treated as a unit in grammar of the programming language. It can be implemented with Deterministic finite Automata. The output is a sequence of tokens sent to parser for syntax analysis.
- Syntactic analysis or Parsing: Also known as syntax analysis; this is the second phase where provided input string is scanned for validation of structure of standard grammer. The syntactical structure is analysed and inspected to check whether the given input is correct in terms of programming syntax.
- Semantic analysis: In the third phase, the compiler scans whether parse tree follows the guidelines of language. The compiler keeps track of identifiers and expressions. A semantic analyzer defines the validity of parse tree and an annotated syntax tree is the output.
- Intermediate code generation: Once the parse tree is semantically confirmed, an intermediate code generator develops three address codes. During the time of the translation of source program into object code, a middle-level language code is generated by the compiler.
- Code optimizer: This is an optional phase of compiler that is used for optimizing the intermediate code. Through this optimization, the program runs fast and consumes less space. To increase the program speed, unnecessary strings of code are eliminated and sequences of statements are organized.
- Code generation: It is the final phase of compiler where the compiler acquires fully optimized intermediate code as input and maps it to machine code. During this phase, the intermediate code is translated into machine code.
Recently completed any professional course/certification from the market? Tell us what you liked or disliked in the course for more curated content.
Click here to submit its review.
Download this article as PDF to read offlineDownload as PDF