调试运行正常结果,直接运行就会第一次对,后面的几次他就随机出现几个空结果,但是结果容器大小又是正确的!功能是词法分析
这是我写的代码
#include
#include
#include
#include
#include
#include
using namespace std;
#define D [0-9]
#define L [a-zA-Z_]
#define H [a-fA-F0-9]
#define E [Ee][+-]?{D}+
#define FS (f|F|l|L)
#define IS (u|U|l|L)*
unordered_map<int, pair> CSET = {
{make_pair(1,make_pair("BREAK","_"))},
{make_pair(2,make_pair("CHAR","_"))},
{make_pair(3,make_pair("DO","_"))},
{make_pair(4,make_pair("DOUBLE","_"))},
{make_pair(5,make_pair("ELSE","_"))},
{make_pair(6,make_pair("IF","_"))},
{make_pair(7,make_pair("INT","_"))},
{make_pair(8,make_pair("RETURN","_"))},
{make_pair(9,make_pair("VOID","_"))},
{make_pair(10,make_pair("WHILE","_"))},
{make_pair(11,make_pair("ID","_"))},
{make_pair(12,make_pair("NUM","_"))},
{make_pair(13,make_pair("STRING","_"))},
{make_pair(14,make_pair("ADD","_"))},
{make_pair(15,make_pair("SUB","_"))},
{make_pair(16,make_pair("MUL","_"))},
{make_pair(17,make_pair("DIV","_"))},
{make_pair(18,make_pair("GT","_"))},
{make_pair(19,make_pair("GE","_"))},
{make_pair(20,make_pair("LT","_"))},
{make_pair(21,make_pair("LE","_"))},
{make_pair(22,make_pair("EQ","_"))},
{make_pair(23,make_pair("NE","_"))},
{make_pair(24,make_pair("ASSIGN","_"))},
{make_pair(25,make_pair("LB","_"))},
{make_pair(26,make_pair("RB","_"))},
{make_pair(27,make_pair("LR","_"))},
{make_pair(28,make_pair("RR","_"))},
{make_pair(29,make_pair("COMMA","_"))},
{make_pair(30,make_pair("SEMI","_"))},
{make_pair(31,make_pair("XOR","_"))}
};
vector> ans;
vector keywords = { "break", "char", "do","double","else","if",\
"int","return","void","while" };
vector symbols = { "+","-","*","/",">",">=","<","<=","==","!=",\
"=","{", "}","(",")",",",";","^","\"\"" };
regex rowzhushi("//.*");
regex kuaizhushi("/\\*.*\\*/");
regex ID("[a-zA-Z_]([a-zA-Z_]|[0-9])*");
regex reNum("(0[xX][a-fA-F0-9]+(u|U|l|L)*?)|(0[0-9]+(u|U|l|L)*?)|([0-9]+(u|U|l|L)*?)|(L?'(\\.|[^\\'])+')\
|([0-9]+[Ee][+-]?[0-9]+(f|F|l|L)?)|([+-]?[0-9]*\\.[0-9]+([Ee][+-]?[0-9]+)?(f|F|l|L)?)|([+-]?[0-9]+\\.[0-9]\
*([Ee][+-]?[0-9]+)?(f|F|l|L)?)");
//regex reNum("^[+-]?(0|([1-9]\\d*))(\\.\\d+)?([LluUFf])?|(0[xX][0-9a-fA-F]+)([LluUFf])?|(-?(\\d+\\.\\d+)e[+-]\\d+)|(L?'(\\.|[^\\'])+')$");
regex split("([,;\\(\\)^\"+=\\n\\t{}<>!])|(\\s+)");
regex preof("/\\*.*");
regex endof(".*\\*/");
void trim(string& s)
{
if (s.empty())
{
return;
}
s.erase(0, s.find_first_not_of(' '));
s.erase(s.find_last_not_of(' ') + 1);
s.erase(0, s.find_first_not_of('\t'));
s.erase(s.find_last_not_of('\t') + 1);
}
int isKeyword(string word) { //判断是否为关键词
vector::iterator it = find(keywords.begin(), keywords.end(), word);
if (it != keywords.end()) {
return it - keywords.begin() + 1;
}
return 0;
}
int isSymbol(string word) { //判断是否为符号
vector::iterator it = find(symbols.begin(), symbols.end(), word);
if (it != symbols.end()) {
return it - symbols.begin() + 14;
}
return 0;
}
void addans(string buffer) {
if (isKeyword(buffer)) {
int index = isKeyword(buffer);
ans.push_back(make_pair(CSET[index].first, CSET[index].second ));
}
else if (regex_match(buffer, reNum)) {
ans.push_back(make_pair(CSET[12].first, buffer ));
}
else if (isSymbol(buffer)) {
int index = isSymbol(buffer);
ans.push_back(make_pair(CSET[index].first, CSET[index].second ));
}
else if (regex_match(buffer, ID)) {
ans.push_back(make_pair(CSET[11].first, buffer ));
}
}
int main(void) {
string filename = "code.txt";
ifstream fin(filename, ifstream::in);
if (fin) {
string line;
while (getline(fin, line)) {
trim(line);
if (regex_match(line, rowzhushi) || regex_match(line, kuaizhushi)) continue;
else if (regex_match(line, preof)) {
while (getline(fin, line))
{
trim(line);
if (regex_match(line, endof)) break;
}
continue;
}
int length = line.size();
string buffer("");
for (int i = 0; i < length; i++) {
char ch = line.at(i);
string test;
test.push_back(ch);
if (regex_search(test, split)) {
if (buffer != "") {
addans(buffer);
buffer = "";
}
buffer = "";
if (ch == '>' || ch == '<' || ch == '!') {
if (i < length-1 && (line.at(i + 1) == '=')) {
string temp(test);
temp.push_back('=');
i++;
int index = isSymbol(temp);
ans.push_back(make_pair(CSET[index].first, CSET[index].second ));
}
else {
int index = isSymbol(test);
ans.push_back(make_pair(CSET[index].first, CSET[index].second ));
}
}
else if (ch == '\"') {
string temp;
temp.push_back('\"');
while (i < length-1 && line.at(i + 1) != '\"') {
temp.push_back(line.at(i + 1));
i++;
}
temp.push_back(line.at(i + 1));
i++;
int index = isSymbol(temp);
ans.push_back(make_pair(CSET[13].first, temp ));
}
else if (ch != '\t' && ch != '\n' && ch != ' ') {
int index = isSymbol(test);
ans.push_back(make_pair(CSET[index].first, CSET[index].second ));
}
}
else if (i == length - 1) {
if (buffer != "") {
buffer.push_back(line.at(i));
addans(buffer);
}
buffer = "";
}
else {
buffer.push_back(ch);
}
}
}
fin.close();
for (vector> ::iterator it = ans.begin(); it != ans.end();it++) {
cout << "(" << it->first << "," << it->second << ")" << endl;
}
//cout << ans.size() << endl;
}
else {
cout << "读取文件错误" << endl;
}
return 0;
}
这是测试文件
/* A test
C program for
scanner
*/
int main()
{
double W, b;
double Y_predicted;
int passenger_id, survived, pclass;
W = 0.0;
b = 0.005;
Y_predicted = 1;
passenger_id= 1000L;
survived = 505u;
pclass = L'\10a0cc';
if (passenger_id >= 100)
W += 5.6372e-10;
else
b = 9.78f -0.005 * W;
if (Y_predicted < 1)
passenger_id = 0X654E;
else
passenger_id = 0X054EL;
survived = 2^pclass;
print("end");
}
代码太多,就不看了
调试和实际运行有2处不同
一个是默认的路径不同,可能读写的不是同一个文件
一个是内存管理不同,调试的相对对内存越界比较宽容