Compiler: Definition, Structures, Types and Application

What is Compiler?

Compiler is a computer program that has the capacity to translate high-level language into a machine language/ low-level language that is readable to the computer. The high-level language is know as source while the low-level language is known as machine code.

Computers usually read instructions in machine language format written in binary. A binary is string of numbers made up of 0s and 1s.  Computers can’t understand human readable languages and humans many not also understand computer language (machine). Therefore programmers work with the programming language the have understanding of, which can then be translated to the machine language which the computer can understand. Compilers do the job of translating high-level (human-readable) language machine (computer-readable) language.

Program Sources Code Analysis

Source code can be analyzed in three main steps. They are: Linear analysis, hierarchical analysis and semantic analysis

Linear Analysis

In linear analysis, the source code characters are read from left to right. The characters are therefore grouped according to their collective meanings, and the groups are called tokens.

Hierarchical Analysis

Having grouped the source codes according to collective meaning known as tokens, the tokens are further divided in order of hierarchy in a nested manner.

Semantic Analysis

Semantic analysis involves checking if the source code components are appropriate in meaning.  

Compiler Structures

The steps in the analysis of source code are further taken in phases, which determine the structure of the program language code. In the compilation process, output of each step becomes the input for the next step. The following are the phases or structures of the compilation process: lexical analyzer, syntax analyzer, semantic analyzer, intermediate code generator (ICG), code optimizer, target code generator

Lexical Analyzer

Also known as scanner, Lexical Analyzer takes the high-level language sources code as the input, scans the source code characters from left to right, groups them into lexemes (groups of characters with same meaning) forming tokens. It removes white spaces and comments, and check deletes the lexical errors.

 Syntax Analyzer

The output of lexical analyzer is the input of syntax analyzer, also known as Parser. The syntax analyzer checks for syntax errors in the source code. It does this by constructing a parse tree of all the tokens. The parse tree must be according to the rules of th source code grammar for the syntax to be correct. The grammar for the codes is also context-free grammar.

Semantic Analyzer

The output of parse tree of the syntax analyzer acts as the input in semantic analyzer. It does the following to the parse tree output:

  • verifies the parse tree,
  • checks for validity of the programming language source code such as compatibility of data types, declaration and initialization of variable, and so on.
  • creates a verified parse tree called annotated parse tree, and
  • finally performs flow checking , type checking etc.

Intermediate Code Generator (ICG)

This generates an intermediate code. The intermediate code is a code that is neither high-level language nor machine language. All compilers have same intermediate code. The intermediate code is converted to the machine language according to the platform.

Code Optimizer

This coverts and optimizes the intermediate code such that it is executed faster using less resources such as CPU, memory. It deletes any meaningless lines of code and rearranges the code, but the meaning of the source code remains unchanged.

Target Code Generator

This is the finally phase of compiler structuring and compilation.

It generates a machine code of the optimized intermediate code. This machine code is relocatabe. Relocatabe machine code can be loaded and run at any time.

Sections of Compiler

The phases of compilation process is divided into two sections:

1 Frond End

The frond end section of compiler comprises of lexical analysis, syntax analysis, and intermediate code generation.

2 Back End

The back end is made up of  the code optimization, and target code generation.

Features of a Compiler

  • A compiler has the following features:
  • Independence
  • Error detection
  • Optimization
  • Data security
  • Speed

Types of Compiler

There are four types of compiler

Bootstrap Compilers

The bootstrap compilers are written in a programming language they have to compile

Cross Compilers

These compilers generate executable machine language code for a platform. This platform is different from the platform on which the compiler is running.

Source to Source or Trancompilers

These types of compilers convert the source code of one programming language to source code of another programming language.

Decompiler

As the name implies, this is the reverse of compiler. It convert machine code into high-level language

Applications/ Uses of Compilers

Compilers have the following uses:

They generate executable code files

Translate code from one language to another.

Make the code independent of the platform

Debug the code of syntax and semantic errors.

Read also: Advantages and Disadvantages of Compiler

Ikechukwu Evegbu

Ikechukwu Evegbu is a graduate of Statistics with over 10 years experience as Data Analyst. Worked with Nigeria's Federal Ministry of Agriculture and Rural Development. A prolific business development content writer. He's the Editor, Business Compiler

Previous Post Next Post