The Wayback Machine - https://web.archive.org/web/20241118184326/https://www.geeksforgeeks.org/working-of-compiler-phases-with-example/
Open In App

Working of Compiler Phases with Example

Last Updated : 24 Aug, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we are going to cover an overview that how we can each compiler phase works individually with the help of an example. Let’s discuss one by one.

Pre-requisite – Introduction to compiler phases

You will see how compiler phases like lexical analyzer, Syntax analyzer, Semantic Analyzer, Intermediate code generator, code Optimizer, and Target code generation. let’s consider an example.

x = a+b*50

The symbol table for the above example is given below. In symbol table are clearly mentions the variable name and variable types.

S.No. Variable name Variable type
1 x float
2 a float
3 b float

Now, here you will see how you can execute the compiler phase at each level and how it works.

  1. Lexical Analyzer :

     In this phase, you will see how you can tokenize the expression.

    x  ->  Identifier-  (id, 1)
    =  ->  Operator  -  Assignment
    a  ->  Identifier-  (id, 2)
    +  ->  Operator  -  Binary Addition
    b  ->  Identifier-  (id, 3)
    *  ->  Operator  -  Multiplication
    50 ->  Constant  -  Integer

    Now, the final tokenized expression is given below.

    (id, 1) = (id, 2) + (Id, 3)*50
  2. Syntax Analyzer :

    In this phase, you will see how you can check the syntax after tokenized the expression.

    S -> Id = E
    E -> E+T | T
    T -> T*F | F
    F -> Id | Integer constant

    SDT for the above expression is given below.

  3. Semantic Analyzer :
    In this phase, you will see how you can check the type and semantic action for the syntax tree. Given below is the diagram of the semantic analyzer.

  4. Intermediate Code Generator :
    In this phase as an input, you will give a modified parse tree and as output after converting into Intermediate code will generate 3 -Address Code. Given below is an expression of the above-modified parse tree.

    3 Address Code –

    t1 = b * 50.0
    t2 = a+t1
    x = t2
  5. Code Optimizer :
    In this phase, you will see as an input will give 3 address code and as an output, you will see optimize code. Let’s see how it will be converted.

    t1 = b* 50.0
    x = a+ t1
  6. Target Code Generator :
    It is the last phase and In this, you will see how you can convert the final expression into assembly code. so, that it will be easy to understand for the processor.

    Mul
    Add
    Store


Similar Reads

