The Wayback Machine - https://web.archive.org/web/20240913143310/https://www.geeksforgeeks.org/source-to-source-compiler/
Open In App

Source to Source Compiler

Last Updated : 01 Nov, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A compiler is a software program that transforms a program or code written in a high-level programming language into a low-level machine-readable language. When we write a program or code which can be in a high-level language, such as C, C++ or such as the one given below.

//Simple Java program
public class simple{ 
  public static void main(String[] args){ 
    System.out.printIn("Hello World")
    }
  }

A high-level language is closer to English, which makes it easier for us to write programs. But we can’t just run the same code onto a computer as machines can’t understand that. All that a computer can understand is binary code, that is ones and zeros. Therefore, the solution to the problem that computers can’t understand high-level languages would be to convert those into machine-understandable binary code. This is the exact task that is done by a compiler.  

The compiling process of the simple ‘hello world’ program up there would be as follows –

  1. We create the ‘hello world’ program and save the file as Hello.java.
  2. When we complete this program, the compiler generates the equivalent machine code and saves it into the class file of the same name but with class extension Hello.class.
  3. And when the program is executed, it is the class file that gets executed and gives the output.

Compiles are of many types, but in this article, we are going to learn about a specific one, that is Source to Source Compiler.

Source To Source Compiler :
A source to source compiler (S2S compiler) is also referred to by three other name, the first is the source to source translator, the second is transcompiler and the third one is transpiler. If we try to summarize the work on the source to source compiler is a sentence, it would be as follows:

A S2S Compiler is given as input the source code of a program to which it returns a source code with the overall same functionality in the same or different programming language. 

Unlike the general compiler whose work is to convert a high-level programming language to a machine language that is binary, the source to source compiler converts one source code from one programming language to another programming language which is at the same level of compilation from machine language. For example, while the traditional compiler may convert C to assembly or Java to bytecode, the source to source compiler may convert one scripting language to another such as Javascript to Python, or C++ to Java. 

Some examples of traditional compilers are listed below :

Name Source Language Target Language
GCC C++ Binary / Machine Language
Clang C++ Binary / Machine Language
JAVAC Java Byte code
GCJ Java Byte code
ECJ Java  Byte code

Some Examples of Source to Source compilers are listed below :

Name  Source Language Target Language
Cfront C++ C
HPHPc PHP C++
Babel ES^+(JS) ES5
ClojureScript Clojure JavaScript
JSSweet Java TypeScript
Swiftify Objective-C Swift
J2ObjC Java Objective-C
Haxe Haxe

ActionScript3, JavaScript, Java, C++,

C#, PHP, Python, Lua

Maia Maia Verilog
Cerberus X Cerberus JavaScript, Java, C++, C#

One of the main source application of source to source compilers is to convert the old code, that is legacy code, to the newer versions of a programming language or an API which helps in retaining the backward compatibility of the code. Tools come in very handy in updating large and old code bases which otherwise would take too much time to do it manually. An example of it could be converting the program of the older version of C++ to the current stable version that is C++20. Or it could also be the case of an API such as the old Dart program to be converted to the newer Dart 2.0 version. The structure of the newer could is dependent on the compiler. It could either be very similar to the original code to ease the development process from one language to the other or from the older version to the newer. Or the code structure can be completely changed so as to make it look original or different.

If we give a closer look at the table above, we can deduce that the initial use of some of the main programming languages, such as PHP or C++, was started as a source to source compilers, as their main use was to convert to the other mainstream programming languages. Today also, if we take a look around, we can identify languages such as Dart, Typescript, CoffeeScript, ECMAScript and others as being mainly used for the conversion to other languages.

Assembly Language Translator :
Assembly language translators are one of the most important examples of the source to source compiler. Below we have discussed in brief the four assembly language translators.

  • Intel CONV86 – 
    It is one of the first compilers which could translate assembly into the binary. Back in the late 70’s, it was made by Intel, which is famous for making processors. The intended purpose for this was to reliably run the program made for an 8-bit processor onto Intel’s 16-bit processor. As per the users, it being one of the first of its kind, it was not able to perform up to the expectations.
  • SCP TRANS86 – 
    Similar to the Intel CONV86, this translator was developed in 1980 by an engineer named Tim Paterson who is known as the creator of the famous 86-DOS. This translator was designed to translate assembly code from Intel 8080 and Zilog Z80 into .ASM code for Intels 16-bit processor Intel 8086. But similar to its previous counterpart, it was unable to perform the job effectively and required a lot of manual correction.
  • Sorcim TRANS86 –
    Sorcim is a start-up that also offered an assembly translator to the market in 1980. It was also invented to convert assembly to MS-DOS. This translator proved to be a better substitute for the previous two.
  • Digital Research XLT86 – 
    This translator appeared in the market in September of 1981 and was developed by Gary Kildall. Before this translator, no one other had the optimizing compiler approach which provided an effective performance. Like SCP TRANS86, its object was also to convert .ASM source code from Intel 8080 into the .A86 code for Intel 8086.

