welcome back guys
so by this moment we may handle multiple
statements and multiple expressions
but so far our literals are pretty much
useless for example the number 42
we cannot do anything with this number
and the first operations which we can
start doing
are the mathematical operations so in
today's lecture we'll talk about binary
expressions
uh specifically about additive
expression multiplicative
and and also about primary expression
and so let's start away from the test
i'm writing the math test okay for
mathematical expressions
and let's do the program two plus two
now such expression is known as
a binary expression what is binary
expression
it has two operands the left-hand side
and the right-hand side
in this case the left-hand side and
right-hand side are both numeric
literals
so that's the simplest binary expression
as you can see we also captured the
operator of this binary expression
and in this case it's plus and but we
also may have chained binary expressions
for example three plus two plus two
and from the first glance it might seem
it's not a binary expression right since
we have here three operands
but in fact it's still the binary
expression right in fact if we take a
look
the left hand side here is just the
complex expression itself and also the
binary expression
that's pretty much it and we should also
say that in math
the plus operator is left associative
what this means
uh this expression should look as three
plus two
and only after that plus two right so
if we use parenthesis it would look like
this now with the plus operator it
doesn't make much difference
but it make difference for the minus
operator for example right if we put
minus here this might change semantics
so the ast note for this program should
be
the minus operator right which left hand
side is the complex expression itself
and it's still the binary expression
with a plus operator
3 plus 2 and minus 2.
sounds great and let's again put here
the left hand side is three plus two and
right
two okay
so let's go ahead and implement this and
if we take a look again at the
expression
um two plus two we see that the literals
here
that is two and two has the higher
precedence than the plus operator itself
that is
in order to evaluate the binary
expression that is the sum
we have to first go and evaluate its
operands which might be arbitrary
complex
so going back to our expression we're
going to refactor it
currently as you see it returns the
literal but in fact it will return the
additive expression that is the
sum plus operator or the minus and as we
said
the literal should go inside the
additive that is the literal has the
higher precedence
than the additive we should also say the
rule
the closer production is to the starting
symbol
in this case to the expression the lower
precedence it has
right since here the additive expression
is directly returned
from the expression this means the
additive has the lower precedence than
the literal which is not returned
directly from the
expression and again here we use the
recursive rule
the additive expression is either the
one literal and this will allow
propagating to just one number
or it might be the actual plus separator
or the minus and let's call it additive
operator
uh so it might be multiple literals
separated by the plus or minus
and again as we said we don't work with
left recursion so basically
we rewrite it and expand to literal plus
literal
so the way it works we first parse the
left hand side which is literal
and then depending whether we have plus
as they look ahead
right we obtain the operator we may go
and parse the right hand side
if it's present and since we need to
have the chain binary expressions
uh this should be a while loop right
while the look ahead
is the additive operator first of all
we're going to consume the operator
right and then we're going to parse the
right hand side
what is the right hand side here well
it's again the literal
and as we said we also need to enforce a
left associativity
that is the left hand side if it's the
complex expression
is recursively becoming the left-hand
side of each sub-expression
and then we just return the the topmost
left-hand side
which again either will be just the
literal or the binary expression
sounds great let's add our math test to
the test runner
let's execute and yes correctly it says
the plus
token is not supported in the tokenizer
so let's go ahead and edit
okay so i'm adding the mathematical
operators plus and minus
and just have the additive operator okay
let's get back and execute and still
have some issue let's see
oh we actually have to take the value of
the plus token right not the token
itself but just the value
so the additive operator that value
which will be the plus
okay let's execute and yes now it's
passed
so congratulations we now have the plus
operator and it might be chained
now let's check that the single number
still works
right when we go to additive and the to
the first sub production which is just
literal and that should be also returned
and yes as we can see for the two
let's manually test the addition 42 plus
23
yes we got the binary expression uh
let's
chain it 42 plus 23 minus 10
and yes as you can see the left hand
side here is the complex expression
that is we've got left associative
operators
okay sounds great but also we may use
multiplication right 2 plus 2
times 2 which in this case should gives
us the answer
6 right because the multiplication has
the higher precedence than the plus
and in this case the evaluation would be
first the right hand side
again because of the higher precedence
so let's go ahead and add the
multiplicative operator right away to
the tokenizer
sounds great and let's write the test
two plus two times two again take a look
at the precedence
the plus correctly has the lower
precedence that is before we are able to
evaluate
plus we have to go first and evaluate
the left hand side and right hand side
and the right hand side here is the
times operator right
that is the times will be evaluated
first that's why it has the higher
precedence
let's do the simple test two times 2
which is just
the binary and let's also show when we
have the same precedence that is 2
times 2 times 2. we still got the left
associative times
so the left hand side here should be the
binary of the times operator
sounds great let's take a look again at
the expression 2 plus 2 times 2.
so previously the operands of the plus
were just the literals
but in this case the operands of the
plus operator would be the
multiplicative expression right we first
need to evaluate 2 times 2
and that would be exactly the upper end
for the plus
so let's get back to additive expression
and refactor it
right the additive expression will be
either the multiplicative
or multiple multiplicative expressions
and the multiplicative expression itself
would be exactly the same
right it would be accepting the literal
but let's actually call it primary
expression right because
besides the literals we will have some
other primary expressions and once again
it's exactly the same handling just we
get the multiplicative operator
and parse the primary expression and for
now let's keep the primary expression
it's just the literal
sounds great okay let's get back to test
and execute
and yes it works so take a look at the
plus separator here has the lower
precedence which is correct
that is the right hand side here the
complex multiplicative expression
which is also binary let's put
2 times 2 times 2 in this case the left
hand side here should be the complex
expression
because of the same precedence and that
is exactly correct
but also in mathematical expression we
may put a group in parenthesis and in
this case they have higher precedence
than the time separator
that is evaluation of this example which
gives us eight
instead of six right we first do two
plus two and only after that
times two so let's go ahead and execute
and
correctly it says parentheses are not
supported let's go ahead and add them
to the tokenizer right open parenthesis
and the closing parenthesis
and let's also add the test all right so
uh 2 plus 2 in parenthesis
times 2 should be the left hand side
the complex expression all right for the
plus operator
and the right hand side is just the
literal
and those grouping parenthesis let's
call them parenthesized expression
would be exactly the primary expression
right this is how we achieve
the parenthesized expression and the
literal to have the higher precedent
than the multiplicative
and again here we got the help from our
lookahead token right since we
build predictive parsing so if the
lookup had just opened parenthesis
we have to parse parenthesis expression
otherwise let's default it just to
literally yet
and later we will add more primary
expressions
sounds great so what is parenthesis
expression well it should start with the
open parenthesis
and should be close with the open
parenthesis what is inside
inside may go any expression write the
very basic expression
might be additive multiplicative or just
a simple number so it's the very basic
expression itself
and the way we handle it is pretty much
simple
right we just eat the open parenthesis
then we parse the expression and eat the
closing parenthesis
as the result we do not return an
explicit ast note
but just the expression which is inside
the group in parenthesis
what this means inside the ast we will
not see any group in parenthesis but we
will see correct precedence
which this parenthesis enforced and that
should be pretty much it
let's get back and execute and yes now
we got
correctly uh the plus operator has the
higher precedence just because
it was put into group in parenthesis
let's return back
and again the times has the car
precedence let's also show that inside
the group in parenthesis we may have any
expression for example simple number
okay it works or for example two plus
two
and it also works and again as you can
see there are no any group in
parenthesis in the ast
but there is correct precedence sounds
great now we have mathematical
expressions which is pretty cool
one slight thing if we take a look at
the additive expression and
into multiplicative they handle pretty
much very similar
right so let's slightly refactor and
move the same parts
into helper function let's call it
binary expression
the only difference the additive
expression builds the multiplicative
and consumes the additive operator and
multiplicative expression
builds the primary and consume
multiplicative operator
so the helper function would consume the
actual builder name
and the operator and we will just
dynamically call
the builder and the operator is passed
from the parameter
and now we may refactor the
multiplicative to be the binary which
constructs
primary expressions separated by the
multiplicative operator
with the same success where we factor
the additive expression which again
constructs
the binary expression consisting of
multiplicative expressions
separated by additive operator okay
looks nice and clear
let's go ahead and do some extra tests
let's execute
and yes it works so to reiterate we now
have mathematical expressions
which already makes our language uh more
useful
however we still cannot store these
expressions anywhere
so in the next lectures we'll start
introducing variables and assignment
expressions
that's it for today thanks and see in
the class