How compilers turn code into programs — Computing, 14–17 years
A program written by a person is not yet in the form a processor can run. Compilers and interpreters bridge that gap, checking code and translating it into actions.
Source code needs a translator
You write source code for people to read, using words such as print or if. A processor understands much simpler instructions, so a compiler translates the whole program before it runs, while an interpreter translates and runs it piece by piece. The result is still the same intended program.
Why do we need translation?
The problem is that humans need readable names and structure, but processors need exact instruction patterns. Early programmers could enter machine instructions directly, but that was slow, fragile and hard to check. Languages and translators were created so people could describe a task clearly without rewriting every processor detail.
From one line to an action
Take the line print(2 + 3). First, the translator recognises print as an instruction to display something. Next, it checks that 2 + 3 is valid and calculates the value 5, or creates instructions for the processor to calculate it. Finally, the running program sends 5 to the screen.
A compiler is not a magic fixer
A common mistake is to think that code which compiles must be correct. This is reasonable because the translator has checked the grammar and some types. But print(2 - 3) may compile perfectly while producing an unexpected result, and a program can run while solving the wrong problem. Translation checks form, not every intention.
Where translators matter
Every time you install a compiled app or run code in a browser, some translator has helped prepare it. A compiler can optimise a game so it responds quickly, while an interpreter makes a small script easy to try on many systems. The choice affects speed, portability and how quickly a programmer sees an error.
Keep exploring
Other languages
Loading MyLeoNes™…