IB Computer Science · A1 · Computer fundamentals
A1.4 Translation
Explain why programs need translating, compare assemblers, compilers and interpreters, and describe the stages of compilation and when each method suits.
1/5Processors run only machine code, so programs need translating
- Processors run only machine code, so programs need translating
- An assembler converts assembly language into machine code
- Compilers translate first and interpreters translate as they run
- Compilation passes through six stages, from tokens to machine code
- The best translation method depends on the situation
- Check your recall
- Practice: 10 questions
Part 1 of 5: Processors run only machine code, so programs need translating
A CPU can execute only instructions written in binary. These instructions are called machine code. Writing programs in machine code is slow and error-prone for people.
Programmers therefore write source code in a high-level language, which uses readable words and statements. A translator is a program that converts source code into machine code or another lower-level form.
A line such as total = price * 2 is easy to read, but a CPU cannot run it. Translation lets the same readable program be turned into instructions for a particular processor.
Key terms · tap one for its definition
machine code
Machine code is the binary instruction set that a particular CPU can execute directly.
source code
Source code is a program written by a programmer in a programming language, before it is translated.
translator
A translator is a program that converts source code written in one language into another form, usually machine code.
Part 2 of 5: An assembler converts assembly language into machine code
Between machine code and high-level languages sits a middle form. In assembly language, short codes such as ADD or LOAD replace binary patterns, so people can read them.
Each assembly instruction matches one machine instruction. An assembler converts assembly language into machine code. Because the match is one to one, the programmer keeps close control of what the CPU does.
Assembly language is tied to one type of processor, so it is not portable between processor types.
Key terms · tap one for its definition
assembly language
Assembly language is a low-level language in which short mnemonic codes each correspond to one machine code instruction.
assembler
An assembler is a translator that converts assembly language into machine code.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: B · Worked explanation
Part 3 of 5: Compilers translate first and interpreters translate as they run
Now compare the two translators used for high-level code. A compiler translates the whole source program into machine code before it runs. The result is an executable, a separate file of machine code that can be run later.
An interpreter translates one statement, runs it, then moves to the next. It produces no separate executable. The interpreter and the source code are needed every time the program runs. That repeated translation is why interpreted programs usually run more slowly.
Compiler compared with interpreter
- Feature
- Translation
- Compiler
- Whole program, before running
- Interpreter
- One statement at a time, while running
- Feature
- Output
- Compiler
- A separate executable
- Interpreter
- None; statements run directly
- Feature
- Speed of running
- Compiler
- Usually faster
- Interpreter
- Usually slower
- Feature
- Errors
- Compiler
- Reported together after translation
- Interpreter
- Found as each statement is reached
Key terms · tap one for its definition
compiler
A compiler is a translator that converts an entire source program into machine code before the program is run.
interpreter
An interpreter is a translator that translates and executes source code one statement at a time, without producing a separate executable.
Part 4 of 5: Compilation passes through six stages, from tokens to machine code
A compiler does not translate in one jump. It works through stages, and each stage hands its output to the next.
- In lexical analysis, the compiler splits the source code into tokens, the smallest meaningful units, such as keywords and names.
- In syntax analysis, it checks that the tokens follow the grammar rules and builds a tree of the program.
- In semantic analysis, it checks meaning, such as matching data types and declared variables.
- In intermediate code generation, it produces a simple form that does not depend on the processor.
- In optimisation, it improves the code so that it runs faster or uses less memory.
- In code generation, it produces the final machine code.
Key terms · tap one for its definition
lexical analysis
Lexical analysis is the compilation stage that splits source code into tokens and discards items such as spaces and comments.
syntax analysis
Syntax analysis is the compilation stage that checks tokens against the grammar rules of the language and builds a parse tree.
semantic analysis
Semantic analysis is the compilation stage that checks that statements are meaningful, for example that data types match and variables are declared.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: C · Worked explanation
Part 5 of 5: The best translation method depends on the situation
Knowing how each translator works, you can match a method to a job. A compiler suits finished software. The user runs the executable without any translator, and the source code stays private.
The executable is made for one type of processor, so it must be compiled again for another. It usually runs faster.
An interpreter suits development and learning. A programmer can run a change straight away and see exactly where an error occurs. The same source code can run on any machine that has the interpreter.
An assembler suits work close to the hardware, such as small devices built into other products. There, each instruction matters.
Check your recall[14 marks]
6 written questions. Answer each one in your head or on paper, then open the mark scheme.
- 1.State[1 mark]
State why source code must be translated before a CPU can run it.
Show mark schemeHide mark scheme
Model answer
A CPU can execute only machine code, which is binary, and source code is not written in that form.
- 1 mark for stating that the CPU can execute only machine code (binary instructions)
- 2.Distinguish[3 marks]
Distinguish between a compiler and an interpreter.
Show mark schemeHide mark scheme
Model answer
A compiler translates the whole source program into an executable before it runs, and the executable can be run without the compiler. An interpreter translates and runs one statement at a time and produces no separate executable.
- 1 mark for stating that a compiler translates the whole program before it runs
- 1 mark for stating that an interpreter translates and executes one statement at a time
- 1 mark for stating that a compiler produces a separate executable but an interpreter does not
- 3.Describe[2 marks]
Describe the role of an assembler.
Show mark schemeHide mark scheme
Model answer
An assembler converts assembly language into machine code. Each assembly instruction usually matches one machine instruction.
- 1 mark for stating that it converts assembly language into machine code
- 1 mark for stating that each assembly instruction matches one machine instruction
- 4.Outline[3 marks]
Outline the purpose of lexical analysis and of syntax analysis.
Show mark schemeHide mark scheme
Model answer
Lexical analysis splits the source code into tokens such as keywords, names and operators. Syntax analysis checks that the tokens follow the grammar rules and builds a tree of the program.
- 1 mark for stating that lexical analysis splits the source code into tokens
- 1 mark for stating that syntax analysis checks the tokens against the grammar rules
- 1 mark for stating that syntax analysis builds a tree (parse tree) of the program
- 5.Justify[4 marks]
Justify why a programmer might use an interpreter while developing a program but release it as a compiled executable.
Show mark schemeHide mark scheme
Model answer
An interpreter lets the programmer run a change straight away and see where an error occurs, which suits development. A compiled executable usually runs faster. Users can run it without any translator, and the source code stays private.
- 1 mark for stating that an interpreter allows a change to be run straight away
- 1 mark for stating that an interpreter shows where an error occurs as the code runs
- 1 mark for stating that a compiled executable usually runs faster
- 1 mark for stating that users need no translator and the source code stays private
- 6.Identify[1 mark]
Identify the stage of compilation that checks that data types match.
Show mark schemeHide mark scheme
Model answer
Semantic analysis.
- 1 mark for identifying semantic analysis

Practice this topic
10 quick-fire multiple-choice questions, easy to hard. Tap an answer to see if you're right and the worked explanation. (The written questions with mark schemes are under “Check your recall” above.)
0 right0 of 10 answered
Includes the 2 quick checks in the note above.
EasyIdentify[1 mark]
Show answer and explanation
Answer: D · Worked explanation
MediumIdentify[1 mark]
Show answer and explanation
Answer: D · Worked explanation
MediumIdentify[1 mark]
Show answer and explanation
Answer: C · Worked explanation
MediumIdentify[1 mark]
Show answer and explanation
Answer: B · Worked explanation
MediumDistinguish[1 mark]
Show answer and explanation
Answer: B · Worked explanation
HardDeduce[2 marks]
Show answer and explanation
Answer: A · Worked explanation
HardExplain[2 marks]
Show answer and explanation
Answer: A · Worked explanation
Hard[2 marks]
Show answer and explanation
Answer: B · Worked explanation