#include<stdio.h>
#include<string.h>
int i,j,k,sign,flag,number,run;
char ch;
char word[10];
char program[500];
int Scan(char program[])
{
char *keyword[8]={"iii""begin","if","then","while","do","end"};
number=0;
flag=0;
j=0;
ch=program[i++];
while(ch=='
'||ch==' '||ch==' '||ch=='
')
{
ch=program[i++];
}
if(ch>='a'&&ch<='z')
{
while(ch>='a'&&ch<='z')
{
word[j++]=ch;
ch=program[i++];
}
i--;
word[j++]=' ';
for(k=0;k<8;k++)
{
if(strcmp(word,keyword[k])==0)
{
flag=1;
sign=k+1;
}
else
{
flag=0;
sign=300;
} //是关键字,不是标识符//
break;
}
}
if(ch>'0'&&ch<'9') //是不是数字//
{
number=0;
while(ch>'0'&&ch<'9')
{
number=number*10+(ch-'0');
ch=program[i++];
}
sign=200;
i--;
}
else
switch(ch)
{
case '=':{
if(ch=='=')
{
word[j++]=ch;
word[j]=' ';
sign=25; //=//
}
break;
}
case '<':{
if(ch=='<')
{
word[j++]=ch;
word[j]=' ';
ch=program[i++];
}
if(ch=='=')
{
word[j++]=ch;
word[j]=' ';
sign=21;
}
else
{
i--;
sign=20;
}
break;
}
case '>':{
if(ch=='>')
{
word[j++]=ch;
word[j]=' ';
ch=program[i++];
}
if(ch=='=')
{
word[j++]=ch;
word[j]=' ';
sign=24;
}
else
{
i--;
sign=23;
}
break;
}
case '(':{
if(ch=='(')
{
word[j++]=ch;
word[j]=' ';
sign=27;
}
break;
}
case ')':{
if(ch==')')
{
word[j++]=ch;
word[j]=' ';
sign=28;
}
break;
}
case ':':{
if(ch==':')
{
word[j++]=ch;
word[j]=' ';
ch=program[i++];
}
if(ch=='=')
{
word[j++]=ch;
word[j]=' ';
sign=18;
}
else
{
i--;
sign=17;
}
break;
}
case '#':{
if(ch=='#')
{
word[j++]=ch;
word[j]=' ';
sign=0;
}
break;
}
case ';':{
if(ch==';')
{
word[j++]=ch;
word[j]=' ';
sign=26;
}
break;
}
case '+':{
if(ch=='+')
{
word[j++]=ch;
word[j]=' ';
sign=13;
}
break;
}
case '-':{
if(ch=='-')
{
word[j++]=ch;
word[j]=' ';
sign=14;
}
break;
}
case '*':{
if(ch=='*')
{
word[j++]=ch;
word[j]=' ';
sign=15;
}
break;
}
case '/':{
if(ch=='/')
{
word[j++]=ch;
word[j]=' ';
sign=16;
}
break;
}
case '@':{
if(ch=='@')
{
word[j++]=ch;
word[j]=' ';
sign=0;
}
}
return sign;
}
int main()
{
int i=0,run=1,sign=0,sum=1,ent;
while(run)
{
int j;
for(j=0;j<500;j++)
program[j]=' ';
for(j=0;j<10;j++)
word[j]=' ';
printf("请输入一段源代码");
do{
ch =getchar();
program[i++]=ch;
}while(ch!='@');
i=0;
do{
sign=Scan(program);
if(sign==13)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==14)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==15)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==16)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==17)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==18)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==20)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==21)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==22)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==24)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==25)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==26)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==27)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign==28)
{
printf("操作符 %8s %5d",word,sign);
sum++;
ent=1;
}
if(sign>=1&&sign<=8)
{
printf("关键字 %8s %5d",word,sign);
sum++;
ent=1;
}
} while(sign!=0);
printf("是否继续?y or N");
getchar();
ch=getchar();
if(ch=='n'||ch=='N')
run=0;
}
return 0;