Interpreter

0
(0)

An interpreter is a program that executes code written in a programming language. It doesn’t translate it into machine code entirely, but rather accepts commands line by line and executes them immediately. You can give the interpreter a command and immediately see if it worked.

This is one of two ways to translate code into a form understandable by a computer. The second option is compilation, when the code is “translated” entirely into machine instructions. We’ll discuss the differences below.

Programming languages ​​that work with interpreters are called interpreted. Most of the popular languages ​​today are interpreted. Some of them are popular options for teaching computer science and programming.

What are interpreters for?

Programming languages ​​are designed to be human-friendly. They are close to English, and their commands are human-readable. This applies primarily to high-level programming languages—those that are closer to humans than to hardware.

Computers don’t understand such languages. By default, code written in them is treated as plain text, not commands. After all, a computer is a computing machine that understands strings of ones and zeros, not text. But writing commands in so-called machine code is a very difficult, almost impossible, task. They are very long, especially if the program is complex, and are incomprehensible to humans.

Therefore, when developing a programming language, a program is always created to translate the code into a form understandable by a computer. An interpreter is one way to implement this process. A program in an interpreted language simply won’t run without an interpreter—the computer won’t understand it.

That is, an interpreter is needed so that programs in a particular language can be launched and executed.

How the interpreter works

The interpreter receives a command written in a programming language as input, reads it, and immediately executes it. Its operation can be roughly divided into two modes: interactive and batch.

Interactive mode.  This is usually done in the console. It’s also called a REPL (read-eval-print loop). It works like this: a user types a command in the console to the interpreter, and it’s executed immediately when they press Enter.

Typically, this requires first launching the interpreter with a separate command, but not always. For example, if a  JavaScript command is written in the browser console, no additional steps are required—browsers have a built-in JS interpreter by default.

Batch mode.  This is used for larger tasks, ones that can’t be written line by line and executed immediately. A user writes some code in a file, saves it with the desired extension, and passes it to the interpreter. The interpreter receives the file, reads the code line by line, and executes it.

Most programs are written in batch mode: writing them in the console and immediately executing them would be inconvenient. The REPL is used for quick calculations, sending queries, and other actions that require a single command.

The difference between an interpreter and a compiler

Another way to “translate” code into a machine-readable form is through compilation. Programs that perform this are called compilers, and the languages ​​that work with them are called compiled languages. The difference is that a compiler doesn’t perform any execution: it receives a code file as input, deciphers it, and translates it into machine code in its entirety. The result is an executable file that can be run within any operating system.

There are several differences between interpreters and compilers, both theoretical and purely practical.

  • The interpreter works with the code line by line, while the compiler translates the entire block of code as a whole.
  • The interpreter executes the code as soon as it “reads” the required line, and the compiler gives it to the system for execution – it only translates it itself.
  • The compiler cannot be used in REPL mode, only in batch mode.

An interpreter can be compared to a simultaneous translator who immediately produces a voice-over of the translation. A compiler, on the other hand, is like a literary translator who translates texts and then sends the translation to those who will work with it.

Famous interpreted languages ​​and their applications

The most popular interpreted languages ​​today are  JavaScript and Python. JavaScript gained popularity because it runs in the browser, making it widely used on the web, especially in front-end development. Python is used in machine learning, data analysis, mathematics, analytics, and automation, as well as on the web and in many other industries.

Other examples of interpreted languages ​​are  PHP, which is actively used on the web for the “server” part, and the universal  Ruby and Perl, which are often used for automation, system administration, or working with text.

Interpreted languages ​​are system- independent, so in theory they can be used anywhere. The main requirement is that the end device where the code will run has an interpreter installed.

But where maximum performance or closeness to the hardware is needed, compiled languages ​​are usually preferred. They execute code faster by translating it into machine code, which the system understands and executes quickly.

Some languages, such as BASIC or Python, have both compiled and interpreted versions. They are used for different purposes.

Multiple interpreters for one language

Most often, a single language has multiple interpreters, or, as they say, multiple implementations or engines. A prime example is JavaScript: it has three main interpreters, one used by Chromium-based browsers, another by Mozilla, and a third by Apple browsers.

Different implementations may differ slightly in execution or in the handling of certain complex queries. Each engine has its own optimization mechanisms and specific features. If the code is expected to run under multiple engines, it’s advisable to take them all into account. For example, consider JavaScript: front-end developers working with the browser-based portion of a website must consider and test the code’s behavior in different browsers. But the general operating principle is the same for all engines.

