Introduction to Compiler

Introduction of Language Processing System 

               Fig :- Language Processing System 

In addition to a compiler many other programs are required to create a target program. Above fig. shows the typical compilation. The preprocessor expands macros into source language statements.  The output of compilation process i.e. target code may requires further processing before it can be run. Fig. shows that the compiler create an assembly code which is translated into machine code by assembler. The linker is used to link some library routines and then the program actually runs. A loader loads all of them into memory and then the program is executed.  

Preprocessor:  A preprocessor produce input to compilers. They may perform the following functions.

 1. Macro processing: A preprocessor may allow a user to define macros that are short hands for longer constructs.

2. File inclusion: A preprocessor may include header files into the program text.

3. Rational preprocessor: these preprocessors augment older languages with more modern flow-of -control and data structuring facilities.

4. Language Extensions: These preprocessor attempts to add capabilities to the language by certain amounts to build-in macro.

 Definition of Compiler

Compiler is a translator program that translates a program written in (HLL) the source program and translate it into an equivalent program in (MLL) the target program. As an important part of a compiler is error showing to the programmer. 

Fig : Structure of Compiler

Interpreter

Interpreter is a translator which is used to convert programs in high-level language to low-level language. Interpreter translates line by line and reports the error once it encountered during the translation process.

It directly executes the operations specified in the source program when the input is given by the user.

It gives better error diagnostics than a compiler.


Languages such as BASIC, SNOBOL, LISP can be translated using interpreters. JAVA also uses interpreter. The process of interpretation can be carried out in following phases.

1. Lexical analysis

2. Syntax analysis

             3. Semantic analysis 

4. Direct Execution

Advantages:

  • Modification of user program can be easily made and implemented as execution proceeds.
  • Type of object that denotes a various may change dynamically.
  • Debugging a program and finding errors is simplified task for a program used for interpretation.
  • The interpreter for the language makes it machine independent.

Disadvantages:

  • The execution of the program is slower.
  • Memory consumption is more.

Assembler

An assembler translates assembly language programs into machine code. The output of an assembler is called an object file, which contains a combination of machine instructions as well as the data required to place these instructions in memory.

Linker

Linker is a computer program that links and merges various object files together in order to make an executable file. All these files might have been compiled by separate assemblers. The major task of a linker is to search and locate referenced module/routines in a program and to determine the memory location where these codes will be loaded, making the program instruction to have absolute references. 

Loader:

Loader is a part of operating system and is responsible for loading executable files into memory and execute them. It calculates the size of a program (instructions and data) and creates memory space for it. It initializes various registers to initiate execution.

Structure of the Compiler Design

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. The phases of a compiler are shown in below

There are two phases of compilation.

a. Analysis (Machine Independent/Language Dependent)

b. Synthesis (Machine Dependent/Language independent)

Compilation process is partitioned into no-of-sub processes called ‘phases’.

Analysis part breaks the source program into constituent pieces and imposes a grammatical structure on them which further uses this structure to create an intermediate representation of the source program.

  • It is also termed as front end of compiler.
  • Information about the source program is collected and stored in a data structure called symbol table.

The analysis phase consists of three phases i.e. Lexical Analysis, Syntax Analysis and Semantic Analysis.



Synthesis part takes the intermediate representation as input and transforms it to the target program.

• It is also termed as back end of compiler.

The design of compiler can be decomposed into several phases, each of which converts one form of source program into another.

The synthesis phase consists of three phases i.e. Intermediate Code Generation, Code Optimization and Code Generation.

Pass: A pass refers to the traversal of a compiler through the entire program.

Phase: Compilation process is partitioned into no-of-sub processes called ‘phases’.