What is the Difference Between Phases and Passes of Compiler

The main difference between phases and passes of compiler is that phases are the steps in the compilation process while passes are the number of times the compiler traverses through the source code.

Programmers write computer programs in high-level languages. These languages follow a syntax similar to the English language. It is easier for programmers to understand this language, but the computer does not understand them. Therefore, it is necessary to convert the source code to machine understandable machine code. A compiler is a special software that supports this conversion. Phase and pass are two terms related to compilers.

Key Areas Covered

1. What are Phases of Compiler
     – Definition, Functionality
2. What are Passes of Compiler
     – Definition, Functionality
3. What is the Difference Between Phases and Passes of Compiler
     – Comparison of Key Differences

Key Terms

Compiler, Phases, Passes

Difference Between Phases and Passes of Compiler - Comparison Summary

What are the Phases of Compiler

There is a sequence of steps in the compilation process. Each phase takes input from the previous stage. The output from one phase goes to the next phase.

  • First, the source code goes through lexical analysis. It scans the source code as a stream of characters and converts them into meaningful lexemes. Then, it represents these lexemes in the form of tokens.
  • The second phase is the syntax analysis or parsing. It takes the generated tokens of the lexical analysis as input and creates a parse tree. It also checks the token arrangements with the source code grammar.
  • The third phase is the semantic analysis. It checks whether the parse tree follows the rules of the programming language.
  • The fourth phase is the intermediate code generator. And, this phase generates an intermediate code of the source code for the target machine. It is easier to convert this code into target machine code.
  • The fifth phase is the code optimization phase, which eliminates the irrelevant code lines and arranges the sequence of statements to speed up the program execution without resource (CPU, memory) wastage.
  • The final phase is the code generation. The code generator converts the intermediate code into a sequence of re-locatable machine code.

Difference Between Phases and Passes of CompilerFurthermore, there is a symbol tree that is maintained throughout all the phases. It contains all identifiers’ names with their types. Moreover, it helps to search and retrieve identifier records quickly.

What are the Passes of Compiler

A pass refers to the number of times the compiler goes through the source code. There are single-pass compilers and multi-pass compilers. Single-pass compiler goes through the program only once. In other words, the single pass compiler allows the source code to pass through each compilation unit only once. It immediately translates each code section into its final machine code.

Multi-pass compiler goes through the source code several times. In other words, it allows the source code to pass through each compilation unit several times. Each pass takes the result of the previous pass as input and creates intermediate outputs. Therefore, the code improves in each pass. The final code is generated after the final pass. Multi-pass compilers perform additional tasks such as intermediate code generation, machine dependent code optimization, and machine independent code optimization.

Difference Between Phases and Passes of Compiler

Definition

Phases refer to units or steps in the compilation process. Passes, in contrast, refer to the total number of times the compiler goes through the source code before converting it into the target machine code. Thus, this is the main difference between phases and passes of compiler.

Quantity/Categories

There are six main phases in the compilation process while there are two types of compilers as single pass and multi-pass compilers. Hence, this is another difference between phases and passes of compiler.

Conclusion

A compiler is a special software that supports this conversion. The main difference between phases and passes of compiler is that phases are the steps in the compilation process while passes are the number of times the compiler traverses through the source code.

Reference:

1. “Compiler Phases – Javatpoint.” Www.javatpoint.com, Available here.
2. “Compiler Passes – Javatpoint.” Www.javatpoint.com, Available here.

Image Courtesy:

1. “COMPILER STRUCTURE EXPLANATION – Page 1” By Sharmila Chandrakanth – Own work (CC BY-SA 4.0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply