← davidhaxton.co.uk
H446 1.2.2 (e)

OCR A Level Computer Science, 1.2.2 (e), stage one of four

lexical
analysis

The only stage that ever touches the characters you typed. It reads them once, left to right, groups them into tokens, throws away everything that was only there for human eyes, and hands the result to the parser. It understands nothing.

What goes in, what comes out

Hold lexical analysis in your head as a contract rather than as a description. The stage promises very little, and most of the marks come from knowing exactly how little that is.

Stages of compilation (lexical analysis, syntax analysis, code generation and optimisation).OCR H446 specification, 1.2.2 (e). The specification names the stage and stops. Everything else on this page is here to make that sentence mean something.

In

  • One stream of characters, read once, left to right
  • No structure, no line meaning, no idea which language construct is being read
  • A position counter, so every character knows its line and column

Out

  • A stream of tokens, each carrying a type, a value and the position it came from
  • A symbol table holding every identifier met, with space for the type and the address that later stages fill in
  • A list of any characters that belong to no token at all

Explicitly not

  • No check that the tokens are in a sensible order, which is stage two
  • No arithmetic. The lexer reads 4 and 2 and never notices the multiplication sign between them
  • No whitespace, indentation or comments. They did their job and are discarded
  • No data types and no addresses yet, only the names

Where the editor stops and the translator starts

An IDE performs lexical analysis constantly. That is what the colours are. It is reasonable to conclude that the editor is doing stage one on the compiler's behalf and passing the result along. It is not. Step along the axis below and watch what actually crosses the boundary.

the editor process

the translator process

crossing the boundary here:

The live lexer

The panel on the left behaves like an editor: it retokenises on every keystroke, colours what it finds, underlines what it cannot read, and keeps nothing. Press Build to run the translator's lexer on the saved text. Notice which counter moves when.

wages.erl
0editor lexer runs
0translator lexer runs
samples

editorCharacters, cut into lexemes

editorTokens, remade on the last keystroke

What is passed on to syntax analysis

Nothing appears below until Build is pressed. This is the output of the translator's lexer at the last build, not the output of the editor. If the text has been changed since, the panels say so, because that is exactly the state you are in when the error you are reading no longer matches the code on screen.

translatorThe token stream, in order

translatorThe symbol table, started here

Why the positions travel with the tokens

Every token carries the line and column of the character it started at. The parser, two stages later, has no access to the source text at all, so when it rejects a token sequence the only way it can tell anybody where the problem is, is by reading the position the lexer attached. An error message pointing at the right line is a service provided by stage one.

When the lexer finds something it cannot read

There are only two ways to fail at this stage: a character that belongs to no token, and a token that opens and never closes. Everything else you would call a syntax error is caught later. The program below contains three lexical faults, and the choice of what to do about them is a design decision rather than a rule.

    Three things that are not lexical errors

    Each of these lexes perfectly. Every character belongs to a token, so stage one reports nothing and passes the stream on. The stage that actually objects is named underneath, and both the tokens and the error come from the compiler running on this page.

    Glossary

    The terms this stage is answered with.

    Back to the top