#include <queue> #include <vector> ex of the .txt file #include <string> push 3/
ID: 3732957 • Letter: #
Question
#include <queue>
#include <vector> ex of the .txt file
#include <string> push 3// this was input first so should be the first out
using namespace std; push 4
class Tokenizer{ pop
private:
queue<vector<string> > *data; //I have a queue of vector of strings in order to be able to get the entire line and not just the first word
public:
Tokenizer();
~Tokenizer();
void Tokenize(string input);// this method reads in a .txt file and gets the entire line and tokenizes validates it and saves it. For ex push 3 is valid but foobar 3 throws an std:: error message "Unexpected Token: foobar"//however I am not sure if I did the throw error message correct
vector<string> GetTokens();//this will retrieve a single set of input tokens that were passed in from the tokenize funtion in a queue fashion so FIFO and throws a std::exception with message "No tokens"//I believe I did this function wrong
};
#include "prog2_1.hpp"
#include <sstream>
#include <queue>
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
Tokenizer::Tokenizer(){
this->data = new queue<vector<string> >();
}
Tokenizer::~Tokenizer(){
delete this->data;
}
void Tokenizer::Tokenize(string input){
stringstream oss(input);
vector<string> line;
while(!oss.eof()){
string s;
oss >> s;
if(line.size() == 2 && line.at(0) == "push")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "pop")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "add")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "sub")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "mul")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "div")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "mod")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "skip")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "save")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "get")
line.push_back(s);
else
throw "Unexpected token: " + s; //I don't think I did this right help pls
}
this->data->push(line);
}
vector<string> Tokenizer:: GetTokens(){
while(!this->data->empty()){ //I believe I did this whole function wrong help please!
return this->data->pop();
}
if(this->data->empty())
throw "No tokens";
}
Explanation / Answer
#include <queue>
#include <vector>
#include <string>
class Tokenizer{
private:
queue<vector<string> > *data; //I have a queue of vector of strings in order to be able to get the entire line and not just the first word
public:
Tokenizer();
~Tokenizer();
void Tokenize(string input);/* this method reads in a .txt file and gets the entire line and tokenizes validates it and saves it.
For ex push 3 is valid but foobar 3 throws an std:: error message "Unexpected Token: foobar"//however I am not sure if I did the throw error message correct
*/
/*
* this will retrieve a single set of input tokens that were passed in from the
* tokenize funtion in a queue fashion so FIFO and throws a std::exception with message "No tokens"//I believe I did this function wrong
*/
vector<string> GetTokens();
};
#include "prog2_1.hpp"
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
Tokenizer::Tokenizer(){
this->data = new queue<vector<string> >();
}
Tokenizer::~Tokenizer(){
delete this->data;
}
void Tokenizer::Tokenize(string input){
stringstream oss(input);
vector<string> line;
while(!oss.eof()){
string s;
oss >> s;
if(line.size() == 2 && line.at(0) == "push")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "pop")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "add")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "sub")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "mul")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "div")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "mod")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "skip")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "save")
line.push_back(s);
else if(line.size() == 1 && line.at(0) == "get")
line.push_back(s);
else
throw("Unexpected token: " + s);
}
this->data->push(line);
}
vector<string> Tokenizer:: GetTokens(){
vector<string> out;
while(!this->data->empty()){
// return type of pop method in queue is void so use front and then pop the element
out = this->data->front();
this->data->pop();
}
if(this->data->empty())
throw "No tokens";
return out;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.