In this episode we’ll implement parenthesis for overriding operator precedence in our arithmetic syntax. This is a continuation of the Nearley.js series for build parsers using JavaScript. Waikit wrote the code, and I replay his session and explain what he did.

The following transcript was automatically generated by an algorithm.

- 00:00 : hello there welcome back to our series
- 00:02 : on building parsers using nearly J yes
- 00:06 : so at this point we have a math
- 00:11 : arithmetic calculator implemented which
- 00:15 : is able to calculate some some simple
- 00:22 : calculations it can add subtract
- 00:24 : multiply and divide so so for example if
- 00:30 : we run this parse yes and feed this
- 00:36 : input to the parser it will do the math
- 00:39 : and give you the results so this should
- 00:41 : have done 3/2 first because that has
- 00:46 : higher presidents then because the
- 00:49 : division has higher presidents in
- 00:51 : addition so 3/2 is 1.5 add that to one
- 00:55 : she give you 2.5 there was precisely the
- 01:00 : result and I'm gonna draw you the parse
- 01:04 : tree right now so so given the string 1
- 01:09 : plus 3 divided by 2
- 01:16 : you'll get our grandma which looks like
- 01:21 : this
- 01:22 : these these blocks are for doing the
- 01:26 : calculation here I'm gonna collapse them
- 01:28 : down so that we can see the full grammar
- 01:34 : okay there we go
- 01:36 : so these are other rules of the grammar
- 01:38 : we got the input the additive
- 01:41 : multiplicative numbers and digits so
- 01:45 : basically the way that this grammar has
- 01:48 : its production rules set up is that the
- 01:52 : top-level
- 01:53 : thing let's do it in Reverse from the
- 01:57 : bottom ok so we start out as an input
- 02:01 : I'm trying to imagine what the parser is
- 02:04 : doing so we start off as an input what
- 02:06 : is it and what is an input it's an
- 02:08 : additive and then what is an additive
- 02:12 : well we its
- 02:13 : multiple could have then plus or minus
- 02:16 : an Editors we do see a plus or minus
- 02:18 : here so it uses this rule to say well
- 02:23 : multiplicative that does work a
- 02:27 : multiplicative can just be a single
- 02:29 : number multiplicative and then a plus
- 02:33 : sign and then another additive so that
- 02:39 : is this rule here we used to say an
- 02:42 : additive is a multiplicative a plus or
- 02:45 : minus sign in this case plus and then
- 02:47 : another additive the additive can be
- 02:50 : used to match the rest of this thing
- 02:52 : and what's an additive because I
- 02:54 : relative can just be a multiplicative
- 02:56 : because 3/2 we know and we'll see it's a
- 03:01 : multiplicative and and then now go down
- 03:05 : them what's a multiplicative it can be a
- 03:07 : number and then multiply or divide on
- 03:10 : them another multiplicative so this guy
- 03:13 : is a number and then I divide and then
- 03:16 : another multiplicative so these three
- 03:24 : things go and here and then the multi
- 03:27 : look at this can just be a symbol simple
- 03:30 : number which two is and then two is a
- 03:34 : single digit so this digit and so forth
- 03:40 : so this is how this whole thing is
- 03:42 : parsed now now in this episode what
- 03:46 : we're gonna work on is to add the
- 03:50 : parenthesis syntax to the mix so that
- 03:55 : this language is able to support
- 03:58 : parentheses so how does that how does
- 04:02 : that work well if we add apprentices
- 04:05 : here this should switch it up switch up
- 04:09 : the order so that it should actually
- 04:14 : first do the addition and then do the
- 04:17 : division so the end result should be 4
- 04:20 : divided by 2 should be 2 how do we do
- 04:24 : that well what we're gonna
- 04:27 : say is the thing at the very low level
- 04:31 : of the room so at the top level is
- 04:33 : additive additive and then
- 04:36 : multiplicative and then we go down to
- 04:38 : the number we allowed that to be either
- 04:41 : a number or another more complicated
- 04:45 : thing within a parenthesis which will
- 04:48 : allow us to force this division to have
- 04:56 : lower presidents basically will have
- 04:59 : lower presidents than the stuff inside
- 05:01 : of the parentheses so let's say if you
- 05:04 : have 1 plus 3 divided by 2 so this the
- 05:08 : plan is again we're gonna start with
- 05:10 : input which first goes to additive but
- 05:19 : this time the additive is going to try
- 05:25 : to go to multiplicative and then find a
- 05:27 : plus and minus sign but that's not gonna
- 05:29 : work
- 05:30 : because of this parenthesis here so
- 05:33 : instead is gonna first go multiplicative
- 05:35 : and then we're gonna say it's and we're
- 05:39 : gonna replace this with an unerring
- 05:43 : which can either be so under II can
- 05:48 : either be a number or something that has
- 05:55 : parentheses in it that's the plan so who
- 05:58 : say oh okay this multiplicative it's
- 06:00 : gonna be an unerring and then the
- 06:03 : division and then another multiplicative
- 06:08 : here I'll just write multi here and then
- 06:12 : multi can be another under E but this
- 06:16 : one is a number now Oh handwriting is
- 06:20 : horrible
- 06:21 : but an unwritten have another additive
- 06:25 : inside of the parenthesis so the honoree
- 06:29 : will take the parenthesis and have an
- 06:32 : additive inside of it which can allowed
- 06:37 : this 1 plus 3 thing
- 06:41 : there's a few more steps that go into it
- 06:42 : but that's the idea let's see that in
- 06:45 : action okay so so that's the goal and
- 06:50 : the way we'll do it is say look at the
- 06:53 : thing called uneri expression let me
- 06:55 : expand this now other thing that's
- 06:59 : called an area expression and change in
- 07:03 : the multiplicative clause and instead of
- 07:06 : going to number on the left hand side
- 07:09 : we'll go to an area expression on the
- 07:11 : left left hand side and then a nunnery
- 07:14 : expression can be either a number or
- 07:18 : parenthesis and then an additive so we
- 07:22 : start back from the top of this duration
- 07:26 : here and we'll do some transformation
- 07:31 : here this we're just gonna take whatever
- 07:36 : is returned by this edit if which is at
- 07:39 : index two because this is zero one and
- 07:42 : two
- 07:43 : okay let's just test that here
- 07:50 : well do one plus one times three plus
- 07:54 : two okay okay so he wrote the code like
- 08:04 : this all right let's run okay no it's
- 08:19 : the purse I'm gonna get for one plus one
- 08:23 : is two times two is four let's do the
- 08:29 : original one where it's like one plus
- 08:32 : three divided by that was my favorite
- 08:39 : division symbol do about two so now that
- 08:41 : should get us - yeah that's - so that's
- 08:47 : it that's how you get parentheses going
- 08:51 : yeah that's why I wanted to cover the
- 08:55 : next episode will cover how to do
- 08:56 : functions function call expressions
- 09:00 : within this earth Matic syntax and I
- 09:04 : will see you then for that one