Computer Algorithms  «Prev  Next»
Lesson 7 Compilers and interpreters
ObjectiveExplain what compilers and interpreters do.

Compilers and Interpreters

High-level programming languages such as BASIC, C, C++, and Java bridge the gap between the languages that humans understand and the language that a computer understands. These programming languages make use of words that we use, but they have a much stricter syntax to make it possible to translate them into the machine language (1s and 0s) required by the computer. It would certainly be nice if we could issue the computer an instruction like:
"Print the numbers 1 through 10."

We are not quite there yet, but we are pretty close. Here's a program written in BASIC programming language that would accomplish this task:
FOR number = 1 TO 10
 PRINT number
NEXT number

There are two ways to translate a program written in a high-level programming language into machine language. One approach involves using a program called a compiler and the other involves using a program called an interpreter. Both a compiler and an interpreter translate source code into machine code. In other words, they both translate the program's instructions from the language of the programmer into the language of the computer.

source code vs. executable machine code graphic
Source code vs. executable machine code graphic

A compiler translates an entire program from source code into machine code, and then this machine code is usually written to a file. Then, in a second step, the computer executes the machine code. The primary advantage of this approach is that because the program is already in machine code it runs very quickly. An important point to note is that a program compiled on a computer with one type of architecture generally can not be run on a computer with a different architecture. For example, the machine code produced by compiling a C program on a PC could not be run on a Mac. The program would need to be compiled using a different C compiler on the Mac.
An interpreter combines the translation and execution of a program.
It translates source code line by line into machine code and executes each line as it is translated. Because the source code has to be translated into machine code each time the program is executed, it generally takes longer to run interpreted programs than compiled programs.
This concludes your introduction to computer basics. The next lesson wraps up this module.