ScholaFly

CS06-07 Computer Science Watch

Compilers, interpreters and assemblers

Watch on YouTube

In this lesson

How to describe how a compiler and an interpreter each translate a high-level program, with worked examples and the mistakes examiners report. AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Software and languages 7 of 8. Full Software and languages playlist: https://www.youtube.com/playlist?list=PLQW0s9-KegDg More at https://scholafly.com

By the end: Describe how a compiler and an interpreter each translate a high-level program - the compiler translates the whole program in one go into a machine code executable and reports errors at the end; the interpreter translates and runs one line at a time, stops at the first error and produces no executable - give the benefits and drawbacks of each and when each is appropriate, and (AQA) describe the assembler as the translator for assembly language.

What it covers

  1. 0:00 Intro: compilers and interpreters
  2. 0:52 Compilers and interpreters: why translators exist
  3. 3:27 The interpreter
  4. 7:14 Compilers and interpreters: how versus why
  5. 11:02 Compilers and interpreters: the assembler

Key words

About this video

Compilers, interpreters and assemblers | GCSE Computer Science | CS06-07

How to describe how a compiler and an interpreter each translate a high-level program, with worked examples and the mistakes examiners report.
AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Software and languages 7 of 8.
Full Software and languages playlist: https://www.youtube.com/playlist?list=PLQW0s9-KegDg
More at https://scholafly.com

Watch first: CS06-06 - https://youtu.be/I7cPHpa44oA
Video code: CS06-07 (search "ScholaFly CS06-07")

#GCSEComputerScience #SoftwareAndLanguages #CompilersAndInterpreters

For teachers
This GCSE Computer Science lesson teaches compilers, interpreters and assemblers. By the end, students should be able to describe how a compiler and an interpreter each translate a high-level program - the compiler translates the whole program in one go into a machine code executable and reports errors at the end; the interpreter translates and runs one line at a time, stops at the first error and produces no executable - give the benefits and drawbacks of each and when each is appropriate, and (AQA) describe the assembler as the translator for assembly language. It works through four worked examples and the mistakes examiners report.

Exam board specification references:
AQA 8525
- 3.4.4
Edexcel 1CP2
- 3.3.2
OCR J277
- 2.5.1

Read the transcript

Imagine someone hands you a novel written in a language you cannot read. Would you rather wait for a translator to write out the whole book in English, or sit beside an interpreter who reads it to you a sentence at a time? Most people pick one straight away as the faster way. The honest answer is that it depends on when you want to start, and how many times you will read it. The full translation keeps you waiting before page one, but then you can read it as often as you like. The interpreter starts at once, but has to do the work again every time you reread. Programs face that same choice, and both answers are in use.

Every translator exists for one reason: the processor executes only machine code, so code in any other language has to be translated before it runs. If that is new, the video on high-level and low-level languages explains why. Picture the language ladder, with the person at the top and the processor at the bottom. Every translator is an arrow pointing down the ladder towards machine code, and no translator ever points up. The compiler and the interpreter are on every course. The assembler is not on every exam board's list, so check yours before you skip it. Here is a short program in a high-level language, five lines long. Line one prints hello, line two sets count to four, line three prints count times two, and line four has a mistake: it uses a variable called total that was never given a value. A compiler is a translator that translates the whole high-level program in one go into machine code. It saves the result as an executable file, a file of machine code the processor can run on its own. While it translates, the compiler collects every error it finds, and it reports them all at the end of translating. Predict this one. The compiler is given the five-line program, with its mistake on line four. Does any line run? No line runs at all. The compiler finishes, flags line four, and makes no executable until line four is fixed, so hello is never printed. Fix line four and compile again. Now the executable is made, and it runs without the compiler and without the source code, the program as the programmer typed it. It can be run again and again with no more translating.

Now the other translator. An interpreter translates and runs a program one line at a time. It reads a line, translates it, carries it out straight away, and then moves to the next line. When it meets an error, it stops at that line and reports it. Over to you. The same five lines are given to an interpreter. What appears before it stops? Hello, then eight, because count is four and four times two is eight. The first three lines run as they are reached, and the interpreter halts at line four, where total has no value. The fifth never runs. An interpreter also leaves no translated file behind. The next time the program runs, every line is translated again, and the interpreter has to be on the computer every time. That is where the book comparison stops. What an interpreter produces is the program running, line by line, and no machine code file is left at the end. Some courses describe the interpreter more precisely. In that model, it does not produce machine code at all. It carries out each statement by calling machine code subroutines, small ready-made blocks of machine code, that are already inside the interpreter itself. Either way, one description works on every paper: an interpreter translates and executes one line at a time, and produces no executable file. One examiner's report says a number of candidates confused interpretation with compilation, and wrote that the source code was translated into machine code. The fix is to keep the outputs apart. A compiler leaves a machine code file behind. An interpreter leaves a program that has run. Now a written question. Describe how an interpreter translates a high-level program, and give two separate points. Pause it there and write both points down. Point one: it reads the source code one line at a time and translates it. Point two: each line is executed straight after it is translated. A third point would also count: it stops as soon as it finds an error, and reports it there. One examiner's report, on a question about how an interpreter works, says weaker answers tended to state that code was translated line by line and the process stopped as soon as an error was encountered. Those two points are true, and they are a start. A fuller description adds that each line is executed straight after translation, and that no executable is produced, so the work is repeated every run.

