Stage 1
PAPER 1 • SOFTWARE AND SOFTWARE DEVELOPMENT • APPLICATIONS GENERATION
Compilation Stages
- A compiler translates high-level source code into a form a computer can run. It does this in stages, beginning with lexical analysis and syntax analysis before moving on to code generation and optimisation.
Visual channel
Verbal channel
- Lexical analysis turns lexemes into tokens.
- Syntax analysis checks grammar and builds a abstract syntax tree.
- Code generation creates object code.
- Optimisation improves speed and memory use.
Activity 1
Sort the compilation tasks
Drag each task into the correct stage. The statements come directly from the activity and answer decks.
Stage 2
Syntax analysis
Stage 3
Code generation
Stage 4
Optimisation
Activity 2
Lexical analysis practice
Source code
Function highest (num1 As Decimal, num2 As Decimal) 'Function to return the higher of two numbers If num1 > num2 Then Return num1 Else Return num2 EndIf EndFunction
Token format
[tokenclass:token]
Remember that comments and white space are ignored by the lexer.
Stage 1
Build the token stream
Continue the token stream for the source code using the format shown above.
Stage 2
Symbol table snapshot
Choose the token class and datatype for each of the tokens on this symbol table.1
1 Identifiers do not yet get their final datatype until the Syntax Analysis stage.
Activity 3
Syntax analysis studio
Stage 1
Checking the syntax
- Trace the correct route through the syntax diagram for If num1 > num2 Then. Choose the pathway that matches the two operands. If you connect If through to Then correctly, you have followed a valid grammar rule.
Stage 2
Build the abstract syntax tree
Drag the tokens into the correct places to build a hierarchical abstract syntax tree for If num1 > num2 Then.
In syntax analysis, how is a abstract syntax tree created?
Stage 3
Update the symbol table
At syntax analysis, the compiler can now update the full symbol table and apply declared data types to the relevant identifiers.