The purpose of this assignment is to practice your knowledge of data structures and problem solving.

The Problem

In a video game, the characters often must say phrases throughout the play of the game. The phrases must be of a certain form (e.g., a noun followed by a verb followed by an object). However, the phrases must also be randomized, so that the character does not repeat exactly the same phrase again and again.

We have been asked to construct a program that randomly generates phrases. The form of each phrase is specified using an input grammar file.

As an example, consider the input grammar file super_simple.g (https://utah.instructure.com/courses/661021/files/110551419/download?download_frd=1) . A possible random phrase of this form is "The mouse stood on the dog." Another is "The cat sat on the mouse."

The formatting rules of the input grammar file are very strict, in order to simplify the task of parsing the input.

  • The file contains the definitions of one or more non-terminals.
  • Non-terminals are always denoted with angle brackets, and the name of a non-terminal may not contain any whitespace. For example, < noun> and < ong-int> are correct non-terminals. < awesome word> and < noun > are not correct non-terminals.
  • Terminals are denoted by the absence of angle brackets.
  • Each non-terminal definition begins with an opening curly brace (on its own line with no extraneous spaces). The non-terminal being defined appears alone on the next line. The choice of one or more productions to which the non-terminal can be expanded appear next (one per line). A single production consists of one or more terminals and/or non-terminals. A single blank space may appear between each terminal and non-terminal; however, no extraneous spaces appear before the first (non-)terminal or after the last (non-)terminal. Finally, the non-terminal definition ends with a closing curly brace (on its own line with no extraneous spaces).
  • Every non-terminal used in a production is defined somewhere in the file, but not necessarily before (or after) the production in which it is used.
  • The starting non-terminal is always identified as < start>.
  • Comments may appear in between non-terminal definitions. Therefore, any lines outside of the curly braces should be ignored.
  • Error-checking on the input grammar file is not required, and you may assume that all grammar file used for grading will be correct.

The following are some sample input grammar files and possible random phrases for each.

  • poetic_sentence.g (https://utah.instructure.com/courses/661021/files/110551418/download? download_frd=1)
    • The waves portend like big yellow flowers tonight.
  • mathematical_expression.g (https://utah.instructure.com/courses/661021/files/110551420/download?download_frd=1)
    • ( y + ( ( x - 2 ) * e ) )
  • assignment_extention_request.g (https://utah.instructure.com/courses/661021/files/110551416/download?download_frd=1)
    • I need an extension because I had to practice for an alligator wrestling meet, and as if that wasn't enough I just didn't feel like working, and then, just when my mojo was getting back on its feet, my dorm burned down.

Requirements

  • Create a new class called RandomPhraseGenerator in a new package called comprehensive. The user will start your program by running this class (i.e., by calling RandomPhraseGenerator's main method).
  • You may create as many other new classes as needed. Make sure that all Java files required by your program are in the comprehensive package.
  • The path and name of the input grammar file will be given as input to your program via the command line (as the first command-line argument). Also given as input will be the number of random phrases your program should generate (as the second command-line argument).
  • The output of your program is simply the random phrase(s), printed one per line. The output must preserve any blank spaces that do or do not appear between terminials and non- terminals. For example, given input grammar file super_simple.g (https://utah.instructure.com/courses/661021/files/110551419/download?download_frd=1) , "The cat sat on the mouse." is a possible random phrase, but "The cat sat on the mouse ." is not.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.