Topic:   SOURCE TO SOURCE COMPILER
          Course Title: SYSTEM   SOFTWARE
                  Course Code: CAP   607

SUBMITTED TO:                          SUBMITTED BY:
Mr. SANJAY SOOD                        MINTOO
JAKHMOLA
                                       REG. NO.: 11009054
                                       ROLL NO.: B20
                                       SECTION: D1006
INTRODUCTION


A compiler, in the broadest sense, is just a tool that converts from a language (or a
way of expressing some knowledge) into another. Most people see compilers as
tools that have source code as input and produce binary files as output.

Source-to-source compilers have as input source code and produce source code as
output. It is assumed that the output can be used as well as the input of another
compiler (or even the same!).

It is also known as transcompiler (or transpiler) and it is also a type of compiler
that takes the source code of a programming language as its input and outputs the
source code in another programming language. For example, it may perform a
translation of a program from Pascal to C.




DEFINITION "transpiler":
A program which reads code in language A and converts it into code for language
B. The transpiler converts between languages at the same level. Examples: (C++
-> C, Pascal -> C).
DIFFERENCE BETWEEN COMPILER
     AND TRANSCOMPILER

A compiler translates source code to machine code that can be understood by the
CPU hardware. A transpiler translates source code to another form of source code
that can be understood by another compiler.

Or

The difference between a compiler and a transpiler is that the compiler usually
converts from a high to a low level language (C -> Assembler, Java -> Bytecode)
while the transpiler converts between languages at (roughly) the same level.
Examples: (C++ -> C, Pascal -> C).




     SOURCE                                                     MACHINE
                                 COMPILER
     CODE                                                          CODE



                  COMPILER WORKING
SOURCE                                                         SOURCE
                                TRANSPILER
      CODE                                                           CODE



                  TRANSPILER WORKING



Why A Source-To-Source Compiler Is
             Useful

One might wonder why a source-to-source compiler is useful if they are
generating again source code and not a binary ready to run like a "full" compiler
would.
Well, it turns that compilers are complex tools. Complex tools because they have
to deal with several processes including, but not limiting to: parsing the input,
checking the semantics, implement these semantics, finding out opportunities of
optimization under the semantics, generating binary code, etc.
So, that is why the source-to-source compiler is useful. A source-to-source
compiler performs a parsing phase but the generated AST must be much more
detailed than the one of a binary generating compiler. The reason is that the output
of the source-to-source compiler must be valid as an input for another compiler.



   SOURCE-TO-SOURCE COMPILER
            EXAMPLE

   1.   ROSE: It is an open source compiler infrastructure to build source-to-
        source program transformation and analysis tools for large-scale Fortran
        77/95/2003, C, C++, OpenMP, and UPC applications.

   2.   DMS Software Reengineering Toolkit: It is a source-to-source
        program transformation tool. It can be used for translating from one
        computer language to another, for compiling domain-specific languages to a
        general purpose language, or for carrying out optimizations or massive
        modifications within a specific language.

   3.   OpenMP: In this implementation is source-to-source, meaning that it
        converts the input program, in Fortran source, which contains OpenMP
        directives into an output program, also in Fortran source. The output
        program explicitly creates and synchronizes parallel threads to implement
        parallelism, as indicated by the OpenMP directives in the input program.

   4.   Cetus: It is a compiler infrastructure for the source-to-source
        transformation of software programs. It currently supports ANSI C. Since
        its creation in 2004, it has grown to over 80,000 lines of Java code, has been
        made available publicly on the web, and has become a basis for several
        research projects.

Source-to-Source Compiler

  • 1.
    Topic: SOURCE TO SOURCE COMPILER Course Title: SYSTEM SOFTWARE Course Code: CAP 607 SUBMITTED TO: SUBMITTED BY: Mr. SANJAY SOOD MINTOO JAKHMOLA REG. NO.: 11009054 ROLL NO.: B20 SECTION: D1006
  • 2.
    INTRODUCTION A compiler, inthe broadest sense, is just a tool that converts from a language (or a way of expressing some knowledge) into another. Most people see compilers as tools that have source code as input and produce binary files as output. Source-to-source compilers have as input source code and produce source code as output. It is assumed that the output can be used as well as the input of another compiler (or even the same!). It is also known as transcompiler (or transpiler) and it is also a type of compiler that takes the source code of a programming language as its input and outputs the source code in another programming language. For example, it may perform a translation of a program from Pascal to C. DEFINITION "transpiler": A program which reads code in language A and converts it into code for language B. The transpiler converts between languages at the same level. Examples: (C++ -> C, Pascal -> C).
  • 3.
    DIFFERENCE BETWEEN COMPILER AND TRANSCOMPILER A compiler translates source code to machine code that can be understood by the CPU hardware. A transpiler translates source code to another form of source code that can be understood by another compiler. Or The difference between a compiler and a transpiler is that the compiler usually converts from a high to a low level language (C -> Assembler, Java -> Bytecode) while the transpiler converts between languages at (roughly) the same level. Examples: (C++ -> C, Pascal -> C). SOURCE MACHINE COMPILER CODE CODE COMPILER WORKING
  • 4.
    SOURCE SOURCE TRANSPILER CODE CODE TRANSPILER WORKING Why A Source-To-Source Compiler Is Useful One might wonder why a source-to-source compiler is useful if they are generating again source code and not a binary ready to run like a "full" compiler would. Well, it turns that compilers are complex tools. Complex tools because they have to deal with several processes including, but not limiting to: parsing the input, checking the semantics, implement these semantics, finding out opportunities of optimization under the semantics, generating binary code, etc. So, that is why the source-to-source compiler is useful. A source-to-source compiler performs a parsing phase but the generated AST must be much more
  • 5.
    detailed than theone of a binary generating compiler. The reason is that the output of the source-to-source compiler must be valid as an input for another compiler. SOURCE-TO-SOURCE COMPILER EXAMPLE 1. ROSE: It is an open source compiler infrastructure to build source-to- source program transformation and analysis tools for large-scale Fortran 77/95/2003, C, C++, OpenMP, and UPC applications. 2. DMS Software Reengineering Toolkit: It is a source-to-source program transformation tool. It can be used for translating from one computer language to another, for compiling domain-specific languages to a general purpose language, or for carrying out optimizations or massive modifications within a specific language. 3. OpenMP: In this implementation is source-to-source, meaning that it converts the input program, in Fortran source, which contains OpenMP directives into an output program, also in Fortran source. The output program explicitly creates and synchronizes parallel threads to implement parallelism, as indicated by the OpenMP directives in the input program. 4. Cetus: It is a compiler infrastructure for the source-to-source transformation of software programs. It currently supports ANSI C. Since its creation in 2004, it has grown to over 80,000 lines of Java code, has been made available publicly on the web, and has become a basis for several research projects.