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 scannerconst string& filename
ifstream filefilename;
if file.isopen
throw runtimeerrorCould not open file";
vector tokens;
string line;
while getlinefile line
line regexreplaceline regex$$; Strip leadingtrailing whitespace
if lineempty line.find line.find string::npos line.find string::npos
continue;
regex tokenregexRwws;
auto wordsbegin sregexiteratorlinebegin line.end tokenregex;
auto wordsend sregexiterator;
for auto i wordsbegin; i wordsend; i
tokens.pushbackistr;
file.close;
return tokens;
Function to parse tokens and categorize them
vector parserconst vector& tokens
vector result;
for sizet i ; i tokens.size; i
const string& token tokensi;
if isalphatoken
if token "read" token "write"
result.pushbacktoken "keyword";
else
result.pushbacktoken "identifier";
else if isdigittoken
if i tokens.size && tokensi && isdigittokensi
result.pushbacktoken tokensi "number";
i ;
else
result.pushbacktoken "number";
else if token :
if i tokens.size && tokensi
result.pushback: "assignment";
i;
else
result.pushbacktoken "unknown";
else if token
result.pushbacktoken "add operator";
else if token
result.pushbacktoken "multioperator";
else
result.pushbacktoken "unknown";
return result;
Function to check the grammar of parsed tokens
bool checkgrammarconst vector& parsedtokens
for const auto& tokenpair : parsedtokens
const string& token tokenpair.first;
const string& category tokenpair.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: ;
getlinecin filename;
vector tokens scannerfilename;
cout "Tokens: ;
for const auto& token : tokens
cout token ;
cout endl;
vector parsedtokens parsertokens;
cout "Parsed Tokens:" endl;
for const auto& tokenpair : parsedtokens
cout tokenpair.first tokenpair.second endl;
if checkgrammarparsedtokens
cout "The expression matches the grammar." endl;
else
cout "The expression does not match the grammar." endl;
catch const exception& e
cerr "Error: ewhat endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
