Compilers and Interpreters of Programming Languages

Table of Contents

Page Content

Introduction

Computers are made up of electronic circuits that understand and operate on binary numbers made up of 1s and 0s. So the special native language made up of 1 and 0 is called the machine language. We can say that the machine language is the interface where hardware and software meet inside a computer. The hardware is designed to fetch, interpret and execute, instructions given in machine language. The problem with machine language is that it is difficult to be used and understood by humans who are used more to the everyday languages such as English, French, etc....
The solution lie in writing programs using a high level programming language( close to human languages), easily understood by programmers (after some training), such as Java, C, C++ which is then transformed into machine language and then executed.

Simplified Example: Why a Compiler or Interpreter is Needed

Suppose we need to add two numbers A and B
This may be programmed as follows
'add A and B' (this language is understood by humans but not by computers)
We now assign a machine codes (made up of 1's and 0's) as follows
let 'add' be '1100'
let 'A' be '1101'
and 'B' be '0110'
Assigning a code to add, A and B has the advantage of being understood by a computer. So 'add A and B' may be written in machine code as follows: 1100 1101 0110

There are two ways that high level programming languages may be transformed: using either an interpreter or a compiler and sometimes a combination of the two.

Compiler

A compiler is a type of software program that is specifically designed to translate an entire high-level programming language such as Java, C, or Python into a lower-level machine language that can be directly understood and executed by the processor.
When a programmer writes code in a high-level language, it contains instructions and statements that are intended to be understood by humans, but not directly by computers. In order to execute this code on a computer, it must first be translated into machine language that the computer can understand. This process of translation is called compilation.
During compilation, the compiler scans the entire code written in the high-level language, checks it for syntax errors, and converts it into machine code. This machine code is then saved in an executable file format, typically with a '.exe' extension, which can be run on the computer.
Once the code has been compiled, the resulting executable file can be executed by the processor without the need for the original high-level code. This means that you can run the program as many times as needed without having to recompile it again.
Compilers are essential tools for software development, as they allow programmers to write code in a high-level language, which is easier to read and write, and then translate it into machine code that can be directly executed by the computer.

Interpreter

An interpreter is a software program that reads and executes high-level programming language code, one line or one statement at a time, without creating an intermediate executable file. Unlike a compiler, which generates machine code from the source code, an interpreter reads and executes the code directly.
When an interpreted program is run, the interpreter reads the code statement by statement, evaluates each statement, and executes it. The interpreter checks for syntax errors as it reads the code and reports them immediately, stopping the program's execution if it encounters an error. In this way, the interpreter provides a more interactive and iterative way of developing and testing programs, allowing developers to quickly modify and test code as they go.
One of the key differences between an interpreter and a compiler is that an interpreter does not generate an intermediate executable file. This means that each time the program needs to be executed, the interpreter must read and interpret the code again, which can make interpreted programs slower than compiled programs. However, interpreted programs can be more flexible, as they can be modified and tested more easily, making them well-suited for languages like Python, which are commonly used in scientific computing and rapid prototyping.
Both interpreters and compilers serve the purpose of translating high-level programming language code into machine code that can be executed by a computer, they differ in their approach to this task. Compilers generate an intermediate executable file, while interpreters read and execute the code directly, one line at a time.

References and Links