Obviously, the perception of human beings and an electronic device such as a computer is different. Humans can understand anything through natural languages, but not a computer. The computer needs a translator to convert written languages in human readable form to computer readable form.
Compiler and interpreter are the types of language translator. What is the language translator? This question may be emerging in your mind.
A language translator is software that translates programs from a source language that can be read in humans to an equivalent program in an object language. The source language is generally a high-level programming language, and the object language is typically the machine language of a real computer.
|Entry||One complete program is needed at a time.||Take only one line of code or instruction at a time.|
|Exit||Generate intermediate object code.||It does not produce any intermediate object code.|
|Working mechanism||The compilation is done before execution.||Compilation and execution are performed simultaneously.|
|Memory||The memory requirement is more due to the creation of object code.||It requires less memory since it does not create an intermediate object code.|
|The mistakes||Show all errors after compilation, all at the same time.||Show error of each line one by one.|
|Error detection||Difficult||Comparatively easier|
|Corresponding Programming Languages||C, C ++, C #, Scala, typescript uses compiler.||Java, PHP, Perl, Python, Ruby uses an interpreter.|
A compiler is a program that reads a program written in the high level language and converts it into the low level machine or language and reports the errors present in the program. Convert all the source code at once or it may take several passes to do so, but in the end, the user gets the compiled code that is ready to run.
The compiler operates in phases; The different stages can be grouped into two parts that are:
- The phase from analysis The compiler is also known as the front end where the program is divided into fundamental constituent parts and verifies the grammar, semantics and syntax of the code, after which the intermediate code is generated. The analysis phase includes a liquid analyzer, a semantic analyzer and a syntax analyzer.
- The phase from synthesis The compiler is also known as the backend in which the intermediate code is optimized and the destination code is generated. The synthesis phase includes the code optimizer and the code generator.
Phases of the compiler
Now let's understand in detail the operation of each stage.
- Lxico Analyzer : Scan the code as a character stream, group the character sequence into lexemes and generate a token sequence with reference to the programming language.
- Syntax analyzer : In this phase, the tokens that are generated in the previous stage are compared with the grammar of the programming language, whether the expressions are synthetically correct or not. It makes the trees stand to do it.
- Semantic analyzer : Verify whether the expressions and statements generated in the previous phase follow or not the rule of the programming language and create annotated analysis trees.
- Intermediate Code Generator : Generates equivalent intermediate code from the source code. There are many representations of intermediate code, but the TAC (three-way code) is the most used.
- Code Optimizer : Improves the time and space requirements of the program. To do this, remove the redundant code present in the program.
- Code Generator : This is the final phase of the compiler in which the destination code is generated for a particular machine. Performs operations such as memory management, allocation of records and specific machine optimization.
The symbol table It is, in a way, a data structure that manages identifiers along with the type of relevant data it is storing. The error handler detects, informs and corrects errors that occur between the different phases of a compiler.
The interpreter is an alternative to implement a programming language and does the same job as a compiler. Interpreter performs lexing, analysis Y type verification similar to a compiler. But the interpreter processes the syntax tree directly to access the expressions and execute the declaration instead of generating code from the syntax tree.
An interpreter may require processing the same syntax tree more than once, so the interpretation is comparatively slower than the execution of the compiled program.
Compilation and interpretation probably combined to implement a programming language. In which a compiler generates an intermediate level code, the code is interpreted instead of being compiled into a machine code.
The use of an interpreter is advantageous during the development of the program, where the most important part is to be able to test a modification of the program quickly instead of executing the program efficiently.
Key differences between compiler and interpreter
Let's look at the main differences between the compiler and the interpreter.
- The compiler takes a program as a whole and translates it, but the interpreter translates a declaration of the program by declaration.
- The intermediate code or destination code is generated in the case of a compiler. As against interpreter does not create intermediate code.
- A compiler is comparatively faster than Interpreter, since the compiler takes the entire program at once, while the interpreters compile each line of code after the other.
- The compiler requires more memory to interpret due to the generation of object code.
- The compiler presents all the errors at the same time, and it is difficult to detect the errors in the contrast interpreter, one by one it shows the errors of each instruction and it is easier to detect them.
- In the compiler, when an error occurs in the program, it stops its translation and, after eliminating the error, the whole program is translated again. On the contrary, when an error occurs in the interpreter, it prevents its translation and, after eliminating the error, the translation resumes.
- In a compiler, the process requires two steps in which, first, the source code is converted to the target program and then executed. While in Interpreter It is a one-step process in which the source code is compiled and executed at the same time.
- The compiler is used in programming languages such as C, C ++, C #, Scala, etc. In the other interpreter it is used in languages such as Java, PHP, Ruby, Python, etc.
The compiler and the interpreter intend to do the same work but differ in the operating procedure, the compiler takes the source code in an aggregate manner, while the interpreter takes constituent parts of the source code, that is, declaration by declaration.
Although both the compiler and the interpreter have certain advantages and disadvantages, as interpreted languages are considered multiplatform, that is, the code is portable. It is also not necessary to compile instructions previously unlike the time-saving compiler. Compiled languages are faster in terms of the compilation process.