c++ 简单的词法分析
scanner.h
#include<iostream> #include<fstream> #include<string> 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); } } //判断是否letter bool 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<s.length()){ //如果是空跳过 while(i<s.length()&&ch==' '){ i++; ch=s[i]; } //是字母 if(isLetter(ch)){ while((isLetter(ch)||isDigit(ch))&&i<s.length()){ temp+=ch; i++; ch=s[i]; } i--; code=reserve(temp); if(code==0){ out<<temp<<'\t'<<"标识符"<<endl; temp=""; }else{ out<<temp<<'\t'<<"关键字"<<endl; temp=""; } }else if(isDigit(ch)){ while(isDigit(ch)){ temp+=ch; i++; ch=s[i]; } i--; out<<temp<<'\t'<<"常数"<<endl; temp=""; }else if(ch=='='){ i++; ch=s[i]; if(ch== '=' ) out<<"=="<<'\t'<<"判断相等"<<endl; else{ i--; out<<"="<<'\t'<<"赋值"<<endl; } } else if(ch=='+'){ i++; ch=s[i]; if(ch=='+') out<<"++"<<'\t'<<"加1"<<endl; else{ i--; out<<"+"<<'\t'<<"加号"<<endl; } } else if(ch=='&'){ i++; ch=s[i]; if(ch=='&') out<<"&&"<<'\t'<<"与"<<endl; else{ i--; out<<"&"<<'\t'<<"按位与"<<endl; } } else if(ch=='|'){ i++; ch=s[i]; if(ch=='|') out<<"||"<<'\t'<<"或"<<endl; else{ i--; out<<"|"<<'\t'<<"按位或"<<endl; } } else if(ch=='-') out<<ch<<'\t'<<"减号"<<endl; else if(ch==';') out<<ch<<'\t'<<"分号"<<endl; else if(ch=='(') out<<ch<<'\t'<<"左括号"<<endl; else if(ch==')') out<<ch<<'\t'<<"右括号"<<endl; else if(ch=='{') out<<ch<<'\t'<<"左花括号"<<endl; else if(ch=='}') out<<ch<<'\t'<<"右花括号"<<endl; else if(ch=='*'){ i++; ch=s[i]; if(ch=='*') out<<"**"<<'\t'<<"运算符"<<endl; else{ i--; out<<"*"<<'\t'<<"乘号"<<endl; } } else if(ch=='<'){ i++; ch=s[i]; if(ch=='=') out<<"<="<<'\t'<<"小于等于"<<endl; else{ i--; out<<"<"<<'\t'<<"小于"<<endl; } } else if(ch=='>'){ i++; ch=s[i]; if(ch=='=') out<<">="<<'\t'<<"大于"<<endl; else{ i--; out<<">"<<'\t'<<"小于"<<endl; } } else return ; i++; ch=s[i]; } }