2012-09-27 1 views
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; 

}왜 내 문자열 반복자에 단어와 함께 내 문자열 배열에있는 단어 ('키워드 ")를 비교하지, 내 lexical_words 기능에, 내 forloop입니까?

나는 토크 나이 프로그램을 작성했습니다

. I 정규식 반복기에서 얻은 단어를 "keyWords"라는 내 목록의 단어와 비교하고 일치하는 항목을 화면에 출력하려고하지만 어떤 이유로 forloop이 카운터를 전진시키지 않고 내 목록의 첫 단어 "while"은 반복자가 생성 한 단어와 비교됩니다.

답변

1

정확하지 않음 :

size = sizeof(keyWords); 

sizeof(std::string*)을 반환하며 배열 keyWords의 요소 수는 반환하지 않습니다. 배열의 요소 수를 인수로 전달하거나 size()에 대해 쿼리 할 수있는 std::vector<std::string> (또는 std::array<std::string, N>)으로 변경하거나 begin()end()을 사용하여 반복합니다. 예 :

대신에 const std::array<std::string, 2>& 인수를 사용하는 함수를 변경하십시오. 편의상 배열을 typedef

+0

어떻게 문자열 배열을 벡터로 변환 할 수 있습니까? – chucknorris

+0

'#include '에서 C++ 11 기능을 사용할 수 있다고 생각하십니까? – hmjd

+0

코드를 변경했지만 – chucknorris