What is a Compiler?: Definition, Structure, Types, Applications

Posted in

What is a Compiler?: Definition, Structure, Types, Applications

Vinay Khatri
Last updated on November 15, 2022

    Here in this post, we have explained what a compiler is and how it works. So let us get started. When we say programming and programming languages, we start thinking of Python, Java, C++, C, JavaScript, Kotlin, and so on, and the most common thing about these languages is that they all are High-level programming languages.

    For each high-level programming language, we need a translator because the computer does not understand the high-language code written by the user. The translator converts the high-level language source file to byte code or object code. Most all programming languages use a compiler and interpreter translator to translate the source code.

    Here in this article, we have given a brief description of the Compiler translator.

    What is a Compiler?

    A compiler is a program that is usually known as a translator, and it is used to turn high-level language into machine language so the computer can execute the human-written code. These days developers only use high-level languages, which are not machine-friendly, and machines and computers do not have a direct approach to these kinds of languages. For that, we use a translator so the machine can understand how that particular code should be executed.

    How does a compiler work?

    A compiler reads source code line by line at once and show appropriate error after reading the code, it works in various phases, and each phase depends on the previous phase. A compiler includes 6 phases which are:

    • Lexical analysis
    • Syntax analysis
    • Semantic analysis
    • Intermediate code generator
    • Code Optimizer
    • Code generator

    Lexical analysis

    This phase of the compiler scans the code and divides the code into groups of tokens.

    Syntax analysis

    This phase of the compiler checks the syntax of the program and assures whether the code is written according to the syntax or not.

    Semantic Analysis

    This phase of the compiler checks the meaning of the program. It checks whether the program code follows a meaningful approach or not.

    Intermediate Code Generation

    This phase of the compiler commences with the generation of Intermediate code; this intermediate code is the middle language of high-level and machine language.

    Code Optimization

    This phase of the compiler deals with the unwanted code written in the program and removes them. Code optimization creates a sequence of intermediate code, so the execution of the code becomes easy.

    Code Generation

    This is the most important phase of the compiler, which accepts the sequence of code from the optimization phase and converts it into object code.


    The compiler just does not convert the high-level code to the object code but also checks its legitimacy. As the working of the compiler is divided into 6 phases, which state that it is a collection of subprograms. Programming languages like C++, Java, C, and Perl use the compiler as a translator.

    People are also reading:


    The three basic types of compilers include single-pass compilers, two-pass compilers, and multi-pass compilers.

    A compiler reads a computer program as a whole, while an interpreter reads a computer program line by line.

    A compiler is used to translate a computer program written in high-level languages into a low-level language that is understandable by computer systems. This is because computer systems can understand the language of only 1s and 0s.

    The different phases of a compiler are Lexical Analysis, Syntactic Analysis or Parsing, Semantic Analysis, Intermediate Code Generation, Code Optimization, and Code Generation.

    Leave a Comment on this Post