Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 61 ยท Getting Started

What Is a Compiler?

Learn what a compiler is, how it differs from an interpreter and a transpiler, why compiler design matters, and run a tiny compiler and VM in Python.

Every program you have ever run started life as text. A person typed total = price * 3, and somehow a processor that only understands numbers and electrical signals did exactly that. The thing that bridges the gap is a translator, and the most famous kind of translator is the compiler. A compiler reads a program written in one language (the source language) and produces an equivalent program in another language (the target language), without running it.

This track teaches you how compilers are built, from the first character they read to the last instruction they emit. You will not just read about the ideas. You will build a lexer, parsers, a type checker, an optimizer and a virtual machine in Python, and every one of them runs in the editors on this page. If you are comfortable with Python functions and classes and basic data structures, you have everything you need.

What a compiler does

A compiler is a program whose input and output are both programs. It does not execute the input. It translates it, and the result can be executed later, as many times as you like, without the compiler being present.

flowchart LR
    A["Source program<br/>total = price * 3"] --> B["Compiler"]
    B --> C["Target program<br/>machine code or bytecode"]
    D["Input data"] --> E["Run the target"]
    C --> E
    E --> F["Output"]

Two separate moments in time: compile time (left) and run time (right).

Keeping those two moments apart is the key idea. Everything the compiler does happens at compile time. Everything the program does happens at run time. A compiler can notice that you used a variable that was never declared (compile time) but it cannot know that the user will type 0 and cause a division by zero (run time). A great deal of language design is about deciding what to check early and what to leave for later.

Compiler, interpreter, transpiler

Three words are often mixed up. They describe different strategies, and real systems frequently mix them.

Kind Reads Produces Example
Compiler source code a separate target program, which runs later GCC translating C into machine code
Interpreter source code (or a tree/bytecode) the program's results, immediately a shell reading and running your commands
Transpiler source code source code in another high-level language TypeScript to JavaScript

An interpreter never produces a standalone program. It walks through your code and does what the code says, step by step. This makes starting fast and debugging friendly, but the interpreter has to be present every time you run the program, and decoding the same line again on every loop pass costs time.

A compiler pays a one-time price up front and then runs the result at full speed. It also gets to look at the whole program before anything runs, which is what makes deep optimization and early error detection possible.

A transpiler (also called a source-to-source compiler) is simply a compiler whose target is another human-readable language. Nothing magical separates it from other compilers, which is why this track treats it as one more back end. You will build one in the transpiler project.

The most important thing to know is that the line is blurry. Python is called an interpreted language, yet the standard implementation, CPython, first compiles your source into bytecode and then runs that bytecode on a virtual machine. Java does the same, and then a JIT compiler translates hot bytecode into machine code while the program runs. JavaScript engines such as V8 do something similar. "Compiled" and "interpreted" describe implementations, not languages.

From source to machine code

Here is the classic journey of a C program through a toolchain such as GCC or Clang. This is shown read-only, because the site cannot run a native compiler on this page:

TEXT
main.c  --preprocess-->  expanded C  --compile-->  main.s (assembly)
        --assemble-->    main.o (object code)  --link-->  main (executable)

The "compiler" proper is only the middle part. A preprocessor expands macros and includes headers first, an assembler turns assembly text into machine bytes, and a linker joins several object files and libraries into one executable. In everyday speech "compiling" covers the whole chain.

Notice that the same source can be compiled for different targets. The same C file can become code for an Intel laptop, an ARM phone, or a WebAssembly module in a browser. Modern infrastructure such as LLVM exists to share the middle and back parts between many languages: Clang (C and C++), rustc (Rust) and Swift all hand their programs to LLVM, which optimizes them and produces machine code for dozens of processors.

A first compiler you can run

Let us make the difference between interpreting and compiling concrete with a program small enough to read in one go. The input is an arithmetic expression. The interpreter computes the answer by walking the tree every time. The compiler translates the expression once into a list of stack-machine instructions, which a tiny virtual machine can then run for any value of x.

We borrow Python's own ast module to read the expression for now, because parsing is the job of later lessons.

Python Interpreting versus compiling an expression

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both approaches give the same answers, which is the compiler's main promise: the translated program must mean exactly what the original meant. The difference is where the work happens. The interpreter re-examines the tree on every call. The compiled code was produced once, and the machine only has to execute plain instructions. In a real system the "virtual machine" would be the physical processor, and the instructions would be its own.

Look at the order of the instructions. x * 2 + 1 became LOAD x, PUSH 2, MULT, PUSH 1, ADD. A stack machine wants operands first and the operator last, so the compiler visits the children before the parent. You will meet this idea again in bytecode virtual machines.

Peek inside the real thing

CPython exposes its own compilation pipeline, so you can watch a real compiler work. The tokenize module shows the lexer's output, ast shows the tree, and dis shows the bytecode.

Python Python compiles your code too

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three stages, three different views of the same line. The exact bytecode names change between Python versions (compilers evolve), but the shape stays the same: load two things, multiply, store the result. Later lessons rebuild each of these stages by hand.

A transpiler in miniature

A transpiler is the same idea with a human-readable target. This toy translates a made-up language with a ^ power operator and show statements into Python text, then runs the result.

Python A toy transpiler

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real transpilers need a proper lexer and parser (a string replace would break on "a^b" inside quotes), and that is exactly what the next chapters teach.

Why learn compiler design?

Few people write production compilers, so why study them?

  • You use compilers all day. Knowing what happens between "save" and "run" explains error messages, build times and performance surprises.
  • Compiler techniques are everywhere. Config file readers, query planners, template engines, linters, formatters, code editors, regex engines and data-format parsers all use lexers, grammars and trees.
  • It sharpens your programming. Few topics combine recursion, trees, graphs, state machines and careful testing as neatly.
  • Domain-specific languages are common. Teams regularly create small languages for rules, queries and build scripts, and good ones need these skills.
  • It is a classic exam and interview subject. You will practice that angle in interview questions.

A short history: the first compilers appeared in the 1950s (Grace Hopper's A-0 system and the FORTRAN compiler by John Backus's team), when many people doubted that machine-generated code could be as fast as hand-written assembly. FORTRAN proved them wrong, and decades of theory on grammars and automata followed, which is what the rest of this track builds on.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main difference between a compiler and an interpreter?

    A compiler produces a target program that runs later and on its own. An interpreter executes the source (or a tree or bytecode form of it) right away and must be present at every run.
  2. 2. CPython compiles source code to bytecode and then runs the bytecode. How should we describe Python?

    Whether a language is compiled or interpreted is a property of its implementation. CPython has a compile step to bytecode and a virtual machine that executes it.
  3. 3. Which of these happens at compile time rather than run time?

    Compile time is everything done before the program runs, such as spotting undeclared names. The other options depend on values or events that exist only while the program is running.
  4. 4. What is a transpiler?

    A transpiler (source-to-source compiler) translates from one high-level language to another, for example TypeScript to JavaScript.

Summary

  • A compiler translates a program from a source language to a target language without running it; compile time and run time are different moments.
  • An interpreter executes the program directly; a transpiler is a compiler that outputs another high-level language.
  • Real systems blend the approaches: CPython compiles to bytecode, and Java and JavaScript engines add JIT compilation.
  • A toolchain around a compiler includes a preprocessor, assembler and linker; frameworks such as LLVM share optimization and code generation between languages.
  • Compiler techniques (lexing, grammars, trees, state machines) appear in many everyday tools, not only in compilers.
  • You ran a miniature compiler and virtual machine in this lesson; the rest of the track builds each part properly.

Next: The Phases of a Compiler