Watch on Instagram

Code Bytes · "How Computers Work" · Episode 8 · Software layers

What a compiler does

A compiler is a translator program. It reads your whole program and turns it into machine code before it runs, so the translated program can then run again and again with no more translating. If it finds a mistake such as a missing bracket, it stops and reports it, and no program is made. Python checks and translates a whole file first too, before running any of it.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

What a compiler is

Your CPU only runs machine code, instructions written as ones and zeros (Code Bytes EP32). You write high-level code instead, so something has to translate it.

A compiler is one kind of translator program. It reads your whole program and turns it into machine code before the program runs. Languages such as C and Rust are usually compiled this way.

The recipe book

Think of a recipe book written in a language the cook can't read. A compiler is like translating the whole book into the cook's language first. Only when the translation is finished does anyone start cooking.

Translate once, run many times

Once the program is translated, the machine code can be run again and again with no more translating. The compiler's job is done. That is one reason compiled programs can start and run quickly.

When there's a mistake

Because the compiler reads the whole program first, it notices some mistakes before anything runs. If it finds one, like a missing bracket, it stops and shows you an error message. It does not make a program, so there is nothing to run until you fix it.

Python does this too (a bit)

The usual Python works a bit like a compiler: before it runs a file, it checks and translates the whole file first, then runs it line by line.

``python print("start") print("end") ``

`` start end ``

The file is checked, then the two lines run in order: start first, then end.

Try it yourself

Open any free online Python editor (search "online Python") and run this three-line file:

``python print("one") print("two") print("three") ``

You should see:

`` one two three ``

Python checked the whole file first, then ran the lines top to bottom.

Good to know

  • Every snippet on this page was run with Python 3 (3.13) and printed exactly what is shown.
  • "Translator program" and the recipe book are simplifications: real compilers also check your code closely and often optimise it (make it faster or smaller) while translating.
  • The usual Python (called CPython) is normally described as an interpreted language. Before running a file it compiles the whole file to bytecode, a simpler set of instructions, and then its interpreter carries that bytecode out. It does not make a machine code file the way a C compiler does.
  • The "check the whole file first" part catches syntax errors, mistakes in how the code is written, like a missing bracket. Other errors only show up while the program is running, and then the earlier lines have already run. For example, this file prints start and then stops with an error on line 2, because nme was never given a value (the last line of the error is shown; Python also prints the file and line above it):

``python print("start") print(nme) ``

`` start NameError: name 'nme' is not defined ``

  • The binary rows in the video are made-up examples, not real instructions.

Answer to the comment question

The last line is missing its closing bracket. How many words get printed?

``python print("start") print("end" ``

Zero. Nothing is printed.

Python checks the whole file before running any of it. It finds the missing ) on line 2 and stops with a syntax error, so not even print("start") runs. Saved as a file and run with Python 3, the output is only the error (its last line shown here; Python also points at line 2 above it):

`` SyntaxError: '(' was never closed ``

Test your knowledge

Quick quiz: What a compiler does

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What does a compiler do?
2Once a program has been compiled, what happens when you run it again?
3What does a compiler do when it finds a missing bracket?
4What does print("start") then print("end") print?

Sources

  • Britannica, "compiler": a compiler is a program that translates a whole program written in a high-level language into machine code before it runs
  • Wikipedia, "Compiler": compilers translate source code into another form, report errors they find, and the translated program can be run many times
  • Python Software Foundation, "Glossary: bytecode": Python source code is compiled to bytecode that the interpreter then carries out
  • Python Software Foundation, "Errors and Exceptions": the difference between syntax errors and errors that happen while a program runs

Video transcript

What does a compiler do before your program even runs?

A compiler is a translator program. It reads your whole program and turns it into machine code, the ones and zeros your CPU runs.

It's like translating a whole recipe book into another language before anyone cooks from it.

Then the translated program can run again and again, with no more translating.

If the compiler finds a mistake, like a missing bracket, it stops and tells you, and nothing runs yet.

Python works a bit like this too: it checks and translates your whole file first, then runs it. Print start, print end. It prints start, then end.

Now the last line is missing its closing bracket. How many words get printed? Comment your answer below!

That's your byte.

More from "How Computers Work"

All "How Computers Work" lessons