What is a Compiler?
Compiler is a program installed on the computer which translates the high-level language which is in the form of human understandable form into a machine level language that is understood by the computer. The machine understandable language is usually in the form of binary representation. This is done in order to create an executable program.
Types of compilers:
- Native code compiler– it converts the code from one language to another when the program has to run on the same kind of platform. This executable code that is generated by the compiler can only be run by that platform.
- One pass compiler– it is a kind of compiler that creates the executable file in exactly one pass.
- Threaded code compiler– replaces a string by a binary code.
- Cross compiler– it is the inverse of native code compiler. It converts the source code into the kind of executable code which can run on different kinds of platforms.
- Source to source compiler– it takes input in a high level language and then converts that code’s output into another high level language only.
- Source compiler– is the type of compiler that converts a code in a high level language into a code in assembly language only.
There are many kinds of compilers out of which the above mentioned are the most important one that you should know.
Different steps the compiler has to perform are- pre-processor, lexical analysis, generation of parse tree, annotation of the parsed tree (semantic analysis), intermediate representation, code optimization and target code generation. These steps can be categorized as front end, back end and middle end respectively, where intermediate representation falls into the category of middle end.
Compilers come under the category of a language processor. Interpreters are also a kind of language processors that directly run a program without converting the code to an executable one. Common examples where compilers are used are- C/C++, Java, etc. Common examples where interpreters are used are- Python.
Various applications of compilers are:
Working of a compiler
Initially, one instruction from the source code is taken and is converted into tokens. These tokens are fed into the lexical analyzer which in turn converts these tokens into a parse tree. This parse tree is then annotated by checking the type of each token in the instruction. For example, in case there is an int data type, it is converted into float data type. Then out of the annotated parse tree, an intermediate code is got and that is in turn converted into an intermediate code. This code will not be optimal i.e, we would want a code that is in its simplest form. So this code is fed to the code optimizer which will make the code much simpler. The final target code is then got by converting the optimized code into a machine level instruction.
Compilers are very important when you code in languages like c and c++, Java etc. There have been adult apps and meet n fuck sites that have gone down do to issues in this area. Without compilers, this code will never run. Compilers convert the source files into an executable code with extension .exe.