Compiler Design

Compiler Design

Introduction to Compiler :

A compiler is a program that translates a source program written in some high-level programming language (such as C, C++, Java etc.) into machine code for some computer architecture (such as the Intel Pentium architecture, Sparc assembly language etc.). Since computer can understand only in binary numbers so a compiler is used to fill the gap otherwise it would have been difficult for a human to find info in the 0 and 1 form. The generated machine code can be later executed many times against different data each time.





Language processing systems using Compiler



There are two parts of compilation;
1. Analysis
2. Synthesis

1. Analysis :-

The analysis part breaks up the source program into constant piece and creates an intermediate representation of the source program. e.g.: Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of this phase.

2. Synthesis :-

The synthesis part constructs the desired target program from the intermediate representation. e.g.: Intermediate Code Generator, Code Generator, and Code Optimizer are the parts of this phase.

 Phases of a Compiler

There are 6 phases in a compiler. Each of this phase help in converting the high-level langue the machine code.

1. Lexical analysis

2. Syntax analysis
3. Semantic analysis
4. Intermediate code generator
5. Code optimizer
6. Code generator

Phase 1: Lexical Analysis

Lexical Analysis is the first phase when compiler scans the source code. This process can be left to right, character by character, and group these characters into tokens.

The primary functions of this phase are:
  • Identify the lexical units in a source code
  • Classify lexical units into classes like constants, reserved words, and enter them in different tables. It will Ignore comments in the source program
  • Identify token which is not a part of the language   

Phase 2: Syntax Analysis

Syntax analysis is all about discovering structure in code. It determines whether or not a text follows the expected format. The main aim of this phase is to make sure that the source code was written by the programmer is correct or not.
Here, is a list of tasks performed in this phase:
  • Obtain tokens from the lexical analyzer
  • Checks if the expression is syntactically correct or not
  • Report all syntax errors
  • Construct a hierarchical structure which is known as a parse tree

Phase 3: Semantic Analysis

Semantic analysis checks the semantic consistency of the code. It uses the syntax tree of the previous phase along with the symbol table to verify that the given source code is semantically consistent. It also checks whether the code is conveying an appropriate meaning.
Functions of Semantic analyses phase are:
  • Helps you to store type information gathered and save it in symbol table or syntax tree
  • Allows you to perform type checking
  • In the case of type mismatch, where there are no exact type correction rules which satisfy the desired operation a semantic error is shown
  • Collects type information and checks for type compatibility
  • Checks if the source language permits the operands or not

Phase 4: Intermediate Code Generation

Once the semantic analysis phase is over the compiler, generates intermediate code for the target machine. It represents a program for some abstract machine.
Functions on Intermediate Code generation:
  • It should be generated from the semantic representation of the source program
  • Holds the values computed during the process of translation
  • Helps you to translate the intermediate code into target language
  • Allows you to maintain precedence ordering of the source language
  • It holds the correct number of operands of the instruction

Phase 5: Code Optimization

The next phase of is code optimization or Intermediate code. This phase removes unnecessary code line and arranges the sequence of statements to speed up the execution of the program without wasting resources. The main goal of this phase is to improve on the intermediate code to generate a code that runs faster and occupies less space.
The primary functions of this phase are:
  • It helps you to establish a trade-off between execution and compilation speed
  • Improves the running time of the target program
  • Generates streamlined code still in intermediate representation
  • Removing unreachable code and getting rid of unused variables
  • Removing statements which are not altered from the loop

Phase 6: Code Generation

Code generation is the last and final phase of a compiler. It gets inputs from code optimization phases and produces the page code or object code as a result. The objective of this phase is to allocate storage and generate relocatable machine code.
It also allocates memory locations for the variable. The instructions in the intermediate code are converted into machine instructions. This phase coverts the optimize or intermediate code into the target language.
The target language is the machine code. Therefore, all the memory locations and registers are also selected and allotted during this phase. The code generated by this phase is executed to take inputs and generate expected outputs.

No comments:

Post a Comment

INSTAGRAM FEED

@maurya_m_j