All pastes #783324 Raw Edit

Someone

public cpp v1 · immutable
·published 2007-11-19 04:23 UTC
rendered paste body
#include <iostream>#include <string>#include <vector>#include <map>using namespace std;struct madtoken{	string token;	int type;	//0 WORD	//1 [	//2 VALUEID	//3 :	//4 PROMPT	//5 ]	//6 SPACE};typedef struct madtoken madtoken;void tokenize(string madlib); /* Grammar: 	madlib: libword+ 	libword:  word		| "["		  valueid 		  ("]"		   |		   ":"		   prompt)	prompt [^\]]+	word [^\[ ]+	valueid: [^\]:]+ */vector<madtoken> toks;int main(){		string madlib; 	madlib += "[1:a proper noun] woke up. [1] didn't want to [2:a verb] today. "; 	madlib += "It's [1]'s job to [2], which makes [1] [meh:a feeling]. ";	madlib += "[1] wants to work somewhere [3:an adjective] like [4:a company]."; 	madlib += " [1] plans to do this by conquering [5:a place] with [3] [animal:an animal(plural)].";	madlib += " Then, everyone in [5] will be [meh] just like [1], and live [ly:an adverb] every after.";	cout << "Not sane libs!" << endl << endl;	map<string, string> inputs;	tokenize(madlib);	string story;	for(int i=0; i<toks.size(); i++)	{		switch(toks[i].type)		{			case 0:			story+=toks[i].token;			break;			case 1:			if(toks[i+2].type != 3)		//If not prompting			{				story+=inputs[toks[i+1].token];				i+=2;			}			else			{				cout << "Enter " << toks[i+3].token <<": ";				cin >> inputs[toks[i+1].token];				story+=inputs[toks[i+1].token];				i+=4;			}			break;			case 6:				story += " ";			}	}	cout << endl << endl << story << endl;}void tokenize(string madlib){	madlib+=" ";	madtoken tok;	int pos = 0;	int state = 0; 	string token="";		// 0 - madlib		// 1 - word		// 2 - valueid		// 3 - prompt	while(pos < madlib.length())	{		switch(state)		{			case 0:								if(madlib[pos]=='[')				{					pos++;    					tok.token="["; tok.type=1;					toks.push_back(tok);								token="";					state=2;  //Begin parsing a valueid				} else				if(madlib[pos]==']')				{					cerr << "Unexpected ']'" << endl;					return;				}				else if(madlib[pos]!=' ')				{					state=1; //Begin parsing a word					token="";				}				else				{					pos++;					tok.type=6; tok.token=" ";					toks.push_back(tok);				}				break;			case 1: //word				if(madlib[pos]=='[' || madlib[pos] == ' ')				{					tok.token=token; tok.type=0;					toks.push_back(tok);					token="";					state=0;				}				else {				token += madlib[pos];				pos++; } 			break;			case 2: //valueid 				if(madlib[pos] != ']' && madlib[pos] != ':')				{					token+=madlib[pos];					pos++;				}				else				if(madlib[pos] == ']')				{					pos++;					tok.token=token; tok.type=2;					toks.push_back(tok);					tok.token="]"; tok.type=5; // ']'					toks.push_back(tok);					token="]";					state=0;				} 				else				if(madlib[pos] == ':')				{					pos++;					tok.token=token; tok.type=2;					toks.push_back(tok);					tok.token=":"; tok.type=3; // ':'					toks.push_back(tok);					token="";					state=3; // start parsing prompt;				}				else				{					cerr << "Expected ']' or ':', found '" << madlib[pos] << "'" << endl;					return;				}			break;			case 3: //prompt (string w/ spaces)				if(madlib[pos]!=']')				{					token+=madlib[pos];					pos++;				}				else				{					pos++;					tok.token=token; tok.type=4;					toks.push_back(tok);					tok.token="]"; tok.type=5; // ']'					toks.push_back(tok);					state=0;				}		}	}	}