Skip to main content

Programming concepts

What is a Compiler?

Definition

A compiler is a program that translates source code into machine code or bytecode before it runs, producing an artifact you execute later.

Also known as: Ahead-of-time compiler

Compiler in more detail

Compilation happens in phases: lexing turns characters into tokens, parsing builds a syntax tree, semantic analysis resolves names and checks types, an optimiser rewrites an intermediate representation into something faster, and a backend emits code for a target architecture. Because all of this happens before execution, the compiler can spend seconds or minutes reasoning about the whole program in ways nothing running under time pressure could.

That upfront work is the trade against an interpreter. You wait for a build, but errors — type mismatches, undefined names, unreachable code — surface before a single line runs, and the emitted code is fast because the analysis is already done. An interpreter starts immediately and reports the same errors only when execution reaches them.

The clean split the textbooks describe barely exists any more. Java and C# compile to bytecode that a virtual machine then compiles again, just in time, using runtime profiles a static compiler could never have; CPython compiles to bytecode and interprets that; JavaScript engines interpret first and compile the hot paths. TypeScript and Babel are transpilers, compiling source to source at the same abstraction level. The useful conclusion is that compiled and interpreted describe implementations, not languages — there is nothing stopping anyone writing a C interpreter or a Python compiler, and both exist.

Compiler example

Ahead of time: build once, run the artifact

  $ gcc -O2 main.c -o app     # source -> machine code
  $ ./app                     # no compiler needed at run time

  $ javac App.java            # source -> .class bytecode
  $ java App                  # JVM interprets, then JIT-compiles
                              # the hot methods to machine code

Errors are caught at different moments

  compiler:    a type error stops the BUILD; nothing ran
  interpreter: the same error appears when execution
               reaches that line, possibly in production

Same language, different implementations
  CPython (bytecode + interpreter)  ·  PyPy (JIT)
  Cython / Nuitka (compile to C)

Compiler — common questions

What is the difference between a compiler and an interpreter?
A compiler translates the whole program before it runs and produces an artifact; an interpreter translates and executes as it goes, with no separate output. Compilation catches errors before execution and runs faster; interpretation starts instantly and is easier to iterate with.
Is Java compiled or interpreted?
Both. javac compiles source to bytecode ahead of time, then the JVM interprets that bytecode and just-in-time compiles frequently executed methods to machine code while the program runs.
What is a transpiler?
A compiler whose output is source code at a similar level of abstraction rather than machine code — TypeScript to JavaScript, or modern JavaScript to an older dialect. It is a compiler by any technical definition; the name just describes where it lands.

Keep reading

Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.

Browse the A-Z glossary