Basic

A tree as the meaning

The parser returns a value with a shape. The shape is the meaning, and the meaning is what every later step reads.

What you will see

text_left pos=7. The place is the reason the tree is refused.

About 1 min of reading

Try It

Print a tree and compare two shapes

1

Print the tree

printf 'add left=1 right=mul left=2 right=3\n' > /tmp/t.txt; cat /tmp/t.txt

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

What it prints
One line with a nested name. The line is the shape printed.
2

Print the other shape

printf 'mul left=add left=1 right=2 right=3\n' > /tmp/u.txt; diff <(cat /tmp/t.txt) <(cat /tmp/u.txt) > /dev/null; echo $? | awk '{print ($1==0) ? "same" : "two meanings"}'

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

What it prints
two meanings. Two shapes for one text is two answers, and the rules decide.
3

Name the leftover place

printf 'tree ok\ntext_left pos=7\n' > /tmp/l.txt; grep pos /tmp/l.txt

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

What it prints
text_left pos=7. The place is the reason the tree is refused.

Data Flow

Tokens to a printed tree
1 The tokens are read in order
↓
2 The rules choose a node
↓
3 Leftover text names a place
↓
4 The tree is printed for the test
Why this works
Why the tree is printed in tests

The print is the shape. A test that prints the shape is a test that names the meaning rather than the answer.

The node is a name and children

Nothing else. A node with only a name and children is a node that every later pass can read.

Examples

The tree is a value

It can be printed, walked, and compared. A value that can be printed is a value that can be tested.

The same text, two trees

Two shapes for one text is two meanings. The rules decide, and the decision is visible in the print.

A partial tree is a failure

Text left over is a place. The place is the answer, and the answer is the reason the tree is refused.

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.

← A grammar as rules that decide shape
Previous Lesson
A grammar as rules that decide shape
Next Lesson
Evaluating the tree
Evaluating the tree →