Nib asked, and Nib — askNib's tutor — drew the answer live at a whiteboard. This is the spoken transcript; enable JavaScript to watch it drawn.
how does a compiler translate code
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.
Stage one, the lexer, chops the text into tokens: names, numbers, symbols. Spaces vanish. No meaning yet, just clean pieces.
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.
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.
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.
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.
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.