Nib asked
How does a compiler translate code?
Every program you run was first understood by a compiler, stage by stage, long before it ever ran.
7 steps, drawn by Nib. Read it here, or watch him draw it. Updated .
1the puzzle
The chip runs only its own tiny machine instructions, never code like this. A compiler translates between them, and not word by word. It works in stages.
2tokens
Stage one, the lexer, chops the text into tokens: names, numbers, symbols. Spaces vanish. No meaning yet, just clean pieces.
3the tree
Stage two, the parser, builds a tree. Times sits lower than plus, so four times two happens first. The tree holds structure that flat text hides.
4meaning check
Stage three checks meaning. Is a declared? Are these numbers? Errors like a missing variable or a type clash get caught here, before anything runs.
5optimize
Now the tree becomes a plain middle form, one tiny step per line, and the optimizer improves it. Four times two is known already, so it becomes eight.
6machine code
Last, code generation picks real instructions for the chip: load a, add eight, store x. An assembler then turns those into the bytes the chip runs.
7the whole idea
So a compiler never reads word by word. It splits, builds a tree, checks, simplifies, then writes instructions. Real toolchains also preprocess first and link after.
Every program you run was first understood by a compiler, stage by stage, long before it ever ran.