# Make a Parser with Nearley.js - Part 7

Published on Sep 10th 2019Duration: 12:16

In this episode we’ll add function expressions to our arithmetic syntax and Toby was force to go off script as Waikit forgot to implement the power functions. We’ll see if Toby holds it together. Again we’ll use Nearley.js as the parser generator.

## Transcript

The following transcript was automatically generated by an algorithm.

• 00:00 : hello there welcome to another video
• 00:01 : about building parsers using nearly Jas
• 00:04 : so at this point we're gonna continue
• 00:07 : from last time in which we made this
• 00:12 : parenthesis syntax work for this
• 00:17 : calculator we've been working on so at
• 00:20 : this point our calculator is able to
• 00:23 : take some calculation based on that
• 00:29 : subtract multiply that were divided and
• 00:33 : give you the calculate the answer for
• 00:36 : you and it can this syntax can support
• 00:41 : parentheses now so this should first do
• 00:46 : this calculation this calculation and
• 00:48 : then the total calculation combined
• 00:52 : let's see if it works
• 00:53 : point six six six repeating so this is
• 00:57 : should be six four divided by 6
• 01:01 : that's right point six six six repeating
• 01:04 : is two over three so that that seems
• 01:06 : correct so now in this episode we're
• 01:08 : gonna add the ability to have built-in
• 01:12 : functions such as square root square
• 01:16 : power I don't remember if we did power
• 01:20 : sign and this syntax is gonna be like
• 01:24 : this so it's gonna be something like
• 01:28 : square root of four should get us two
• 01:39 : for example I think we're also doing
• 01:40 : power so we do power like this the
• 01:44 : answers should be 4 so this syntax
• 01:46 : involves a short name for the function
• 01:50 : so that's the function name and left
• 01:55 : paren and a right paren
• 02:01 : my writing is horrible
• 02:04 : and then some some some normal
• 02:08 : expression in them inside of as the
• 02:12 : argument of the function and I would
• 02:14 : just be whatever at up at this point
• 02:17 : whatever expression this calculator can
• 02:19 : handle and the whole thing is yet
• 02:22 : another expression which can again be
• 02:24 : compounded with some other symbols to
• 02:30 : create a even more complicated
• 02:33 : expression so let's see how we did this
• 02:36 : way kick wrote this code and I'm gonna
• 02:39 : replay what he did and I explained his
• 02:43 : train of thought as we go okay so so the
• 02:48 : way we're gonna do it is we you created
• 02:51 : this thing last bit so called uneri
• 02:54 : expression which is like it's a
• 02:56 : one-owner e meaning one whereas binary
• 03:00 : meaning two so an under a expression is
• 03:03 : visually thing only has one thing in it
• 03:06 : and we said that that was an expression
• 03:09 : that's surrounded by parentheses or it
• 03:11 : could be a number I'm gonna add one more
• 03:13 : thing to it now it's gonna be is gonna
• 03:16 : be the result of executing a function
• 03:18 : we'll call that the function and then
• 03:21 : define it so what is a function a
• 03:23 : function is a function name first a
• 03:25 : function name it's actually gonna call
• 03:27 : it built-in function okay function name
• 03:29 : and then an O right parameter or I
• 03:33 : should say left parameter and then an
• 03:36 : additive which is like some expression
• 03:41 : and then I write print that makes sense
• 03:45 : and what is a built-in function let's
• 03:47 : have some building we're gonna have
• 03:49 : square square root sine cosine absolute
• 03:52 : value we didn't even add power here oops
• 03:55 : I think he forgot to add power
• 04:01 : we might have to rectify that later but
• 04:04 : okay so how are you gonna do the
• 04:07 : calculation here well depending on which
• 04:09 : built-in function it is we're gonna have
• 04:10 : to do a different thing that's why we're
• 04:12 : gonna use a switch statement
• 04:14 : we're gonna pick so there's a FN is name
• 04:17 : of the function which was picked out of
• 04:20 : this array of matches here and if you
• 04:22 : remember this array of mattress is like
• 04:25 : this and I'll write the indices for you
• 04:30 : here the first one is zero second one is
• 04:32 : one etc Oh again my handwriting is
• 04:37 : horrible
• 04:38 : so data at zero that's that the built-in
• 04:41 : function and they'll give you the
• 04:42 : function name because that came from
• 04:44 : over here oh we need some IDs here to
• 04:49 : unpack this one I think you'll have to
• 04:52 : do that pretty soon and then the value
• 04:55 : is gonna come from index 3 this one so
• 05:00 : the additive is gonna come in here and
• 05:02 : that will get us the value because our
• 05:04 : editors symbol already will calculate
• 05:07 : the number for us at this point okay and
• 05:11 : then so depending on what the function
• 05:14 : name is if it's a square and we're just
• 05:17 : gonna multiply the value by itself if
• 05:20 : it's a square root we're gonna use
• 05:21 : JavaScript's math square root function
• 05:27 : okay he's testing the square root
• 05:30 : function out in genius make sure that
• 05:34 : exists and in JavaScript there's also a
• 05:37 : sine function cosine further than the
• 05:38 : absolute function and again he forgot
• 05:40 : about the power okay so we're gonna run
• 05:43 : the parser again and then let's see if
• 05:47 : it handles the square root function now
• 05:50 : no why is that probably because he
• 05:54 : forgot these IDs okay so putting them
• 05:58 : back in now makes it work and if you
• 06:02 : don't know what these IDs stand for I'll
• 06:06 : go back to our I believe the second
• 06:11 : second video in this series about using
• 06:16 : nearly je s and will have an explanation
• 06:18 : about why this ID transformer function
• 06:22 : is needed and yeah at this point we're
• 06:25 : able to we're able to call these
• 06:29 : functions that seems pretty cool let's
• 06:33 : do absolute value so like if I do - -
• 06:37 : for ABS here so absolute value of 2
• 06:44 : minus 4 is 2 2 times the square root of
• 06:48 : 10 is 20 the square root of 16 2 times
• 06:54 : the square root of 16 is 4 4 times 2
• 06:57 : okay so we've got these functions
• 07:00 : working now and they all right
• 07:02 : let's go off script here and make powers
• 07:06 : work so in order for powers to work here
• 07:09 : we actually need to be able to support
• 07:12 : multiple arguments so we need a thing
• 07:15 : called argument list and then use the
• 07:17 : recursive trick to have multiple
• 07:20 : arguments so first of all let's call
• 07:23 : this guy argument list and then define
• 07:29 : what that is okay so an argument list
• 07:34 : can either be just one argument or it
• 07:37 : can just be an argument in there
• 07:39 : followed by yet another argument list
• 07:41 : which allows it to recurse what's an
• 07:48 : additive some optional whitespace a
• 07:52 : comma some more optional white space and
• 07:55 : then another argument list or it could
• 07:59 : simply be one additive and let that be
• 08:02 : yet yeah let's go with that
• 08:06 : there's a possibility of allowing empty
• 08:09 : argument list but I'm actually not Debbi
• 08:13 : for something like generating a random
• 08:14 : number or something nothing gonna not
• 08:17 : allow that for now and if you do want to
• 08:22 : allow it I'll give that to you the
• 08:25 : viewer as a homework assignment okay so
• 08:29 : Oh actually this is not gonna work
• 08:32 : because we want to represent an argument
• 08:34 : list as an array so we're gonna take
• 08:38 : this edit path here take the data from
• 08:44 : it and return an array that takes the
• 08:49 : zero element or the first element of
• 08:51 : this match but this one where I know the
• 08:56 : argument list is gonna get returned me
• 08:58 : an array probably from this one I'm
• 09:01 : gonna also return an array so data is
• 09:04 : gonna return an array which has data
• 09:07 : zero in it so zero is that one and then
• 09:11 : what's this one zero one two three four
• 09:16 : this is for I'm no this data at four is
• 09:20 : gonna be give me an array because here
• 09:24 : I'm returning array and the other one
• 09:26 : also returns me array so I'm gonna
• 09:27 : spread it into this array so now this
• 09:31 : argument list it's going to be could
• 09:39 : have multiple things in it so data at
• 09:43 : three is gonna be the arguments uncle
• 09:47 : arcs could be more than one value but in
• 09:53 : these cases they're not gonna be more
• 09:57 : than one value so I'm gonna say first
• 10:00 : part is X 0 and second part is arts one
• 10:10 : and then
• 10:13 : change these value references to first
• 10:17 : art and then for power I'll do math at
• 10:25 : POW has in first art and then second
• 10:29 : argh okay so now that's done they can
• 10:33 : regenerate the parser can be a run gen
• 10:39 : parser and then let's try power POW to
• 10:49 : to to third oh that does not work
• 10:58 : oh I know why I forgot to know I don't
• 11:04 : know why let's read the error message
• 11:10 : expecting to see one of the following oh
• 11:13 : I didn't add power to the list of
• 11:16 : built-in functions that is why okay
• 11:23 : fixing that I'm gonna send a parse for
• 11:27 : one more time and now it works we can do
• 11:32 : powers now two to the fourth is 16 2 to
• 11:38 : the fifth so what is 2 to the 12th 4096
• 11:46 : awesome now you know your binary numbers
• 11:49 : um and that's I think that's a good
• 11:52 : place to stop for this episode and I
• 11:55 : hope to see you next time for some more
• 11:57 : parser in some parser information some
• 12:03 : information about parsers next episode
• 12:07 : we'll probably get in more into AST
• 12:10 : abstract syntax trees so if you're
• 12:13 : interested in that I will see you next