Scene 1 First guest at the table
TwigOne on the left, one on the right.Good. A perfect fit.
LineyWhoa! Did you already work it out?
TwigNope. So far I've only worked out how it fits together.
The Life of Code 02
We have all the pieces, so why won't the code work?
Last time, Snip cut the code into little pieces.
Now those pieces have arrived at Twig's table.
But once you have the pieces, can you build any code you like?
So that's your guess.
Today, let's watch how Twig handles this code.
TwigOne on the left, one on the right.Good. A perfect fit.
LineyWhoa! Did you already work it out?
TwigNope. So far I've only worked out how it fits together.
LineyThere's one extra piece this time?
TwigSo there is.Well, let's try putting this one together too.
TwigHmm.
LineyWhat's wrong? Is something missing?
TwigI'm not sure yet.But it doesn't fit like this.
LineyUh-oh. The tree didn't get built!
TwigHaving a piece doesn't mean you can stick it just anywhere.
LineySo what now?
TwigFirst, we find out where it got stuck.
LineyThey look alike, but one works and the other doesn't.
TwigThat's why the rules for putting pieces together matter, not just the pieces.
LineyHow do you learn those rules?
TwigTry building one yourself. You'll catch on fast.
A little experiment
Now it's your turn to hang the pieces.
The pieces arrive in the order they are written. Pick up a piece, then pick the right spot for it.
It's okay if it doesn't fit. Finding where it gets stuck is an important part of the experiment too.
Topaz compiler 5.21.1 record
expression/ binaryoperator addjoins two values · plusTakes up cells 1 to 5expression/ integer1numberTakes up cell 1expression/ integer2numberTakes up cell 5Topaz compiler 5.21.1 record
expression/ binaryoperator multiplyjoins two values · timesTakes up cells 1 to 5expression/ integer6numberTakes up cell 1expression/ integer3numberTakes up cell 5Topaz compiler 5.21.1 record
TPZ2001error
expected an expression
Points to cell 5
In plain wordsIt means an expression should come here. To the right of the plus sign, a times sign showed up before any value.
Topaz compiler 5.21.1 record
TPZ2001error
expected an expression
Points to where the expression ends
In plain wordsIt means an expression should come here. The code ended before a value showed up to the right of the plus sign.
The assembly table moves when JavaScript is on.
Compare these two.
6 * 31 + * 2
Both have a * in them, right?
But one can be put together and the other can't.
The symbol itself isn't wrong. What matters is where it's used.
The real inside story
The languages we speak have rules for building sentences.
Code has rules like that too.
At the assembly table, you just played at parsing. A compiler also checks the pieces against the grammar rules to work out how the code is built.
Draw the result as connected branches, and you get what is called a syntax tree.
A syntax tree with the grammar details we don't need taken out is called an abstract syntax tree, or AST for short.
This drawing shows the expression part of the record, with one addition and two numbers.
1 + 2In this code, the part that stands for adding joins two numbers.
Topaz compiler 5.21.1 record
This record comes from the compiler of Topaz, the language this site runs its examples in.expression/ binaryoperator addjoins two values · plusTakes up cells 1 to 5expression/ integer1numberTakes up cell 1expression/ integer2numberTakes up cell 51 + * 2This time, part of it breaks the grammar rules.
A problem like this is called a syntax error.
Instead of passing broken code along as if it were a normal sum, the compiler tells you what went wrong.
Topaz compiler 5.21.1 record
TPZ2001error
expected an expression
Points to cell 5
In plain wordsIt means an expression should come here. To the right of the plus sign, a times sign showed up before any value.
Not quite.
Code that fits the grammar isn't always a correct calculation.
Even after the tree is finished, there is more to check.
We'll get to that in the next episode.
What we found out today
A compiler puts tokens together following the grammar to work out how the code is built.
If part of it breaks the grammar, the compiler points out the error.
So why is this code a problem?
3 +In this expression, + is a plus sign that joins two values.
There is a 3 on the left, but the code ended before the value it needs on the right.
So the plus can't be completed as it is.
Now you know that putting pieces together the right way matters just as much as cutting them well.
Field GuideRevisit these wordsParsingSyntax TreeError Message
Next story
This time, Twig finished a tree without a hitch.
But then Tami came over with a magnifying glass.
1 + "apple"TamiThe shape looks fine.But can these two really be added?
Next time, let's meet type checking.