A compiler is a program that translates a source program written in some high-level programming language (such as Java) into machine code for some computer architecture (such as the Intel Pentium architecture). The generated machine code can be later executed many times against different data each time.
An interpreter reads an executable source program written in a high-level programming language as well as data for this program, and it runs the program against the data to produce some results. One example is the Unix shell interpreter, which runs operating system commands interactively.
Note that both interpreters and compilers (like any other program) are
written in some high-level programming language (which may be
different from the language they accept) and they are translated into
machine code. For a example, a Java interpreter can be completely
written in C, or even Java. The interpreter source program is
machine independent since it does not generate machine code. (Note the
difference between generate and translated into machine
code.) An interpreter is generally slower than a compiler because it
processes and interprets each statement in a program as many times as
the number of the evaluations of this statement. For example, when a
for-loop is interpreted, the statements inside the for-loop body will
be analyzed and evaluated on every loop step. Some languages, such as
Java and Lisp, come with both an interpreter and a compiler. Java
source programs (Java classes with .java
extension) are
translated by the javac
compiler into byte-code files (with
.class
extension). The Java interpreter, called
the Java Virtual Machine (JVM), may actually interpret byte codes
directly or may internally compile them to machine code and then
execute that code (JIT: just-in-time compilation).
Compilers and interpreters are not the only examples of translators. Here are few more:
Source Language | Translator | Target Language |
LaTeX | Text Formater | PostScript |
SQL | database query processor | Query Evaluation Plan |
Java | javac compiler | Java byte code |
Java | cross-compiler | C++ code |
English text | Natural Language Understanding | semantics (meaning) |
Regular Expressions | JLex scanner generator | a scanner in Java |
BNF of a language | CUP parser generator | a parser in Java |