1.2.1 How it works
When you press "Run", the program text travels along a path of five stages. Each stage produces something new from the previous one. Step through the figure to follow the program Doble on its journey.
ProgramThe travelling program
Algorithm Doble
Define n As Integer <- 21;
Write n * 2;
EndAlgorithmOutput
42
The program finishes normally.
1Lexical analysis. The text is split into pieces called tokens:
Algorithm,Doble,Define,n,As,Integer… Spaces and comments are kept aside.Stage
tokens
2Syntax analysis. The tokens are grouped into a tree: an algorithm containing a definition and a write.
Stage
tokens
tree
3Semantic analysis. The engine checks names and types:
nexists, it is anIntegerand so isn * 2.Stage
tokens
tree
checked types
4Translation. The tree becomes an intermediate representation and then simple instructions for the virtual machine.
Stage
tokens
tree
checked types
instructions
5Execution. The virtual machine runs the instructions one by one and writes 42.
Stage
tokens
tree
checked types
instructions
Output
42
A column of boxes joined by arrows: the source text, lexical analysis, syntax analysis, semantic analysis, translation and execution. A dashed red line next to the first three marks that they form the analysis. Each step highlights one stage.
1.2.2 Check first, run afterwards
The first three stages form the analysis. If they find an error, the program does not run: it would make no sense to run something that is not understood. That is why a typing mistake shows up immediately, without waiting for the program to reach that line.
Some errors cannot be seen by the analysis because they depend on real values: dividing by zero, going outside an array or reading a variable without a value. Those are detected when running, at the exact moment they happen.
In short
- The engine works in five stages: lexical, syntax, semantics, translation and execution.
- If the analysis finds errors, the program does not run.
- Errors that depend on concrete values appear when running.