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>(またはstd::array<std::string, N>) に変更してクエリを実行するか、 andsize()を使用して反復します。例えば: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 に答える