0
#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",正在与迭代器生成的单词进行比较。

4

1 回答 1

1

这是不正确的:

size = sizeof(keyWords); 

因为它将返回sizeof(std::string*),而不是数组中的元素数keyWords。要么将数组中的元素数量作为参数传递,要么更改为可以查询它的std::vector<std::string>(or ),或者使用and进行迭代。例如:std::array<std::string, N>size()begin()end()

const std::array<std::string, 2> keyWords = { { "do",
                                                "while" }
                                            };

更改函数以const std::array<std::string, 2>&取而代之的是一个参数。为方便起见,您可以typedef使用数组:

typedef std::array<std::string, 2> keyword_array_t;
于 2012-09-27T14:30:56.037 回答