Two Laptop Bag*
The moose likes Java in General and the fly likes Writing a Recursive Descent Parser, I think I'm done, well almost? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Writing a Recursive Descent Parser, I think I Watch "Writing a Recursive Descent Parser, I think I New topic
Author

Writing a Recursive Descent Parser, I think I'm done, well almost?

Jon Bud
Greenhorn

Joined: Jan 29, 2009
Posts: 12
Here is what I have,'
My grammar is basically an assignment statement:
Assignment -> Identifier = Expression
E -> Term + E | Term -E | Term
T -> Primary * T | Primary / T | Primary
P -> I | (E)
Identifier -> a | b | ... | y | z

Its anyways my input string "s" which is a global variable, even though its in the grammar, it keeps telling me its not in the grammar. I'm pretty sure I've got the methods correct, but please take a double look. I think the problem is that I'm not doing something correctly in my main method where I say if(assignment() && s.length < index. The second part of this statement is just to make sure that we are at the end of the string. Thanks for helping out.





Richard Walker
Greenhorn

Joined: Feb 20, 2009
Posts: 4
Hi Jon.

As you say, nearly there. The first thing to look at is the multiple calls to term() in express() and primary() in term(). They have a side effect when they are successful (changing the value of index), so they can't be successful more than once at the same index which is what you would need for the else parts of your tests to succeed.

I think you'll also find that you need to take a bit more care with your charAt() indexes when you get to the end of s.
Jon Bud
Greenhorn

Joined: Jan 29, 2009
Posts: 12
So I re-did the code some what to what you said. I check for the index to make sure its not out of bounds but I still am not getting my strings to be recognized.

Richard Walker
Greenhorn

Joined: Feb 20, 2009
Posts: 4
OK. Scratch that attempt - it's not really any improvement. Let's go back to your first listing.

My suggestion is to re-think your implementations of express() and term() so that you don't expect to successfully call term() and primary() more than once. The reason is that if, for example, express() is called with the input buffer containing a term and a minus then the first test, for term and a plus, would fail even though term() was successful and incremented the index. Then next check, for term and a minus, which you would expect to succeed will fail as well. This time because term() now won't match the character at the new index position (because it's now a minus).

Instead of thinking of an expression as three alternatives (either a term followed by a plus or a term followed by a minus or a term on its own) which leads to checking for a term three times, try thinking sequentially: Have I got a term? If not, I haven't got an expression. If so, have I got a plus or a minus? If not, I have got an expression. If so, have I got an expression?... and so on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing a Recursive Descent Parser, I think I'm done, well almost?