What is the difference between compiler and interpreter? Can a programming language have both a compiler and an interpreter?

  • 3
    can - etki
  • 2
    BASIC has traditionally had many different compilers and interpreters. - VladD
  • 2
    See also - D-side

3 answers 3

To make a difference, let's find out what it is:

Compiler - a program or technical tool that compiles.

Compilation is the translation of a program written in a high-level source language into an equivalent program in a low-level language close to the machine code (absolute code, object module, sometimes into assembly language). The input information for the compiler (source code) is a description of the algorithm or a program in a domain-specific language, and the output of the compiler is an equivalent description of the algorithm in a machine-oriented language (object code). Wikipedia

An interpreter is a program (a kind of translator) that performs interpretation.

Interpretation - pooperator (pokomandny, line-by-line) analysis, processing and immediately execution of the original program or request (as opposed to compilation, in which the program is broadcast without executing it). Wikipedia

Thus, we can conclude that the compiler converts the source code to close to the machine code. The interpreter, however, allows you to perform some representation of the program, converting it into machine code on the fly.

If you take the java language, then it has both a compiler and an interpreter. The compiler converts the source code into bytecode. A bytecode at the initial stage of program execution is processed by the interpreter.

The compiler is a separate javac program, the interpreter is built into jvm . In addition to the usual compiler, in jvm there is just-in-time compilation. because the interpretation is rather slow, the hot spots in the program are compiled into machine code by the jit compiler, thus speeding up execution.

  • The compiler is a separate javac program - there is some problem with the terminology, because jvm also has an entity called compiler openjdk.java.net/groups/hotspot/docs/… - etki
  • I added the answer by pointing to jit compilers. I agree, there is a little confusion, but javac is a compiler, everything is correct here docs.oracle.com/javase/7/docs/technotes/tools/windows/… - Artem Konovalov
  • one
    The interpreter, however, allows you to perform some representation of the program, converting it into machine code on the fly. The interpreter does not perform the conversion "on the fly"; this is already a run-time compilation. The interpreter already contains ready-made executable code for each elementary instruction, and, after reading the next line of code, it forms a chain of calling these ready-made executable codes and parameters for transmission to them. - Akina
  • @Akina did not quite understand the idea. Where is the error in my reasoning - the byte code is converted by the interpreter into machine code, which is immediately executed. However, it is not saved anywhere. The essence of my answer lies precisely in this detail. The compilation saves the result of the work, and the interpretation, converts, translates the representation into machine code, which is then executed - Artem Konovalov
  • 2
    More likely no than yes. What difference does the compilation of the entire code or a single operator, is saved on the disk or not? The difference is that the true interpreter, in contrast to the true compiler, already contains ready-made compiled procedures for executing language constructs, and it is they who perform the required actions. And in this sense, an intermediate case, compilation into a P-code, is neither one nor the other (well, or both, how to look), but historically this option was attributed to compilation. - Akina

1) The difference is that the compiler converts the code of an algorithmic language into an executable (bytecode or native) code (in the limit - into machine codes), while the interpreter independently recognizes and executes the program code line by line.

2) Yes, it can. Even the ancient turbo / quick basic - and they could. And Borland Turbo BASIC was "in one bottle", while Microsoft Quick Basic used, like all modern, external compiler.

UPD: Probably, it is still possible to formulate this way: the interpreter for program execution must have the source code of the program (in its original or formalized form) in an algorithmic language, while compilation creates an executable module that does not contain the program text in an algorithmic language. Also, if debugging information is not added during compilation, full restoration of the source code is not possible.

    the interpreter executes the program, translators including. compilers convert without doing