- [Craig] In this video, we're going to be looking at the characteristics of compilers and interpreters.
(uplifting piano jingle)
On the screen now is a summary diagram
which shows how we translate low- and high-level language source code
into machine code or binary.
Now, it should be noticed that this diagram is an abstraction,
so some of the complexity has been taken away here,
but it's certainly sufficient enough knowledge for GCSE.
So assembly code,
that's low-level language code,
is translated into machine code using an assembler.
Now we've shown you that here for completeness,
but you don't actually need to know how an assembler works at GCSE level.
High-level language source code can either be translated into machine code
using an interpreter
or a compiler.
So let's take a look at the difference.
So here we're going to look at an example of compiler.
Now, I've got a short program at the top here, and I've deliberately got an error in line two.
Line two should say print, so that's a syntax error.
I've put P-R-I-N and I've gone to run that program.
And you can see the output at the bottom there.
You can see we have an error on line two, which is preventing our program from running.
This error needs correcting before the program will run, as shown here.
And this is the essence of a compiler.
It won't run unless all the errors in the code are corrected first.
It tries compiling the entire program before executing it.
So this is exactly the same program as the one we just saw in Python.
However, this time we've written it in a very early high-level language called BBC Basic.
Now this is interpreting the language.
So let's see what happens when I run it.
Well again, I've got an error here deliberately,
in line 30, it says N-X-T and it should say next,
N-E-X-T.
But you can see the program has partially executed
The first line, line 10, has executed and been run.
And the second line, line 20, has executed and run.
And indeed, it's printed out the contents of the variable counter to the screen.
It's then reached line 30 and it's discovered a mistake.
So with an interpreter,
we are translating each line of code and then running it before moving on to the next.
So I can correct the syntax error in line 30.
Let's just do that now.
And then if I run it, you can see the program runs exactly the same as the earlier version did with the compiler.
So just to recap then, a compiler translates source code from high-level languages into object code
and then into machine code ready to be processed.
The whole program is translated into machine code before it's run.
The advantages here is there's no need for further translation at runtime.
The speed of execution is faster and the code is usually optimised.
It also keeps the original source code secret.
The disadvantages is the source code is easier to write in a high-level language,
but the program will not run with syntax errors,
which can make it more difficult to write the code.
The code would need to be recompiled every single time you made a single change to the source code.
And it's designed for a specific processor, that's the finished compiled code.
With an interpreter,
we translate the source code from high-level language into machine code,
ready to be processed by the CPU, just like we do with compiled code.
But this time, the program is translated line by line as the program is running.
The advantage is it's easy to write the source code because the program will always run,
stopping when it finds the first syntax error.
Code does not need to be recompiled when the code is changed,
and it's easier to try out commands when the program has paused after finding an error.
This makes interpreted languages very easy for beginner programmers to learn to write code.
The disadvantage is translation software is needed every time the program is run.
This causes a slower speed of execution over compiled machine code.
The code isn't optimised and the original source code is needed.
(uplifting piano jingle)