#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:
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 ["<<ch<<"] is a identifier !"<<"\n";
return(YES);
}
else return(NO);
}
int word_analyse::isdigit(int ch)
{ if(((ch>='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"<<"["<<ch<<"] !"<<"\n";
return(OTC);
}
if((ch[0]=='0')&&(ch[1]=='\0'))
{ cout<<"\n"<<"This is a DEC number, DEC value is"<<"[0] !"<<"\n";
return(DEC);
}
if((ch[0]>='0')&&(ch[0]<='9')&&(ch[0]!='0'))
{ cout<<"\n"<<"This is a DEC number, DEC value is"<<"["<<ch<<"] !"<<"\n";
return(DEC);
}
if((ch[0]=='0')&&(ch[1]=='x'))
{ cout<<"\n"<<"This is a HEX number, HEX value is"<<"["<<ch<<"] !"<<"\n";
return(HEX);
}
return(OK);
}
int word_analyse::scan()
{ int i=0;
int j=0;
int x=1;
//clrscr();
cout<<"Please input !"<<"\n";
ch=getc(stdin);
while((ch==' ')||(ch==10))ch=getc(stdin);
if((isright(ch)==NO)){ cout<<"\n"<<"ERROR"<<"\n"; return(ERROR); }
ungetc(ch,stdin);
while(x)
{ ch=getc(stdin);
if((isletter(ch)==YES))
{ i=0;
while (isletter(ch)||isdigit(ch))
{
code[j][i]=ch;
ch=getc(stdin);
i++;
}
code[j][i]=0;
if(iskeyword(code[j])==0)
{
if(isalpha(code[j]))cout<<"The identifier's ID is ["<<j<<"] !"<<"\n";
}
j++;
if(ch==10)x=0;
}
if((isdigit(ch)==YES))
{ i=0;
while(isdigit(ch)||(ch=='x')||((ch>='a')&&(ch<='f')))
{
code[j][i]=ch;
ch=getc(stdin);
i++;
}
code[j][i]=0;
judge(code[j]);
cout<<"The number's ID is ["<<j<<"] !"<<"\n";
j++;
if(ch==10)x=0;
}
if(ch==':')
{ i=0;
code[j][i]=ch;
ch=getc(stdin);
if(ch=='='){ code[j][i++]=ch; cout<<"The ["<<"code[j]"<<" is (ASSIGN-OP, -) ."; j++; ch=getc(stdin); if(ch==10)x=0; }
}
switch(ch)
{
case'+':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (PLUS-OP, AND) ."<<"\n"; } //ch=getc(stdin); if(ch==59)ungetc(ch,stdin); }
break;
case'-':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (MINUS-OP, SUB) ."<<"\n"; } //ch=getc(stdin);if(ch==59)ungetc(ch,stdin); }
break;
case'*':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, MUL) ."<<"\n"; }// ch=getc(stdin); if(ch==59)ungetc(ch,stdin);}
break;
case'/':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, DIV) ."<<"\n";} // ch=getc(stdin);if(ch==59)ungetc(ch,stdin); }
break;
case'=':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, EQ) ."<<"\n"; }//ch=getc(stdin);if(ch==59)ungetc(ch,stdin); }
break;
case'>':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, GT) ."<<"\n"; }//ch=getc(stdin);if(ch==59)ungetc(ch,stdin); }
break;
case'<':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, LT) ."<<"\n"; }//ch=getc(stdin);if(ch==59)ungetc(ch,stdin);}
break;
case'(':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, LP) ."<<"\n"; }//ch=getc(stdin);if(ch==59)ungetc(ch,stdin); }
break;
case')':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (RELATION-OP, RP) ."<<"\n";}// ch=getc(stdin); if(ch==59)ungetc(ch,stdin); }
break;
case';':{ i=0; code[j][i]=ch; code[j][i++]=0; cout<<"\n"<<"The ["<<char(ch)<<"] is a identifier (SEMICOLON, -) ."<<"\n"; }//ch=getc(stdin); }
break;
}
if(ch==10)x=0;
}
return(OK);
}
int main()
{
char ch1;
word_analyse word;
while(ch1!='q')
{
word.scan();
ch1=getch();
}
return(OK);
}
评论0