How Does a Compiler Turn Code into Instructions a Chip Can Run? | How It Works
On this page
What is the short answer?
A processor can carry out only machine code: instructions written as patterns of bits in its own instruction set. People write source code, text made of names, numbers and symbols. A compiler is a program that translates source code into machine code before the program runs.
It works in stages. It splits the text into tokens, arranges them into a syntax tree by the language's grammar, checks that the program makes sense, rewrites it in a simpler intermediate representation, improves it, and writes instructions for one target processor.
Definition: A compiler translates a program in a high-level language into an equivalent sequence of machine instructions, after first checking the program for errors.
An interpreter instead carries out a program statement by statement as it runs. A just-in-time compiler mixes the two.
What are source code and machine code, and why does each processor need its own?
A lecture page from the compilers course at the University of Texas at Austin describes a computer as a fast calculator joined to a large memory. Machine instructions move data between the two or name an operation. Machine language is the only language the computer runs directly, but its numeric codes and fixed memory addresses make it hard for people to write.
The rules for those instructions form an instruction set architecture (ISA). A University of Illinois course text of September 2024 calls the ISA the interface between hardware and software: what a programmer may ask the hardware to do. Machine code is the compact binary form of its instructions. The text names x86-64 and ARM as the two most prominent families.
Cornell University's systems course explains, in its spring 2026 notes, that each ISA gives its own meaning to strings of bits. So every program must end up as machine code for the processor that will run it.
How does a compiler break the text into tokens?
The first stage is lexical analysis, or scanning. The UT Austin notes describe the lexer grouping input characters into meaningful units, like the words of a sentence, and storing each as a token. It also turns numbers written as text into binary.
MIT's course Computation Structures (6.004), in its spring 2017 lecture on compilers, says the scanner produces a sequence of tokens, each labelled with the type of that piece of text: in the example later on this page, names, numbers and operators. Spaces are removed, because they only separate tokens.
MIT's lecture says the scanner also reports illegal tokens: text that fits no kind of token the language allows.
How does the compiler build a tree from the grammar?
Tokens alone are a flat list. The parser gives them structure. The UT Austin notes describe parsing as assigning structure to a string of words according to a grammar, much like diagramming a sentence at school. The result is a parse tree.
A grammar is a set of rules saying how the pieces of a language combine: for example, that an expression can be two smaller expressions joined by an operator. MIT's lecture says the parser turns the tokens into a syntax tree that holds the program's structure for the later stages.
The tree settles what a flat list leaves open. Multiplication comes before addition, so the grammar places a multiplication lower in the tree, where it is worked out first. If the tokens fit no rule, the parser reports a syntax error.
How does the compiler check meaning, and why does it stop on an error?
A program can follow the grammar yet make no sense. Semantic analysis checks its meaning. MIT's lecture gives examples: numeric operations need numeric operands, text cannot be stored in a variable meant for whole numbers, and a variable should not be used before it has a value.
For this the compiler keeps a symbol table, a record of every name and its declared type. The UT Austin notes add that when a whole number meets a real number in a sum, the "higher" type wins: the whole number is converted, a step called coercion, and the result is real.
MIT's lecture says a compiler first checks that the program is correct, then generates instructions if it passes. These checks happen at compile time, before anything runs. Two illustrative mistakes:
- Syntax error: total = price 3 + tax has nothing joining price and 3, so no grammar rule fits.
- Type error: if count is declared to hold whole numbers, count = "seven" stores text in it. The checker finds the clash in the symbol table.
Either way, the UT Austin notes say, an error stops code generation.
How does the compiler improve the program and write machine code?
Next, MIT's lecture says, the compiler rewrites the tree as an intermediate representation (IR). A common IR is a control flow graph: basic blocks, runs of operations carried out as a unit, linked where the program jumps or branches.
Optimisation rewrites the IR to cost less. The UT Austin notes set the rule: the new program must give the same output for every input while using less time or space. MIT's lecture names several methods:
- Constant folding: working out an expression made only of constants once, while compiling. The UT Austin notes give 2 × 3.14 becoming 6.28.
- Constant propagation: using the value of a variable that always holds one value.
- Dead code elimination: removing assignments to variables that are never used.
- Common subexpression elimination: computing repeated arithmetic only once.
The gain varies: the UT Austin notes say not everything can be optimised, and the payoff depends on how often the code runs. Last, the code generator writes instructions for the target ISA and assigns values to registers, small stores inside the processor.
Courses elsewhere teach the same stages. Besides UT Austin and MIT, IIT Kanpur's Compiler Design course in India, in its 2023-24 second semester, ran from lexical and syntax analysis through semantic analysis and intermediate representations to code generation and optimisation.
How is a compiler different from an interpreter or a just-in-time compiler?
An ahead-of-time compiler translates the program in one phase and the program runs later, as a fall 2025 lesson in Cornell University's advanced compilers course describes it. MIT's lecture says an interpreter instead carries out each statement as it processes it, so a statement in a loop may be processed many times.
MIT's course weighs the two. A compiler pays the processing cost once, so the program runs faster, but compiling, running and fixing takes longer. An interpreter keeps flexibility while the program runs, at the price of speed.
A just-in-time (JIT) compiler translates code when it is time to run it, Cornell's lesson says. A typical design first interprets, which is quick to start but slow to execute, and counts how often each function or loop runs. Code that runs often, hot code, is then compiled.
A JIT can use facts gathered while the program runs. It protects its guesses with checks called guards; if one fails, it returns to the interpreter, a step called deoptimisation. The lesson adds that no language is compiled or interpreted by nature.
When and where did compilers begin?
Three dated steps from named sources show how compilers began:
- 1952, the United States. The Computer History Museum in the United States says Grace Hopper completed A-0 for the UNIVAC I. It let users give instructions in English-like words instead of numbers, and the museum says it had several features of a modern compiler.
- 1954, the United Kingdom. The University of Manchester's computing history pages say a Mark 1 Autocode by Tony Brooker was made available that year as an easy-to-learn scientific programming language. Most programmers preferred it, the page says, though it made the machine much slower.
- 1957, the United States. IBM's history page says its Mathematical Formula Translating System, Fortran, debuted that year, and IBM soon offered the first Fortran compiler to users of the IBM 704. IBM says John Backus's team built the first optimising compiler, whose code ran nearly as fast as code written by hand.
How does this work with real numbers?
Both examples are illustrative: the code is in no real language, the machine is imaginary and the values are made up.
How does one line become four machine steps?
Take the illustrative line total = price × 3 + tax.
- Tokens: the scanner finds 7: total (name), = (assignment), price (name), × (operator), 3 (number), + (operator) and tax (name).
- Tree: = sits at the top, with total on its left and + on its right. The + joins × (holding price and 3) and tax. Because × sits lower, it is worked out first.
- Instructions: for an imaginary machine with one working register: LOAD price, MUL 3, ADD tax, STORE total. That is 4 instructions from 7 tokens.
- Check: with illustrative values price = 20 and tax = 5, the register holds 20, then 20 × 3 = 60, then 60 + 5 = 65, stored in total.
How many multiplications does constant folding save?
Suppose the illustrative line seconds = 60 × 60 × 24 sits inside a loop that runs an illustrative 1,000 times.
- Without folding: each pass does 2 multiplications: 60 × 60 = 3,600, then 3,600 × 24 = 86,400. Over the loop, 2 × 1,000 = 2,000 multiplications.
- With folding: the compiler does the 2 multiplications once, while compiling, and writes 86,400 into the machine code. The running program does none.
- Saved: 2,000 − 0 = 2,000 multiplications at run time, with the same result.
Where can the student go deeper in World 101?
This page follows one line of a program from text to machine instructions. The World 101 subject Computing Fundamentals covers the wider field: how computers store and process information, and how their hardware and software work together as one system.
Three related subjects take these ideas further, each from its own side. Software Engineering looks at how large programs are designed, built, tested and kept working by teams. Algorithms & Computation studies the step-by-step methods that programs follow, and what can and cannot be computed efficiently. Computer Engineering turns to the hardware: how processors are designed to carry out an instruction set, the other side of the agreement a compiler writes for.
Sources
- G. S. Novak Jr., University of Texas at Austin, CS 375 Compilers lecture notes; read 17 November 2026.
- C. Terman, MIT OpenCourseWare, 6.004 Computation Structures, Spring 2017, Lecture 11 "Compilers"; read 17 November 2026.
- L. Tychonievich, University of Illinois, CS 340 text "ISAs and Assembly", 5 September 2024.
- Cornell University, CS 3410 notes, "Processors", Spring 2026; read 17 November 2026.
- Cornell University, CS 6120 Advanced Compilers, Lesson 12 "Dynamic Compilers", Fall 2025; read 17 November 2026.
- Computer History Museum, Timeline of Computer History, Software and Languages: A-0 (1952); read 17 November 2026.
- University of Manchester, School of Computer Science, "Mark 1 Story" (Autocode, 1954); read 17 November 2026.
- IBM, History, "Fortran" (debut and IBM 704 compiler, 1957); read 17 November 2026.
- S. Biswas, IIT Kanpur, CS 335 Compiler Design, semester 2023-24-II; read 17 November 2026.
Common errors and misconceptions
- Misconception: A language is compiled or interpreted by its nature. Correct: Cornell's compilers course says no language has a built-in property that makes it one or the other.
- Misconception: A compiler runs the program. Correct: An ahead-of-time compiler only translates it; the machine code it produces runs later.
- Misconception: Machine code built for one processor runs on any processor. Correct: Each instruction set gives its own meaning to bits, so a program must become machine code for the processor that runs it.
- Misconception: Optimisation changes what a program does to make it faster. Correct: An optimised program must give the same output for every input; only its cost in time or space may change.
- Misconception: An interpreter translates the whole program before running it. Correct: It carries out each statement as it processes it, so a statement in a loop may be processed many times.
Key takeaways
- A processor runs only machine code in its own instruction set, so the source code people write must be translated before it can run.
- A compiler works in stages: lexical analysis into tokens, parsing into a syntax tree, semantic checks, an intermediate representation, optimisation and code generation.
- The parser follows the language's grammar, and the semantic checks use a symbol table to catch errors such as storing text in a whole-number variable.
- A compiler reports syntax and type errors at compile time and generates instructions only for a program that passes its checks.
- Optimisations such as constant folding and dead code elimination must keep the output the same while cutting the cost in time or space.
- Interpreters carry out statements as they go; just-in-time compilers start by interpreting, then compile hot code using facts gathered while the program runs.
Test yourself
What does lexical analysis produce?
A sequence of tokens, each labelled with its type, such as a name, a number or an operator.
Why does the compiler build a tree instead of keeping a flat list of tokens?
The tree shows the program's structure by the grammar's rules, for example that a multiplication is worked out before the addition around it.
How does a compiler find a type error such as storing text in a whole-number variable?
It looks up the variable's declared type in its symbol table and finds that the stored value does not match it.
In the first worked example, how many tokens and instructions were there?
Seven tokens and four instructions for the imaginary machine: LOAD price, MUL 3, ADD tax and STORE total.
How many multiplications does folding 60 × 60 × 24 save in a loop run 1,000 times?
Each pass would do 2 multiplications, so 2 × 1,000 = 2,000 are saved; the compiler writes 86,400 into the code instead.
What does a just-in-time compiler do when a guard fails?
It falls back from the compiled code to the slower interpreter, a step called deoptimisation, so the results stay correct.
Curious Computing: how computers, code and the internet work.
Go deeper in World 101: Computing Fundamentals
Write about it
Turn a question from this chapter into a paper.
The Writing Studio has a structure guide, a rubric and citation tools for 15 formats, from essays to research papers. A free account keeps up to five drafts. Learnacy Essential adds feedback from an AI writing coach and submission to The OYI Review.
Publication depends on review and is not guaranteed.
