Originally posted on makeuseof.
There are many ways to distinguish programming languages. For a start, they fall into different paradigms: functional, object-oriented, and more besides.
You can also classify a programming language by its translation method, something that has a major effect on a language’s performance. Compiled programming languages are usually faster than interpreted ones. So, in situations like game development that demand speed, developers tend to use compiled languages.
What Is Translation?
The translation process converts code written by a programmer into machine code that a computer can execute. Machine code is a type of low-level language, which has ones and zeros. So, what the translator does is converts the high-level code you create in a programming language to machine code.
Without translators, you would have to code in machine language. Each high-level programming language that you know uses one of three translation methods: a compiler, an interpreter, or a hybrid of the two.
What Is a Compiler?
A compiler is software that converts source code written in a high-level language into low-level code for execution.
The diagram above represents a compiler in its most basic form. The compiler has several phases. Each phase transforms the code from one state to another. The goal of each compiler phase is to create an output that is easier for the succeeding phase to manipulate. The general structure of a compiler is as follows:
- Scanner: this phase takes a stream of characters and groups them into tokens that represent identifiers, string literals, and so on.
- Parser: this phase groups the tokens based on the grammar of the source programming language. It creates an abstract syntax tree which is a collection of expressions that make up the program.
- Semantics: this phase conducts a semantic analysis on the abstract syntax tree (AST). It uses the rules of the source language to add meaning by assigning types to the AST expressions and checking their validity. The AST then becomes an intermediate representation.
- Intermediate Representation (IR): this phase converts the original program’s source code to machine code. It produces a simplified version of assembly code. The IR uses one or more optimizers to improve the IR code and to collect information for the machine it is running on. An optimizer can make a program more efficient, faster, or even smaller.
- Code Generator: this phase consumes the optimized IR code and converts it into machine code.
Which Programing Languages Use Compilers?
Some popular compiled programming languages include:
One advantage to using a compiled language is that it identifies errors during compilation. This lets you fix such bugs, and then try to compile the program again. Compiled languages are less likely to fail once they begin execution. These languages will not even generate a program to run if the source code has syntax errors. But semantic errors, and other forms of run-time bug, will get past it.
Compiled languages also execute very quickly after they compile.
What Is an Interpreter?
An interpreter is a program that translates and executes a single line of code at a time. This process repeats until the interpreter arrives at the final line of code in the given program or script.
As you can see from the diagram above, an interpreter takes two inputs. First, it takes the entire source code (program). It then reads the first line of the program (as an input), translates, and executes it. If that line executes correctly, it moves on to the next line in the program or script.
Unlike a compiler, an interpreter does not translate an entire program into machine code. Instead, it parses and analyses a given line of code before executing it. An interpreter should begin running a program—especially a larger one—before a compiler has even finished translating it.
Which Programing Languages Use Interpreters?
Some popular interpreted programming languages include:
Although an interpreter begins executing code faster, it will still fail if it encounters an error. As the programmer, you would need to fix such an error and restart the program. This event occurs every time the interpreter encounters a new error. Some errors may lie dormant if they relate to a rare set of circumstances. In such cases, testing is more important than ever.
Interpreters are usually easier to develop than compilers, and their programs are more portable by design.
What Is Hybrid Translation?
Hybrid translation employs a compiler and an interpreter. Hybrid translation compiles high-level source code to a lower-level form, such as bytecode. It then uses an interpreter to run that bytecode.
Hybrid translation may differ from one programming language to another but will use this general structure. One of the more popular programming languages that use hybrid translation is Java. The Java compiler translates its source code into Java Virtual Machine (JVM) bytecode. The interpreter then translates the JVM bytecode to machine code.
Which Programing Languages Use Hybrid Translation?
Some popular hybrid programming languages include:
- Visual Basic
With hybrid translation, you get the best of both worlds. Compiling the code first allows you to resolve bugs at the earliest opportunity. The bytecode that hybrid compilers create is easier to interpret than a high-level program source code.
The Value of Knowing the Different Translation Methods
You should understand the specific translation method that a language uses, particularly if you’re using it for a new project. A language translation method is a major part of a programming language’s identity. It can affect how you distribute your program and how users will run it.
Each translation approach has its own merits. Top programming languages like C++, Python, and Java all use different translation methods. Alongside a language’s paradigm, its translation method is one of the most important traits you should be aware of.
There are several paradigms that a programming language can use. Most of the top programming languages are multi-paradigm languages; they support the use of two or more distinct paradigms. Three of the most popular paradigms are imperative, object-oriented, and functional programming.