| CIT 594 Assignment 2: Random sentence generation CIT 594, Spring 2007 |
Write a text file containing BNF grammar for English sentences. Here is an example you can start with, but you should either write your own, or "improve" this one. In either case, replace the vocabulary items (the "terminals") with your own vocabulary items.
Read in your BNF grammar and use it to generate about twenty random sentences.
Write a main class SentenceGenerator that does the following:
JFileChooser to prompt the user for a file containing the BNF
grammar rules.You should have the following classes and their constructors and methods:
public class SentenceGenerator
public static void main(String[] args)List<String> generate(String term)
List<String> as a sentencepublic class Grammar
Grammar() // constructorpublic void addRule(String ruleText) throws IllegalArgumentExceptionpublic Definitions getDefinitions(String nonterminal)public void print() // prints the
grammar neatly, on multiple lines
class Definitions
extends ArrayList<SingleDefinition>
public String toString() // returns a single line
class SingleDefinition extends ArrayList<String> public String toString() // returns a single line
public class GrammarTest extends TestCase
addRule. Remember to test whether addRule throws
an IllegalArgumentException for
some obvious cases of malformed BNF grammar rules.getDefinitions.print. You also don't need to test
the Grammar constructor, since all it does is create a Map<String,
Definitions>. public class DefinitionsTest extends TestCasepublic class SingleDefinitionTest extends TestCase toString method. The line
returned by toString should be legal BNF. You do not need to create a GUI for this program (although you may if you
wish). If you do, SentenceGenerator should extend JFrame.
Your BNF rules should be kept in a Map<String, Definitions>
in the class. SentenceGenerator Grammar
All non-private elements should have a well-written Javadoc comment. Remember that a Javadoc comment is supposed to tell the user everything s/he needs to know in order to use that element.
It is possible to write the generate method as an iteration rather
than as a recursion. Don't! Part of the purpose of this assignment is to get
you familiar with recursion.
Generate the Javadoc and look it over before you turn in your program.
BnfTokenizer.java (use this tokenizer when you read in the grammar) and BnfTokenizerTest.java.
Zip up and turn in your complete project, including all test files and the generated Javadoc, via Blackboard, before midnight, Tuesday January 23.