#include<iostream>
#include<regex>
#include<string>
#include<fstream>
#include<list>
#include<vector>
#include<string>
using namespace std;
void lexical_words(string,string*,vector<string> &); // the forloop is not properly comparing the words in my "keyWords" with words from the iterator
void lexical_integer(string);
void lexical_operators(string);
void lexical_reals(string);
void lexical_separators(string);
void lexical_identifies(string);
list<string> someWords;
void lexical_integer(string seq)
{
ofstream fout;
fout.open("output.txt",ios::app);
regex digits("(\\d+)");
regex_iterator<string::iterator> itd(seq.begin(), seq.end(), digits);
regex_iterator<string::iterator> end;
for (; itd != end; ++itd)
{
cout << itd->str() <<" " <<" integer"<< endl;
fout<<itd->str()<< " "<<" integer"<<endl;
}
fout.close();
}
void lexical_identifier(string seq)
{
ofstream fout;
fout.open("output.txt",ios::app);
regex digits("^[a-zA-Z_][a-zA-Z0-9_]*$");
regex_iterator<string::iterator> itd(seq.begin(), seq.end(), digits);
regex_iterator<string::iterator> end;
for (; itd != end; ++itd)
{
cout << itd->str() <<" " <<" identifier"<< endl;
fout<<itd->str()<< " "<<" identifier"<<endl;
}
fout.close();
}
void lexical_separators(string seq)
{
ofstream fout;
fout.open("output.txt",ios::app);
regex digits("(\\W)");
regex_iterator<string::iterator> itd(seq.begin(), seq.end(), digits);
regex_iterator<string::iterator> end;
for (; itd != end; ++itd)
{
cout << itd->str() <<" " <<" Separator"<< endl;
fout<<itd->str()<< " "<<" Separator"<<endl;
}
fout.close();
}
void lexical_reals(string seq)
{
ofstream fout;
fout.open("output.txt",ios::app);
regex digits("^[0-9]*(\\.[0-9]+)$");
regex_iterator<string::iterator> itd(seq.begin(), seq.end(), digits);
regex_iterator<string::iterator> end;
for (; itd != end; ++itd)
{
cout << itd->str() <<" " <<" real number"<< endl;
fout << itd->str() <<" " <<" real number"<< endl;
}
fout.close();
}
**void lexical_words(string seq,string * keyWords,vector<string>& sVector)
{
ofstream fout;
fout.open("output.txt",ios::app);
regex words("(\\w+)");
regex_iterator<string::iterator> itd(seq.begin(), seq.end(), words);
regex_iterator<string::iterator> end;
//int size;
//size = sizeof(keyWords);
for (; itd != end; itd++)
{
for(int k = 0; k < sVector().size;k++)
{
if(keyWords[k] == itd->str())
{
cout << itd->str() <<" " <<" keyword"<< endl;
fout << itd->str() <<" " <<" keyword"<< endl;
}
}
}
fout.close();
}**
int main()
{
char operators[] ={'+','-','*','`','!','\\','&','|','~',
'<','>','=',':','%','^'};
char separators[] = { '(', ')', '{','}','[', ']', ';', ':', '"', '?', ',', '.', '//', '#'};
char numbers[] = {'0','1','2','3','4','5','6','7','8','9'};
string keyWords[] = {"while","for","do","if","else","int","double","long","array","break",
"case","catch","switch","bool","char","class","const","continue","default","delete",
"enum","event","enum class","explicit","extern","false","finally","float","friend","goto"
"new","private","protected","static","short","true","void","try","this","struct","throw","signed","cin","cout","using"
"namespace","std"};
vector<string> sVector;
sVector.assign(keyWords,keyWords+47);
string line;
ifstream fin;
string fName;
//cout<<"please enter your file name"<<endl;
//cin>>fName;
//fin.open(fName.c_str());
//while(!fin)
//{
//cout<<"wrong file name"<<endl;
//cin>>fName;
fin.open("C:\\Users\\vishal\\Desktop\\tokens.txt");
//}
while(!fin.eof())
{
fin>>line;
lexical_words(line,keyWords,sVector);
lexical_identifier(line);
lexical_separators(line);
lexical_integer(line);
lexical_reals(line);
}
cout<<"The same output has also been created in a file called output.txt"<<endl;
system("pause");
return 0;
}
我写了一个分词器程序。我想将我从正则表达式迭代器中获得的单词与我的列表中名为“keyWords”的单词进行比较,如果它们匹配则将它们输出到屏幕上。但是,由于某种原因,我的 forloop 没有推进计数器,并且只有我的列表 "keyWords" 的第一个单词,即 "while",正在与迭代器生成的单词进行比较。