Lexical Analysis
• Comments and unnecessary whitespace are removed
• Keywords, operators and other symbols are replaced with tokens
• Identifiers, such as the names of variables and subroutines, are entered into a symbol table
• The series of tokens and the symbol table are passed on to the next stage
Syntax Analysis
• The series of tokens is checked against the rules (the syntax) of the language
• An error is reported wherever a rule is broken, such as a missing bracket
• If no rules are broken, an abstract syntax tree is built to represent the structure of the program
Code Generation
Optimisation
• Removing instructions that have no effect on the result
• Removing variables and subroutines that are never used
• Replacing a group of instructions with a shorter or faster group that does the same job
total = price + 5 // add the delivery charge
| Stage | What happens to this line |
|---|---|
| Lexical analysis | The comment and the spaces are removed. total and price are entered into the symbol table, and the line becomes a series of tokens: identifier, assignment operator, identifier, addition operator, number |
| Syntax analysis | The tokens are checked against the rule for an assignment statement. The rule is met, so the statement is added to the abstract syntax tree |
| Code generation | The tree is converted into object code: instructions that load price, add 5 and store the result in total |
| Optimisation | The object code is examined and any unnecessary instructions are removed |