ContentsThe library

Turning Text Into Instructions

Precedence Is Not a Table You Memorise, It Is the Shape of the Rules

Last timeFrom Text to Pieces

A parser turns a flat list into a tree. The order of operations you learned as a table is actually a consequence of how the grammar was layered, and nothing else.

A grammar is a set of shapes

The previous stage produced a flat list of tokens with no structure. This stage

builds the structure, and it does so against a written description of what

structures are allowed. That description is the grammar.

A grammar is nothing more than a list of rules, each saying that one kind of

thing may be made of some other things in a particular order.

FIG 1A grammar for arithmetic, complete
plaintext
expression  ->  expression + term
expression  ->  expression - term
expression  ->  term

term        ->  term * factor
term        ->  term / factor
term        ->  factor

factor      ->  number
factor      ->  identifier
factor      ->  [ expression ]

that is the whole thing: nine rules, three kinds
of thing, and no table of precedence anywhere
Nine lines define the arithmetic of most languages. Note what is absent: there is no statement anywhere about which operator binds tighter, and no list of priorities. The behaviour everyone memorises is already fully determined by these nine lines.

Parsing is fitting the token list into these shapes. Starting from the top kind

of thing, is there an arrangement of rules that accounts for every token, in

order, with none left over? If yes, that arrangement is the tree. If no, the

input is not a program in this language, and that is exactly what a syntax

error is.

FIG 2Fitting the tokens of a plus b times c
stepsteptokens leftwhat the parser decidedtree so farwhat happened
11a + b * ca is a factor, and a factor is a termone leafWorking bottom up, the smallest shapes are recognised first. A bare name is a factor by the rule, and a lone factor is a term by another.
22+ b * chold the term, an operator followsone leafThe parser cannot finish the expression yet because the rule for addition needs a term on the right as well.
33b * cb times c is a term by the multiplicatiotwo leaves joinedThis is the moment that decides everything. The multiplication rule completes while the addition rule is still waiting, so the product becomes one object.
44nothingterm plus term is an expressionthree leaves, one shapeOnly now does the addition complete, and what it adds is the finished product. The grouping was never chosen, it fell out of which rule could finish first.
4 steps
At no point did anything consult a precedence table. The multiplication rule finished before the addition rule could, because of where the two rules sit relative to each other in the grammar, and the tree records the result.

The tree is the program

The result is a tree, and from here on the tree is the program. The text was a

way of writing it down.

The lesson stops here

4 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Before It Can Read Your Program It Has to Decide Where the Words End
  2. 02Precedence Is Not a Table You Memorise, It Is the Shape of the Rulesyou are here
  3. 03The Stage That Finds Out Whether Your Names Refer to Anythingopening only
  4. 04One Neutral Form in the Middle Turns a Multiplication Into an Additionopening only
  5. 05Every Transformation Obeys One Rule, and the Rule Is Narrower Than You Thinkopening only
  6. 06The Compiler Deleted Your Safety Check Because You Had Already Broken the Ruleopening only
  7. 07Thousands of Values and Sixteen Places to Keep Themopening only
  8. 08Your Instructions Arrive in Order and Are Executed in Whatever Order Suitsopening only

Read alongside