实验报告学号:姓名:专业:计算机科学与技术班级:2班第9周#include<fstream>#include<string>#include<iostream>using namespace std;static int rowCounter = 1;//静态变量,用于存储行数static bool bracketExist = false;//判断注释存在与否,false为不存在class Lex{public:ofstream output;string line = "";Lex(string inputLine){line = inputLine;scan(Trim(line));rowCounter++;}string Trim(string &str)//函数用于去除每行前后空格{int s = str.find_first_not_of(" \t");int e = str.find_last_not_of(" \t");str = str.substr(s, e - s + 1);str += "\0";return str;}void scan(string inputLine){ofstream output;output.open("SampleOutput.txt", ios::app);string line = inputLine;int i = 0;string str = "";int temp;string token = "";output << rowCounter << ": " << line << endl;//输出每行while (line[i] != '\0')//根据DFA扫描并判断{if (line[i] == '{')//注释{bracketExist = true;}if (bracketExist == true){output << "\t" << rowCounter << ": ";while (line[i] != '}'){output << line[i];//不处理,直接输出if (line[i + 1] != NULL){i++;}elsebreak;}if (line[i] == '}')//注释结束{output << line[i]<<endl;bracketExist = false;}}if (bracketExist == false){//数字while (isdigit(line[i])){temp = temp * 10 + line[i];if (!isdigit(line[i + 1])){output << "\t" << rowCounter << ": " << "NUM, val= " << temp - '0' << endl;}if (line[i + 1] != NULL&&isdigit(line[i + 1]))i++;elsebreak;}temp = 0;//符号while (!(isdigit(line[i]) || (line[i] >= 'a'&&line[i] <= 'z') ||(line[i] >= 'A'&&line[i] <= 'Z') || line[i] == ' ' || line[i] == '{' || line[i] == '}')) {token = token + line[i];if (isdigit(line[i + 1]) || (line[i + 1] >= 'a'&&line[i + 1] <= 'z') || (line[i + 1] >= 'A'&&line[i + 1] <= 'Z') || line[i + 1] == ' ' || line[i + 1] == '{' || line[i + 1] == '}' || line[i + 1] == NULL){if (isToken(token)){output << "\t" << rowCounter << ": " << token << endl;}else{int j = 0;while (token[j] != '\0'){output << "\t" << rowCounter << ": " << token[j] << endl;j++;}}}else{i++;continue;}if (line[i + 1] != NULL)i++;elsebreak;}token = "";//字母while ((line[i] >= 'a'&&line[i] <= 'z') || (line[i] >= 'A'&&line[i] <= 'Z')){str = str + line[i];if (!((line[i + 1] >= 'a'&&line[i + 1] <= 'z') || (line[i + 1] >= 'A'&&line[i + 1] <= 'Z'))){if (isResearvedWord(str)) //判断是否是保留字{output << "\t" << rowCounter << ": " << "Reversed Word: " << str << endl;break;}else{output << "\t" << rowCounter << ": " << "ID, name= " << str << endl;break;}}if (line[i + 1] != NULL)i++;}str = "";if (line[i + 1] != NULL){i++;}elsebreak;}if (line[i + 1] == NULL){if (line[i] == ';')output << "\t" << rowCounter << ": " << line[i];break;}}//清空,以备下一行读取line = "";str = "";temp = 0;token = "";output << endl;output.close();}bool isResearvedWord(string s)//存储保留字,并判断{string reservedWord[8] = { "if", "then", "else", "end", "repeat", "until", "read", "write" };bool judge = false;for (int i = 0; i < 8; i++){if (s == reservedWord[i]){judge = true;break;}}return judge;}bool isToken(string s)//存储符号,并判断{string token[10] = { "+", "-", "*", "/", "=", "<", "(", ")", ";", ":=" };bool judge = false;for (int i = 0; i < 10; i++){if (s == token[i]){judge = true;break;}}return judge;}};int main(){ifstream input;input.open("SampleInput.tny");string line[50];int i = 0;while (getline(input, line[i])){//cout << line[i] << endl;i++;}input.close();cout << endl << endl << "Reading source file completed!!" << endl;int j = 0;remove("SampleOutput.txt");for (j = 0; j < i; j++){Lex lex(line[j]);}cout << endl << endl << "Writing file completed!!" << endl << endl << endl;return 0;}四、重要数据结构string line[]:用于存储每一行的字符,并逐个读取分析。