Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Compilers and interpreters: two ways to translate

After this lesson: I can say how a compiler and an interpreter differ.

50 min lesson9 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

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

WordWhat it meansExample
Source codea program as people write ithello.c
Machine codeinstructions as numbers a processor runs
Translatora program that turns source code into something a computer can run
Compilera translator that turns the whole program into machine code before it runs
Interpretera translator that reads and runs a program one statement at a time
Tokenone 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

QuestionCompilerInterpreter
When is the program translated?All of it, before it runsA statement at a time, as it runs
Is a program file made?Yes, it can be run again with no compilerNo, the interpreter is needed each time
When is a fault found?Before anything runsWhen that line is reached
Which runs faster?Usually the compiled programUsually slower
In the labgccpython3

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

Command
cd ~/compiler

Shown 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

Command
gcc hello.c -o hello

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Run the program gcc made

Command
./hello

Shown 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

Command
gcc -Wall -c scope.c

Shown 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

Command
printf 'print("start")\nprint(total)\n' > late.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Run it with the interpreter

Command
python3 late.py

Shown 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

Command
echo 'print(7 * 7)' > square.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Split it into tokens

Command
python3 -m tokenize square.py

Shown 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

Command
python3 -m dis square.py

Shown 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?

About this lab and its source: Compilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.