简单的词法分析程序
编译原理实验:简单的词法分析程序
#include<iostream> #include<fstream> #include<string> using namespace std; #define D 10 #define O 11 #define K 17 char Delimiter[D]={'<', '>', ',', ';', '(', ')', '{', '}' ,'[',']' }; char Operator[O]={'=','+', '-', '*', '/', '%'}; //">=", "<=", "<<",">>" "++", "--","==" string keyword[K]={"include", "define", "iostream", "using", "namespace", "std", "int", "main","double", "bool", "float", "cout", "for", "else", "while", "do","cin"}; //判断是不是界符 bool Delimit(const char c) { for(int i=0;i<D ;i++) { if(c==Delimiter[i]) return true; } return false; } //判断是不是运算符 bool Operat(const char c) { for(int i=0;i<O;i++) { if(c==Operator[i]) return true; } return false; } //判断是不是关键字 bool Keyword(string s) { for(int i=0;i<K;i++) { if(s==keyword[i]) return true; } return false; } //判断是不是字母 bool Character(const char c) { if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) return true; else return false; } //判断是不是数字 bool Number(const char c) { if(c>='0'&&c<='9') return true; else return false; } //判断string中是否为数字,包括小数、科学计数法 bool Strnumber(string s) { for(string::iterator itr=s.begin();itr!=s.end();itr++ ) { if(Number(*itr)) continue; else if (*itr=='.') for(string::iterator it=itr+1;it!=s.end();it++ ) if (Number(*it)==true) break; else if(*it=='e') { for(string::iterator itr1=it+1;itr1!=s.end();itr1++) { if (!Number(*itr1)) { return false; } } } else return false; else if (*itr=='e') { for(string::iterator itr1=itr+1;itr1!=s.end();itr1++) { if (!Number(*itr1)) { return false; } } } else { return false; } } return true; } //判断是否为空格 bool Kongge(char c) { if (c==' ' ||c=='\n' ||c=='\t') return true; else return false; } int main() { ifstream sin("daima.txt"); ofstream sout("shuchu.txt"); if(!sin.is_open()) cout<<"文件打开出错"<<endl; char c; c=sin.get(); while(!sin.eof()) { if(Delimit(c)) { string s; if (c=='>') { s.push_back(c); sin.get(c); if (c=='='||c=='>') { s.push_back(c); sout<<s<<'\t'<<"Operator"<<endl; sin.get(c); } else sout<<s<<'\t'<<"Delimiter"<<endl; } else if (c=='<') { s.push_back(c); sin.get(c); if (c=='='||c=='<') { s.push_back(c); sout<<s<<'\t'<<"Operator"<<endl; sin.get(c); } else sout<<s<<'\t'<<"Delimiter"<<endl; } else { sout<<c<<'\t'<<"Delimiter"<<endl; c=sin.get(); } } else if(Operat(c)) { if (c=='=') { string s; s.push_back(c); c=sin.get(); if (c=='=') { s.push_back(c); sout<<s<<'\t'<<"Operator"<<endl; sin.get(c); } else { sout<<s<<'\t'<<"Operator"<<endl; } } else if(c=='+') { string s; s.push_back(c); c=sin.get(); if (c=='+') { s.push_back(c); sout<<s<<'\t'<<"Operator"<<endl; sin.get(c); } else { sout<<s<<'\t'<<"Operator"<<endl; } } else if(c=='-') { string s; s.push_back(c); c=sin.get(); if (c=='-') { s.push_back(c); sout<<s<<'\t'<<"Operator"<<endl; sin.get(c); } else { sout<<s<<'\t'<<"Operator"<<endl; } } } else if(c=='_') { string s; while(true) { if(Kongge(c)==true||Operat(c)==true ||Delimit(c)==true) break; else { s.push_back(c); sin.get(c); } } sout<<s<<'\t'<<"Identifier"<<endl; } else if(Number(c)) { string s; while(true) { if(Kongge(c)==true||Operat(c)==true ||Delimit(c)==true) break; else { s.push_back(c); sin.get(c); } } if(Strnumber(s)==true) sout<<s<<'\t'<<"Number"<<endl; else sout<<s<<'\t'<<"error character"<<endl; } else if(Character(c)) { string s; while(true) { if(Kongge(c)==true||Operat(c)==true ||Delimit(c)==true) break; else { s.push_back(c); sin.get(c); } } if(Keyword(s)) sout<<s<<'\t'<<"Keyword"<<endl; else sout<<s<<'\t'<<"Identifier"<<endl; } else if (Kongge(c)==false) { sout<<c<<'\t'<<"Error Character"<<endl; c=sin.get(); } else c=sin.get(); } cout<<"词法分析结束"<<endl; /**sin.get(c); cout<<c<<endl; string read; while(getline(sin, read, ' ')) // 逐词读取方法三 { cout << read << endl; } sout.write(c,strlen(c)); **/ sin.close(); sout.close(); return 0; }