Created
December 3, 2016 18:00
-
-
Save drpventura/1e2d37443838a4086e853f4b629932c6 to your computer and use it in GitHub Desktop.
Code from final exam video.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <cstdlib> // for rand | |
#include "utils.h" | |
using namespace std; | |
/** | |
* NOTE: You may add other headers | |
* as well as your own functions | |
* as desired | |
*/ | |
/** | |
* Returns true if and only if the string is a non-terminal. | |
* It must not error out if the string is empty. | |
* @param s the string to test. | |
* @return true if and only if the string is a non-terminal. | |
*/ | |
bool is_non_terminal(const string &s) { | |
// TODO: replace return below with your implementation. | |
return false; | |
} // end is_non_terminal | |
/** | |
* Extracts just the definitions from the grammar file. | |
* | |
* @returns vector of strings where each string is the lines for | |
* a given definition (without the braces) | |
* | |
* Example: | |
* read_grammar_defs("grammars/Poem.g") returns | |
* [ <start> The <object> <verb> tonight. ; , <object> waves ; big yellow flowers | |
; slugs ; , <verb> sigh <adverb> ; portend like <object> ; die <adverb> ; , <adverb> wa | |
rily ; grumpily ; ] | |
*/ | |
vector<string> read_grammar_defs(const string &filename) { | |
// TODO: replace return below with your implementation. | |
return vector<string> {}; | |
} // end read_grammar_defs | |
/** | |
* Takes a string in the format of those in the vector returned | |
* by read_grammar_defs and reformats it in the form of a vector | |
* with the first element being the non-terminal and the other | |
* elements being the productions for that non-terminal. | |
* | |
* Remember that a production can be empty. | |
* | |
* Example: | |
* split_definition("<start>You <adj> <name> . ;May <curse> . ;") | |
* returns [<start>, You <adj> <name> ., May <curse> .] | |
* split_definition("<start> ; You <adj> <name> . ;") | |
* returns [<start>, , You <adj> <name> .] | |
*/ | |
vector<string> split_def(const string &raw_def) { | |
// TODO: replace return below with your implementation. | |
return vector<string> {}; | |
} // end split_def | |
/** | |
* Type alias to make declaring the type of the | |
* grammmar less messy. | |
* | |
* Pick one of these for the type of your grammar map | |
* NOTE: you must use a map where the keys are the | |
* (string) non-terminals, however, the value part | |
* can be what you want. | |
* | |
*/ | |
//using GrammarType = map<string, string>; // probably the worst choice | |
//using GrammarType = map<string, vector<string>>; | |
using GrammarType = map<string, vector<vector<string>>>; | |
/** | |
* Takes a vector of vector of strings of definitions where | |
* each of the definitions have been processed by | |
* split_def and returns a map that | |
* is the grammar where the key values are the non-terminals | |
* for a rule and the values are the vectors containing the | |
* productions (depending on how which GrammarType, | |
* you selected) | |
*/ | |
GrammarType to_grammar(const vector<vector<string>> &defs) { | |
// TODO: replace return below with your implementation. | |
GrammarType result; | |
// populate the map | |
return result; | |
} // end to_grammar | |
/** | |
* Given a grammar (as returned by to_grammar) | |
* returns a string that is a randomly generated sentence from | |
* that grammar | |
* | |
* Once the grammar is loaded up, begin with the <start> production and expand it to generate a | |
* random sentence. | |
* Note that the algorithm to traverse the data structure and | |
* return the terminals is extremely recursive. | |
* | |
* The grammar will always contain a <start> non-terminal 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 | |
* (i.e. 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 names of non-terminals should be considered | |
* case-insensitively, <NOUN> matches <Noun> and <noun>, for example. | |
*/ | |
string expand(const GrammarType &grammar, string non_term = "<start>") { | |
// TODO: replace return below with your implementation. | |
return ""; | |
} // end expand | |
int main() { | |
// seed the random number generator | |
srand(time(nullptr)); | |
// string file_contents = slurp("grammars/Poem.g"); | |
// cout << split(file_contents, "\\{.+?\\}") << endl; | |
// map<string, string> translations; | |
// translations["hello"] = "hola"; | |
// translations["pants"] = "pantalones"; | |
// cout << translations << endl; | |
// | |
// cout << translations["hello"] << endl; | |
vector<int> nums {1, 2, 3, 4, 5}; | |
cout << nums << endl; | |
transform(begin(nums), end(nums), begin(nums), | |
[](int n) { | |
return n * n; | |
} | |
); | |
cout << nums << endl; | |
vector<string> names {"Radar", "Penelope", "Chiki"}; | |
vector<int> lengths; | |
back_insert_iterator<vector<int>> it(lengths); | |
transform(begin(names), end(names), it, | |
[](auto s) { | |
return s.size(); | |
} | |
); | |
cout << names << endl | |
<< lengths << endl; | |
// TODO: implement this | |
// loop | |
// Prompt user for name of grammar file | |
// if input is exit, then quit | |
// otherwise, | |
// read grammar file, and then print 3 random | |
// expansions of the grammar | |
} // end main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment