What is a Compiler? It’s phases and examples

A compiler is the software code that is used to compile the source code of any programming language i.e. C, C++ etc.

After reading this complete article you will understand phases of a compiler like Lexical Analysis Phase, Syntax Analysis, Semantic Analysis, Intermediate Code Generation, Code Optimizer and the difference between compiler vs interpreter.

What is compiler
What is compiler: credit – https://kukuruku.co

What is a compiler?

A compiler reads whole-program source code written by a programmer and compiles it. It also shows you logical, syntax errors if any.

If you are a programmer, many times you see few syntax or logical errors occurs in your programming code. These errors are shown by compiler of that programming language.

A compiler reads whole-program code in one time and compiles it. It is very fast as compared to an interpreter.

It is also responsible to read source code and converts it into output assembly code or executable code.

A compiler also converts human-readable source code into a machine language and vice versa for showing output or result of a program.

Compiler phases

  • Lexical Analysis
  • Syntax Analysis
  • Semantic Analysis
  • Intermediate Code Generation
  • Code Optimization
  • Code Generation
  • Symbol Table

Lexical Analysis

The lexical analysis phase is responsible for converting whole source code into a plain text. You can understand it as a scanner which is used to scan complete program code and store it as a text. This scanned text is the combination of characters and meaningful lexemes. Which is also known as tokens.

Syntax Analysis

This is the next phase of the compilation process. It is also known as parsing. This phase checks all tokens created by the lexical unit.
It reads complete scanned format as an input and generates a syntax tree also known as a parse tree.

Syntax analysis checks each and every token arrangement as per syntax rules.

Semantic Analysis

It moves your program data into the next level. Actually it checks the whole parse tree that is developed in the syntax analysis phase.

It takes syntax tree as an input and checks it against programming language rules. For example – assignment of values, identifiers and their types, expressions etc.

Finally, it gives an annotated syntax tree as an output.

Intermediate Code Generation

As the name suggests, it is the code between a high-level language and machine language. Actually, it makes the conversion process easy. So that high-level language easily converts into targeted machine code.

Code Optimization

Optimization means using resources in a better way. Similarly, code optimization refers to an arrangement of intermediate code in such a manner that all program executes with high-speed output wasting resources i.e. memory, CPU etc.

Code Generation

Code generation phase converts intermediate code that is optimized by above code optimization phase into a targeted machine language. Keep in mind, it converts intermediate code into a sequence of re-locatable machine code.

Symbol Table

A symbol table is used to store all the identifier’s name with their data types etc. Actually, it stores a data structure for each phase of a compiler. When any phase needs any data for checking or implementing, it directly retrieves from a symbol table.

Difference between a compiler and an interpreter?

1.Compiler read whole source code at a time and compilers it. An interpreter reads a single line of code at a time and shows you errors if any then reads next line of code.
2.A compiler is faster than an interpreter.The interpreter is slower than a compiler.
3.e.g. of compiler-based languages are C, C++, C# etc.e.g of interpreter based language is fox-pro, PHP, Python, Ruby etc.

You can also read


Hope that you understand the compiler, its phases and also the difference between a compiler and an interpreter. Please ask your questions in below comment box.

Don’t forget to share this post in your social family.

Leave a Comment

Show Buttons
Hide Buttons