Question: i need help with this C + + code, the output should be like the picture. code: #include #include #include #include #include using namespace std;

i need help with this C++ code, the output should be like the picture.
code: #include
#include
#include
#include
#include
using namespace std;
// Function to read the file and tokenize the input
vector scanner(const string& filename){
ifstream file(filename);
if (!file.is_open()){
throw runtime_error("Could not open file");
}
vector tokens;
string line;
while (getline(file, line)){
line = regex_replace(line, regex("^+|+$|()+"),"$1"); // Strip leading/trailing whitespace
if (line.empty()|| line.find("//")==0|| line.find("/*")!= string::npos || line.find("*/")!= string::npos){
continue;
}
regex token_regex(R"(\w+|[^\w\s])");
auto words_begin = sregex_iterator(line.begin(), line.end(), token_regex);
auto words_end = sregex_iterator();
for (auto i = words_begin; i != words_end; ++i){
tokens.push_back(i->str());
}
}
file.close();
return tokens;
}
// Function to parse tokens and categorize them
vector> parser(const vector& tokens){
vector> result;
for (size_t i =0; i tokens.size(); ++i){
const string& token = tokens[i];
if (isalpha(token[0])){
if (token == "read" || token == "write"){
result.push_back({token, "keyword"});
} else {
result.push_back({token, "identifier"});
}
} else if (isdigit(token[0])){
if (i +2 tokens.size() && tokens[i +1]=="." && isdigit(tokens[i +2][0])){
result.push_back({token +"."+ tokens[i +2], "number"});
i +=2;
} else {
result.push_back({token, "number"});
}
} else if (token ==":"){
if (i +1 tokens.size() && tokens[i +1]=="="){
result.push_back({":=", "assignment"});
++i;
} else {
result.push_back({token, "unknown"});
}
} else if (token =="+"){
result.push_back({token, "add operator"});
} else if (token =="*"){
result.push_back({token, "multi-operator"});
} else {
result.push_back({token, "unknown"});
}
}
return result;
}
// Function to check the grammar of parsed tokens
bool check_grammar(const vector>& parsed_tokens){
for (const auto& token_pair : parsed_tokens){
const string& token = token_pair.first;
const string& category = token_pair.second;
if (category == "unknown" ||
(category == "assignment" && token !=":=")||
(category == "keyword" && (token != "read" && token != "write"))){
return false;
}
}
return true;
}
int main(){
try {
string filename;
cout "Enter the filename: ";
getline(cin, filename);
vector tokens = scanner(filename);
cout "Tokens: ";
for (const auto& token : tokens){
cout token "";
}
cout endl;
vector> parsed_tokens = parser(tokens);
cout "Parsed Tokens:" endl;
for (const auto& token_pair : parsed_tokens){
cout "(" token_pair.first "," token_pair.second ")" endl;
}
if (check_grammar(parsed_tokens)){
cout "The expression matches the grammar." endl;
} else {
cout "The expression does not match the grammar." endl;
}
} catch (const exception& e){
cerr "Error: " e.what() endl;
}
return 0;
}
i need help with this C + + code, the output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!