9

Possible Duplicate:
How does an interpreter/compiler work

Hi what is the exact difference between the Compiler and interpreter ?

Community
  • 1
  • 1
Vinoth K
  • 465
  • 1
  • 10
  • 19
  • This is something that could be looked up by a google search – Raj Sep 06 '10 at 11:08
  • 7
    We should be nicer to noobs. I don't see any problems in asking such a question here. – pavanlimo Sep 06 '10 at 11:13
  • @pavan, after the first hundred questions of this class (i.e. duplicate and/or trivial), you may start to see the problem... :-) – Péter Török Sep 06 '10 at 11:17
  • 1
    Hmmm.. may be. But until then, I'll keep saying `be nice`. :) – pavanlimo Sep 06 '10 at 11:39
  • @pavan, being nice is not the same as allowing low quality questions to flood the site. We can explain to newcomers the SO conventions or point them to the SO search box politely and nicely (at least the first few times :-) – Péter Török Sep 06 '10 at 11:57
  • @Peter, true. You comment pointing tot he duplicate was apt I'd think. – pavanlimo Sep 06 '10 at 13:02
  • Another extant version: [*what is the difference between compiler and interpreter?*](http://stackoverflow.com/questions/3618074/), and similar topics [*What is the difference between implementing a compiler and an interpreter?*](http://stackoverflow.com/questions/475223/), [*There is no such thing as a “compiled language” or “interpreted language”*](http://stackoverflow.com/questions/3440297/), [*Compiled vs. Interpreted Languages*](http://stackoverflow.com/questions/3265357/). It is a fine question, but it has been done several times already. – dmckee --- ex-moderator kitten Sep 06 '10 at 18:18

2 Answers2

9

From Wikipedia:

  • Compiler: A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code).
  • Interpreter: An interpreted language is a programming language in which programs are 'indirectly' executed ("interpreted") by an interpreter program. This can be contrasted with a compiled language which is converted into machine code and then 'directly' executed by the host CPU.
Noel M
  • 15,812
  • 8
  • 39
  • 47
6

Compiler

A compiler generates machine-dependent assembly code which can then be assembled and linked to into the appropriate machine op-codes to allow the program to execute. This process can only be run at build time.

Interpreter

An interpreter generates machine-independent code which can then be on-the-fly compiled to assembly code (e.g. Just-in-Time compilation). This process can be executed at build time and / or run time.

Hope this helps!

husterk
  • 184
  • 1
  • 10
  • Few interpreters ever produce machine code. Unless the machine code is going to be persisted and reused (in which case the thing doing the conversion is a compiler) most interpreters figure out what needs to be done and just do it without generating any machine code first. The only case where an interpreter would normally generate machine code would be for something like the 8080's IN/OUT instruction which requires a hard-coded address. To handle an "OUT 100,5" instruction, the interpreter would store "OUT 64h" and "RET" instructions at some fixed spot, load A with 5, and CALL that spot. – supercat Sep 27 '11 at 15:55
  • @supercat Can you please explain "most interpreters figure out what needs to be done and just do it without generating any machine code first" bit more? how does interpreter execute the code with out generating machine code – Trident Jun 04 '20 at 18:53
  • @Trident: Generally, they use the equivalent of a giant `if`/`else` and/or `switch` statement. For example, the MS-BASIC interpreter that replaces keywords with tokens in the range 128-255 but leaves other parts of program text as-is would say "if (first byte of statement is a token) use a switch to select an action. Otherwise, scan until an `=` is found, making note of the first two characters one saw, as well as whether one saw a `%` or `$`. Then evaluate whatever follows the `=` as an expression, and store that to the variable identified before the `=`'. – supercat Jun 04 '20 at 19:12
  • then how about the left out parts of the program, how will they be handled? – Trident Jun 04 '20 at 19:18
  • my basic question is actually around JIT compiler, JIT does two jobs, 1. it executes the bytecode directly. 2. it also saves the machine code for some components in an anticipation of future reuse. I understood the compilation part. But did not understand how JIT executes the bytecode with out compiling it to machine code – Trident Jun 04 '20 at 19:22
  • There is too much of garbage on internet, i think i got the answer - JIT compiles only those methods that are called at runtime. These methods are compiled the first time they are called, and then they are stored in a cache. When the same methods are called again, the compiled code from a cache is used for execution. – Trident Jun 04 '20 at 19:49