Translation Software

Computer Science A2 · System Software. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science A2 topics

The idea

An interpreter reads one statement of source code, checks it, and carries it out straight away. It then moves to the next statement. No object code file is produced, so the interpreter and the source code are needed every time the program runs.

A compiler translates the whole program in stages: lexical analysis, syntax analysis, code generation and optimisation. The result is object code that runs on its own.

The grammar of a language is written down with syntax diagrams or with Backus-Naur Form (BNF). Compilers often change expressions into Reverse Polish Notation (RPN), where the operator comes after its operands, so no brackets are needed and a stack can evaluate it.

In simpler words

An interpreter reads one line of code, checks it and runs it, then moves on. A compiler translates the whole program first into object code that runs on its own. It works in stages: lexical analysis, syntax analysis, code generation and optimisation. Grammar rules can be written in BNF. Reverse Polish Notation puts the operator after its numbers, so no brackets are needed.

An everyday comparison

An interpreter is like a live translator at a speech, turning each sentence as it is said. A compiler is like translating a whole book before anyone reads it; then readers need only the translation. The comparison stops working because a compiler also optimises the code, which is more than translating it word by word.

Rules to remember

Common mistake

For − and ÷ students use the two popped values the wrong way round. The value popped first is the right-hand operand, so 8 3 − means 8 − 3 = 5, not 3 − 8.

In the exam

Name each compiler stage in order and state what it does: lexical analysis removes spaces and comments, makes tokens and builds the symbol table; syntax analysis checks grammar and builds a parse tree. For RPN, show the stack after each step. Push operands; for an operator, pop two values and work out second popped operator first popped, then push the result. For BNF, explain ::= and |, and show how a rule that uses its own name allows repetition.

Worked example

Evaluate the RPN expression 7 2 5 * + 3 − using a stack.

  1. Push 7, 2 and 5. The stack holds 7, 2, 5.
  2. Read *: pop 5 and 2, work out 2 × 5 = 10, push 10. The stack holds 7, 10.
  3. Read +: pop 10 and 7, work out 7 + 10 = 17, push 17.
  4. Push 3. Read −: pop 3 and 17, work out 17 − 3 = 14, push 14.

Answer: 14 (the infix form is 7 + 2 × 5 − 3).

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

← Purposes of an Operating System (OS)Encryption, Encryption Protocols and Digital Certificates →Take a Computer Science A2 test