How exactly the code should work is determined by a specification—a large document, usually from the language’s developers, that describes in detail all possible commands and principles. The creators of interpreters are guided by this document.

Advantages of interpreted languages

Interactivity.  Code is executed line by line, which is convenient. If you need to test a simple query, you can simply launch the interpreter and issue a command in the console—everything will run instantly, without the time-consuming compilation and transmission to the system. This can also be used to perform simple actions, for example, to automate routine tasks.

Speed ​​of learning and writing code.  The aforementioned interactivity makes the language easier to learn. Firstly, you can immediately see the result of any action, and secondly, if an error occurs, the interpreter will point it out. And for the same reason, writing code is faster.

Ease of debugging.  Debugging code written in an interpreted language is easier for the same reason: it’s executed immediately. With compiled languages, an error might occur in one place and become noticeable to the compiler in another. This complicates debugging. With interpreted languages, this is easier: the interpreter starts complaining as soon as it reaches the erroneous location.

Moreover, if a compiled program encounters an error, it won’t execute at all. An interpreted program, however, will execute up to the point of failure. This is also convenient for debugging, and helps solve at least part of the problem before the program crashes.

Human-readable.  Most interpreted languages ​​are high-level, close to human understanding. This means they have a clear syntax, their code is easy to read, and the languages ​​themselves are relatively easy to learn. It’s certainly easier than writing in low-level languages ​​or even machine code.

Platform independence.  Execution is handled by the interpreter, not the system. Therefore, the language is OS-independent, meaning it’s cross-platform by default. This distinguishes interpreted languages ​​from compiled ones, where execution is directly dependent on the system. After all, different operating systems perceive machine code differently, so compiler programs are specific to each system. And code compiled for Windows won’t run on Linux or Mac.

Optimization mechanisms.  Interpreters themselves also play into the programmer’s hands: they have built-in mechanisms that optimize the code themselves. This means that if the code isn’t written most optimally, the interpreter will mitigate the lack of optimization using built-in mechanisms.

Of course, this doesn’t mean we should forget about code quality. It’s still important. But this feature allows us to focus on making the code clean and human-readable, rather than sacrificing clarity for the sake of a few milliseconds.

Disadvantages of interpreted languages

Slow program performance.  Programs written in interpreted languages ​​run slower than those written in compiled languages ​​because the code isn’t executed directly by the system. However, the speed difference is mitigated by two factors: the optimization of the interpreters themselves and the absence of an intermediate step—compilation, or translation into machine code. Although compiled languages ​​are preferred for performance-critical processes, interpreted languages ​​are sufficient for most tasks.

Risk of error.  We mentioned above that debugging code in interpreted languages ​​is easier. This is true, but this advantage has a downside. Since the program runs immediately and executes until the error occurs, it’s easy to miss an error in rarely executed code blocks. There’s a risk that the program will run without the developer ever realizing the error. Compiled languages ​​are immune to this: if there’s an error anywhere, the program simply won’t run. However, this problem can be avoided through proper testing.

Dependency on an installed interpreter.  Interpreted languages ​​are independent of the operating system, but they do depend on an interpreter. For code to run, you need not only to download the file but also install an interpreter. Without one, the computer simply won’t be able to run the program—there’ll be nothing to understand it. However, an executable file created with a compiler can be run anywhere, but only on the target OS.

Open source code.  When compiled into machine code, the source code is hidden, preventing anyone from seeing how a particular function was implemented. Interpreted languages ​​lack this advantage: the code isn’t translated into an executable file, but is executed directly by the interpreter. Therefore, the source code remains open and can be viewed by anyone. This is a disadvantage if the program code is a trade secret or the developer simply wants to hide it for some reason. Additional tools will have to be used to “hide” the code.

Which languages ​​are better to learn: compiled or interpreted?

There’s no definitive answer as to which is better: both types have their own applications, advantages, and disadvantages. Therefore, we recommend focusing on the field you want to work in and the languages ​​commonly used there. For example, in game development and when creating applications for various operating systems, compiled languages ​​are typically used. On the web and for automation, interpreted languages ​​are preferred. You can also try different options and figure out which approach suits you best.

Learn more about programming and information technology with our courses. We’ll help you find a new, high-paying career.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email
CONTINUE LEARNING Next: Compiler →

Leave a Reply

Your email address will not be published. Required fields are marked *

farm equipment transport maine. heavy equipment transport ramsey mn.