Section 14.3
Translation
14.3 Compilation Phases
There are three stages:
- Lexical analysis
- Syntax and semantic analysis
- Code generation
14.3.1 Lexical Analysis
- Extra spaces removed
Print       A$
becomes...
Print A$
- Comments are removed
REM My lovely program
Print A$
becomes...
Print A$
- Simple error check
E.g. Illegal variable names are flagged
- Keywords, variables, constants and operators are converted to
tokens.
Tokens are unique codes that are stored in the symbol table.
This is the process of checking to see that the sequence of input characters
is a valid sentence.
Examples
- The use of stacks to check that brakets are correctly paired
- Checking to see that string data is not being assigned to an integer
variable
- By the use of meta-language such as Backus-Naur form
14.3.3 The Symbol Table
This table contains an entry for every keyword, variable, constant and operator
(called identifiers)
Generating the Symbol Table
Example Program
Dim Radius As Single
Dim Area As Single
Const Pi = 3.1415926536 As Single
Input Radius
Area = Pi * Radius * Radius
Token
|
Item Name
|
Kind of Item
|
Data Type
|
Run Time Value
|
1
|
|
|
|
|
2
|
Input
|
Keyword
|
|
|
3
|
Pi
|
Constant
|
Single
|
3.1415926536
|
4
|
Radius
|
Variable
|
Single
|
?
|
5
|
=
|
Operator
|
|
|
6
|
Area
|
Variable
|
Single
|
?
|
7
|
|
|
|
|
8
|
*
|
Operator
|
|
|
Using this table, the program can be tokenised as the lexical string:
2 4
6 5 3 8 4 8 4
Note that the syntax analyser fills in the columns "Kind of Item" and "Data
Type". The lexical analyser only adds the "Item Name" and "Run Time Value".
The most common way of organising the symbol table is to use a hash table.
The identifier is hashed to a memory location.
14.3.4 Code Generation
In this final phase the machine code (or object code) is produced.
Code Optimisation
Some compilers attempt to make the object code run quickly by removing redundant
instructions and by spotting better ways to produce the same effect as the source
program.
Disadvantages
- compilation time is increased
- unwanted results may be produced