Basic

Errors that name a place

A compiler that says only that it failed is a compiler that cannot be fixed. The place is the difference, and the place comes from the token.

What you will see

One line naming a rule. A named rule is a message that can be argued with.

About 1 min of reading

Try It

Collect two errors with places

1

Write two failures

printf 'line=3 pos=8 expected=rparen\nline=7 pos=2 expected=name\n' > /tmp/e.txt; wc -l < /tmp/e.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
2. Two lines is a list, and a list is what a reader wants.
2

Bound the recovery

printf 'skip_max 12\n' > /tmp/b.txt; cat /tmp/b.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
skip_max 12. The number is written, which is what stops the loop.
3

Name the rule in the message

printf 'rule=expr expected=rparen got=plus pos=8\n' > /tmp/m.txt; cat /tmp/m.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line naming a rule. A named rule is a message that can be argued with.

Data Flow

A failure to a list of places
1 The token place is carried
↓
2 The rule that expected something is named
↓
3 Recovery skips a bounded number of tokens
↓
4 The errors are returned as a list
Why this works
Why recovery is bounded

Recovery that never stops is a loop. The bound is a number of tokens skipped, and the number is written.

The error is part of the contract

A bad program must answer. The answer is the contract, and the contract is checked by a test like every other answer.

Examples

The place is carried

Every token kept a place. The place is why an error can point at a line rather than at the file.

One error is not the last

The parser can keep going and collect more. Collecting more is a list, and a list is what a reader wants.

The message names the rule

The message says which rule expected what. A message that names the rule is a message that can be argued with.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Evaluating the tree
Previous Lesson
Evaluating the tree
Next Lesson
Testing a compiler
Testing a compiler →