program-translator-tools-assemblers-compilers-interpreters

Program translator tools – Assembler, Compiler and Interpreter

In my previous posts about Programming Language Generations and Low-level vs High-level languages, I mentioned the term Program translator tools. If you haven’t read them yet, I would suggest you to read for better context.

In this post, I’m going to discuss more about the program translator tools. More about what each translator tool, the Assembler, the Compiler and the Interpreter does.

Today, we use high-level languages such as Java, C, C++, JavaScript in our programs. As mentioned in the other posts, processor talk in machine language. To execute high-level languages, they should be converted into low-level languages. And a translator tool is used for this purpose to translate a high-level language into a low-level language.

program-translator-tool

As programming languages evolved by abstraction, the introduction of second generation programming languages lead to development of the assembler. Similarly, the third generation programming languages lead to development of compilers and interpreters.

The Assembler

The Assembler converts the assembly language into machine language.

Abstraction over the machine language lead to the creation of assembly language in the second generation. This abstraction lead to a need for a translator tool to convert the assembly languages into machine language. This led the path to developing the Assembler.

The assemblers can compile the assembly language like a compiler as well as interpret interactively like an interpreter.

The Compiler

In our day-to-day life as a programmer, we compile a program after completing it.

What is this compiling mean?

It is the translation process of converting our program in high-level language into a low-level language and it is done by the compiler.

So, what does the compiler do?

The compiler convert high-level language code into another language in a single session.

Ok wait, there are two key points to note here.

  1. It compiles in a single session.
  2. Converting high-level language into another language.

Depending on the size of the source code, the speed of the compiler might vary. Since it has to scan the entire source code, the larger the code base, the slower the translation.

Compiles in a single session

What do I mean by a single session here?

When compiling a program, the compiler reads the entire source code in high-level language, as a whole in one go and translates it into the required type language. It is an all or none approach where the compiler would either compiles the entire code or abort compilation when facing error.

Converting high-level language into another language

What does it mean? Being a translator tool, shouldn’t it convert a high level language into machine language?

Well, not necessarily. Based on the compiler’s type, it can convert a high-level language into another high-level language or an intermediate low-level language or directly to machine language.

C++ compilers compile the program directly to machine code.

Let’s see some types of compilers.

Bytecode Compilers

A bytecode is an intermediate code between the high-level language and the machine level language. Compilers used for languages, like Java, generate bytecodes which are later interpreted in a virtual machine, like JVM. The bytecodes are like machine codes to the virtual machine. This adds portability and platform independence to the program.

Source-to-source Compilers

A source-to-source compiler compiles a high-level language into another high-level language. For example, a source-to-source compiler can translate a program written in python into a program written in JavaScript.

Just In Time (JIT) Compilers

The JIT compilers generally runs inside an interpreter. Most of the modern time languages such as JavaScript, Java, Python have JIT compilers. Basically, a JIT compiler defers the compilation till the run time, i.e., it doesn’t compile the code till during the runtime. During interpretation of statements, when the interpreter recognizes a “hot” code, it uses the JIT compiler to compile the “hot” code into machine code to increase the performance. A “hot” code is a code that is executed frequently.

De-compiler

De-compiler translates a low-level language back to high-level language.

Platform oriented compilers

Native compilers

A native compiler is intended to generate code that runs only on computers that are of the same type of the computer where the compiler runs. It generates platform-dependent code.

Cross compilers

Cross compilers generate code that can run on different types of systems. For example, a program compiled in a Windows machine will also work in a Linux system. It generates platform-independent code.

Compiling to assembly language

A compiler can also translate a high-level language into assembly language by passing options. An external assembler translates the generated assembly language into machine language later. This increases performance.

The Interpreter

Just like the compiler, interpreters also compile the code. But unlike compilers, they do not compile the entire code in a single session. An interpreter reads each statement and executes it immediately.

Come to think of it, our processor is an interpreter by itself isn’t it 🤔? It fetches machine instructions one by one from the memory and executes it immediately.

They are more helpful in debugging where the programmer can set breakpoints and debug a program line by line.

Let’s see some types of interpreters.

Bytecode interpreters

We saw how bytecode compilers work in the previous section. Let’s discuss the interpreter part of it. The bytecode compilers generate bytecodes which are like machine codes to the virtual machine. This virtual machine includes the bytecode interpreters which translates each statement of the bytecode into machine code and execute it immediately.

Abstract Syntax Tree interpreters

The Abstract Syntax Trees interpreters, transform the source code into Abstract Syntax Tree and traverse through the tree to execute statements.

Few more

There are other types of interpreters such as Threaded code interpreters and Self-interpreters that I did not explain here.

What’s next?

Next, I’ll post and example for program translator tools. I’ll explain how both compilers and interpreters work together in Java. Stay tuned 😁


I hope this post was helpful 😊. If you find this post informative, support us by sharing this with fellow programmers in your circle 😀.

For any suggestions, improvements, reviews or if you like us to cover a specific topic, please leave a comment.
Follow us on twitter @thegeeksclan and in Facebook.
#TheGeeksClan #DevCommunity

error

Enjoy this blog? Please spread the word :)