代码共106行 短小精悍 但效果强大
大致思路:
①预处理 去除无效字符
②逐个分析 分情况讨论(重点)
#include#include using namespace std; // 关键字表 string table[48] = { "#", "main", "if", "then", "while", "do", "static", "int", "double", "struct", "break", "else", "long", "switch", "case", "typedef", "char", "return", "const", "float", "short", "continue", "for", "void", "sizeof", "", "", "+", "-", "*", "/", "**", "==", "<", "<>", "<=", ">", ">=", "=", "[", "]", ";", "(", ")", "{", "}", "++", "--" }; void fun(string s) //判断 s 是否在关键字表中 { for(int i = 0; i < 48; i ++) //关键字 if(s == table[i]) { cout << "(" << i << "," << s << ")" << endl; return ; } cout << "(25," << s << ")" << endl; //标识符 } int main() { FILE *fp; if((fp = fopen("C:/Users/yuyu/Desktop/test.txt","rt")) == NULL) { cout << "can not open the file!" << endl; return 0; } char ch; string s0, s1; // s0为初始串,s1为预处理后的串 while((ch = fgetc(fp)) != EOF) s0 += ch; //预处理分为两步 //第一步:去注释、换行、回车、tab (转化为空格) for(int i = 0; i < s0.length(); i ++) { if(s0[i] == '/' && s0[i + 1] == '/') { do s0[i] = ' '; while(s0[++i] != 'n'); } if(s0[i] == 'n' || s0[i] == 't' || s0[i] == 'r') s0[i] = ' '; } //第二部:去空格 for(int i = 0; i < s0.length(); i ++) { if(s0[i] == ' ' && s0[i+1] == ' ') continue; else s1 += s0[i]; } cout << "after processing :" << endl << s1 << endl; int i = 0; while(i < s1.length()) { string s2; //储存中间结果的串 if(isalpha(s1[i])) // 字母 { while(isalpha(s1[i]) || isdigit(s1[i])) //关键字或字母数字串(标识符) s2 += s1[i ++]; fun(s2); } else if(s1[i] == ' ') // 空格 { i ++; continue; } else if(isdigit(s1[i])) // 数字 { while(isdigit(s1[i])) s2 += s1[i ++]; if(isalpha(s1[i])) // 数字 + 字母 非法 -1 { while(isalpha(s1[i]) || isdigit(s1[i])) s2 += s1[i ++]; cout << "(-1," << s2 << ")" << endl; } else // 数字串 { cout << "(26," << s2 << ")" << endl; } } else //单个符号 { s2 += s1[i]; if(s1[i+1] != ' ' && !isdigit(s1[i+1]) && !isalpha(s1[i+1])) // 两个符号 { if(s1[i] == s1[i + 1]) s2 += s1[i], i ++; // ++ -- ** == else if(s1[i] == '>' && s1[i + 1] == '=') s2 = ">=", i ++; else if(s1[i] == '<' && s1[i + 1] == '=') s2 = "<=", i ++; else if(s1[i] == '<' && s1[i + 1] == '>') s2 = "<>", i ++; } fun(s2); i ++; } } }



