www.pudn.com > pl0.zip > WordAny.cpp


#ifndef __WORDANY_CPP 
#define __WORDANY_CPP 
 
#include "WordAny.h" 
 
string ImToken[TOTAL]={"program","const","var","procedure","begin","if","while","call","read","write","end","then","else","do","odd"}; 
 
void GetChar(char &ch,int &i,string passage) 
{ 
	if(i='a') 
		return true; 
	else if(ch<='Z' && ch>='A') 
		return true; 
	else 
		return false; 
} 
 
bool IsDigit(char ch) 
{ 
	if(ch<='9' && ch>='0') 
		return true; 
	else 
		return false; 
} 
 
int Reserve(const string strTokenFile) 
{ 
	for(int i=0;i<14;i++) 
		if(strTokenFile.compare(ImToken[i])==0) 
			return i+1; 
	return 0; 
} 
 
void Retract(int &i,char &ch) 
{ 
	i--; 
	ch=' '; 
} 
 
void WordAnaly(string passage,int &i,int &j,Word *Token,const int row) 
{ 
	string strTokenFile; 
	char ch; 
	int code; 
 
	strTokenFile=""; 
	GetChar(ch,i,passage); 
	GetBC(ch,i,passage); 
	if(IsLetter(ch)) 
	{ 
		while(IsLetter(ch) || IsDigit(ch)) 
		{ 
			Concat(strTokenFile,ch); 
			GetChar(ch,i,passage); 
		} 
		Retract(i,ch); 
		code=Reserve(strTokenFile); 
		Token[j].ChanCont(strTokenFile); 
		Token[j].ChanXY(i,row); 
		if(code==0)			 
			Token[j].ChanWType($id); 
		else 
			Token[j].ChanWType(code); 
		j++; 
	} 
	else if(IsDigit(ch)) 
	{ 
		while(IsDigit(ch)) 
		{		 
			Concat(strTokenFile,ch); 
			GetChar(ch,i,passage); 
		} 
		if(IsLetter(ch)) 
		{ 
			cout<<"错误的标识符,第"<' || ch=='=') 
			Concat(strTokenFile,ch); 
		else  
		Retract(i,ch); 
		Token[j].ChanCont(strTokenFile); 
		Token[j].ChanWType($lop); 
		Token[j].ChanXY(i,row); 
		j++; 
	} 
	else if(ch=='>') 
	{ 
		Concat(strTokenFile,ch); 
		GetChar(ch,i,passage); 
		if(ch=='=') 
			Concat(strTokenFile,ch); 
		else 
		Retract(i,ch); 
		Token[j].ChanCont(strTokenFile); 
		Token[j].ChanWType($lop); 
		Token[j].ChanXY(i,row); 
		j++; 
	} 
	else if(ch==';') 
	{ 
		Concat(strTokenFile,ch); 
		Token[j].ChanCont(strTokenFile); 
		Token[j].ChanWType($); 
		Token[j].ChanXY(i,row); 
		j++; 
	} 
	else if(ch==':') 
	{ 
		Concat(strTokenFile,ch); 
		GetChar(ch,i,passage); 
		if(ch=='=') 
			Concat(strTokenFile,ch); 
		else 
		{ 
			cout<<"错误的输入!!"<<":"<<"后应该为"<<"\"=\",第"<