开始学习编译原理了耶~
关于编译原理的全部练习,按照老规矩,仍是用我最喜欢的C#语言来实现,运行在.NetCore平台上~
关于这个系列的全部代码已经上传到github了,项目主页:html
https://github.com/Deali-Axy/CompilerConstructionLearning
对C或C++等高级程序设计语言编写的源程序中的//注释和/ …/注释进行删除,保留删除后的源程序。要求以文件形式进行保存。
程序主要功能就是消除已经编写好的源程序中的注释。在源程序中注释有两种形式,一种是单行注释,用“//”表示,另外一种是多行注释,用“/ …/”表示。针对这两种形式,程序中用了if..else..语句加以判断,并作出相应的处理。在这里还有可能出现另外一种状况,上述两种注释符号可能出如今引号中,出如今引号中的注释符号并无注释功能,所以在引号中出现的注释符号不该该被消除。因此,此次编写的程序将要分三种状况分析。
if (ch != temp) { // 这里就是单行注释 ofile.put(ch); ch = ifile.get(); }
或者ios
if (ch != temp) { /* 这里就是单行注释 */ ofile.put(ch); ch = ifile.get(); }
if (ifile.fail() || ofile.fail()) { cerr << "open file fail\n"; return EXIT_FAILURE; /*返回值EXIT_FAILURE(在cstdlib库中定义),用于向操做系统报* 告打开文件失败*/ }
ifile.close(); // 关闭文件 ofile.close(); cout << "/////*////ret/rtr////"; system("pause"); return 0;
能够看到这一行git
cout << "/////*////ret/rtr////";
这个字符串用双引号包起来的代码中有不少斜杠,因此要避免将这些斜杠识别为注释。
这里我用的方法是在处理注释前先把包含注释符号的字符串替换掉,等注释删除以后,再添加回去。github
注释写得很详细啦,配合上面的思路分析,我就再也不继续分析代码了~正则表达式
var sReader = new StreamReader(filePath); var newSource = ""; var inBlock = false; var replaceFlag = false; var tempLine = ""; // 用于保存被替换的特殊行代码 while (!sReader.EndOfStream) { var line = sReader.ReadLine(); if (line.Length == 0) continue; // 去除空行 var quotationPattern = "^(.*?)\".*//.*\""; var quotationResult = Regex.Match(line, quotationPattern); if (quotationResult.Success) { System.Console.WriteLine("替换特殊代码,双引号中包裹注释斜杠"); tempLine = quotationResult.Groups[0].Value; replaceFlag = true; line = Regex.Replace(line, quotationPattern, REPLACEMENT); } // 单行注释 if (line.Trim().StartsWith(@"//")) continue; if (line.Trim().StartsWith(@"/*") && line.EndsWith(@"*/")) continue; // 注释块 if (Regex.Match(line.Trim(), @"^/\*").Success) inBlock = true; if (Regex.Match(line.Trim(), @"\*/$").Success) { inBlock = false; continue; } // 行后注释 // 使用非贪婪模式(.+?)匹配第一个// var pattern = @"^(.*?)//(.*)"; // var pattern = @"[^(.*?)//(.*)]|[^(.*?)/\*(.*)\*/]"; var result = Regex.Match(line, pattern); if (result.Success) { System.Console.WriteLine("发现行后注释:{0}", result.Groups[2]); line = result.Groups[1].Value; } // 还原被替换的代码 if (replaceFlag) { System.Console.WriteLine("还原特殊代码"); line = line.Replace(REPLACEMENT, tempLine); replaceFlag = false; } if (inBlock) continue; newSource += line + Environment.NewLine; } var outputPath = "output/exp1.src"; System.Console.WriteLine("去除注释完成,建立新文件。"); using (var sWriter = new StreamWriter(outputPath)) { sWriter.Write(newSource); } System.Console.WriteLine("操做完成!文件路径:{0}", outputPath);
#include <iostream> #include <fstream> #include <iomanip> #include <cstdlib> using namespace std; int main() { cout << '/'; ifstream ifile; //创建文件流对象 ofstream ofile; ifile.open("f:\\上机实验题\\C++\\ConsoleApplication2\\ConsoleApplication2\\源.cpp"); //打开F盘根目录下的fileIn.txt文件 ofile.open("f:\\上机实验题\\C++\\ConsoleApplication2\\ConsoleApplication2\\源.obj"); if (ifile.fail() || ofile.fail()) { //测试打开操做是否成功 cerr << "open file fail\n"; return EXIT_FAILURE; /*返回值EXIT_FAILURE(在cstdlib库中定义),用于向操做系统报* 告打开文件失败*/ } char ch; ch = ifile.get(); //进行读写操做 while (!ifile.eof()) { if (ch == 34) { //双引号中若出现“//”,双引号中的字符不消除 char temp = ch; //第一个双引号 ofile.put(ch); ch = ifile.get(); while (!ifile.eof()) { if (ch != temp) { //寻找下一个双引号 ofile.put(ch); ch = ifile.get(); } else { ofile.put(ch); break; } } ch = ifile.get(); continue; //双引号状况结束,从新新一轮判断 } if (ch == 47) { //出现第一个斜杠 char temp2 = ch; ch = ifile.get(); if (ch == 47) { //单行注释状况 ch = ifile.get(); while (!(ch == '\n')) ch = ifile.get(); } else if (ch == '*') { //多行注释状况 while (1) { ch = ifile.get(); while (!(ch == '*')) ch = ifile.get(); ch = ifile.get(); if (ch == 47) break; } ch = ifile.get(); } else { ofile.put(temp2); //temp2保存第一个斜杠,当上述两种状况都没有时,将此斜杠输出 } //ch = ifile.get(); } //cout << ch << endl; ofile.put(ch); //将字符写入文件流对象中 ch = ifile.get(); //从输入文件对象流中读取一个字符 } ifile.close(); //关闭文件 ofile.close(); cout << "/////*////ret/rtr////"; system("pause"); return 0; }
#include <iostream> #include <fstream> #include <iomanip> #include <cstdlib> using namespace std; int main() { cout << '/'; ifstream ifile; ofstream ofile; ifile.open("f:\\上机实验题\\C++\\ConsoleApplication2\\ConsoleApplication2\\源.cpp"); ofile.open("f:\\上机实验题\\C++\\ConsoleApplication2\\ConsoleApplication2\\源.obj"); if (ifile.fail() || ofile.fail()) { cerr << "open file fail\n"; return EXIT_FAILURE; } char ch; ch = ifile.get(); while (!ifile.eof()) { if (ch == 34) { char temp = ch; ofile.put(ch); ch = ifile.get(); while (!ifile.eof()) { if (ch != temp) { ofile.put(ch); ch = ifile.get(); } else { ofile.put(ch); break; } } ch = ifile.get(); continue; } if (ch == 47) { char temp2 = ch; ch = ifile.get(); if (ch == 47) { ch = ifile.get(); while (!(ch == '\n')) ch = ifile.get(); } else if (ch == '*') { while (1) { ch = ifile.get(); while (!(ch == '*')) ch = ifile.get(); ch = ifile.get(); if (ch == 47) break; } ch = ifile.get(); } else { ofile.put(temp2); } } ofile.put(ch); ch = ifile.get(); } ifile.close(); ofile.close(); cout << "/////*////ret/rtr////"; system("pause"); return 0; }
https://github.com/Deali-Axy/CompilerConstructionLearning/blob/master/code/exp/exp1/Exp1.cssegmentfault
参考资料:微信