Professional Documents
Culture Documents
#include <algorithm>
#include <fstream>
#include<ctype.h>
#include <vector>
#include <string>
return true;
}
return false;
}
bool is_My_Statement(const string &str)
{
const vector< string> statements{ "for", "while" };
for (const auto& statement : statements)
if (statement == str)
return true;
return false;
}
return false;
}
return false;
}
string buffer;
ifstream file(nameOfFile);
char data;
if (file.is_open()) {
cout << "Lexical Analyzer" << endl;
while (file.eof()) {
ch = file.get();
// Output the text from the file
}
}
else {
cout << "error while opening the file\n";
exit(0);
}
bool multiComment = false, singleComment = false;
while (file >> noskipws >> ch)
{
if (singleComment || multiComment)
{
if (singleComment && ch == '\n')
singleComment = false;
if (ch == '/')
multiComment = false;
}
continue;
}
if (ch == '/')
{
string comm(1, ch);
file >> ch;
if (ch == EOF)
{
My_Role_Of_Token(comm);
break;
}
if (ch == '*')
{
multiComment = true;
comm += ch;
}
else if (ch == '/')
{
singleComment = true;
comm += ch;
}
if (multiComment || singleComment)
{
My_Role_Of_Token(comm);
continue;
}
}
if (is_Illegal(string(1, ch)))
{
if (!buffer.empty())
{
My_Role_Of_Token(buffer);
buffer = "";
}
continue;
}
if (is_My_Operator(string(1, ch)))
{
if (!buffer.empty() && !is_My_Operator(buffer))
{
My_Role_Of_Token(buffer);
buffer = "";
}
}
if (is_My_Separator(string(1, ch)))
{
if (!buffer.empty())
{
My_Role_Of_Token(buffer);
buffer = "";
}
if (is_My_Separator(string(1, ch)))
{
My_Role_Of_Token(string(1, ch));
continue;
}
}
buffer += ch;
}
file.close();
char str[100];
string Read_and_write_string;
char c;
file.open("Readme.txt");
if (!file.is_open()) {}
else
{
while (!file.eof())
{
file.get(c);
if (c != ' ' && c != '\n' && c != '\t')
{
if (c == '/')
{
file.get(c);
if (c == '/')
{
while (c != '\n')
{
file.get(c);
}
}
else if (c == '*')
{
file.get(c);
while (c != '*')
{
file.get(c);
}
file.get(c);
}
file.get(c);
}
if (c != '\n' && c != '\t')
{
Read_and_write_string += c;
}
}
}
Read_and_write_string = Read_and_write_string.substr(0,
Read_and_write_string.length() - 1);
cout << Read_and_write_string << "\n\n";
}
}
int main()
{
My_lexical_Analyzer("Readme.txt");
system("pause");
return 0;
}