#include <iostream>#include <fstream>#include <cstring>#include <cstdlib>#include <cctype>#include <string>using namespace std;const int LINE=99999;const int BUF_SIZE=1000;int totalline=0; //总行数int useful[LINE]= {0};//简单类型const int STYPES_SIZE=3;string simpleTypes[STYPES_SIZE]= {"integer_type","real_type","void_type"};const int NODE_NUM=999;int count=0; //结点数组的结点个数typedef struct eNode { string enode_name; int enode_value; eNode *next;} eNode;typedef struct vNode{ string vnode_name; int vnode_type; eNode *head;} vNode;vNode vnodelist[NODE_NUM];int allude[NODE_NUM]; //编号映射数组void toSameLine(const char* filename,const char* destname){ ifstream fin(filename); ofstream fout(destname); if(!fout) { cout<<"create error"; return; } if(!fin) { cout<<"open error"; return; } char buf[BUF_SIZE]; char tmp[BUF_SIZE]; fin.getline(buf,1000); while(!fin.eof()) { fin.getline(tmp,1000); if(tmp[0]=='@') { fout<<buf<<endl; totalline++; strcpy(buf,tmp); } else { strcat(buf,tmp); } } cout<<"写到一行完成!一共有"<<totalline<<"行"<<endl; fin.close(); fout.close();}void pointToLine(ifstream &fp,int line) //将文件指针指向指定行{ fp.seekg(0L,ios::beg); char buf[BUF_SIZE]; int i=0; for(; i<line; i++) { fp.getline(buf,BUF_SIZE); }}void regulateTree(const char* filename,const char* destname){ ifstream fin(filename); if(!fin) { cout<<"open error"<<endl; return; } ofstream fout(destname); if(!fout) { cout<<"create error"<<endl; return ; } int i=0; char buf[BUF_SIZE]; string str; while(!fin.eof()) { fin.getline(buf,BUF_SIZE); str=buf; if(str.find("srcp")!=string::npos) useful[i]=-1; if(str.find("srcp: test.c")!=string::npos) { useful[i]=1; //有用节点 // cout<<str<<endl; // fout<<str<<endl; } i++; } //cout<<"no"; fin.clear(); fin.seekg(0,ios::beg); i=0; while(!fin.eof()) { fin.getline(buf,BUF_SIZE); if(useful[i]!=0) //如果不是待定节点 { int pos=0; str=buf; string s; int num; pos=str.find('@',pos); while(pos<str.length()&&pos!=string::npos) { int t=str.find(' ',pos); s=str.substr(pos+1,t-pos-1); num=atoi(s.c_str()); if(useful[num-1]==0||useful[num-1]==-1) { if(useful[i]==1) useful[num-1]=1; else useful[num-1]=-1; } pos=str.find('@',pos+1); } } i++; } //cout<<"no"; fin.clear(); fin.seekg(0,ios::beg); i=0; while(!fin.eof()) { fin.getline(buf,BUF_SIZE); string s=buf; int pos=s.find("call_expr"); if(useful[i]==1 || pos!=string::npos) { fout<<buf<<endl; // cout<<buf<<endl; } i++; } fin.close(); fout.close();}bool isElimanate(string nodename,string s,string &nodenum,string& restr) //根据节点类型判断是否删掉s代表的字段{ int i; string former; for(i=0; i<STYPES_SIZE; i++) { if(nodename==simpleTypes[i]) return true; } if(nodename.compare("identifier_node")==0) { if(s.find("lngt")!=string::npos) return true; } if(nodename.compare("scope_stmt")==0&&s.compare("begin")!=0&&s.compare("end")!=0&&s.compare("line")!=0&&s.compare("next")!=0) return true; if(s.find("algn")!=string::npos||s.find("srcp")!=string::npos) return true; int pos=0; if((pos=s.find('@'))!=string::npos) { string tmp; /* while(!isspace(s[pos])&&pos<s.length()) pos++;*/ tmp=s.substr(pos+1,s.length()-pos-1); former=tmp; int n=atoi(tmp.c_str()); tmp=s.substr(0,pos+1); if(useful[n-1]!=1) { nodenum=former; restr=tmp; return true; } else { nodenum=former;//返回字段编号 restr=tmp; return false; } } nodenum=-1; restr=s; return false;}/******建立抽象语法树**************/void createAbstractTree(const char* filename){ ifstream fin(filename); if(!fin) { cout<<"open error"<<endl; return ; } int renum=0; //为结点重新编号 int totalnum=0; char buf[BUF_SIZE]; string s,tmp; eNode *p=0; int len; while(!fin.eof()) { fin.getline(buf,BUF_SIZE); s=buf; len=s.length(); if(len>0) { int i=1,j,n; j=s.find(' ',i); tmp=s.substr(i,j-i); n=atoi(tmp.c_str()); allude[count]=n; count++; } } totalnum=count; fin.clear(); fin.seekg(0,ios::beg); count=0; while(!fin.eof()) { fin.getline(buf,BUF_SIZE); //cout<<buf<<endl; s=buf; len=s.length(); if(len>0) { int i=1,j; while(!isalpha(buf[i])&&i<len) i++; j=s.find(' ',i); tmp=s.substr(i,j-i); cout<<tmp<<" "; vnodelist[count].vnode_name=tmp; vnodelist[count].head=new eNode; vnodelist[count].head->next=NULL; p=vnodelist[count].head; while(j<len) { while(isspace(buf[j])&&j<len) { j++; } if(j==len) break; i=j; j=s.find(':',i); j++; while(isspace(buf[j])) j++; while(!isspace(buf[j])&&j<len) j++; tmp=s.substr(i,j-i); cout<<tmp<<" "<<endl; string temp,former; if(!isElimanate(vnodelist[count].vnode_name,tmp,former,temp)) //判断字段能否被消除 { eNode* e=new eNode; if(!e) { cout<<"allocate error"; return ; } renum=atoi(former.c_str()); if(renum>0) { int k=0; for(; k<totalnum; k++) { if(renum==allude[k]) { break; } } e->enode_name=temp; e->enode_value=k; } else { e->enode_name=temp; e->enode_value=-1; } e->next=NULL; p->next=e; p=p->next; } } count++; } } fin.close();}// 遍历抽象树void printTree(vNode v[],int n,const char* filename){ ofstream fout(filename); if(!fout) { cout<<"create error"; return; } int i=0; eNode *p; for(; i<n; i++) { // cout<<v[i].vnode_name<<" "; fout<<"@"<<i<<": "<<v[i].vnode_name<<" "; p=v[i].head->next; while(p) { // cout<<p->enode_name<<" "; if(p->enode_value!=-1) fout<<p->enode_name<<p->enode_value<<" "; else { fout<<p->enode_name<<" "; } p=p->next; } // cout<<endl; fout<<endl; } fout.close();}void freespace(vNode v[],int n){ eNode *p,*q; int i=0; for(; i<n; i++) { q=v[i].head; p=v[i].head->next; while(p) { q->next=p->next; delete p; p=q->next; } }}int main(){ const char* filename="e:\\b.txt"; const char* destname="e:\\写到一行.txt"; const char* f3="e:\\有用节点.txt"; const char* final="e:\\消除冗余后的抽象树.txt"; toSameLine(filename,destname); cout<<filename<<"->"<<destname<<" 写入一行完成"<<endl; cout<<"开始规范化...."<<endl; regulateTree(destname,f3); cout<<destname<<"->"<<f3<<" 规范化完毕"<<endl; cout<<"建立抽象树..."<<endl; createAbstractTree(f3); cout<<"共"<<count<<"结点"<<endl; cout<<"建立抽象树完毕"<<endl; printTree(vnodelist,count,final); cout<<"抽象语法树写入"<<final<<"完毕"<<endl; cout<<"*****************"<<endl; cout<<useful[1782]; freespace(vnodelist,count); return 0;}