Show understanding of the various stages in the compilation of a program.
What is Translation Software?
Translation software converts source code written in a high‑level programming language into a form that can be executed by a computer. The most common type is a compiler, which performs a series of systematic transformations.
Stages of Compilation
The compilation process can be divided into distinct stages, each with a specific purpose and output. The figure below shows the typical flow.
Suggested diagram: Flow of compilation stages from source code to executable.
Lexical Analysis (Scanning)
Input: Raw source code characters.
Output: Stream of tokens (identifiers, keywords, literals, operators).
Key data structure: Symbol table (stores identifiers).
Syntax Analysis (Parsing)
Input: Token stream.
Output: Parse tree or abstract syntax tree (AST) representing the grammatical structure.
Typical technique: Context‑free grammar; parsing methods include LL(k) and LR(k).
Semantic Analysis
Input: AST.
Output: Annotated AST with type information and scope resolution.
Checks performed: Type compatibility, declaration before use, array bounds, etc.
Intermediate Code Generation
Input: Annotated AST.
Output: Platform‑independent intermediate representation (IR) such as three‑address code.
Example of a three‑address instruction: \$t_1 = a + b\$
Optimization
Input: IR.
Output: Optimised IR.
Goals: Reduce execution time, memory usage, or power consumption.
Common techniques: Constant folding, dead‑code elimination, loop invariant code motion.