#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
int num;
char *word;
}WORD; //存储种别码,单词
char *rword[]={"begin","if","then","while","do","end","he"};
char ci;
int ainput,toke; char input[300];char token[300]=" ";
char mgetch(){ //读取一个字符
ci=input[ainput];
ainput=ainput+1;
return (ci);
}
void getbc(){ //去空格
while(ci==' '||ci==10)
{ ci=input[ainput];
ainput+=1;
}
}
int letter() //判断是否为字母
{ if(ci>='a'&&ci<='z'||ci>='A'&&ci<='Z')
return 1;
else
return 0;
}
int number() //判断是否为数字
{
if(ci>='0'&&ci<='9')
return 1;
else
return 0;
}
int reserve() // 判断是否为关键字
{
int i=0;
while(strcmp(rword[i],"he"))
{
if(!strcmp(rword[i],token))
{
return i+1;
}
i++;
}
return 10;
}
void link() //链接单词
{
token[toke]=ci;
toke+=1;
token[toke]=' ';
}
void back(){
ainput-=1;
}
WORD * scaner(){
WORD *myword;
myword=(WORD*)malloc(sizeof(WORD));
myword->num=10;
myword->word="";
toke=0;
mgetch();
getbc();
if(letter()){
while(letter()||number())
{
link();
mgetch();
}
back();
myword->num =reserve();
myword->word=token;
return(myword);
}
else if(number()) {
while(number())
{
link();
mgetch();
}
back();
myword->num=11;
myword->word=token;
return(myword);
}
else switch(ci){
case '=': mgetch();
if(ci=='='){
myword->num=39;
myword->word="==";
return(myword);
}
back();
myword->num=25;
myword->word="=";
return(myword);
break;
case'+': myword->num=13;
myword->word="+";
return(myword);
break;
case'-': myword->num=14;
myword->word="-";
return(myword);
break;
case'*': myword->num=15;
myword->word="*";
return(myword);
break;
case'/': myword->num=16;
myword->word="/";
return(myword);
break;
case'(': myword->num=27;
myword->word="(";
return(myword);
break;
case')': myword->num=28;
myword->word=")";
return(myword);
break;
case'[': myword->num=29;
myword->word="[";
return(myword);
break;
case']': myword->num=30;
myword->word="]";
return(myword);
break;
case'{': myword->num=31;
myword->word="{";
return(myword);
break;
case'}': myword->num=32;
myword->word="}";
return(myword);
break;
case',': myword->num=33;
myword->word=",";
return(myword);
break;
case':': mgetch();
if(ci=='='){
myword->num=18;
myword->word=":=";
return(myword);
}
myword->num=33;
myword->word=":";
return(myword);
break;
case';': myword->num=26;
myword->word=";";
return(myword);
break;
case'>': mgetch();
if(ci=='='){
myword->num=24;
myword->word=">=";
return(myword);
}
back();
myword->num=23;
myword->word=">";
return(myword);
break;
case'<': mgetch();
if(ci=='='){
myword->num=22;
myword->word="<=";
return(myword);
}
back();
myword->num=20;
myword->word="<";
return(myword);
break;
case'!': mgetch();
if(ci=='='){
myword->num=40;
myword->word="!=";
return(myword);
}
back();
myword->num=-1;
myword->word="error";
return(myword);
break;
case' ': myword->num=1000;
myword->word="over";
return(myword);
break;
default:
myword->num=-1;
myword->word="error";
return(myword);
}
}
int main()
{
int over=1; WORD * oneword; int i=0;
oneword=(WORD*)malloc(sizeof(WORD));
ainput=0;
printf("输入源代码:");
do{
ci=getchar();
input[i]=ci;
i++;
}while(ci!='#');
while(over<1000&&over!=-1)
{
oneword=scaner();
if(oneword->num<1000)
printf("(%d,%s)
",oneword->num,oneword->word);
over=oneword->num;
}
}