Sunteți pe pagina 1din 29

What is language translator?

= Computers are digital devices. All types of commands, data and instructions required to be
converted into machine code which is the combination of 0s and 1s. So, we give instructions to
the computer in a language which is similar to English and easier for us to understand. The
computer translates these instructions into the machine language with the help o language
translators. Language translators are of three types.
(i) Assembler: An assembler is software that converts programs written in assembly language to
machine language.
(ii) Interpreter: An interpreter is software that converts programs written in high level language
to machine language code. It converts one instruction at a time.
(iii) Compiler: A compiler is software that converts programs written in high level language to
machine code in a single operation.

Difference between Compiler, Interpreter and Assembler


Lets get some of our concepts clear before to head on to programming in java.This
and perhaps next few posts will help you clear some computer programming
concepts.
Both compiler and interpreter convert human readable high level language like
Java,C++ etc into machine language but there is difference in the way both
function.So lets take a look to understand the differences.

Difference between Compiler and Interpreter

Compiler scans the entire program once and then converts it into machine
language which can then be executed by computer's processor.In short
compiler translates the entire program in one go and then executes it.
Interpreter on the other hand first converts high level language into an

intermediate code and then executes it line by line. This intermediate code is
executed by another program.

The execution of program is faster in compiler than interpreter as in


interpreter code is executed line by line.

Compiler generates error report after translation of entire code whereas in


case of interpreter once an error is encountered it is notified and no further
code is scanned.

Example Python is an interpreted language whereas C,C++ are compiled


languages.Java however uses both compiler and interpreter.We will look into
how this exactly works in case of java in next post.

Diagrammatic representation-

What is Assembler?
Assembler is used for converting the code of low level language (assembly
language) into machine level language.If you have worked on microprocessors like
8085 and 8086 the module which converts assembly language into machine
language is nothing but Assembler.

Java - Compiled or interpreted?


As our basics are now clear lets get on to Java. Well you must have heard that Java
is both compiled and interpreted language. When it comes to the question How Java works? It goes something like below -

When you run javac HelloWorld.java java compiler is invoked which


converts human readable code(Contents of .java file) to java byte
codes(intermediate form). This bytecodes are stored in a special file(called
Class file) with .class extension.

Finally when you run java HelloWorld java interpreter is invoked which
reads these bytecodes line by line, convert it into machine language and
execute it.

This is why Java is called as both compiled as well as interpreted language. But this
is not all. There is another concept called - Just-in-time compilation

JIT(Just-in-time compilation)
You can read the wiki page for complete details but here I am only going to provide
relevant points from Java perspective.

JIT as the name suggests does just in time or on the fly compilation of
java byte codes to native machine language that can be directly
executed by the processor.

JIT compiler runs only after the program(JVM) has started. so it has access to
dynamic runtime information whereas a standard compiler doesn't and can
make better optimizations like inlining functions that are used frequently.

But question may arise - Addition time is spent every time to compile the
code?
Yes it is true if we use pure JIT then additional time is spent compiling every time
code is run and hence a new technique was introduced - HotSpot Compiling. In this

JVM maintains a count of number of time a module/routine is executed. When


program/JVM first starts it goes through normal compilation to bytecodes
followed by interpretation.

But if the count of execution exceeds a limit the corresponding byte codes
are compiled to machine code by JIT and are directly executed thereafter.

The advantage is that there is no initial delay due to the compiling.

Above picture best represents the overview.

Java compiler (javac) converts Java code to byte code and then Java
interpreter (java) interprets bytecodes line by line, converts it into native
code and executes it.

However if a "hot spot" is encountered - typically a piece of code or method


that is getting executed very frequently then JIT compiler comes up. Compiles
the byte code to native code with optimizations (as JIT has runtime
information) and thereafter that piece of code is never interpreted - the
native code generated by JIT is directly executed.

Yes this will have an additional overhead in terms of time and memory
footprint as JIT performs optimizations and compilation into native code on
the fly (as your application is running) but subsequent calls to that part of
code are efficient and fast.

Assembler, Compiler, Interpreter, Linker, Loader

