Answered step by step
Verified Expert Solution
Question
1 Approved Answer
esign: You are required to implement RandomSentenceGenerator classes with the following methods: Java / C + + A constructor that is passed the name of
esign:
You are required to implement RandomSentenceGenerator classes with the
following methods:
JavaC
A constructor that is passed the name of the file containing the
grammar rules.
A method, randomSentence to return a random sentence
generated from the grammar.
Java A toString method to return the grammar rules in a readable format.
C The overloaded output stream operator to display the
grammar rules in a readable format.
C A destructor to delete any dynamically allocated space used to store the grammar.
You may add methods to your class as needed. You must implement a separate
main program to test your RandomSentenceGenerator class.
Storing the grammar:
Feel free to use any class library ADTs to store and manipulate the various
components of the grammar. In C be sure to store the terminals and variables
as strings allocated to appropriate size ie do not store using a large fixedsize
buffer Recall that a hash table is particularly good for doing quick lookups, so
data you often need to search would best be organized in a hash table, although
you may use other appropriate data structures if you choose.
Expanding the grammar:
Once the grammar is loaded up begin with the production and expand
it to generate a random sentence. Note that the algorithm to traverse the data
structure and print the terminals is extremely recursive.
The grammar will always contain a variable to begin the expansion. It
will not necessarily be the first definition in the file, but it will always be defined
eventually. Your code can assume that the grammar files are syntactically correct
ie have a start definition, have the correct punctuation and format as described
above, don't have some sort of endless recursive cycle in the expansion, etc.
The one error condition you should catch reasonably, and for which you should
throw an exception, is the case where a variable is used but not defined. It is
fine to catch this when expanding the grammar and encountering the undefined
variable rather than attempting to check the consistency of the entire grammar
while reading it
The names of variables should be considered caseinsensitively,
matches and for example.
Input:
Your program should take one command line argument, which is the name of the
grammar file to read. An error message should be displayed if the file is not
found.
Output:
Your program should print the grammar rules and three random sentences from
the grammar and exit.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started