Translation Software, BNF and Reverse Polish Notation
1. The Stages of Compilation
| Stage | Purpose | Output |
|---|---|---|
| 1. Lexical analysis | Break the source into tokens | A stream of tokens plus a symbol table |
| 2. Syntax analysis | Check the tokens obey the grammar | A parse tree, or syntax errors |
| 3. Code generation | Produce object code | Machine code or intermediate code |
| 4. Optimisation | Improve the code's speed or size | Optimised object code |
Lexical analysis
The source code is read character by character and grouped into tokens — keywords, identifiers, operators and literals. Comments and unnecessary whitespace are removed. Identifiers are entered into a symbol table recording each name, its type and eventually its memory address.
Syntax analysis
The token stream is checked against the language's grammar rules, a process called parsing. If the tokens form a valid construct a parse tree is produced; if not, a syntax error is reported. Some semantic checks also occur here, such as detecting an undeclared identifier or a type mismatch.
Code generation and optimisation
The parse tree is converted into object code. Optimisation then improves it without changing what it does:
- Removing code that can never execute (dead code)
- Moving calculations that never change out of a loop
- Evaluating constant expressions at compile time — 4 * 5 becomes 20
- Using registers instead of main memory where possible
2. Backus-Naur Form (BNF)
BNF is a formal notation for defining the syntax of a language. It states precisely what counts as valid, in a form a compiler writer can implement directly.
| Symbol | Meaning |
|---|---|
| ::= | "is defined as" |
| | | "or" — alternatives |
| < > | A non-terminal — something defined elsewhere by another rule |
| plain text | A terminal — an actual character or symbol appearing in the language |
Applying the rules
| Candidate | Valid identifier? | Why |
|---|---|---|
| count | Yes | Letters only |
| count2 | Yes | Identifier followed by a digit |
| 2count | No | No rule allows an identifier to begin with a digit |
A syntax diagram shows the same rules pictorially, with boxes for non-terminals, rounded boxes for terminals, and arrows showing permitted paths including loops for repetition.
3. Reverse Polish Notation (RPN)
In reverse Polish notation the operator is written after its operands — also called postfix. Ordinary algebra is infix, with the operator between them.
| Infix | RPN (postfix) |
|---|---|
| 3 + 4 | 3 4 + |
| 3 + 4 * 5 | 3 4 5 * + |
| (3 + 4) * 5 | 3 4 + 5 * |
| A * B + C / D | A B * C D / + |
Evaluating RPN with a stack
Read left to right. Push operands. On an operator, pop two operands, apply it, push the result.
4. Exam Focus
Quick self-check
- Name the four stages of compilation and state the output of each.
- State two things lexical analysis removes from the source code.
- What is stored in the symbol table?
- Write BNF rules defining a non-negative integer.
- Convert (A + B) * (C - D) to RPN.
- Evaluate 8 2 / 3 + showing the stack at each step.