Grouping of Phases in Compiler Design
A compiler is software that translates a high-level language into machine-understandable form. Typically, a compiler is made up of six states and the input code in high-level language also known as Source code passes through each state one by one, each state processes the code, resulting in machine-understandable code or object code as an output. T
5 min read
Phases of a Compiler
Prerequisite - Introduction of Compiler design We basically have two phases of compilers, namely the Analysis phase and Synthesis phase. The analysis phase creates an intermediate representation from the given source code. The synthesis phase creates an equivalent target program from the intermediate representation. A compiler is a software program
7 min read
Incremental Compiler in Compiler Design
Incremental Compiler is a compiler that generates code for a statement, or group of statements, which is independent of the code generated for other statements. Examples : C/C++ GNU Compiler, Java eclipse platform, etc. The Incremental Compiler is such a compilation scheme in which only modified source text gets recompiled and merged with previousl
5 min read
Advantages of Multipass Compiler Over Single Pass Compiler
Programmers, write computer programs that make certain tasks easier for users. This program code is written in High-Level Programming languages like C, C++, etc. Computer device doesn't understand this language or the program written by a programmer, so the translator that translates the High-Level Program code into Machine Readable Instructions is
6 min read
Difference Between Native Compiler and Cross Compiler
Compilers are essential tools in software development, helping to convert high-level programming languages into machine-readable code. Among various types of compilers, native and cross-compilers are commonly used for different purposes. This article explains the difference between a native compiler and a cross compiler, shedding light on their fun
5 min read
Working of Lexical Analyzer in compiler
In this article, we are going to cover how the lexical analyzer works and will also cover the basic architecture of lexical analyzer. Let's discuss one by one. Pre-requisite - Introduction to Lexical Analyzer Lexical Analyzer : It is the first phase of a compiler is known as Scanner (It's scan the program).Lexical Analyzer will divide the program i
2 min read
Phases of a Assembler
The Assembler is a program that converts assembly language into machine language that can be executed by a computer. The Assembler operates in two main phases: Analysis Phase and Synthesis Phase. The Analysis Phase validates the syntax of the code, checks for errors, and creates a symbol table. The Synthesis Phase converts the assembly language ins
4 min read
Compiler Design - GATE CSE Previous Year Questions
Solving GATE Previous Year's Questions (PYQs) not only clears the concepts but also helps to gain flexibility, speed, accuracy, and understanding of the level of questions generally asked in the GATE exam, and that eventually helps you to gain good marks in the examination. Previous Year Questions help a candidate practice and revise for GATE, whic
4 min read
Compiler Theory | Set 2
The following questions have been asked in the GATE CS exam. 1. Given the following expression grammar: E -> E * F | F+E | F F -> F-F | id which of the following is true? (GATE CS 2000) (a) * has higher precedence than + (b) - has higher precedence than * (c) + and — have same precedence (d) + has higher precedence than * Answer(b) Precedence
3 min read
Syntax Directed Translation in Compiler Design
Parser uses a CFG(Context-free-Grammar) to validate the input string and produce output for the next phase of the compiler. Output could be either a parse tree or an abstract syntax tree. Now to interleave semantic analysis with the syntax analysis phase of the compiler, we use Syntax Directed Translation. Conceptually, with both syntax-directed de
5 min read
Understanding Typecasting as a compiler
Let's start our discussion with a very few lines of C - C/C++ Code #include <stdio.h> int main() { int x = 97; char ch = x; printf("The value of %d in character form is '%c'",x, ch); return 0; } Output : The value of 97 in character form is 'a' Let us understand that how the compiler is going to compile every instruction in this pro
5 min read
Error detection and Recovery in Compiler
In this phase of compilation, all possible errors made by the user are detected and reported to the user in form of error messages. This process of locating errors and reporting them to users is called the Error Handling process. Functions of an Error handler. DetectionReportingRecoveryClassification of Errors Compile-time errorsCompile-time errors
6 min read
Error Handling in Compiler Design
The tasks of the Error Handling process are to detect each error, report it to the user, and then make some recovery strategy and implement them to handle the error. During this whole process processing time of the program should not be slow. Functions of Error Handler: Error DetectionError ReportError RecoveryError handler=Error Detection+Error Re
7 min read
Shift Reduce Parser in Compiler
Prerequisite – Parsing | Set 2 (Bottom Up or Shift Reduce Parsers) Shift Reduce parser attempts for the construction of parse in a similar manner as done in bottom-up parsing i.e. the parse tree is constructed from leaves(bottom) to the root(up). A more general form of the shift-reduce parser is the LR parser. This parser requires some data structu
8 min read
Various Data Structures Used in Compiler
A compiler is a program that converts HLL(High-Level Language) to LLL(Low-Level Language) like machine-level language. The compiler has various data structures that the compiler uses to perform its operations. These data structures are needed by the phases of the compiler. Now we are going to discuss the various data structures in the compiler. The
4 min read
Compiler Design | Detection of a Loop in Three Address Code
Prerequisite - Three address code in Compiler Loop optimization is the phase after the Intermediate Code Generation. The main intention of this phase is to reduce the number of lines in a program. In any program majority of the time is spent actually inside the loop for an iterative program. In the case of the recursive program a block will be ther
3 min read
Synthesis Phase in Compiler Design
Pre-requisites: Phases of a Compiler The synthesis phase, also known as the code generation or code optimization phase, is the final step of a compiler. It takes the intermediate code generated by the front end of the compiler and converts it into machine code or assembly code, which can be executed by a computer. The intermediate code can be in th
4 min read
Labeling Algorithm in Compiler Design
Labeling algorithm is used by compiler during code generation phase. Basically, this algorithm is used to find out how many registers will be required by a program to complete its execution. Labeling algorithm works in bottom-up fashion. We will start labeling firstly child nodes and then interior nodes. Rules of labeling algorithm are: Traverse th
3 min read
Transition diagram for Identifiers in Compiler Design
Transition diagram is a special kind of flowchart for language analysis. In transition diagram the boxes of flowchart are drawn as circle and called as states. States are connected by arrows called as edges. The label or weight on edge indicates the input character that can appear after that state. Transition diagram of identifier is given below: T
4 min read
Input Buffering in Compiler Design
The lexical analyzer scans the input from left to right one character at a time. It uses two pointers begin ptr(bp) and forward ptr(fp) to keep track of the pointer of the input scanned. Input buffering is an important concept in compiler design that refers to the way in which the compiler reads input from the source code. In many cases, the compil
5 min read
Bootstrapping in Compiler Design
Bootstrapping is a process in which simple language is used to translate more complicated program which in turn may handle for more complicated program. This complicated program can further handle even more complicated program and so on. Writing a compiler for any high level language is a complicated process. It takes lot of time to write a compile
4 min read
Loop Optimization in Compiler Design
Loop Optimization is the process of increasing execution speed and reducing the overheads associated with loops. It plays an important role in improving cache performance and making effective use of parallel processing capabilities. Most execution time of a scientific program is spent on loops. Loop Optimization is a machine independent optimizatio
4 min read
Semantic Analysis in Compiler Design
Semantic Analysis is the third phase of Compiler. Semantic Analysis makes sure that declarations and statements of program are semantically correct. It is a collection of procedures which is called by parser as and when required by grammar. Both syntax tree of previous phase and symbol table are used to check the consistency of the given code. Type
2 min read
Error Recovery Strategies in Compiler Design
The error may occur at various levels of compilation, so error handling is important for the correct execution of code. There are mainly five error recovery strategies, which are as follows: Panic modePhrase level recoveryError productionGlobal correctionSymbol tablePanic Mode:This strategy is used by most parsing methods. In this method of discove
4 min read
Difference between Cross-Assembler and Compiler
1. Cross-Assembler : A cross-assembler is an assembler that runs on a computer with one type of processor but generates machine code for a different type of processor. For example, if we use a PC with the 8086 compatible machine language to generate a machine code for the 8085 processor, we need a cross-assembler program that runs on the PC compati
3 min read
BNF Notation in Compiler Design
BNF stands for Backus Naur Form notation. It is a formal method for describing the syntax of programming language which is understood as Backus Naur Formas introduced by John Bakus and Peter Naur in 1960. BNF and CFG (Context Free Grammar) were nearly identical. BNF may be a meta-language (a language that cannot describe another language) for prima
3 min read
Target Code Generation in Compiler Design
Target code generation is the final Phase of Compiler. Input : Optimized Intermediate Representation. Output : Target Code. Task Performed : Register allocation methods and optimization, assembly level code. Method : Three popular strategies for register allocation and optimization. Implementation : Algorithms. Target code generation deals with ass
2 min read
Register Allocation Algorithms in Compiler Design
Register allocation is an important method in the final phase of the compiler . Registers are faster to access than cache memory . Registers are available in small size up to few hundred Kb .Thus it is necessary to use minimum number of registers for variable allocation . There are three popular Register allocation algorithms . Naive Register Alloc
5 min read
Language Processing System in Compiler Design
The computer is an intelligent combination of software and hardware. Hardware is simply a piece of mechanical equipment and its functions are being compiled by the relevant software. The hardware considers instructions as electronic charge, which is equivalent to the binary language in software programming. The binary language has only 0s and 1s. T
4 min read
Source to Source Compiler
A compiler is a software program that transforms a program or code written in a high-level programming language into a low-level machine-readable language. When we write a program or code which can be in a high-level language, such as C, C++ or such as the one given below. //Simple Java program public class simple{ public static void main(String[]
6 min read
Article Tags :
three90RightbarBannerImg