So far this has been how each translator works. Why you would choose one is a separate matter, and an exam question says which of the two it wants. A compiled program runs faster, because it is already machine code and nothing is translated while it runs. It can also be given to users without the source code, so the code stays private, and they need no translator of their own. The compiler's drawback is the wait. The whole program must be compiled again after every change before any of it can be tested, and all the errors arrive together at the end. An interpreter's benefit is speed of testing. The program starts at once, and it stops at the line where an error is, so mistakes are quicker to find and fix while the program is being written. Its drawback is that the program runs slower, because every line is translated again each time it runs. Anyone who runs it needs the interpreter, and gets the source code too. A different question now. Which of these describes how a compiler works, rather than why you would choose it? A, it produces programs that run faster. B, it translates the whole program in one go into an executable file. C, it keeps the source code private. B is the one. A and C are benefits, the reasons you would choose a compiler, while B says what the compiler does. One examiner's report on a question asking how these translators work says a good number of students did not answer the question asked, and compared the advantages and disadvantages of compilers and interpreters instead. The fix is to read the command first. If it asks how, describe what the translator does. If it asks why, or when, give the benefits. Another examiner's report records the most common correct description of a compiler: the whole source code is translated in one go, and errors are reported at the end. If you had those two points ready, you already have the answer many students reach. Now a scenario. A developer called Priya is writing a new app. Explain why she might use an interpreter while she is writing and testing it, and a compiler when she is ready to give it to customers. Write both reasons down before I show you mine. While she is still testing, it runs her code straight away and halts where a mistake is, so she can find and fix it quickly. When she gives it to customers, the compiler makes an executable that runs faster, needs no translator on their computers, and keeps her source code private.

The third translator is the assembler, the one not on every course. An assembler translates assembly language, the low-level language written in mnemonics, into machine code, one assembly instruction into one machine code instruction. Mnemonics are short code words that stand for machine code instructions, and the video on machine code and assembly language covers them. Each translator has its own input. The compiler and the interpreter take high-level code. The assembler takes assembly language, and nothing else. Machine code needs no translator at all. Now a matching question. Which translator leaves each of these behind: a machine code executable file, one machine code instruction per line of assembly, or no translated file at all? The compiler leaves an executable file. The assembler leaves one machine code instruction for each line of assembly. The interpreter leaves no translated file at all. One examiner's report says the multiple choice question on program translators was well answered. Picking out the right translator is something students tend to manage. Saying what a translator does is harder, and a different report shows it. It says many students thought an assembler turned high-level code into low-level code, or turned low-level language into high-level language. The fix is to tie each translator to its input. The assembler only ever takes assembly language, and every translator points down the ladder towards machine code.

Now put the translators to the test, one question at a time.

First, a program is compiled once and then run a hundred times. How many times was it translated? Once. The executable is already machine code, so each of the hundred runs needs no translating.

Now a fresh one. An interpreter runs a program with an error on line seven. What happens to lines one to six, and to line eight? Lines one to six run, one at a time. The interpreter stops at line seven and reports the error, so line eight never runs.

Now the choice. Which translator suits a developer who is still testing, and which suits a finished program for customers? The interpreter while testing, because errors show at the line where they happen. The compiler for customers, because the executable runs fast without a translator or the source code.

Now the last. What does an assembler take in, and what does it produce? It takes in assembly language and produces machine code, one machine code instruction for each assembly instruction.

So the translated book is the compiler: you wait once, and then read as often as you like. The interpreter beside you is the interpreter program: you start at once, and pay again on every reading.

If this one's yours, give it a thumbs-up. If it is still settling, give it a day and look again, starting with the five-line program.

Next in the chapter is The IDE. Hopefully that's next in your feed, but if not, search for ScholaFly C S oh six, oh eight.

Every other video is on scholafly.com too, easy to search by video code or by keyword. Whatever you're revising, go there and have a look, or, watch the next video.

Related terms

For: AQA GCSE 8525, Edexcel GCSE 1CP2, OCR GCSE J277

On the specification

BoardSpecStatement
AQA GCSE 85253.4.4Classification of programming languages and translators
Edexcel GCSE 1CP23.3.2Explain how an interpreter differs from a compiler in the way it translates high-level code into machine code.
OCR GCSE J2772.5.1Languages
For teachers

This GCSE Computer Science lesson teaches compilers, interpreters and assemblers. By the end, students should be able to describe how a compiler and an interpreter each translate a high-level program - the compiler translates the whole program in one go into a machine code executable and reports errors at the end; the interpreter translates and runs one line at a time, stops at the first error and produces no executable - give the benefits and drawbacks of each and when each is appropriate, and (AQA) describe the assembler as the translator for assembly language. It works through four worked examples and the mistakes examiners report.