www.pudn.com > word.zip > word.cpp


#include 
#include 
#include 
#include 
using namespace std; 
class CIV               //存储的输出结构 
{ 
public: 
	int code;           //种别码 
	int index;          //变量或常量在变量表或常量表中的位置索引,其他则为-1; 
	string value;       //单词值 
public: 
	CIV(int c,int in,string&v) 
	{ 
		code=c; 
		value=v; 
		index=in; 
	} 
	CIV& operator=(const CIV &s) 
	{ 
		code=s.code; 
		value=s.value; 
		index=s.index; 
		return *this; 
	} 
	CIV(const CIV&s) 
	{ 
		code=s.code; 
		value=s.value; 
		index=s.index; 
	} 
		 
}; 
class  word 
{ 
    string* keywords;     
private: 
	vector symbols;    //符号表 
	vector out;           //输出 
	vector  consts;    //常量表 
	string source;             //读入的源字符串 
	int len;                   //长度 
	int current;               //当前位置 
	char ch;                  //当前字符 
	string token; 
public: 
	word(string&s,int l,string* key_list) 
	{ 
		source=s; 
		len=l; 
		current=0; 
		ch=' '; 
		keywords=key_list; 
	} 
	bool GetChar() 
	{ 
		if(current>=len) 
		{ 
			ch=0; 
			return false; 
		} 
	    ch=source[current++]; 
		return true; 
	} 
	bool GetBC() 
	{ 
		while(ch==' ') 
		{ 
			if(!GetChar()) 
				return false; 
		} 
		return true; 
	} 
	void Concat() 
	{ 
    	token+=ch; 
	} 
	int Reserve() 
	{ 
		for(int i=0;i<8;i++) 
			if(token==keywords[i]) 
				return 1; 
	     return 2;  
	} 
	bool Retract() 
	{ 
		if(current==0) 
		{ 
			ch=0; 
			return false; 
		} 
		else 
		{ 
			current--; 
			ch=0; 
		} 
	} 
    int InsertId() 
	{ 
		symbols.push_back(token);  
		return symbols.size()-1; 
	} 
	bool IsLetter(char c) 
	{ 
		if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) 
			return true; 
		return false; 
	} 
	bool IsDigit(char c) 
	{ 
		if(c>='0'&&c<='9') 
			return true; 
		return false; 
	} 
	bool parse(); 
	void print(); 
	  
}; 
bool word::parse() 
{ 
	int code,index; 
	string value; 
	while(GetChar()) 
	{ 
		if(!GetBC()) continue; 
		if(IsLetter(ch)) 
		{ 
			while(IsLetter(ch)||IsDigit(ch)) 
			{ 
				Concat(); 
				if(!GetChar()) break; 
			} 
			Retract(); 
			code=Reserve(); 
			if(code==2) 
			{ 
				index=InsertId(); 
				value=token; 
				token.erase(); 
			} 
			else 
			{ 
				index=-1; 
				value=token; 
				token.erase(); 
			} 
			CIV cv(code,index,value); 
            out.push_back(cv);   
		} 
		else if(IsDigit(ch)) 
		{ 
			while(IsDigit(ch)) 
			{ 
				Concat(); 
				if(!GetChar()) break; 
			} 
     		if(IsLetter(ch)) 
			{ 
			  while(IsLetter(ch)) 
			  { 
				Concat(); 
				if(!GetChar()) break; 
			  } 
			  cout<<"单词:"<'||ch=='<'||ch=='='||ch=='!') 
		{ 
			char ss[3]={'\0'}; 
			ss[0]=ch; 
			GetChar(); 
			if(ch=='=') 
		 		ss[1]='='; 
			else 
				Retract(); 
			string s(ss); 
			CIV cv(4,-1,s); 
			out.push_back(cv); 
		} 
		else if(ch==','||ch==';'||ch=='{'||ch=='}'||ch=='('||ch==')') 
		{ 
			char ss[2]={'\0'}; 
			ss[0]=ch; 
			string s(ss); 
			CIV cv(5,-1,s); 
			out.push_back(cv); 
		} 
	 
	} 
	return true; 
} 
void word::print() 
{ 
	vector::iterator it; 
	for(it=out.begin();it!=out.end();it++) 
		 cout<<"     ("<<(*it).code<<",\""<<(*it).value<<"\")"<>filename; 
     
 
 	string source; 
	fp=fopen(filename.c_str(),"r"); 
    if(fp==NULL) 
	{ 
		cout<<"不能打开文件 "<parse(); 
	cout<<"输出结果为:"<print(); 
 	 
	return 1; 
}