3
votes

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?]

4
In 1. where you say "interpreter" the second time, you mean "compiler" right? - Eric
No, Eric, I really mean interpreter. - utxeee

4 Answers

1
votes

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)

3
votes

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.)

0
votes

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.

0
votes

What is a translator?

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, i.e. Java -> JVM
  • Assemblers - translates assembly language code to machine code, i.e. x86as -> x86
  • High-level translators - translates code from one PL to another, i.e. Java -> C
  • Decompilers - translates low-level code to high-level code, i.e. JVM -> Java

What is an interpreter?

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 code) language is ~10 slower
  • If an instruction is called repeatedly, it will be analysed repeatedly - time-consuming!
  • No need to compile code

Differences

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


Source