Compiler Design Phases of a Compiler - Compiler Design

What are the different Phases of a Compiler?

Compiler process is a combination of various phases. The inputs are taken by each stage from the previous stage and the source program is represented and the output of that particular phase is fed as input to the next phase of the compiler. The phases of the compiler are depicted as follows:

Compiler Phases

Lexical Analysis

The first phase of the compiler is a text scanner. The source code is scanned as a stream of characters and is converted into meaningful lexemes. These lexemes are represented by the lexical analyzer as tokens.

Syntax Analysis

The second phase is Syntax Analysis or parsing. The tokens that are produced by the lexical analysis are taken as input by this phase and a parse tree or a syntax tree is generated by this phase. The token arrangements are checked against the source code grammar in the sense that the expressions that are made by the tokens are checked by parser for syntactically correct.

Semantic Analysis

Here the parse tree constructed is checked whether it follows the language rules. For instance, assignment of values is between compatible data types, and adding string to an integer. The identifiers, their types are expressions are tracked by the semantic analyzer and checked whether the identifiers are declared prior to using or not. An annotated syntax tree is generated as an output by the semantic analyzer.

Intermediate Code Generation

An intermediate code of the source code is generated by the compiler for the target machine. A program for some abstract machine is represented by this phase, which is between the high-level language and the machine language.

Code Optimization

The code optimization of the intermediate code is done by the next phase. It is assumed that optimization helps in removing the unnecessary code lines and a sequence of statements is arranged in the order to speed up the program execution without wasting the resources such a CPU, memory etc.

Code Generation

The optimized representation of the intermediate code is taken by the code generator and is mapped to the target machine language. The intermediate code is translated into sequential re-locatable machine language by the code generator. The tasks are performed by the instructions of the machine code.

Symbol Table

Throughout the phases of the compiler, the data-structure maintained is the symbol table. It stores the names of the identifiers along with their types. The identifier score is quickly searched and retrieved by enabling the search easy by the symbol table. Scope management also uses the symbol table.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Compiler Design Topics