Phases of Compiler




Phases of a Compiler

  • A compiler operates in phases. A phase is a logically interrelated operation that takes source program in one representation and produces output in another representation.
  • There are two phases of compilation.
    • Analysis (Machine Independent/Language Dependent)
    • Synthesis (Machine Dependent/Language independent)
Two phases of Compilation

Two phases of Compilation

Phases of compiler

Phases of Compiler

Lexical Analysis

  • Lexical analysis is the first phase of a compiler. It takes the modified source code from language preprocessors that are written in the form of sentences. The lexical analyzer breaks these syntaxes into a series of tokens, by removing any whitespace or comments in the source code.
Lexical Analysis

Lexical Analysis

Syntax Analysis

  • The second stage of translation is called syntax analysis or parsing. In this phase expressions, statements, declarations are identified by using the results of lexical analysis.
  • A Syntax analyzer creates the syntactic structure of the given program.
Syntax Analysis

Syntax Analysis

Semantic Analyzer

  • A Semantic analyzer checks the source program for semantic errors and collects the type information for the code generation.
  • Type checking is an important part of semantic analyzer.
Semantic Analyzer

Semantic Analyzer

Intermediate Code Generation

  • Intermediate code generation is the process by which a compiler's code generator converts some intermediate representation of source code into a form (e.g., machine code) that can be readily executed by a machine.
  • The input to the code generator typically consists of a parse tree or an abstract syntax tree.
Intermediate Code Generation

Intermediate Code Generation

Code Optimization

  • Code optimization is any method of code modification to improve code quality and efficiency. A program may be optimized so that it becomes a smaller size, consumes less memory, executes more rapidly, or performs fewer input/output operations.
code-optimization

Code Optimization

Code Generator

  • The last phase of translation is code generation. A number of optimizations to Reduce the length of machine language program are carried out during this phase.
  • The output of the code generator is the machine language program of the specified computer.
Code Generator

Code Generator

Symbol Table Management

  • The Symbol table management, or bookkeeping, keeps track of the names used by the source program and records the essential information.
  • An essential function of a compiler is to record the variable names used in the source program and collect information about various attributes of each name.
  • These attributes may provide information about the storage allocated for a name, its type, its scope.
  • In the case of procedure names, such things as the number and types of its arguments, the method of passing each argument (for example, by value or by reference), and the type returned are maintained in symbol table.
  • The symbol table is a data structure containing a record for each variable name, with fields for the attributes of the name. The data structure should be designed to allow the compiler to find the record for each name quickly and to store or retrieve data from that record quickly.
Symbol table management

Symbol table management

Error Handler

  • One of the most important functions of a compiler is the detection and reporting of errors in the source program. The error message should allow the programmer to determine exactly where the errors have occurred. Errors may occur in all or the phases of a compiler.
Error handler

Error handler

Grouping of Phases into Passes

  • In an implementation, activities from several phases may be grouped together into a pass that reads an input file and writes an output file.
  • The front-end phases of lexical analysis, syntax analysis, semantic analysis, and intermediate code generation might be grouped together into one pass.
  • Code optimization might be an optional pass.
  • A back-end pass consisting of code generation for a particular target machine.
Grouping of Phases into Passes

Grouping of Phases into Passes

  • 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

Compiler Construction Tools

There are some specialized tools to implement various phases of a compiler.

Compiler construction tools

Compiler construction tools

Scanner generators

  • Scanner generators that produce lexical analyzers from a regular-expression description of the tokens of a language. Unix has a tool for Scanner generator called LEX.

Parser generators

  • It automatically produce syntax analyzers (parse tree) from a grammatical description of a programming language. Unix has a tool called YACC which is a parser generator.

Syntax-directed translation engines

  • Syntax-directed translation engines that produce collections of routines for walking a parse tree and generating intermediate code.

Data-flow analysis engines

  • Data-flow analysis engines that facilitate the gathering of information about how values are transmitted from one part of a program to each other part. Data-flow analysis is a key part of code optimization.

Code-generator

  • Code-generator that produce a code generator from a collection of rules for translating each operation of the intermediate language into the machine language for a target machine.

Compiler-construction toolkits

  • It provide an integrated set of routines for constructing various phases of a compiler.


Related Searches to Phases of Compiler