# Parsing Exercise

Check out a free preview of the full Building Your Own Programming Language course:
The "Parsing Exercise" Lesson is part of the full, Building Your Own Programming Language course featured in this preview video. Here's what you'd learn in this lesson:

## After seeing Steve live code the parsing of numbers, the students are instructed to add functionalities to their parser so that it is able to parse both identifiers and strings.

Get Unlimited Access Now

Transcript from the "Parsing Exercise" Lesson

[00:00:00]
>> Steve Kinney: We'll take a look at some of the first few tests first. We'll look at parse.test.js. So it should return a token of the same numerical level for tokens, right? So hey, if you come across a number in our token bin, why don't you turn this into a numeric literal, right?

[00:00:23] And so this is just for the number two. The number two would have just been turned into one number token with a value of 2. And we turn it into the actual literal syntax, right, and this is mostly because that's what value this is. And so that will be the kind of the first thing and then we can say okay strings, should become string literals, names become identifiers, right?

[00:00:44] For a lot of our values, they're pretty straightforward. And then we'll start to string some stuff together with some call expressions and stuff along those lines. So we're gonna kinda do this in two phases. We're gonna use those parentheses to kind of inspire what the kind of hierarchical syntax is.

[00:01:00] And then we'll actually parse the values to actually figure out the rest of the tree. So let's head into parse.js. And you can see that I've done a teeny bit of work for you. So I've got these utilities here and this opening parenthesis and this closing parenthesis should need no introduction.

[00:01:18] You can imagine what they do. Peek and pop are kind of functional programming things. Peek looks at the first thing in an array. So it's like what is the next token, right? And pop gives you the first thing off the array. So it's basically a way to look at the stack of tokens that we picked up.

[00:01:37] All right, so let's go ahead and kinda get the general kinda structure in place here which is, we'll say, parse. Parse is gonna take our tokens.
>> Steve Kinney: So pop it off the front.
>> Steve Kinney: If the token.type,
>> Steve Kinney: Is a number.

[00:02:18] And this is one of those things where honestly when I was doing this for realsies I had a is number token helper and I never thought about it. I never thought about the fact that I would mistype number ever again. But I'm not doing that just for the sake of clarity, right?

[00:02:31] If I do too many helpers like this is cool, [LAUGH] I don't really know what's going on. So then we'll return and I would again use a function almost like an action creator on my Redux to make the actual object, cuz I don't trust myself.
>> Steve Kinney: NumericLiteral, but I'm pair programming with a live chat and eight other people in the room.

[00:02:51] If I make any mistakes, everyone will immediately tell me. So I don't have to worry about any of that stuff.
>> Steve Kinney: Token.value.
>> Steve Kinney: Right, and that should kind of pop that out. I need to just adjust this real quick, cuz this is eventually what it'll look like. First, we're gonna parenthesize, which is use those parentheses to create our data structure.

[00:03:14] We don't fully have that yet. I mean, I have this empty thing I can just kinda immediately return. Let's actually do that.
>> Steve Kinney: That should do the trick. We'll find out when I unskip the test, though, right?
>> Steve Kinney: Let's unskip the first one and see if I've done anything terrible.

[00:03:51]
>> Steve Kinney: All right, first one passes, doing so far so good. I'd love for you to get the identifiers in the strings working. Should be real quick, it's the same basic pattern. They're still just single values, if just a string comes in or just an identifier comes in. So it's the same basic pattern that we have there.

[00:04:10] So I have set up the ability to find a single number. We have a token with just a number in it, right? We can go ahead and turn that into the appropriate abstract syntax tree node. It's not really a tree with only one node on it, it's just a leaf kinda hanging out there.

[00:04:26] But we have that in place and we're gonna kinda use that cuz effectively, at the end of the day, our call expressions are branches. And the tree ends when you get to something that only has either an identifier, a string, or a number, right? So we're gonna input the kinda ends of our trees first.

[00:04:48] So here we have Number. The exercise that I want you to do is we have two more tests. We have tests for implementing the exact same pattern for our identifiers, right, those would be our function names, and our strings. So those tests over in parse.test.js always have Exercise 3 Begin.

[00:05:06] Your job is to get these two passing and I'll see you back here.