scanner.h
#include#include #include using namespace std;class Scanner{ private: string infile; string outfile; string key[33]; string helpkey[33]; public: Scanner(string infile_temp,string outfile_temp); void readFile(); void getToken(string s,ofstream &out); bool isLetter(char ch); bool isDigit(char ch); int reserve(const string& s);};
scanner.cpp
#include "scanner.h"using namespace std;Scanner:: Scanner(string infile_temp,string outfile_temp){ infile = infile_temp; outfile = outfile_temp; string key_temp[33] = { "","auto","double","int","struct","break","else","long","switch", "case", "enum","register","typedef","char","extern","return","union","const", "float","short","unsigned","continue","for","signed","void","default","goto", "sizeof","volatile","do","if","while","static"}; for(int i=0;i<33;i++){ key[i] = key_temp[i]; } }void Scanner::readFile(){ ifstream in(infile); ofstream out(outfile); string s; while(getline(in,s)){ getToken(s,out); }}//判断是否letterbool Scanner::isLetter(char ch){ if((ch>=65&&ch<=90)||(ch>=97&&ch<=122)||ch==35||ch==46) return true; else return false;}//判断是否数字bool Scanner::isDigit(char ch){ if(ch>=48&&ch<=57) return true; else return false;}//查找关键字int Scanner::reserve(const string& s){ for(int i=1;i<33;i++) if(s==key[i]) return i; return 0;}void Scanner::getToken(string s,ofstream &out){ size_t i=0,code; char ch; string temp=""; ch=s[i]; while(i'){ i++; ch=s[i]; if(ch=='=') out<<">="<<'\t'<<"大于"< "<<'\t'<<"小于"<