Compiler Design

What are phases of compiler? Explain in detail

A compiler operates in phases, each of which transforms the source program from one representation to another.

1. Lexical Analysis:

In a compiler, lexical analysis is known as linear analysis or scanning. In lexical analysis, the stream of characters making up the source program is read from left to right and grouped into tokens that are sequences of characters having collective meaning.

For example, the assignment statement position = initial + rate * 60 will be grouped into the following tokens.

(i) identifier – { position, initial, rate}

(ii) operator – { =, +, * }

(iii) digits – { 60 }

2. Syntax Analysis:

Syntax analysis is known as hierarchical analysis or parsing. It involves grouping the tokens of the source program into grammatical phrases that are used by the compiler to synthesize the output.

3. Semantic Analysis:

The semantic analysis phase checks the source program for semantic errors and gathers type information for the subsequent code generation phase. It uses the hierarchical structure determined by the syntax analysis phase to identify the operators and operands of the expression and expression. An important part of the semantic analyzer is type checking.

4. Symbol Table Management:

  • Symbol Table is a data structure containing a record for each identifier, with fields for the attributes of the identifier. The data structure allows us to find each identifier for the record quickly, and to store or retrieve data from that record quickly.
  • An important function of the compiler is to record the identifiers used in the source program and collect information about the various attributes of each identifier.
  • When an identifier in the source program is detected by the lexical analyzer (which partitions the input text, delivering a sequence of comments and basic symbols), the identifier is entered into the Symbol Table.
  • The remaining phases of the compiler enter information about identifier into the Symbol Table and then use this information in various ways. In the semantic analysis and intermediate code generation phase, we need to know what type of identifiers are there in the program. The code generation phase enters and uses detailed information about the storage assigned to the identifiers.

5. Error Detection and Reporting:

Errors can be encountered in any phase. Once an error is detected, a phase must know how to deal with it so that the compilation process can proceed smoothly. A compiler that stops at the first encountered error is not useful.

Usually, the syntax and semantic analysis phases handle a large fraction of errors that can be detected by a compiler. Let us see how the different analysis phases handle errors.

Syntax Analysis Phase: This phase determines the errors where the token stream violates the structure rules of the language. Semantic Analysis Phase: In this phase, the compiler tries to detect those constructs which have the right syntactic structure, but have no meaning to the operation involved. Lexical Analysis Phase: It can detect errors where the characters remaining in the input do not form any token of the language.

6. Intermediate Code Generation:

After syntax and semantic analysis, compilers generate an intermediate representation of the source program. This intermediate representation should have two important properties:

(i) It should be easy to produce.

(ii) It should be easy to translate into the target program.

7. Code Optimization:

The code optimization phase attempts to improve the intermediate code, so that the running time of the machine code can be improved.

8. Code Generation:

The code generation phase of the compiler is the generation of the target code, consisting of relocatable machine code or assembly code. Memory locations are selected for each of the variables used by the program. Then intermediate instructions are translated into a sequence of machine instructions that perform the same task.

THE GROUPING OF PHASES

  • The discussion of phases deals with the logical organization of a compiler. In an implementation, activities from several phases may be grouped together into a pass that reads an input file and writes an output file.
  • For example, the front-end phases of lexical analysis, syntax analysis, semantic analysis, and intermediate code generation might be grouped together into one pass.
  • Code optimization may be a not obligatory pass. Then there could be a back-end pass consisting of code generation for a particular target machine. Some compiler collections have been created around carefully designed intermediate representations that allow the front end for a particular language to interface with the back end for a certain target machine.
  • With these collections, we can produce compilers for different source languages for one target machine by combining different front ends with the back end for that target machine.
  • In the same way, we can produce compilers for different target machines, by combining a front end with back ends for different target

Leave a Reply

Your email address will not be published. Required fields are marked *