-
Title:
05xps-01 Parsing States Solution
-
Description:
-
Welcome to the Homework 2 solutions.
-
In this first question we're going to go over how to reason about the state of a chart
-
when parsing a token in a given grammar.
-
Right here I have a grammar that identifies JavaScript function calls.
-
Let's say I have a function, pow, which computes the power of a number
-
to another number--doesn't really matter.
-
The idea is that this function computes 5 to the 6th.
-
But what we care about is the tokens right here.
-
And to show that this string is in this grammar, we're going to walk through it.
-
Here I've written our first starting rule, which goes to id, some parentheses,
-
and then optional arguments.
-
So the id in this case is pow, and then we have 2 parentheses,
-
and the optional arguments in the case of our example is 5 and 6.
-
We don't have to have anything here. As the title implies, it's optional.
-
It could go right to the empty string. But we do have arguments.
-
In this case, we're going to have some kind of expression
-
or an expression followed by a comma with more arguments.
-
And in this case we have 2, so the optional arguments goes to arguments.
-
It's not empty, so it's going to go to this rule.
-
And then from arguments we're going to go to this rule
-
where expression is going to be 5.
-
We have the comma. That matches our comma.
-
The rest of the rule is going to be ARGS, which goes to--
-
Since this is our last argument, it's just going to go straight to expression, which is 6.
-
And we were able to match this string to the grammar
-
using the set of replacement rules given.
-
So I just went through that to demonstrate how this grammar matches JavaScript function calls.
-
But that's not really what the question is asking.
-
The question is asking about the chart.
-
We're going to see what happens when we parse a certain set of tokens.
-
Instead of writing the actual string, I just have the tokens that matter.
-
And for the sake of parsing, that's all we care about.
-
This can be matched by what I had before.
-
But all we care about is the tokens.
-
Now, when we parse this, what we want to ask is
-
what is in Chart 2, the third entry in our chart?
-
And so we're given a set of rules that may be in the chart.
-
I'm just going to go through them real quick. So here are your choices.
-
We're trying to figure out--as a reminder--what's in the second entry in our chart
-
when we parse this list of tokens and this grammar.
-
And since we're asking a question, we should remember question marks.
-
So there's really 2 ways to solve this.
-
The first is you work out the chart by hand.
-
That's what we're going to do because it's easier to kind of explain what's going on.
-
An alternative which is equally valid is to take the code that we wrote in lecture
-
and modify it to print the chart at every given state.
-
This is probably what you would want to do if you were asked this question more than once.
-
But, like I said, for the sake of going over the answer, we're going to work it out by hand.
-
Okay. Here's our input. Here is our grammar.
-
We want to know what's in Chart 0.
-
We haven't read anything in yet, so we're just going to be at the beginning
-
of the start substitution rule.
-
We introduced this in Chart State 0.
-
So now we're on Chart 1 and we've read in 1 token, id.
-
We're just going to shift on this rule because id is the first token in this rule.
-
And this comes from 0 as well.
-
So now the moment of truth, Chart 2.
-
Let's see what we can do.
-
We're going to see the token--that's the left parenthesis--and we're going to shift.
-
So here there are 2 things that are possible.
-
One is that optional arguments is empty.
-
We don't know yet because we haven't seen the rest of the input token list.
-
In that case, we're going to be past optional arguments.
-
There's also the case where optional arguments isn't empty,
-
which is actually what's going on with our input list.
-
So let's write both those out.
-
Here we've moved past optional arguments,
-
presumably because it's empty by this rule right here.
-
Then we have the other choice where optional arguments is not empty
-
and we're going to be processing it.
-
So regarding this first rule in our chart, we have the optional arguments being empty.
-
So we need to make sure to add that.
-
We introduced this in Chart State 2.
-
We have the other case where optional arguments is not empty,
-
and we're going to go through ARGS now.
-
So I have written out both ARGS rules,
-
and since we haven't read in this next token, we don't have anything
-
to go further on in either rule,
-
so at the beginning of each rule.
-
And presumably, the next chart state we will shift over with the new expression.
-
So now we have everything we need to answer the question.
-
We're just going to check off the choices that were in our Chart State 2.