www.pudn.com > compiler.rar > cifa.cpp


#include"string.h" 
#include"iostream.h" 
#include"stdio.h" 
#include"conio.h" 
 
#define OK 1 
#define ERROR 0 
#define TRUSE 1 
#define FALSE 0 
#define MAX 200 
#define MAX2 
#define YES 1 
#define NO 0 
#define IF 2 
#define ELSE 3 
#define WHILE 4 
#define DO 5 
#define SWITCH 6 
#define INT 22 
#define CHAR 23 
#define FLOAT 24 
#define OTC 8 
#define DEC 10 
#define HEX 16 
#define KEY 20 
#define ALPHA 21 
 
class word_analyse 
{    int ch; 
	 char code[MAX][MAX]; 
public: 
	//word_analyse(); 
	int scan(); 
	int isletter(int ch); 
	int isdigit(int ch); 
	int isright(int ch); 
	int iskeyword(char * ch); 
	int isalpha(char * ch); 
	int judge(char * ch); 
}; 
 
int word_analyse::isletter(int ch) 
{ 
	if(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z'))) 
		return(YES); 
	else return(NO); 
} 
 
int word_analyse::isalpha(char * ch) 
{ 
	if(((ch[0]>='a')&&(ch[0]<='z'))||((ch[0]>='A')&&(ch[0]<='Z'))) 
	{ cout<<"\n"<<"The ["<='0')&&(ch<='9'))) 
        return(YES); 
   else return(NO); 
} 
 
int word_analyse::isright(int ch) 
{  
   if(((ch>='0')&&(ch<='9'))||((ch>='a')&&(ch<='z')) 
	   ||((ch>='A')&&(ch<='Z'))||(ch=='+')||(ch=='-') 
	   ||(ch=='*')||(ch=='/')||(ch=='=')||(ch=='==') 
	   ||(ch=='%')||(ch=='<')||(ch=='>')||(ch=='(')||(ch==')')) 
	    return(YES); 
   else return(NO); 
} 
 
int word_analyse::iskeyword(char * ch) 
{ 
   
  if((ch[0]=='i')&&(ch[1]=='n')&&(ch[2]=='t')&&(ch[3]==0)) 
  { cout<<"\n"<<"The [int] is a keyword !"<<"\n";    return(INT);        } 
  if((ch[0]=='c')&&(ch[1]=='h')&&(ch[2]=='a')&&(ch[3]=='r')&&(ch[4]==0)) 
  { cout<<"\n"<<"The [char] is a keyword !"<<"\n";    return(CHAR);      } 
  if((ch[0]=='f')&&(ch[1]=='l')&&(ch[2]=='o')&&(ch[3]=='a')&&(ch[4]=='t')&&(ch[5]==0)) 
  { cout<<"\n"<<"The [float] is a keyword !"<<"\n";    return(FLOAT);        } 
  if((ch[0]=='i')&&(ch[1]=='f')&&(ch[2]==0)) 
  { cout<<"\n"<<"The [if] is a keyword !"<<"\n";    return(IF);        } 
  if((ch[0]=='e')&&(ch[1]=='l')&&(ch[2]=='s')&&(ch[3]=='e')&&(ch[4]==0)) 
  { cout<<"\n"<<"The [else] is a keyword !"<<"\n";  return(ELSE);      } 
  if((ch[0]=='w')&&(ch[1]=='h')&&(ch[2]=='i')&&(ch[3]=='l')&&(ch[4]=='e')&&(ch[5]==0)) 
  { cout<<"\n"<<"The [while] is a keyword !"<<"\n"; return(WHILE);     } 
  if((ch[0]=='d')&&(ch[1]=='o')&&(ch[2]==0)) 
  { cout<<"\n"<<"The [do] is a keyword !"<<"\n";    return(DO);        } 
  if((ch[0]=='s')&&(ch[1]=='w')&&(ch[2]=='i')&&(ch[3]=='t')&&(ch[4]=='c')&&(ch[5]=='h')&&(ch==0)) 
  { cout<<"\n"<<"It is a keyword [if] !"<<"\n";	return(SWITCH);    } 
  return(FALSE); 
} 
 
int word_analyse::judge(char * ch) 
{ 
     if((ch[0]=='0')&&((ch[1]>='0')&&(ch[1]<='7'))) 
	  { 
		cout<<"\n"<<"This is a OTC number, OTC value is"<<"["<='0')&&(ch[0]<='9')&&(ch[0]!='0')) 
	  { cout<<"\n"<<"This is a DEC number, DEC value is"<<"["<='a')&&(ch<='f'))) 
		{ 
			code[j][i]=ch; 
			ch=getc(stdin); 
			i++; 
		} 
		code[j][i]=0; 
		//ungetc(ch,stdin); 
		judge(code[j]); 
		cout<<"The number's ID is ["<':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<