What is a Compiler & what does it do?

High-level language to machine code translation, also known as compilation, is the process of converting a program written in a high-level programming language into machine code that can be executed by a computer's central processing unit (CPU). This process is performed by a program called a compiler.

The first step in the compilation process is called lexical analysis, in which the compiler scans the source code and converts it into a stream of tokens, which are the basic building blocks of the program. These tokens are then passed on to the next step, called syntax analysis, in which the compiler checks the source code for grammatical errors and ensures that it follows the syntax of the programming language.

Once the source code has been analyzed and found to be error-free, the compiler moves on to the next step, called semantic analysis. During this step, the compiler checks the source code for semantic errors, such as undeclared variables or type mismatches. If any errors are found, the compilation process stops and the programmer is notified of the problem.

If the source code passes semantic analysis, the compiler proceeds to the code generation step. During this step, the compiler generates machine code that corresponds to the source code. This machine code is then passed through an optimizer, which makes the code more efficient by removing unnecessary instructions and rearranging the code to take advantage of the CPU's architecture.

Finally, the machine code is assembled into an executable file that can be run on a computer. This executable file contains all the information needed to run the program, including the machine code and any necessary data or resources.

In summary, compilation is the process of converting a program written in a high-level language into machine code that can be executed by a computer. The process is performed by a compiler and consists of several steps, including lexical analysis, syntax analysis, semantic analysis, code generation, and optimization. By translating high-level languages into machine code, the compiler enables the computer to execute the program and produce the desired results.

0
Subscribe to my newsletter

Read articles from Harshwardhan Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshwardhan Singh
Harshwardhan Singh

Hey there! I'm Harsh, Software Developer who loves to explore technology and writes about my learnings in the tech domain. Want to hear more from me? You can follow me on my Twitter: https://twitter.com/harshwsingh.