DEV Community

Cover image for Introduction of compiler and interpreter
Mohiuddin Abdul Kader
Mohiuddin Abdul Kader

Posted on

Introduction of compiler and interpreter

Compiler and interpreter both are one kind of translator program. They do translate our code from high-level language to low-level or convert it to binary code. Compiler and interpreter work in two different ways. Let's give a deep dive into the compiler and interpreter. Let's know which languages are high-level languages now in this era. Most widely uses high-level languages are: Python, Java, C++, C#, Visual Basic, JavaScript, PHP and so on.

Compiler:

Compilers translate our code from a higher-level language to something that the computer can understand.

As to how a compiler works, that is indeed complicated. I will attempt to briefly outline the main stages of the process, but this will be a very cursory overview.

  • Lexing - break up the text of the program into "tokens". The tokens are the "words" of the programming language, such as identifiers (keywords, variable names, function names, etc.) or operators (=, *, &, etc.).
  • Parsing - convert the sequence of tokens into a parse tree, which is a data structure representing various language constructs: type declarations, variable declarations, function definitions, loops, conditionals, expressions, etc.
  • Optimization - evaluate constant expressions, optimize away unused variables or unreachable code, unroll loops if possible, etc. Translate the parse tree into machine instructions (or JVM byte code).

Modern compilers are very smart, and, consequently, very complicated.

How compiler works

Interpreter:

An interpreter is a computer program that is used to directly execute program instructions written using one of the many high-level programming languages.

The interpreter transforms the high-level program into an intermediate language that it then executes, or it could parse the high-level source code and then performs the commands directly, which is done line by line or statement by statement.

To make a long story short; an interpreter translates the high-level language line by line, which is done only when the target program is run.

How interpreter works

Core pros and cons of compiler and interpreter

  • Interpreter is memory efficient. On the other hand compiler is not memory efficient.
  • Compiler: compiled code run faster. Interpreter: interpreted code run slower.

Top comments (0)