Written once, run on millions of phones
A game for a phone is written by people, in words they can read. The phone's processor cannot read those words. It only runs its own instructions, which are numbers.
Something has to translate the game first. There are two main ways to do it, and you can see both on your lab machine.
By the end of this lesson
Note
I can say how a compiler and an interpreter differ.
Key words
| Word | What it means | Example |
|---|---|---|
| Source code | a program as people write it | hello.c |
| Machine code | instructions as numbers a processor runs | |
| Translator | a program that turns source code into something a computer can run | |
| Compiler | a translator that turns the whole program into machine code before it runs | |
| Interpreter | a translator that reads and runs a program one statement at a time | |
| Token | one word of a program, such as a name, a number or a sign |
The cells students fill in are left blank in this preview.
Three kinds of translator
An assembler turns assembly into machine code, one instruction for each line.
A compiler reads the whole program first. If anything is wrong it stops and reports it, and nothing runs. If all is well it writes machine code you can run again and again.
An interpreter reads one statement, runs it, then reads the next.
A compiler and an interpreter side by side
| Question | Compiler | Interpreter |
|---|---|---|
| When is the program translated? | All of it, before it runs | A statement at a time, as it runs |
| Is a program file made? | Yes, it can be run again with no compiler | No, the interpreter is needed each time |
| When is a fault found? | Before anything runs | When that line is reached |
| Which runs faster? | Usually the compiled program | Usually slower |
| In the lab | gcc | python3 |
The cells students fill in are left blank in this preview.
Before you start
Note
Press Start the lab on the bar at the top and wait for the machine to say it is running. Each Run button types its command into the machine's terminal, one after another.
Go to the compiler folder
cd ~/compilerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Compile hello.c into a program
gcc hello.c -o helloShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Run the program gcc made
./helloShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Compile a program with a fault
gcc -Wall -c scope.cShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
An interpreter finds a fault late
gcc read all of scope.c and refused it. It made no program at all.
Now write a Python file of two lines. The first prints a word. The second uses a name, total, that was never given a value. Watch what Python does.
Write a two-line Python file
printf 'print("start")\nprint(total)\n' > late.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Run it with the interpreter
python3 late.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A compiler works in stages
Lexical analysis splits the characters into tokens and throws away spaces and comments.
Syntax analysis checks the tokens against the language's grammar and builds a tree of the program. Code generation turns the tree into machine code, and optimisation makes it smaller or faster.
Write one line of Python
echo 'print(7 * 7)' > square.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Split it into tokens
python3 -m tokenize square.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think an interpreter never translates anything. Python first turns your file into bytecode, simple instructions for its own machine. The next Run shows it. Look for 49: Python worked out 7 × 7 before running anything.
Show the bytecode
python3 -m dis square.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Going further: a calculator's lexer
The lab's calc is a calculator with a lexer made by flex and a parser made by bison. Press Open terminal, type make, then echo "7 @ 2" | ./calc.
It prints two lines. Which line came from the lexer and which from the parser? How can you tell?