Donc j'essayais de construire un petit scanner lexical qui atomise le texte et détermine le type de chaque jeton. La sortie doit être un fichier texte portant le numéro de ligne d'un jeton &, tapez à chaque ligne. Si le jeton n'est accepté par aucun élément RE, il doit signaler une erreur significative indiquant le numéro de ligne du jeton, le jeton et l'erreur. J'ai utilisé la bibliothèque regexp en C++, et maintenant j'essaie d'impliquer la fonction d'itérateur sur le code ci-dessous mais je ne savais pas comment l'utiliser ici.C++ en utilisant la fonction d'itération pour le compilateur
#include <iostream>
#include <string>
#include <regex>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
ofstream myfile;
myfile.open("mytext1.txt");
myfile << " int 33.2 + bla 059 3 " << endl;
myfile << " void nn + fbla 09 3 " << endl;
myfile << " int float + bsla 09 3.2 " << endl;
myfile.close();
string s;
regex keywords("int|if|else|while|float|return|void|breack|for");
regex id("[[:alpha:]]+[[:d:]]*[[:alpha:]]*", regex_constants::icase);
regex integer("[[:d:]]+");
regex floatt("[[:d:]]+[.]+[[:d:]]+");
regex symbolls("[&&]|[||]|[<=]|[>=]|[==]|[<]|[>]|[!=]|[=]|[(]|[)]|[{]|[}]|[;]|[,]|[.]|[+]|[-]|[*]|[/]|[/*]|[*/]");
regex comment("//[[:w:]]*");
ifstream myfile2("mytext1.txt");
//int linenum= 1;
if (myfile2.is_open())
{
while (getline(myfile2, s, ' '))
{
cout << s << ",";
//cout <<linenum<< s << ",";
bool match = regex_match(s, floatt);
if (match) cout << "float number" << endl;
match = regex_match(s, integer);
if (match)cout << "integer number" << endl;
match = regex_match(s, keywords);
if (match){ cout << "keywords" << endl; goto a;
}
match = regex_match(s, id);
if (match)cout << "identifer" << endl;
a: match = regex_match(s, comment);
if (match)cout << "comment" << endl;
match = regex_match(s, symbolls);
if (match)cout << "symbolls" << endl;}
} myfile2.close();
system("pause");
return 0;
}