Congratulations on completing the lexical analysis part of the course.
We're now going to move on to parsing where we will take our lexical analysis tokens and create what's
known as an abstract syntax tree.
So let's get on with the lecture and I'll explain all this to you.
So firstly, what is a parser?
Parser is a tool that produces an abstract syntax tree composed of nodes.
So parses make the nodes by reading the token input from the lexa.
Here is an example of an abstract syntax tree for this source input insert germain in it x equals 50
produces this abstract syntax tree.
So we see after parsing we end up with a function node whose function name is main, and the function
node has a body node and also a vector of arguments.
Now the vector of arguments has no branches here, no nodes branching off because we don't have any
arguments here.
So our body nodes can have infinite number of statements.
So we can see we have a variable node whose name is X and its value is a number node of value 50.
So you can see this entire tree represents this source input here.
So this is great because we can easily iterate through these and you will notice there's no return statement
here.
Right?
So during validation, we can loop through the function body node and we can see that there's no return
statement.
So then we can issue a warning that they're not returning a value in a function that returns an integer.
So again, we can easily iterate through them.
They can nest infinitely.
So here's another example of an abstract semantics tree.
But this time we've added a return statement so we can see we still have our function.
No, we have our body.
No, we have our vector arguments.
But this time our body node has two children.
It has a children of in it x equals 50 and it has a return statement node which returns the value 20
OC.
So you can see return statement node has an expression of number node which returns 20.
So why a pause is important.
They provide structure for an input file.
The nodes can branch off each other which allow us to create stability and logic.
It makes it easier to validate the code.
As I've just described, we can loop through the whole body node there.
If we don't see a statement return and the function return type is an integer, then we know that we
should issue them a warning.
Right, because they haven't issued a written statement.
Also, it makes it easier to compile the input file.
So essentially, rather than us going through the tokens and trying to remember things, we just build
a tree based on the logic we want and then that tree can be used going forward.
So some final reminders.
A node is an entity that can be part of an abstract sanctus tree.
So simply put, when you have two nodes connected together, then we have an abstract syntax tree.
A node on its own is just called a node.
So that's the difference between a node and an abstract semantics tree.
An abstract sanctuary is just a way to describe multiple nodes connected together.
And I'm just going to end this lecture by showing that even mathematics are made up of nodes in compilers.
So we take the expression 50 plus 20, we end up with an expression node who's operated as plus.
And on the left operand we have a number node of 50.
On the right operand we have a number node of 20 different nodes, allow four different children.
So a body node can have an infinite amount of children, for example, because we can have an infinite
amount of statements and expression.
Node, on the other hand, such as 50 plus 20, can only have two nodes.
Okay, now if we were to do 50 plus 20 plus 30, the right operand of the expression node would be another
expression node.
So for example, now we have 50 plus 20 plus 30.
What do we have?
We have an expression node of plus whose left node is 50.
The right node of that is another expression node whose left node is 20 and who's right node is 30.
So if that doesn't make completely sense, don't worry, because as we go through the course, it will
start to make sense.
However, that is basically what's happening with expressions because we can only have two children.
Whenever the expression gets too long, we just create another expression node and we make it the right
node of the current expression.
So in our case, 50 plus 20 plus 30, we end up with this abstract sancta extreme.
So thanks very much for watching and we will now get on with creating the Plaza.