A little about Compilers



What is a compiler?


Compiler is used for conversion of source code in high-level that in a low-level language. High level Language is a human understandable language such as English and low-level language is a machine understandable language such as binary.


The program written in a high-level language goes through various transformations for getting converted to machine level language. The resulting target program may be in an object or machine level language.


This computer software is known as a Compiler, which takes the input from user, and converts it into hardware language. Since the hardware language is hard for us to grasp, we need a mechanism such that we could communicate with the hardware or machine.


The mechanism of the conversion is achieved with the help of a computer software, which acts like a bridge between a human and a machine.


High-level source code is fed as an input and a target program is generated, which is done through the 6 phases of Compilation. It also results the errors if present in the program. Errors may be Lexical, Syntactic or Semantic.


Optimization is the utmost need of any machine, since the compilation process is cost dependent. Intermediate codes are optimized by various techniques such as sub-expression elimination, variable code elimination, etc. so as to reduce the costs of compilation and machine.


Compiler uses binding to generate a meaningful and executable code. At first, when the code is about to be written at any instance of time, nothing is known except the problem, such as Definitions and declarations, variables and objects. Then the various step by step procedures are carried out to result in the target source code.


Various Tasks in Compilation.


Planning:


Planning or Conceptualization prepares a blueprint for compiler is created to proceed towards the problem. Some strategies, algorithms and data structures are employed for the same. Strategies decide program's flow which enables the algorithm generation.


Algorithms, are a set of instructions to enable the machine understand the needs of users. Data structures such as arrays, enums, lists, unions are used for storage, modification and manipulation of data.


 


Coding:


After the blueprint is ready, the actual code is written, which consists of the declarations and definitions of functions, constants and variables, data structures to be used, with data types etc. Functions are designed to perform a particular tasks.



Compilation:
The code when completed, i then passed for compilation to the compiler to get machine instructions.



Linking:
After the code conversion, functions, variables and data need to be stored and accessed later. Linker provides them the memory addresses.



Loader:
The libraries are loaded and eventually, the actual addresses of code and data are created.


 


Execution:
After all the processes of compiling, linking and loading, the last phase of execution is implemented. The variables defined in the code get the value at the execution time.


 


The compilation process is divided in to two parts, front end and the back end. The initial 4 phases are the Front-end part and the remaining the back end.


1. Lexical Analyzer – Program is read in form of lexemes and is converted into tokens or regular expressions. These are the expressions which a lexical analyzer can understand. Lex analyzer also removes white-spacing and comments from input. Lex is a tool used for the purpose.


2. Syntax Analyzer – Tokens are scanned, and CFG (Context Free Grammar) constructs parse tree with its help. Rules or productions are used for representing the program. Syntax errors are also detected by this phase. 


3. Semantic Analyzer– Parse trees are verified for validity, i.e. whether they represent maningful sentences or not. Then a parse tree with respect to the rules and input is generated, following a type check, Label check and Flow control check.


4. Intermediate Code Generator – The intermediate code generated is machine executable. Intermediate codes can be Three address code, Polish Notations etc. After intermediate code conversion, machine language code is obtained. This is done by last two phases that are platform dependent.


We don't require to build a new compiler for different language every time. We can use already existing compiler codes and make changes accordingly.


5. Code Optimizer – Code is analyzed and transformed for better and cost-optimum results. This doen't changes the way of code execution, only reducing the lines of code. Optimization can be of two types, machine independent and machine dependent.


6. Target Code Generator – The final task for making a successful compiler is to have a machine understandable code. The output of code however, depends on assemblers used by the machines. This code can be made platform or machine dependent and independent both.

Editor: Aastha Gupta Added on: 2020-05-24 14:22:53 Total View:575







Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---