Difference Between Compiler And Interpreter
How does an interpreter/compiler work? What is the difference between interpreter and compiler.
Interpreter Vs Compiler: Difference Between Interpreter and Compiler. A program written in high-level language is called a source code. We need to convert the source code into machine code and this is accomplished by compilers and interpreters. Hence, a compiler or an interpreter is a program that converts program written in high-level language into machine code understood by the computer.
developer- Mar 20, 2017 The interpreter takes the single statement or the single line and it translates it. It works step by step for the analyzing of the source code. The interpreter has made the debugging or the removal of the errors very easy and handy as well. It tells about the errors after analyzing each statement. Difference Between Compiler and Interpreter.
- In my early days, when I had asked, how should I learn computer science and do something innovative? I always had come across, start learning computer.
8 Answers
Compilers
Compilers were the first sort oftranslator program to be written. The idea is simple: You write the program, then hand it to the compiler which translates it. Then you run theresult.
Interpreters
An interpreter is also a program that translates a high-level language into a low-level one, but it does it at the moment the program is run. You write the program using a text editor or something similar, and then instruct the interpreter to run the program. Ittakes the program, one line at a time,and translates each line beforerunning it: It translates the firstline and runs it, then translates thesecond line and runs it etc.
Compiler characteristics:
- spends a lot of time analyzing andprocessing the program
- the resulting executable is some formof machine- specific binary code
- the computer hardware interprets(executes) the resulting code
- program execution is fast
Interpreter characteristics:
- relatively little time is spentanalyzing and processing the program
- the resulting code is some sort ofintermediate code
- the resulting code is interpreted byanother program
- program execution is relatively slow
An S -> T translator accepts code expressed in source language S, and translates it to equivalent code expressed in another (target) language T.
Examples of translators:
- Compilers - translates high level code to low level code, e.g. Java -> JVM
- Assemblers - translates assembly language code to machine code, e.g. x86as -> x86
- High-level translators - translates code from one PL to another, e.g. Java -> C
- Decompilers - translates low-level code to high-level code, e.g. Java JVM bytecode -> Java
An S interpreter accepts code expressed in language S, and immediately executes that code. It works by fetching, analysing, and executing one instruction at a time.
Great when user is entering instructions interactively (think Python) and would like to get the output before putting in the next instruction. Also useful when the program is to be executed only once or requires to be portable.
- Interpreting a program is much slower than executing native machine code
- Interpreting a high-level language is ~100 times slower
- Interpreting an intermediate-level (such as JVM bytecode) language is ~10 slower
- If an instruction is called repeatedly, it will be analysed repeatedly - time-consuming!
- No need to compile code
Behaviour
A compiler translates source code to machine code, but does not execute the source or object code.
An interpreter executes source code one instruction at a time, but does not translate the source code.
Performance
- A compiler takes quite a long time to translate the source program to native machine code, but subsequent execution is fast
- An interpreter starts executing the source program immediately, but execution is slow
Interpretive compilers
An interpretive compiler is a good compromise between compilers and interpreters. It translates source program into virtual machine code, which is then interpreted.
An interpretive compiler combines fast translation with moderately fast execution, provided that:
- VM code is lower than the source language, but higher than native machine code
- VM instructions have simple formats (can be quickly analysed by an interpreter)
Example: JDK provides an interpretive compiler for Java.
user207421Compiler, transforms source code in one computer language to another one.
Interpreter, executes source code directly (usually inside its own virtual machine).
(source: answers.com)
Generally interpreter is performance costly.
GlorfindelWhat is the difference between interpreter and compiler?
The Compiler scans whole program first and then translates it into machine code which will be executed by the computer processor.
The Interpreters translates one statement into machine language, executes it, and proceeds to next statement.
For example: Compiler will give almost all the error at once but interpreter will run till the instruction you have written wrong
How does an interpreter/compiler work?
- Unlike compiled languages which are translated into machine language ahead of time (right).
- Interpreted languages are translated at runtime.
- dBASE and BASIC interpreters (middle) translate the original source code.
- Java and Visual Basic (left) interpreters translate bytecode, which is an intermediate language compiled from the original source code.
The Difference vs How they Work
Q:What are Compilers and interpreters used for ?
A: Most programs are written in High-level languages (c#,java...). High-level languages are which contain understandable words and phrases. On the other hand computer (by the time I wrote this article that is) understand machine code which is 0's and 1's only aka Binary/Machine Code. Therefore we need to convert the High-level code into source code which is (Machine code/binary). Hence the word converting.
So we conclude a Compiler/interpreter job is to Translate high-level code into machine code.
But both have a different way of 'Translating' the code
Difference:
Compiler:
converts source code to some kind of intermediate form. For static language, a compiler usually converts the source code to assembly, which usually did not get stored to disk, then the assembler is invoked to convert the assembly to binary code, which is usually stored as object file(.o or .obj suffix usually), then linker is invoked to link object file(s) to binary executable. Also it is common to refer to this whole process of compiling, assembling, linking as compiling. So you may call gcc a compiler, but it actually invokes cc1 which is the compiler to compile, as which is the assembler to assemble, ld which is the linker to link.
Interpreter:
language which has a intermediate so called bytecode form, the source code is first converted to byte code, this process can be called compiling, javac serves as an example. Bytecode cannot be run on host machines, it needs a program, which is actually the process from the viewpoint of OS, to interpret the bytecode to the host machine, this program is called a interpreter, think of java. Some language, like python, do the compiling and interpreting work with a single
— both from: https://www.quora.com/What-are-the-differences-between-a-compiler-an-interpreter-and-an-assembler-Provide-examples
Comparing
Interpreter
- It takes less amount of time to analyze the source code but the overall execution time is slower.
- No intermediate object code is generated, hence are memory efficient.
- Continues translating the program until the first error is met, in which case it stops. Hence debugging is easy.
Compiler
- It takes large amount of time to analyze the source code but the overall execution time is comparatively faster.
- Generates intermediate object code which further requires linking, hence requires more memory.
- It generates the error message only after scanning the whole program. Hence debugging is comparatively hard.
— from: https://www.programiz.com/article/difference-compiler-interpreter
Examples with Languages
Interpreted
- Python
- Ruby
- PHP
- JAVA(Almighty)
- Perl
- R
- Powershell
compiled
- C
- C++
- C#
- Objective-C
- SWIFT
- Fortran
Compilers - A compiler translates a source language to a target language. Then the target language takes input and gives output.
Interpreters - Instead of generating a target code, Interpreter appears to take input directly along with source code and gives output.
Then machine-language targeted program produced by the compiler is much faster than the interpreter at mapping inputs to output. However, since interpreter executes source program line by line it gives better error diagnostics than compiler.
Reference - Compilers: Principles, Techniques, and Tools by Aho aka the dragon book
Difference Between Compiler And Interpreter Chart
What is the difference between interpreter and compiler.
To put it directly and intuitively:
Compiler is what translates the human-readable source code into machine code which the computer could read and execute.
Interpreter is what pretends to be or simulates a computer to read and execute the source code directly.
Look at PLAI book, it's the best intro to dymanic language realization I found ever:
Programming Languages: Application and Interpretation(c) Shriram Krishnamurthi
- [home] https://cs.brown.edu/~sk/Publications/Books/ProgLangs/2007-04-26/
- [html] http://cs.brown.edu/courses/cs173/2012/book/
- [pdf] http://www.cs.brown.edu/courses/cs173/2012/book/book.pdf
This book centers on writing interpreter for dynamic language in Scheme (dr.Racket), using it you can write your own interpreter for any language, and add some tips on OOP from
- [OOP PLAI] https://users.dcc.uchile.cl/~etanter/ooplai/ (c) Éric Tanter
and SmallTalk and SOM: Simple Object Machine:
All modern interpreters include compiler inside: compile highlevel elements into low-level but portable byte-code, or use JIT for compiling into machine code into RAM.
PS: If anybody wants to write SmallTalk system on Python, please note me.
protected by Andrew MortonApr 2 at 14:34
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged compiler-constructioninterpreter or ask your own question.
It is a general notion that people try to classify programming languages as either “compiled” language or “interpreted” language. Even experienced programmers tend to get confused here! But the fact is, programming languages are neither “compiled” nor “interpreted” types. They can be both at the same time. Compiling or interpreting -both are 2 different ways of implementing the same program written using a programming language. A program written in C language can either be compiled or can be interpreted. Same is the case with Java or any other programming languages. The only requirement is, we need a C or Java compiler to compile a C/Java program and similarly we need an C/Java Interpreter to interpret a program written in C/Java. So the difference is not with programming languages, it is with the way programs written in different languages are implemented.
Any one serious about programming should understand the working of compilers,interpreters and the differences between them.So here I am trying to outline generic differences between compiling and interpreting (compilers vs interpreters).
Though I said programs written in any programming language can be either compiled or interpreted, it is not the case always. Theoretically what I wrote above is right – any program can be compiled/interpreted. But a programming language is usually developed with an orientation to one particular form of execution – for example- C language was designed to be compiled where as Java was designed to be interpreted. But there are interpreters available for C programs too which will be helpful as debugging aids. But in most cases a C program is compiled for execution and not interpreted. Before going through differences, keep in mind the following technical terms.
Compile time – The time taken to compile a program.
Run time – The time taken for executing a program.
Source code– The program in its user written form of the language. Source code is given as input to the compiler.
Object code– is actually the machine code which is obtained by converting source code. The computer can read and execute machine code directly. An object code is also known as binary code/machine code.
So the primary difference between a compiler and interpreter is in the way a program is executed. A compiler reduces the source code to machine code and then save it as an object code before creating an executable file for the same. When executed, the compiled program is executed directly using the machine code (object code). Where as an interpreter does not convert the source code to an object code before execution. An interpreter executes the source code line by line and conversion to native code is performed line by line while execution is going on (at runtime). In such a scenario, the run time required for an interpreted program will be high compared to a compiled program. Even though the run time required for interpreted program are high, the execution using an interpreter has its own advantages. For example -interpreted programs can modify themselves at runtime by adding/changing functions. A compiled program has to be recompiled fully even for the small modifications we make to a small section of the program; where as an in the case of an interpreter there is no such problem (only the modified section needs to be recompiled).
Let us summarize the advantages of both implementation methods – compiling and interpreting
Advantages of using compiler:-
- Since compiler converts the program to native code of the target machine (object code), faster performance can be expected.
- There is a scope for code optimisation.
Advantages of using interpreter:-
- Process of execution can be done in a single stage. There is no need of a compilation stage.
- Alteration of codes possible during runtime.
- Really useful for debugging the codes (because source code execution can be analyzed in an IDE)
- Facilitates interactive code development.
Comments are closed.