This post is tightly linked to Chapter 28 and Chapter 31. We keep enforcing that computers only understand binary or machine code. However, 4GL programming languages keep getting created. In this post we will delve into how programming languages get translated into machine code.
You might be thinking, "oh dear, why do I need to know this?" but from a developer's perspective it is worth giving this topic a chance. Let us kick this off with a fun fact...
Why are there so many programming languages? To put it very simply, a programming language is nothing more than a tool to solve complex problems. It is impossible to create a single tool that is good for everything. There are many different problems that need solving, so we need different tools. A programming language tends to be known for certain features and characteristics that make it especially suitable for specific tasks. For example, Java is a great choice for building games and solving real-world business problems, but Haskell, despite it being so old and so raw, is still used in research and academia to make new headways in Artificial Intelligence.
Source Code and Executable Code
When we write a program in our preferred programming language the code that we produce is called source code. On the right is a screenshot of our code repository in VS Code.
Not quite sure if you noticed but when we run our programs in class VS Code displays a small toaster notification that says "Compiling...". Computers do not really understand Java (or any other programming language), only humans understand Java. Therefore the source code needs to be translated into executable code i.e. the binary language that the computer understands!
This is not a simple task, one of the greatest challenges is to preserve the semantics of our program. Semantics refers to the true meaning and context of what has been written in the programming language.
I think we can easily relate to this because even in human situations, translation from one language to another between human languages can be challenging. Most of us are bi-lingual and when we try to translate between one language and another we are careful to not change the meaning! Otherwise we can get unwanted reactions like...
Assembler
This was covered in detail in Chapter 31. For your convenience, we have extracted the relevant bit here:
Compilers and Interpreters
Depending on the programming language, we either use a compiler or an interpreter to translate the code in a high-level language into executable code. The key difference between the two lies in the how. For instance, Java is a language that is compiled, if you are interested in knowing what is under the hood you might want to read this post. On the other hand, JavaScript is a language that is interpreted.
Compiler
A compiler is a special piece of software that takes the entire source code, without syntax errors, and translates it all into a separate file before the program runs! If your code contains syntax errors, the compiler will fail and thus your program cannot run at all.
In most IDEs syntax errors are highlighted in red. Since executable code is saved into a separate file you can compile a program in Java without running it; those of you reading this and remember yourself using BlueJ IDE should understand.
Advantages
Disadvantages
Interpreter
An interpreter is a special piece of software, that performs the translation during run-time.
Therefore translation occurs line by line in the order of script execution. If the code contains syntax errors the interpreter catches them during run-time, causing the process to stop abruptly. JavaScript suffers from this because it is a language that gets updated frequently but it is usually not a huge problem. If you open the Dev Tools as you are browsing you might notice JavaScript errors in the console. A lot of syntax errors are usually related to old rules being used, these show up in yellow:
Advantages
Disadvantages
Hot Exam Topic 🔥
This is a very small chapter but surprisingly questions about compilers and interpreters appear common, even in IGCSE exams. Luckily, the nature of the questions do not really vary so we thought it was worth putting a few questions here to help you study.
Q. Explain the difference between compiler and interpreter.
A. Both translate code written in a high-level language to executable code. A compiler translates all the code in one go before run-time. An interpreter performs translation line by line in the order of script execution.
Q. Explain why a programmer would make use of both an interpreter and a compiler.
A. First of all a programmer can use multiple high-level languages to solve a complex problem. In fact, it is very common to find people who know both JavaScript and Java. A programming language tends to have one type of translator. When a programmer is testing code written in Java, a compiler is used. On the other hand, when a programmer needs to test/debug code in JavaScript an interpreter is used.
Q. When is it more useful to use a compiler rather than an interpreter and vice-versa?
A. Using a compiler is better for overall performance. If a programming language is being used to do difficult tasks that require a bit of power, it is better not to waste precious resources on translation. A compiled program can be used and run multiple times. On the other hand, when there is a lot of code doing simple things, like form validation on a website, it might be worth just using an interpreter because as the user is filling in a form he/she does not need other parts of the program. Therefore translation happens as needed.
Comments