What is Compiler? Programming languages are notations for describing computations to people and to machines. The world as we know it depends on programming languages because all the software running on all the computers was written in some programming language. But, before a program can be run, it must be translated into a form in which it can be executed by a computer. The software systems that do this translation are called compilers.
But to understand Compilers we need to understand Translators. If you didn’t see our Translator article, then see the article now for a better understanding.
What is Compiler?
A compiler is a computer program that transforms the source language written in one language into the target language written in another language. An important role of the compiler is to report any errors in the source program that it detects during the translation process.
If the target program is an executable machine-language program, it can then be called by the user to process inputs and produce outputs; see the figure below.
Features of a Compiler
Here are some of the characteristics:
- The speed of compilation
- Machine code accuracy.
- Code should not modify its meaning.
- The speed of the target code.
- Detects errors well.
- Checking the code correctly according to grammar.
Uses/Application of Compilers
Below are the Application of Compilers.
- This aids in making the code platform-independent.
- Removes syntax and semantic mistakes from the code.
- Make code executable files.
- Translates code from one language to another.
An interpreter is another common kind of language processor. Instead of producing a target program as a translation, an interpreter appears to directly execute the operations specified in the source program on inputs supplied by the user, as shown in the figure below.
The machine-language target program produced by a compiler is usually much faster than an interpreter at mapping inputs to outputs. An interpreter, however, can usually give better error diagnostics than a compiler, because it executes the source program statement by statement.
Java language processors combine compilation and interpretation, as shown in the figure below. A Java source program may first be compiled into an intermediate form called bytecodes. The bytecodes are then interpreted by a virtual machine. A benefit of this arrangement is that bytecodes compiled on one machine can be interpreted on another machine, perhaps across a network. In order to achieve faster processing of inputs to outputs, some Java compilers, called just-in-time compilers, translate the bytecodes into machine language immediately before they run the intermediate program to process the input.
Advantages: Interpreter over Compiler
The advantages that an Interpreter has over a Compiler are as below:
- An interpreter can usually give better error diagnostics than a compiler, because it executs the source program statement by statement.
- Debugging an interpreted program is very simple since just one line of code is translated and run at a time.
- The Interpreter reports errors for a single line of code at a time since translation and execution occur concurrently.
- Unlike a compiler, which temporarily stores the Object code on disk, interpreters are memory-efficient since no temporary storing of the translated code occurs.
- The Interpreter examines one line at a time, thus it takes less time to analyze the source program; however, the Compiler analyzes the entire program at once, so it takes more time to analyze.
Advantages: Compiler over Interpreter
The advantages that a Compiler has over an Interpreter are as below:
- The machine language target program produced by a compiler is usually much faster than an interpreter at mapping inputs to outputs.
- Because compilers evaluate programs before compiling them, all faults are found and rectified before the produced code is generated.
- An intermediate code is also known as the Object code generated which can then be used each time the program is to be run, thus eliminating the need for compiling the source program each time.
- Compiling a program is usually faster than interpreting it.
Difference Between Compiler and Interpreter
|Scans the entire program and translates it as a whole into machine code.||Translates statements one by one.|
|Execution is faster.||Execution is slower.|
|You can’t change the program without going back to the source code.||Interpreted programs can run on computers that have the corresponding interpreter.|
|Store machine language as machine code on the disk.||Not saving machine code at all.|
|It is based on a language translation linking-loading model.||It is based on the Interpretation Method.|
|Generates output programs (in the form of exe) that can be run independently from the original program.||Do not generate output programs. So they evaluate the source program every time during execution.|
|Target programs execute independently and do not require the compiler in the memory.||The interpreter exists in the memory during interpretation.|
|Generates Object Code which further requires linking, hence requires more memory.||No Object Code is generated, hence is memory efficient.|
|The compiler sees the entire code upfront. Hence, they perform lots of optimizations that make code run faster||Interpreters see code line by line, and thus optimizations are not as robust as compilers|
|Difficult to implement as compilers cannot predict what happens at turn time.||Interpreted languages support Dynamic Typing|
|It is best suited for the Production Environment||It is best suited for the program and development environment.|
|Compiler displays all errors and warnings at the compilation time. Therefore, you can’t run the program without fixing errors||The interpreter reads a single statement and shows the error if any. You must correct the error to interpret the next line.|
|Compilers generate intermediate machine code.||Interpreters never generate any intermediate machine code.|
|Display all errors after compilation, all at the same time.||Displays all errors of each line one by one.|