3

So, let me see if I get this clearly or not.

  1. When we say the differences between a compiler and an interpreter is that an interpreter translates high-level instructions into an intermediate form, which it then executes. [I think the compiler also translate high-level instructions into an intermediate form but at this moment it generate the object code instead of executing it, right?]

  2. An interpreter reads the source code one instruction or line at a time, converts this line into machine code and executes it. [The interpreter itself doesn't convert the code to machine code, it evaluates the instruction (after that had been parsed) using ist own precompiled functons. E.g. Add expression in the high-level language will be evaluated using the interpreter add function which has been previously compiled, right?]

utxeee
  • 953
  • 1
  • 12
  • 24

3 Answers3

3

The key difference is this: An interpreter processes the source code as it runs it. It does not convert the source into machine code, it simply uses its own code to accomplish what the source directs. A compiler converts the source code into machine code that can be run directly.

Not all compilers are separate from the execution process. For example, most Java run-times include a "JIT compiler" that compiles Java code while it's running, as needed.

You can have things in-between. Essentially, a process similar to compiling can be used first to convert the source code into something smaller and easier to interpret. This compiled output can then be interpreted. (For example, a first pass could convert, say 'if' to 53, 'else' to 54, 'for' to 55, and so on -- this will save the interpreter from having to handle variable-length strings in code that doesn't actually deal with strings.)

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

I would agree with the first, although it is not necessarily true that the interpreter is working on one line at a time (it could do optimizations based on knowledge of the whole code).

The second I think is slightly off: the compiler does create "machine code" (which could be byte code, for a JVM). The interpreter executes parts of its own program based on the input (so far same as compiler), which executed parts are performing the computation described in the input (as opposed to performing computation to calculate the needed machine code).

It is possible to blur the lines between the two as a compiler can generate code that will be interpreted at the time of execution (to provide runtime optimization based on factors that are not available at compile time)

Attila
  • 28,265
  • 3
  • 46
  • 55
  • Hi Attila, if I understand it right you agree with me concerning the point 2 ? – utxeee Apr 15 '12 at 12:07
  • Not exactly: the bold part is incorrect: no translation to execution code is done on the part of the interpreter (actively): that was done when the interpreter was compiled (assuming it is not being interpreted itself :)). The interpreter uses its existing code (already in machine code) to perform the needed computation. This is essentially what you have in the brackets ([]), but not what the bold part says – Attila Apr 15 '12 at 12:16
  • The bold part haven't been wrote by me I read it somewhere. I just highlighted it because I don't agree with it. My words are just the ones in the brackets. Thus you really agree with me :D – utxeee Apr 15 '12 at 12:25
0

You're right about (1).

Ad (2), an interpreter need not read source code one instruction at a time, because that's simply too expensive when interpreting code that contains loops. More likely, it reads entire expressions, statements, functions, or even source files, translates those to an intermediate format and evaluates that.

Note that neither a compiler nor an interpreter need to generate machine code at any point; many programming languages, including Java, Python, but also older ones like Prolog, are commonly compiled to virtual machine bytecodes. In Python and Prolog, the "interpreter" is commonly a combined bytecode compiler/bytecode interpreter.

The best introduction to compilation and interpretation that I know of are chapters 4 and 5 of SICP, which start by discussing a very simple interpreter and iteratively improve it until it's a full-fledged compiler.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Hi larsman, but when we are using gcc it really generate machine code, right ? – utxeee Apr 15 '12 at 12:03
  • @user1334379: yes, GCC produces machine code, as do all major C and C++ compilers. Interestingly, GCC's Java compiler GCJ can also produce machine code, unlike other Java compilers. – Fred Foo Apr 15 '12 at 12:38