Skip to content

SpecificationI · Overview

1.2

The journey of a program: from text to execution

Before running a program, the engine breaks it into pieces, checks that it makes sense and translates it into simple instructions for its virtual machine.

Markdown version

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;
EndAlgorithm

Output

42

The program finishes normally.

  1. 1Lexical analysis. The text is split into pieces called tokens: Algorithm, Doble, Define, n, As, Integer… Spaces and comments are kept aside.

    Stage

    tokens

  2. 2Syntax analysis. The tokens are grouped into a tree: an algorithm containing a definition and a write.

    Stage

    tokens

    tree

  3. 3Semantic analysis. The engine checks names and types: n exists, it is an Integer and so is n * 2.

    Stage

    tokens

    tree

    checked types

  4. 4Translation. The tree becomes an intermediate representation and then simple instructions for the virtual machine.

    Stage

    tokens

    tree

    checked types

    instructions

  5. 5Execution. The virtual machine runs the instructions one by one and writes 42.

    Stage

    tokens

    tree

    checked types

    instructions

    Output

    42

Figure 1.2-1.The five stages of the engine. The first three happen before anything runs; if any of them finds errors, the program never runs.

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.