Assembler: A computer will not understand any program written in a language, other than its
machine language. The programs written in other languages must be translated into the machine
language. Such translation is performed with the help of software. A program which translates an
assembly language program into a machine language program is called an assembler. If an
assembler which runs on a computer and produces the machine codes for the same computer
then it is called self assembler or resident assembler. If an assembler that runs on a computer and
produces the machine codes for other computer then it is called Cross Assembler.
Assemblers are further divided into two types: One Pass Assembler and Two Pass
Assembler. One pass assembler is the assembler which assigns the memory addresses to the
variables and translates the source code into machine code in the first pass simultaneously. A
Two Pass Assembler is the assembler which reads the source code twice. In the first pass, it reads
all the variables and assigns them memory addresses. In the second pass, it reads the source code
and translates the code into object code.
Compiler: It is a program which translates a high level language program into a machine
language program. A compiler is more intelligent than an assembler. It checks all kinds of limits,
ranges, errors etc. But its program run time is more and occupies a larger part of the memory. It
has slow speed. Because a compiler goes through the entire program and then translates the
entire program into machine codes. If a compiler runs on a computer and produces the machine
codes for the same computer then it is known as a self compiler or resident compiler. On the
other hand, if a compiler runs on a computer and produces the machine codes for other computer
then it is known as a cross compiler.
Interpreter: An interpreter is a program which translates statements of a program into machine
code. It translates only one statement of the program at a time. It reads only one statement of
program, translates it and executes it. Then it reads the next statement of the program again
translates it and executes it. In this way it proceeds further till all the statements are translated
and executed. On the other hand, a compiler goes through the entire program and then translates
the entire program into machine codes. A compiler is 5 to 25 times faster than an interpreter.
By the compiler, the machine codes are saved permanently for future reference.
On the other hand, the machine codes produced by interpreter are not saved. An interpreter is a
small program as compared to compiler. It occupies less memory space, so it can be used in a
smaller system which has limited memory space.
Linker: In high level languages, some built in header files or libraries are stored. These libraries
are predefined and these contain basic functions which are essential for executing the program.
These functions are linked to the libraries by a program called Linker. If linker does not find a
library of a function then it informs to compiler and then compiler generates an error. The
compiler automatically invokes the linker as the last step in compiling a program.
Not built in libraries, it also links the user defined functions to the user defined
libraries. Usually a longer program is divided into smaller subprograms called modules. And
these modules must be combined to execute the program. The process of combining the modules
is done by the linker.

Related Links

Java theory and practice: Dynamic compilation and performance


measurement

What does a just-in-time (JIT) compiler do?(SO)

Static Keyword in Java

Mounting Solaris NFS Share on Linux(Ubuntu)

Difference between data encapsulation and data abstraction in java.

Building Java projects with Maven.

- See more at: http://opensourceforgeeks.blogspot.in/2013/03/difference-betweencompiler-interpreter.html#sthash.bYLQNezV.dpuf

Compiler Interpreter Assembler Debugger

What is an Compiler?

In general, compiler is a computer program that reads a program


written in one language, which is called the source language, and
translates it in to another language, which is called the target
language. Traditionally, source language is a high level language
such as C++ and target language is a low level language such as
Assembly language. However, there are compilers that can convert
a source program written in Assembly language and convert it to
machine code or object code. Assemblers are such tools. On the
other hand, Interpreters are tools that execute instructions
written in some programming language. Interpreter can either
directly execute high level source code or translate them to
intermediate code and then interpret it or execute precompiled
code.

What is an Assembler?
Assembler is software or a tool that translates Assembly language
to machine code. So, an assembler is a type of a compiler and the
source code is written in Assembly language. Assembly is a human
readable language but it typically has a one to one relationship
with the corresponding machine code. Therefore an assembler is
said to perform isomorphic (one to one mapping) translation.
Advanced assemblers provide additional features that support
program development and debugging processes. For example, the
type of assemblers called macro assemblers provides a macro
facility.

What is an Interpreter?
An interpreter is a computer program or a tool that executes
programming instructions. An interpreter may either execute the
source code directly or converts the source to an intermediate
code and execute it directly or execute precompiled code produced
by a compiler (some interpreter systems include a compiler for
this task). Languages like Perl, Python, MATLAB and Ruby are
examples of programming languages that use an intermediate code.
UCSD Pascal interprets a precompiled code. Languages like Java,
BASIC and Samltalk first compile the source to an intermediate
code called bytecode and then interpret it.

What is a Debugger?
Debugger is a computer program that is used to find bugs/errors
in other programs. Debugger allows executing a program and
inspecting each step in the program execution. It also allows

stopping the execution of the program at some point and changing


some variable values and then continuing the execution. All of
these capabilities are provided to help the programmer to make
sure that her program is behaving correctly and to help in
identifying bugs in the code. Most of the debuggers provide the
ability to execute a program step by step (also called single
stepping), pausing to examine the current state of the program by
providing a breakpoint and tracking variable values. Some
advanced debuggers allow the programmer to skip a location that
causes a crash or a logical error in the code and continue
execution from a different location. Some of the popular
debuggers are GNU Debugger (GDB), Microsoft Visual Studio
Debugger, etc.

Compiler V/s Interpreter V/s Assembler


Compiler
1.Compiler translates a high level language program into machine level language.
2.translates each high level language instruction into a set of machine level instructions
3.one to many correspondence.
4.Examples are C, COBOL, Java, etc. .

Interpreter
1.Compiler translates a high level language program into machine level language.
2.It takes one statement of a high level language program, translates it into machine language
instructions and immediately executes it.
3.a one to one relationship with the corresponding machine code
4.Languages like Java, BASIC and Samlltalk first compile the source to an intermediate code called
bytecode and then interpret it.

Assembler
1.Assembler translates a assembly language program into machine level language.
2.It takes one statement of a assembly language program, translates it into machine language
instructions and immediately executes it.
3.typically has a one to one relationship with the corresponding machine code

S-ar putea să vă placă și