2.5.1 Languages
Programs are written in programming languages. The computer's processor cannot understand most of them directly. This topic explains the two types of language, why a translator is needed, and how the two kinds of translator work.
High-level and low-level languages
High-level languages
Examples are Python, Java, C# and the OCR Exam Reference Language you use in this course. One line of a high-level program, such as print("Hello"), may need many instructions from the processor.
Features of high-level languages:
- •Use words and symbols that people understand, such as
if,whileandprint. - •Not tied to one type of computer. The same program can usually run on different processors, as long as there is a translator for each.
- •Have built-in features such as loops, functions and string handling, which make programs quicker to write.
- •Easier to test and to fix because the code is readable.
Low-level languages
The most important low-level language to know is machine code. This is the binary instructions (1s and 0s) that the processor runs directly. A low-level language gives the programmer control of the hardware, such as the exact memory used.
Features of low-level languages:
- •Each instruction is very basic and often does one small job.
- •Written for one type of processor. A program for one type of processor does not work on another type.
- •Can run very fast and use little memory because the programmer has detailed control.
- •Hard to read, write and fix, so programs take much longer to write.
Comparing the two
| High-level | Low-level | |
|---|---|---|
| Readability | Easy for humans to read | Hard to read |
| Time to write | Quicker | Slower |
| Needs translating | Yes, to machine code | Machine code does not. |
| Works on different computers | Usually yes | No, specific to one processor |
| Control of hardware | Less control | Very detailed control |
| Speed and memory use | Often slower and larger | Often faster and smaller |
Purpose of each
High-level languages are used for most software: websites, apps, games and business systems. Low-level code is used where speed, size or direct control of the hardware matter, for example in the small programs inside machines and devices (embedded systems) and in parts of an operating system.
Why translators are needed
The processor can only run machine code. A program written in a high-level language must be turned into machine code before the computer can run it.
The two types of translator you must know are the compiler and the interpreter. The code a programmer writes is called the source code. The machine code a compiler produces is called the object code (also called the executable).
Compiler
How it works:
- 1.The programmer writes the program in a high-level language.
- 2.The compiler checks and translates all of the code.
- 3.If there are errors, the compiler reports them and does not produce the finished program.
- 4.If there are no errors, an executable file is made. This file can be run again and again without the compiler or the source code.
| Benefits of a compiler | Drawbacks of a compiler |
|---|---|
| The finished program runs fast because it is already machine code | The whole program is compiled, so there is a delay before you can run it |
| The program can be run without the compiler or the source code | Errors are reported only after the whole program has been translated, so there may be a long list |
| The source code is hidden. People get only the executable and cannot easily copy or change it | The machine code only works on one type of processor, so it must be compiled again for a different one |
Interpreter
How it works:
- 1.The interpreter reads the first line, translates it and runs it.
- 2.It then moves on to the next line, and so on.
- 3.If it finds an error, it stops at that line and reports it.
| Benefits of an interpreter | Drawbacks of an interpreter |
|---|---|
| Good for testing: you can run the program at once and a mistake is reported at the line where it happens | The program runs more slowly because each line must be translated every time it is run |
| Easy to fix a mistake and run again straight away | The interpreter and the source code must be present to run the program, so the code is not hidden |
| Can run on any type of computer that has the right interpreter | Errors later in the program are found only when that line is reached |
Comparing compiler and interpreter
| Compiler | Interpreter | |
|---|---|---|
| Translates | The whole program at once | One line at a time |
| Output | A separate machine code file | No separate file |
| Speed when program runs | Fast | Slower |
| Errors | Shown after translating the whole program | Reported at the line where the program stops |
| Source code needed to run | No | Yes |
| Best for | Finished programs that will be given to users | Writing and testing a program |
An interpreter. The student can run the program straight away after each change, and the error is shown at the line where the program stops, so mistakes are easier to find.
A company wants to sell a finished game and does not want customers to see its code. Which is best?
A compiler. The game is turned into an executable that runs fast, and customers receive only the machine code, not the source code.
Exam technique
- •State and give: a short fact. "State one feature of a high-level language."
- •Describe: say how something works. "Describe how an interpreter translates a program" needs one line at a time, run straight away, stop at an error.
- •Explain: give a reason. "A translator is needed because the processor can only run machine code."
- •Compare: write about both, using words such as "whereas" and "however". Give a point for each, for example "A compiler translates all the code at once, whereas an interpreter translates one line at a time."
- •Recommend or choose for a scenario: pick one and give reasons that match the facts in the scenario. If you are asked for a drawback, give one.
- •Speed, size and code readability are good points for low-level versus high-level answers. Hiding source code and needing the translator are good points for compiler versus interpreter.