1 Star 1 Fork 0

周逸群/lex_analysis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 4.08 KB
一键复制 编辑 原始数据 按行查看 历史
周逸群 提交于 2022-04-27 09:43 . first and final commit
#include<iostream>
#include<fstream>
#include <iomanip>
#include "lex.cpp"
#include<vector>
using namespace std;
ifstream lex_fin;
ifstream std_fin;
vector<int> wrongcase;
string dir_p= "D:\\2022_Spring\\CST-编译原理\\lab\\lab1\\testfiles\\";
string infilename_prefix= "testfile";
string std_prefix= "output";
string outfilename_prefix= "lex_output";
int maxn=6;//文件夹数量
int maxfn= 10;//一个文件夹下的文件数量
bool one_folder_test(int n){//测试一个目录下所有文件
bool allpass= 1;
//文件路径
string dir= dir_p+ char('0'+n);
string inFileName;
string outFileName;
//生成测试文件名并进行词法分析
for(int i=1; i<=maxfn;i++){
char *s;
sprintf(s,"%d", i);
inFileName= dir+ "\\"+ infilename_prefix + s+ ".txt";
outFileName= dir+ "\\"+ outfilename_prefix +s+ ".txt";
cout << endl;
lex_top(inFileName, outFileName,1);
}
//当前文件夹内词法分析完毕
cout << "=== === ==="<< endl;
cout << "*_All lex analyses are done_*"<< endl;
cout << "=== === ==="<< endl;
//读取词法分析输出和标准输出进行对比
for(int i=1; i<=maxfn; i++){
char s1[10];
sprintf(s1,"%d", i);
cout << endl;
cout << "--- --- --- --- --- --- --- --- ---"<< endl;
cout << left;
cout << "Case "<< i << endl;
cout << "--- --- ---"<< endl;
lex_fin.open(dir+ "\\"+ outfilename_prefix + s1+ ".txt");
std_fin.open(dir+ "\\"+ std_prefix + s1+ ".txt");
//比较
int count_l= 1;
bool isPassed= 1;//标记当前文件夹是否全部通过测试。默认为1,有错误则为0
cout << " Error "<< setw(15) << "lex_output" <<" "<< setw(15)<<"std_output" << endl;
while(1){//逐行对比,不同则报错;未同时结束则报错;
string lex_l, std_l;
lex_fin>> lex_l;
std_fin>> std_l;
if(lex_l!= std_l){
cout<< left;
cout << "At line "<< setw(5)<<count_l<< ": "<<" "<<setw(15)<< lex_l<<" " << setw(15)<< std_l<< endl;
isPassed= 0;
}
count_l++;
//至少一个文件末尾,非同时结束则报错
int isEnd= lex_fin.eof()+ std_fin.eof();
if(isEnd==2){//同时读完
if(isPassed==1){
cout << "Case pass"<< endl;
}
break;
}
else{
switch(isEnd){
case 0: break;//都在读取中
case 1: //长度不一致
isPassed= 0;
cout << "different line numbers!"<< endl;
break;
default:cout << "error"<< endl;
}
}
}
if(isPassed==0){
allpass= 0;
}
}
return allpass;
}
int main(){
cout << "Enter to select test mode"<< endl;
cout << "1= selected file test, other= folder test"<< endl;
int mode;
//cin >> mode;
mode= 1;
if(mode==1){//在指定文件上运行
cout << "*_Running File Test_*"<<endl;
// infile outfile
// lex_top(".\\mytestcase\\emptychar.txt", "output.txt");
lex_top(".\\mytestcase\\词法综合测试.txt", ".\\mytestcase\\output.txt",1);
}
else{//按文件夹运行
cout << "*_Enter folder number(-1=ALL)_*"<< endl;
int folder_n;
cin >> folder_n;
if(folder_n!=-1){//测试testfiles下一个文件夹下所有数据
if(one_folder_test(folder_n)){
cout << endl;
cout << "*_ALL PASS!_*"<< endl;
};
}
else{//测试testfiles下全部文件夹的所有数据
bool folder_all_pass=1;
for(int i=1; i<=maxn; i++){
if(one_folder_test(i)==0){
folder_all_pass= 0;
}
}
if(folder_all_pass){
cout << endl;
cout << "*_ALL PASS!_*"<< endl;
}
}
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/roswellii/lex_analysis.git
git@gitee.com:roswellii/lex_analysis.git
roswellii
lex_analysis
lex_analysis
master

搜索帮助