Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- terminate called after throwing an instance of 'std::out_of_range'
- what(): basic_string::substr
- bool is_well_formed(ifstream& ifs, string& error_msg) {
- // your code goes here
- string fname, line;
- Token tok;
- Lexer lexer;
- tags.insert("blue");
- tags.insert("red");
- tags.insert("cyan");
- tags.insert("white");
- tags.insert("yellow");
- tags.insert("magenta");
- tags.insert("dim");
- tags.insert("underline");
- tags.insert("bold");
- while (getline(cin, fname)) {
- // tries to open the file whose name is in string fname
- string name = fname.substr(1, fname.length() - 2);
- cout << "Name" + name;
- ifs.open(name.c_str());
- if (ifs.fail()) {
- cerr << "ERROR: Failed to open file " << fname << endl;
- ifs.clear();
- } else {
- while (getline(ifs, line)) {
- lexer.set_input(line);
- while (lexer.has_more_token()) {
- tok = lexer.next_token();
- string tmpTok = tok.value;
- switch (tok.type) {
- case TAG:
- // If it has /, remove / from tmpTok
- if (tok.value[0] == '/') {
- tmpTok = tmpTok.substr(1, tmpTok.length() - 1);
- }
- if (tags.find(tmpTok) == tags.end()) {
- // Check whether the encountered tag is valid
- error_return("Tag " + tmpTok + " is invalid!");
- } else {
- // Valid Tag encountered
- stack < string > tagstack;
- tagstack.push(tmpTok);
- // Check if the tags are formed properly
- if (tok.value[0] == '/') {
- // Remove / from tmpTok
- string closingTag = tmpTok;
- string openingTag = tagstack.top();
- tagstack.pop();
- if (closingTag.compare(openingTag) != 0) {
- error_return(
- closingTag + "doesn't match"
- + openingTag);
- } //else
- // return true; // if the file is well formed
- }
- }
- break;
- case IDENT:
- cout << "IDENT: " << tok.value << endl;
- break;
- case ERRTOK:
- error_return("Syntax error on this linen");
- //cout << "Syntax error on this linen";
- break;
- case ENDTOK:
- break;
- }
- }
- }
- }
- }
- return true; // if the file is well-formed
- }
- void print_well_formed_file(ifstream& ifs) {
- //Check if file is well formed.
- string line;
- Lexer command;
- if (is_well_formed(ifs, line)) { //if well formed display
- command.set_input(line);
- display(command);
- }
- }
- void display(Lexer cmd_lexer) {
- string file_name;
- if (!parse_input(cmd_lexer, file_name)) {
- error_return("Syntax error: display <filename>");
- return;
- }
- ifstream ifs(file_name.c_str());
- string error_msg;
- if (ifs) {
- if (!is_well_formed(ifs, error_msg)) {
- error_return(error_msg);
- } else {
- ifs.clear(); // clear EOF flag
- ifs.seekg(0, ios::beg); // go back to the very beginning
- print_well_formed_file(ifs);
- }
- } else {
- error_return("Can't open " + file_name + " for reading");
- }
- ifs.close();
- }
- validate <file name>
- display <file name>
- exit
- string name = fname.substr(1, fname.length() - 2);
Add Comment
Please, Sign In to add comment