CIT 594 Assignment 2 Clarifications
CIT 594, Spring 2007

This is in response to a number of questions I've gotten about the assignment.

Consider:

<adjective> ::= big | red | really ugly

This is a rule. Or if you want to count it that way, it's three rules. In any case, there is no difference between the above rule and the following:

<adjective> ::= big | red
<adjective> ::= really ugly

or between either of the above and

<adjective> ::= big
<adjective> ::= red
<adjective> ::= really ugly

For any of the above, there is one thing being defined, and it has three definitions. The definitions are the things on the right of the ::=. There are three SingleDefinitions, each of which is a list of terminals and/or nonterminals. Using the obvious (but non-Java) notation for lists, these SingleDefinitions are:

["big"]
["red"]
["really", "ugly"]

and each of these is a definition for "<adjective>".

I put in "really ugly" to demonstrate why each definition has to be a list. (It would be possible for the first two definitions to be just strings, not lists, but it makes programming a whole lot easier if we use a consistent representation.) These are the definitions of <adjective>, so the Definitions object for <adjective> is [["big"], ["red"], ["really", "ugly"]]. This is a list of definitions, so it's a list of lists. Specifically, it's a list of SingleDefinitions.

The most important (and probably the only) field in your Grammar class is a Map. The Map is declared as

Map<String, Definitions> grammar =
             new HashMap<String, Definitions>();

In your Map you will have a key "<adjective>", which is a String, and its value will be [["big"], ["red"], ["really", "ugly"]], which is a Definitions object. Each element of the Definitions object is a SingleDefinition.


Your program is to read in BNF rules from a file. I gave you a sample file, sample-grammar.txt. Your program can assume that there is one and only one rule on each line, where a rule can have more than one definition. In other words, a typical line looks like:

<adjective> ::= big | red | really ugly

You do not have to deal with:

<adjective> ::= big <adjective> ::= red

or with:

<adjective> ::= big | red |
                really ugly

However, it's a really good idea to ignore blank lines. (A line line is "blank" if line.trim().length() == 0. )


I gave you a sample BNF grammar, but I also want you to make up your own. I want you to have some experience writing BNF as well as processing it. You don't have to do anything brilliant or complicated, but at least do a little more than changing the words in the sample grammar.

I like it when I get interesting grammars! Your "sentences" don't have to be 100% perfect English, or anywhere close. You won't be graded on getting the grammar "right" (whatever that means). But I do want you to experiment with the grammar a bit and see what you can come up with.