Homework 5 (Grammar Solver) FAQ

Q: I am utterly confused about grammars, what this assignment is about?
A: In terms of this assignment, a grammar is a set of rules that corresponds to a sybmol. For instance, the adj grammar relates to many different adjectives like purple, loud, sticky, and fake. To put it simply, one grammar has many rules. This relationship is also known as non-terminal to terminal. For this assignment, we want you to navigate through these grammars and create them. Notice that some grammars have grammars in their rule sets as well. Upon encountering this situation, remember that it is a grammar that needs to be generated as well.

Take a look at the decision tree in the assignment specifications, this should help give you a visualization of the problem at hand.
Q: What is a map? How do I use a map?
A: Look at the WordCount program written during Friday's lecture. Remember that a map stores a relationship between a pair (known as a key to value relationship). What kinds of things do you think you could pair up in this assignment? Take note that the assignment write up states that you will have to include a Map, TreeMap, and String split method in your solution.

Java has a more detailed definition of a Map on the Java API.
Q: What type of data should my Map store?
A: Unforutantely, this aspect of the assignment is up to you to figure out. Remember the methods you're going to have to call, along with the idea of a grammar and how it relates to its rules.
Q: I'm having trouble breaking apart the String using Scanner or StringTokenizer.
A: Review the assignment writeup, it states not to do so.
When I use String.split(), I get some empty strings in the array. Why?
A: A subtle point to this method is that empty strings will be taken into account when splitting a string. For example, the string " grammar " will be split into an array of ["", "grammar", ""]. Maybe there is something we can do to the String so that it takes care of this paticular behavior?
A: Pay attention at when and how you're splitting things. Could you be not accounting for trailing whitespace?
Q: I try to split the String, but it doesn't break it apart at the right places. Why not?
A: Look out for whitespace. The assignment writeup tells you a great strategy for tackling this issue.
Q: How do I spot symbols if they don't start/end with "<" and ">" like in senteneces 2 and 3?
A: The "<" and ">" symbols are not special cases you should be concerned with. Treat them as you would any other symbol, word, or character.
Q: Spaces are in the wrong places in my output. Why?
A: Try using other characters like "_", "~", or "!" instead of spaces to find out where there's extra whitespaces.
Q: How do I debug my recursive method?
A: Try using System.out.println to find out which grammar s are being generated at certain points in your program. Try before and after your recursive step as well as in your base case.
Q: In my recursive case, why doesn't it join together the various pieces correctly?
A: Remember that setting a String = to another variable within a recursive step will not translate in all instances of that method. Be sure to actively add onto your String instead of reassigning it.
Q: My recursive method is really long and icky. How can I make it cleaner?
A: Remind yourself of the strategy in approaching recursive solutions. When are you stopping each recursive call? Make sure you're not making any needless or redundant checks inside of your code.
Q: When I run sentence 3.txt, I get really long math expressions? Is that okay?
A: Yes, that's expected. Math can get really complicated someimes XD