These four assembly code translators mark the beginning for the source to source compilers, which now had made many folds of progress.



Similar Reads

Incremental Compiler in Compiler Design
Incremental Compiler is a compiler that generates code for a statement, or group of statements, which is independent of the code generated for other statements. Examples : C/C++ GNU Compiler, Java eclipse platform, etc. The Incremental Compiler is such a compilation scheme in which only modified source text gets recompiled and merged with previousl
5 min read
Advantages of Multipass Compiler Over Single Pass Compiler
Programmers, write computer programs that make certain tasks easier for users. This program code is written in High-Level Programming languages like C, C++, etc. Computer device doesn't understand this language or the program written by a programmer, so the translator that translates the High-Level Program code into Machine Readable Instructions is
6 min read
Difference between Native compiler and Cross compiler
1. Native Compiler : Native compiler are compilers that generates code for the same Platform on which it runs. It converts high language into computer's native language. For example, Turbo C or GCC compiler. if a compiler runs on a Windows machine and produces executable code for Windows, then it is a native compiler. Native compilers are widely us
6 min read
Compiler Design - GATE CSE Previous Year Questions
Solving GATE Previous Year's Questions (PYQs) not only clears the concepts but also helps to gain flexibility, speed, accuracy, and understanding of the level of questions generally asked in the GATE exam, and that eventually helps you to gain good marks in the examination. Previous Year Questions help a candidate practice and revise for GATE, whic
4 min read
Compiler Theory | Set 2
The following questions have been asked in the GATE CS exam. 1. Given the following expression grammar: E -> E * F | F+E | F F -> F-F | id which of the following is true? (GATE CS 2000) (a) * has higher precedence than + (b) - has higher precedence than * (c) + and — have same precedence (d) + has higher precedence than * Answer(b) Precedence
3 min read
Syntax Directed Translation in Compiler Design
Parser uses a CFG(Context-free-Grammar) to validate the input string and produce output for the next phase of the compiler. Output could be either a parse tree or an abstract syntax tree. Now to interleave semantic analysis with the syntax analysis phase of the compiler, we use Syntax Directed Translation. Conceptually, with both syntax-directed de
5 min read
Peephole Optimization in Compiler Design
Peephole optimization is a type of code Optimization performed on a small part of the code. It is performed on a very small set of instructions in a segment of code. The small set of instructions or small part of code on which peephole optimization is performed is known as peephole or window. It basically works on the theory of replacement in which
2 min read
Understanding Typecasting as a compiler
Let's start our discussion with a very few lines of C - C/C++ Code #include <stdio.h> int main() { int x = 97; char ch = x; printf("The value of %d in character form is '%c'",x, ch); return 0; } Output : The value of 97 in character form is 'a' Let us understand that how the compiler is going to compile every instruction in this pro
5 min read
Compiler Design | Syntax Directed Definition
Prerequisite - Introduction to Syntax Analysis, Syntax Directed Translation Syntax Directed Definition (SDD) is a kind of abstract specification. It is generalization of context free grammar in which each grammar production X --> a is associated with it a set of production rules of the form s = f(b1, b2, ......bk) where s is the attribute obtain
4 min read
Error detection and Recovery in Compiler
In this phase of compilation, all possible errors made by the user are detected and reported to the user in form of error messages. This process of locating errors and reporting them to users is called the Error Handling process. Functions of an Error handler. DetectionReportingRecoveryClassification of Errors Compile-time errorsCompile-time errors
6 min read
Error Handling in Compiler Design
The tasks of the Error Handling process are to detect each error, report it to the user, and then make some recovery strategy and implement them to handle the error. During this whole process processing time of the program should not be slow. Functions of Error Handler: Error DetectionError ReportError RecoveryError handler=Error Detection+Error Re
7 min read
Shift Reduce Parser in Compiler
Prerequisite – Parsing | Set 2 (Bottom Up or Shift Reduce Parsers) Shift Reduce parser attempts for the construction of parse in a similar manner as done in bottom-up parsing i.e. the parse tree is constructed from leaves(bottom) to the root(up). A more general form of the shift-reduce parser is the LR parser. This parser requires some data structu
8 min read
Data flow analysis in Compiler
It is the analysis of flow of data in control flow graph, i.e., the analysis that determines the information regarding the definition and use of data in program. With the help of this analysis, optimization can be done. In general, its process in which values are computed using data flow analysis. The data flow property represents information that
5 min read
Various Data Structures Used in Compiler
A compiler is a program that converts HLL(High-Level Language) to LLL(Low-Level Language) like machine-level language. The compiler has various data structures that the compiler uses to perform its operations. These data structures are needed by the phases of the compiler. Now we are going to discuss the various data structures in the compiler. The
4 min read
Compiler Design | Detection of a Loop in Three Address Code
Prerequisite - Three address code in Compiler Loop optimization is the phase after the Intermediate Code Generation. The main intention of this phase is to reduce the number of lines in a program. In any program majority of the time is spent actually inside the loop for an iterative program. In the case of the recursive program a block will be ther
3 min read
Synthesis Phase in Compiler Design
Pre-requisites: Phases of a Compiler The synthesis phase, also known as the code generation or code optimization phase, is the final step of a compiler. It takes the intermediate code generated by the front end of the compiler and converts it into machine code or assembly code, which can be executed by a computer. The intermediate code can be in th
4 min read
Labeling Algorithm in Compiler Design
Labeling algorithm is used by compiler during code generation phase. Basically, this algorithm is used to find out how many registers will be required by a program to complete its execution. Labeling algorithm works in bottom-up fashion. We will start labeling firstly child nodes and then interior nodes. Rules of labeling algorithm are: Traverse th
3 min read
Transition diagram for Identifiers in Compiler Design
Transition diagram is a special kind of flowchart for language analysis. In transition diagram the boxes of flowchart are drawn as circle and called as states. States are connected by arrows called as edges. The label or weight on edge indicates the input character that can appear after that state. Transition diagram of identifier is given below: T
4 min read
Input Buffering in Compiler Design
The lexical analyzer scans the input from left to right one character at a time. It uses two pointers begin ptr(bp) and forward ptr(fp) to keep track of the pointer of the input scanned. Input buffering is an important concept in compiler design that refers to the way in which the compiler reads input from the source code. In many cases, the compil
5 min read
Bootstrapping in Compiler Design
Bootstrapping is a process in which simple language is used to translate more complicated program which in turn may handle for more complicated program. This complicated program can further handle even more complicated program and so on. Writing a compiler for any high level language is a complicated process. It takes lot of time to write a compile
4 min read
Loop Optimization in Compiler Design
Loop Optimization is the process of increasing execution speed and reducing the overheads associated with loops. It plays an important role in improving cache performance and making effective use of parallel processing capabilities. Most execution time of a scientific program is spent on loops. Loop Optimization is a machine independent optimizatio
4 min read
Types of Parsers in Compiler Design
The parser is that phase of the compiler which takes a token string as input and with the help of existing grammar, converts it into the corresponding Intermediate Representation(IR). The parser is also known as Syntax Analyzer. Types of Parser: The parser is mainly classified into two categories, i.e. Top-down Parser, and Bottom-up Parser. These a
2 min read
Semantic Analysis in Compiler Design
Semantic Analysis is the third phase of Compiler. Semantic Analysis makes sure that declarations and statements of program are semantically correct. It is a collection of procedures which is called by parser as and when required by grammar. Both syntax tree of previous phase and symbol table are used to check the consistency of the given code. Type
2 min read
Error Recovery Strategies in Compiler Design
The error may occur at various levels of compilation, so error handling is important for the correct execution of code. There are mainly five error recovery strategies, which are as follows: Panic modePhrase level recoveryError productionGlobal correctionSymbol tablePanic Mode:This strategy is used by most parsing methods. In this method of discove
4 min read
Difference between Cross-Assembler and Compiler
1. Cross-Assembler : A cross-assembler is an assembler that runs on a computer with one type of processor but generates machine code for a different type of processor. For example, if we use a PC with the 8086 compatible machine language to generate a machine code for the 8085 processor, we need a cross-assembler program that runs on the PC compati
3 min read
Working of Compiler Phases with Example
In this article, we are going to cover an overview that how we can each compiler phase works individually with the help of an example. Let's discuss one by one. Pre-requisite - Introduction to compiler phases You will see how compiler phases like lexical analyzer, Syntax analyzer, Semantic Analyzer, Intermediate code generator, code Optimizer, and
3 min read
Working of Lexical Analyzer in compiler
In this article, we are going to cover how the lexical analyzer works and will also cover the basic architecture of lexical analyzer. Let's discuss one by one. Pre-requisite - Introduction to Lexical Analyzer Lexical Analyzer : It is the first phase of a compiler is known as Scanner (It's scan the program).Lexical Analyzer will divide the program i
2 min read
BNF Notation in Compiler Design
BNF stands for Backus Naur Form notation. It is a formal method for describing the syntax of programming language which is understood as Backus Naur Formas introduced by John Bakus and Peter Naur in 1960. BNF and CFG (Context Free Grammar) were nearly identical. BNF may be a meta-language (a language that cannot describe another language) for prima
3 min read
Target Code Generation in Compiler Design
Target code generation is the final Phase of Compiler. Input : Optimized Intermediate Representation. Output : Target Code. Task Performed : Register allocation methods and optimization, assembly level code. Method : Three popular strategies for register allocation and optimization. Implementation : Algorithms. Target code generation deals with ass
2 min read
Register Allocation Algorithms in Compiler Design
Register allocation is an important method in the final phase of the compiler . Registers are faster to access than cache memory . Registers are available in small size up to few hundred Kb .Thus it is necessary to use minimum number of registers for variable allocation . There are three popular Register allocation algorithms . Naive Register Alloc
5 min read
Article Tags :