9618 · 16.2
Translation Software
Computers only understand binary, a language of 1s and 0s. Translation software acts like a multilingual expert, converting the high-level languages we write in (like Python or Java) into the machine code the CPU can execute.
Need to know
What you need to know
- **Lexical Analysis:** The source code is read, and whitespace/comments are removed. The code is broken down into a stream of 'tokens'. For example, `x = 10;` becomes `IDENTIFIER(x)`, `EQUALS_OP`, `INTEGER_LITERAL(10)`, `SEMICOLON`.
- **Syntax Analysis (Parsing):** The stream of tokens is checked against the grammar rules of the language. The compiler builds a data structure, like a parse tree, to represent the program's structure. If the code breaks a rule (e.g., `x = 10 + ;`), a syntax error is reported.
- **Semantic Analysis:** The meaning of the code is checked. This stage catches errors that are grammatically correct but logically nonsensical, such as trying to add a string to an integer in a strongly-typed language, or using an undeclared variable.
- **Intermediate Code Generation & Optimisation:** The compiler generates a low-level, platform-independent version of the code. It then optimises this code to make it faster or smaller, for example, by removing redundant calculations.
- **Code Generation:** The optimised intermediate code is translated into the final machine code for the target CPU architecture.
- **Linking:** The generated object code is combined with code from libraries (e.g., for printing to the screen) to produce the final executable file.
Explanation
Code Translators: From Our Language to Theirs
- A programmer writes source code in a high-level language like Python or Visual Basic.
- The source code is fed into a translator program: either a compiler or an interpreter.
- A compiler processes the entire file, creating a separate executable file of machine code. An interpreter reads and executes the code one line at a time.
- The CPU runs the resulting machine code, either from the executable file or as directed by the interpreter, to perform the program's tasks.