Programming languages can seem confusing and foreign to a beginner. However, many coders will confirm that learning new programming languages is one of the easiest aspects of software development.
It doesn’t mean it has always been this way. Programmers have not always had programming languages that are as easy to understand as the ones available today. How did we translate computer language into something that humans could understand?
What is Python Interpreter?
Binary is the only language that computers can truly understand and is completely foreign to what we are used to. Programs written in “higher-level” languages, which are easier to read by humans, turn into a series 1’s and zero’s that computers can understand. A python translator works the same as any other language interpreter. It converts the Python language that a coder inputs into a computer to binary so that the computer can understand what the coder is saying. There are two main ways it does this: compiling or interpreting.
The compiler’s job is to convert a program into instructions that are specific to a particular type of computer. This could be Windows, MacOS or a Raspberry Pi. Each one has different hardware that can respond to different instructions. A compiler can only target one of these computers at a given time.
Interpreters are a bit more different. They don’t create another program to run, but they do run programs. It may seem odd that there are two ways this can work. Compilers do this extra step before running the code, while interpreters run it directly. Interpreters can run Python code, which is a great example.
Although these two methods are very similar in their goal (to get programs to run), the way they do it is quite different. Performance is the key difference. A compiler can target a specific hardware set, so it can more intelligently apply optimizations that make the program more efficient. Although these are two different techniques they both require the same steps for a compiler or an interpreter to convert human-written code into a program that a computer can run.
How to add Python interpreter
The process can be broken down into three sections: the frontend, middle and backend. The front-end deals with the program’s structure, while the middle focuses more on the meaning. The back-end deals with the lower-level parts.
Scanner is the first step in getting a program ready for execution. Some things that people put in their programs are not necessary for code to be readable. The scanner’s job is to remove all this and convert each part of the program into smaller pieces called “tokens”. These tokens look just like words and punctuation in spoken and written languages. The following Python code is an example:
def foo(a), x = 2
This will be transformed into a series tokens, something like:
The importance of lines and spaces in Python is the same as in English writing (e.g. poetry). Therefore, the scanner won’t remove them since they have meaning. The scanner has removed all unnecessary information. Now, the process can move on to parsing.
Like all languages, programming languages have a grammar. This defines their structure, order, meaning, and the meaning of every word and symbol. This might look like this in English:
This phase’s primary purpose is to create an Abstract Syntax Tree (AST). This is a way to present the program in a format that is easy to understand for the interpreter. While the AST is being constructed, the program will be checked to ensure it adheres to the grammar required by the language. This could be transformed into a tree that looks like the one in the previous example.
If the program passes all of the tests at the front-end it is time for the middle.
Semantic Analysis & Intermediate Representations
The next step is to verify that the program is logical. Is it trying to add things that cannot be combined? Is it able to compare things that don’t make sense? The interpreter will also create a way to locate variables used by the program during this phase. When it encounters a statement, the interpreter will know exactly where to find it.
This new table is often called a symbol-table. Now it’s time to create something that the back end can use. This is commonly called an intermediate representation (IR), as it is the last part of the middle section. This IR has the advantage that almost any language can be translated into it, and the back-end can still use all its power. This is where it all begins