Lesson 1 of 7

The Compiler Pipeline

Most , from GCC to the TypeScript checker, follow the same sequence of stages. This lesson gives an overview of each stage so the rest of the track has a map.

Text
source text
   |
   v  LEXER        characters -> tokens ("words")
   v  PARSER       tokens -> syntax tree (AST)
   v  SEMANTIC     names resolved, types checked
   v  IR           tree -> intermediate representation
   v  OPTIMIZER    IR -> better IR (most of the speed-up)
   v  CODEGEN      IR -> assembly / bytecode / JS / ...
   |
   v  output program

Front end = understand the source (lexer..semantic).
Back end  = produce the target (optimizer..codegen).

The front/back split saves a lot of work: LLVM is a shared back end, so Rust, Swift, Clang, and Julia each wrote only a front end and got strong optimization for many CPUs. Same trick in reverse: one front end can target many back ends — that's how the same C code compiles for x86, ARM, and .

This isn't just for 'real' compilers. A linter is a front end that reports patterns instead of generating code. A formatter parses the code and prints the tree back out neatly. TypeScript's compiler type-checks and then emits JavaScript — a compiler whose target language is another high-level language (a 'transpiler'). Syntax highlighting in your editor is a lexer running on every keystroke.

Common question

Does a compiler translate a program line by line?

A compiler is a series of translations. The lexer turns characters into tokens; the parser turns tokens into a tree that captures the structure; semantic analysis resolves names, checks types and annotates the tree; optimisation rewrites an intermediate form into a cheaper equivalent; code generation outputs instructions for the target machine, and the linker the pieces.

Knowing the stages pays off every day. It explains which errors show up when, why one missing bracket produces a flood of nonsense, what a is showing you, and why the code that runs can look quite different from the code you wrote while still computing the same result.

Source: The classic compiler stages are lexing, parsing, semantic analysis, optimisation and code generation. Each one needs the complete output of the stage before it.

Common mix-ups

Each of these sounds reasonable. Think about why it is wrong, then open the explanation.

  • The source is read top to bottom, so translation must happen the same way.

  • An interpreter is just a slow compiler.

  • Syntax errors and type errors are found at the same stage.

Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.