Anophoo

rsg

Sep 28th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. /**
  2.  * File: rsg.cc
  3.  * ------------
  4.  * Provides the implementation of the full RSG application, which
  5.  * relies on the services of the built-in string, ifstream, vector,
  6.  * and map classes as well as the custom Production and Definition
  7.  * classes provided with the assignment.
  8.  */
  9.  
  10. #include <map>
  11. #include <fstream>
  12. #include "definition.h"
  13. #include "production.h"
  14. using namespace std;
  15.  
  16. /**
  17.  * Takes a reference to a legitimate infile (one that's been set up
  18.  * to layer over a file) and populates the grammar map with the
  19.  * collection of definitions that are spelled out in the referenced
  20.  * file.  The function is written under the assumption that the
  21.  * referenced data file is really a grammar file that's properly
  22.  * formatted.  You may assume that all grammars are in fact properly
  23.  * formatted.
  24.  *
  25.  * @param infile a valid reference to a flat text file storing the grammar.
  26.  * @param grammar a reference to the STL map, which maps nonterminal strings
  27.  *                to their definitions.
  28.  */
  29.  
  30. static void readGrammar(ifstream& infile, map<string, Definition>& grammar)
  31. {
  32.   while (true) {
  33.     string uselessText;
  34.     getline(infile, uselessText, '{');
  35.     if (infile.eof()) return;  // true? we encountered EOF before we saw a '{': no more productions!
  36.     infile.putback('{');
  37.     Definition def(infile);
  38.     grammar[def.getNonterminal()] = def;
  39.   }
  40. }
  41.  
  42. /**
  43.  * Recursive function
  44.  */
  45.  
  46. void rsg(string str, map<string, Definition> grammar){
  47.   Definition def = grammar[str];
  48.   Production pr = def.getRandomProduction();
  49.   for (Production::iterator curr = pr.begin(); curr != pr.end(); ++curr) {
  50.     string tmp = *curr;
  51.     if (tmp[0] != '<') {
  52.       cout << tmp << endl;
  53.     } else {
  54.       rsg(tmp);
  55.     }
  56.   }
  57. }
  58.      
  59. /**
  60.  * Performs the rudimentary error checking needed to confirm that
  61.  * the client provided a grammar file.  It then continues to
  62.  * open the file, read the grammar into a map<string, Definition>,
  63.  * and then print out the total number of Definitions that were read
  64.  * in.  You're to update and decompose the main function to print
  65.  * three randomly generated sentences, as illustrated by the sample
  66.  * application.
  67.  *
  68.  * @param argc the number of tokens making up the command that invoked
  69.  *             the RSG executable.  There must be at least two arguments,
  70.  *             and only the first two are used.
  71.  * @param argv the sequence of tokens making up the command, where each
  72.  *             token is represented as a '\0'-terminated C string.
  73.  */
  74.  
  75. int main(int argc, char *argv[])
  76. {
  77.   if (argc == 1) {
  78.     cerr << "You need to specify the name of a grammar file." << endl;
  79.     cerr << "Usage: rsg <path to grammar text file>" << endl;
  80.     return 1; // non-zero return value means something bad happened
  81.   }
  82.  
  83.   ifstream grammarFile(argv[1]);
  84.   if (grammarFile.fail()) {
  85.     cerr << "Failed to open the file named \"" << argv[1] << "\".  Check to ensure the file exists. " << endl;
  86.     return 2; // each bad thing has its own bad return value
  87.   }
  88.  
  89.   // things are looking good...
  90.   map<string, Definition> grammar;
  91.   readGrammar(grammarFile, grammar);
  92.   int numberOfSentences = 3;
  93.   for (int i = 1; i <= numberOfSentences; i++) {
  94.     cout << "Version #" << i << ": --------------------------" << endl;
  95.     // Do recursion three times
  96.     rsg("<start>", grammar);
  97.   }
  98.   cout << "The grammar file called \"" << argv[1] << "\" contains "
  99.        << grammar.size() << " definitions." << endl;
  100.  
  101.   return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment