CuriouSTEM

View Original

An Introduction to Compilers

Many of us may have heard of programming languages such as C, C++, Python etc. These languages are called higher level programming languages. They were developed for various applications/purposes and for usage in selective domains such as commerce, business analysis, scientific research etc. Higher level languages have concepts such as characters, strings, numbers etc, making them very easy to understand and use. As we know, computers can only understand commands given in binary form (0s and 1s). Initially when computers were designed, programmers had to write instructions in 0s and 1s, which was not only tedious but extremely prone to errors.

The compiler is an application that can convert programs written in higher level language to lower level language that the computer can understand and execute. The program written in higher level language is called the source code, while the output of the compiler that can be executed is called object code. A compiler typically goes through multiple processes such as parsing, semantic analysis, code optimization, code generation etc. The first compiler was thought to have been designed way back in 1952. Since there was a shortage of memory in early days, the compiler program was split into multiple small programs and run one by one.

A compiler can be represented by three stages: front end, middle, back end. The front end deals mainly with correctness of syntax or semantics of the program in a given programming language. If there are errors, the compiler notifies the user prompting them to correct it. The front end would transform source code to Intermediate representation(IR). The middle part deals with optimization of the IR such as removal of redundant code or useless code. The back end takes the code from the middle stage, performs more transformations and optimizations that are specific to the CPU architecture. The back end creates object code, memory allocation and scheduling of instructions.

Picture Source: binarymove.com