Compiler Vs Interpreter

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Compiler vs Interpreter

Definition of Compiler
A compiler is a program that reads a program written in the high-level
language and converts it into the machine or low-level language and
reports the errors present in the program. It converts the entire source
code in one go or could take multiple passes to do so, but at last, the user
gets the compiled code which is ready to execute.

Compiler operates on phases; various stages can be grouped into two


parts that are:

 Analysis Phase of the compiler is also referred to as the front end in


which program is divided into fundamental constituent parts and checks
grammar, semantic and syntax of the code after which intermediate code is
generated. Analysis phase includes lexical analyzer, semantic analyzer and
syntax analyzer.
 Synthesis phase of the compiler is also known as the back end in which
intermediate code is optimized, and the target code is generated. Synthesis
phase includes code optimizer and code generator.

Phases of Compiler

Now let’s understand the working of each stage in detail.

1. Lexical Analyzer: It scans the code as a stream of characters, groups the


sequence of characters into lexemes and outputs a sequence of tokens with
reference to the programming language.
2. Syntax Analyzer: In this phase, the tokens that are generated in the
previous stage are checked against the grammar of programming
language, whether the expressions are syntactically correct or not. It
makes parse trees for doing so.
3. Semantic Analyzer: It verifies whether the expressions and statements
generated in the previous phase follow the rule of programming language
or not and it creates annotated parse trees.
4. Intermediate code generator: It generates an equivalent intermediate
code of the source code. There are many representations of intermediate
code, but TAC (Three Address Code) is the used most widely.
5. Code Optimizer: It improves time and space requirement of the program.
For doing so, it eliminates the redundant code present in the program.
6. Code generator: This is the final phase of the compiler in which target
code for a particular machine is generated. It performs operations like
memory management, Register assignment, and machine-specific
optimization.

The symbol table is somewhat a data structure which manages the


identifiers along with the relevant type of data it is storing. Error
Handler detect, report, correct the errors encountering in between the
different phases of a compiler.

Definition of Interpreter

The interpreter is an alternative for implementing a programming


language and does the same work as a compiler. Interpreter
performs lexing, parsing and type checking similar to a compiler. But
interpreter processes syntax tree directly to access expressions and
execute statement rather than generating code from the syntax tree.

An interpreter may require processing the same syntax tree more than
once that is the reason why interpretation is comparatively slower than
executing the compiled program.
Key Differences Between Compiler and Interpreter

Let’s look at major differences between Compiler and Interpreter.

1. The compiler takes a program as a whole and translates it, but interpreter
translates a program statement by statement.
2. Intermediate code or target code is generated in case of a compiler. As
against interpreter doesn’t create intermediate code.
3. A compiler is comparatively faster than Interpreter as the compiler take the
whole program at one go whereas interpreters compile each line of code
after the other.
4. The compiler requires more memory than interpreter because of the
generation of object code.
5. Compiler presents all errors concurrently, and it’s difficult to detect the
errors in contrast interpreter display errors of each statement one by one,
and it’s easier to detect errors.
6. In compiler when an error occurs in the program, it stops its translation
and after removing error whole program is translated again. On the
contrary, when an error takes place in the interpreter, it prevents its
translation and after removing the error, translation resumes.
7. In a compiler, the process requires two steps in which firstly source code is
translated to target program then executed. While in Interpreter It’s a one-
step process in which Source code is compiled and executed at the same
time.
8. The compiler is used in programming languages like C, C++, C#, Scala,
etc. On the other Interpreter is employed in languages like PHP, Ruby,
Python, etc.

https://techdifferences.com/difference-between-compiler-and-interpreter.html
The compilation process in C

6. Describe the stages of the compilation process.

You might also like