can someone help me fix this? C++ code default: if (isalpha(ch)) { // for keywords or IDENT while (file.get(ch) && (isalpha(ch) || isdigit(ch) || ch == '_')) { lexeme += ch; } file.unget(); // push back the non-alpha character if (lexeme == "read") return "read KEY_READ"; if (lexeme == "write") return "write KEY_WRITE"; if (lexeme == "while") return "while KEY_WHILE"; if (lexeme == "do") return "do KEY_DO"; return "IDENT"; // identifier not matched with any keyword } if (isdigit(ch)) { // for INT_LIT while (file.get(ch) && isdigit(ch)) { lexeme += ch; } file.unget(); // push back the non-digit character return "INT_LIT"; } break; } return "UNKNOWN"; // unknown lexeme } int main() { ifstream file("file.txt"); if (!file.is_open()) { cerr << "Unable to open file"; return 1; } string token; while (!(token = getNextToken(file)).empty()) { cout << token << endl; } file.close(); return 0; } It will not print the IDENT and or the INT_LIT lexemes with the tokens
can someone help me fix this? C++ code
default:
if (isalpha(ch)) { // for keywords or IDENT
while (file.get(ch) && (isalpha(ch) || isdigit(ch) || ch == '_')) {
lexeme += ch;
}
file.unget(); // push back the non-alpha character
if (lexeme == "read") return "read KEY_READ";
if (lexeme == "write") return "write KEY_WRITE";
if (lexeme == "while") return "while KEY_WHILE";
if (lexeme == "do") return "do KEY_DO";
return "IDENT"; // identifier not matched with any keyword
}
if (isdigit(ch)) { // for INT_LIT
while (file.get(ch) && isdigit(ch)) {
lexeme += ch;
}
file.unget(); // push back the non-digit character
return "INT_LIT";
}
break;
}
return "UNKNOWN"; // unknown lexeme
}
int main() {
ifstream file("file.txt");
if (!file.is_open()) {
cerr << "Unable to open file";
return 1;
}
string token;
while (!(token = getNextToken(file)).empty()) {
cout << token << endl;
}
file.close();
return 0;
}
It will not print the IDENT and or the INT_LIT lexemes with the tokens
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 5 images