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

Posted in

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

Vinay Khatri
Last updated on July 18, 2022

    Here in this post, we have explained What is compiler 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 Compiler?

    A compiler is a program that is usually known as a translator and it is used to turn the 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 could understand how that particular code should be executed.

    How does a compiler work?

    A compiler read source code line by line, at once and show appropriate error after reading the code, it works on 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 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 deal with the unwanted code written in the program and remove 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.

    Conclusion

    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:

    Leave a Comment on this Post

    0 Comments