www.pudn.com > wordcompile.rar > word.cpp


#include  
#include  
#include  
bool isAlpha(char ch) 
{ 
	if(ch<='z'&&ch>='a'||ch<='Z'&&ch>='A') 
		return true; 
	return false; 
} 
 
 
bool isDigit(char ch) 
{ 
   if(ch<='9'&&ch>='0') 
		return true; 
	return false; 
} 
 
bool isCalc(char ch) 
{ 
	if(ch=='+')	return true; 
	else if(ch=='+')	return true; 
    else if(ch=='-')	return true; 
	else if(ch=='*')	return true; 
	else if(ch=='/')	return true; 
	else if(ch=='=')	return true; 
	else if(ch=='>')	return true; 
	else if(ch=='<')	return true; 
	else return false; 
} 
 
 
bool isWord(char st[]) 
{ 
	if(strcmp(st,"void")==0)	return true; 
	else if(strcmp(st,"char")==0)	return true; 
    else if(strcmp(st,"main")==0)	return true; 
	else if(strcmp(st,"if")==0)	return true; 
	else if(strcmp(st,"else")==0)	return true; 
	else if(strcmp(st,"end")==0)	return true; 
	else if(strcmp(st,"print")==0)	return true; 
	else if(strcmp(st,"scan")==0)	return true; 
	else if(strcmp(st,"then")==0)	return true; 
	else if(strcmp(st,"read")==0)	return true; 
	else if(strcmp(st,"write")==0)	return true; 
	else return false; 
} 
 
 
 
 
void main() 
{  
	char strs[255],strd[255],ch; 
	int  st,sw,i=0; 
	cout<<"请输入待分析的程序:"< " 
标志符就是以字母开头,由任意个字母或数字连接组成,除了关键字以外的所有字符串 
测试数据及运行结果 
运行程序,将会提示输入待分析的程序(即待分析的字符串)。程序的运行过程及运行结果如下: 
 
测试数据1如下: good asdflj 1+532 a987=x end 
运行结果如下: 
 
请输入带分析的程序: 
good asdflj 1+532 a987=x end 
 
 对此语句进行词法分析如下: 
    good - 标识符 
    asdflj - 标识符 
    1 - 数字 
    + - 算符 
    532 - 数字 
    a987 - 标识符 
    = - 算符 
    x - 标识符 
    end - 关键字 
 
  分析完毕... 
 
 
测试数据2如下:if a5<20 then print OK else end234=b9 end 
运行结果如下: 
 
请输入带分析的程序: 
if a5<20 then print OK else end234=b9 end 
 
 对此语句进行词法分析如下: 
    if - 关键字 
    a5 - 标识符 
    < - 算符 
    20 - 数字 
    then - 关键字 
    print - 关键字 
    OK - 标识符 
    else - 关键字 
    end234 - 标识符 
    = - 算符 
    b9 - 标识符 
    end - 关键字 
 
  分析完毕... 
 
*/