What is Compiler Passes?

The whole source program can be processed several times before generating the assembly/machine code.

Pass − One complete scan or processing of the source program. Various phases can be arranged into one pass. Lexical, syntax & semantic analysis are often grouped in a single pass. Each pass reads the source program and writes output into an intermediate file, which then can be read by subsequent passes, i.e., the output of one pass will be input to the next pass.

Single-Pass Compiler − In a single-pass compiler, when a line source is processed it is scanned and the tokens are extracted. Then the syntax of the line is analyzed and the tree structure and some tables including data about each token are constructed.

Finally, after the semantical part is checked for correctness, the code is produced. The equivalent process is repeated for each line of the program until the whole program is compiled. Usually, the entire compiler is built around the parser, which will call procedures that will perform different functions.

A single-pass compiler is fast since all the compiler code is loaded in the memory at once. It can process the source text without the overhead of the operating system having to shut down one process and start another. A single-pass tends to impose some restrictions upon the program constants, types, variables, and procedures that must be defined before they are used.

The component of a single-pass compiler is inter-related, much closer than the components of a multi-pass compiler. This requires all the programmers working on the project to know about the entire project.

Multi-Pass Compiler − The compiler scans the input source once and makes the first modified form, then scans the first-produced form and produces a second modified form, etc., until the object form is produced. Such a compiler is known as a multi-pass compiler.

In the case of the multi-pass compiler, each function of the compiler can be performed by one pass of the compiler. For instance, the first pass can read the input source, scan and extract the tokens and save the result in an output file.

The second pass can read the file which was produced in the first pass, do the syntactical analysis by building a syntactical tree and associate all the information relating to each node of the tree. The output of the second pass then is a file containing the syntactical tree.

The third pass can read the output file created by the second pass and perform the optimization by restructuring the tree structure.