#include <stdio.h>
#include <string.h>
int Input1(char a)
{
int i=0;
switch(a)
{
case(‘+‘):
printf("%c\t 13\n",a);i++;break;
case(‘-‘):
printf("%c\t 14\n",a);i++;break;
case(‘*‘):
printf("%c\t 15\n",a);i++;break;
case(‘/‘):
printf("%c\t 16\n",a);i++;break;
case(‘:‘):
printf("%c\t 17\n",a);i++;break;
case(‘<‘):
printf("%c\t 20\n",a);i++;break;
case(‘>‘):
printf("%c\t 23\n",a);i++;break;
case(‘=‘):
printf("%c\t 25\n",a);i++;break;
case(‘;‘):
printf("%c\t 26\n",a);i++;break;
case(‘(‘):
printf("%c\t 27\n",a);i++;break;
case(‘)‘):
printf("%c\t 28\n",a);i++;break;
case(‘#‘):
printf("%c\t 0\n",a);i++;break;
}
return i;
}
int Input2(char a[])
{
int i=0;
if((strcmp(a,"if")==0))
{printf("if\t 2\n"); i++;}
else if((strcmp(a,"do")==0))
{printf("do\t 5\n"); i++;}
else if((strcmp(a,":=")==0))
{printf(":=\t 18\n"); i++;}
else if((strcmp(a,"<=")==0))
{printf("<=\t 21\n"); i++;}
else if((strcmp(a,"<>")==0))
{printf("<>\t 22\n"); i++;}
else if((strcmp(a,">=")==0))
{printf(">=\t 24\n"); i++;}
return i;
}
int Input3(char a[])
{
int i=0;
if((strcmp(a,"end")==0))
{printf("end\t 6\n"); i++;}
else if((strcmp(a,"dd*")==0))
{printf("dd*\t 11\n"); i++;}
return i;
}
int Input4(char a[])
{
int i=0;
if((strcmp(a,"then")==0))
{printf("then\t 3\n"); i++;}
return i;
}
int Input5(char a[])
{
int i=0;
if((strcmp(a,"begin")==0))
{printf("begin\t 3\n"); i++;}
else if((strcmp(a,"begin")==0))
{printf("begin\t 1\n"); i++;}
else if((strcmp(a,"while")==0))
{printf("while\t 4\n"); i++;}
return i;
}
/*int Input8(char a[])
{
int i=0;
if((strcmp(a,"|(||d)*")==0))
{printf("begin\t 3\n"); i++;}
return i;
}*/
#define MAX 100
struct Code
{
char chars[MAX];
};
int main()
{
char SC[MAX];
Code code[MAX];
int i,j,k,n;
printf("请输入代码: ");
gets(SC);
printf("单词符号 种别码\n");
i=j=k=0;
n=1;
while(SC[i]!=‘\0‘)
{
code[k].chars[j]=SC[i];
code[k].chars[j+1]=‘\0‘;
switch(n)
{
case(1): if(Input1(code[k].chars[j])==1)
{k++;j=-1;}break;
case(2): if(Input2(code[k].chars)==1)
{k++;j=-1;}break;
case(3): if(Input3(code[k].chars)==1)
{k++;j=-1;}break;
case(4): if(Input4(code[k].chars)==1)
{k++;j=-1;}break;
case(5): if(Input5(code[k].chars)==1)
{k++;j=-1;}break;
/*case(8): if(Input8(code[k].chars)==1)
{k++;j=-1;}break; */
default:
printf("%s\t语法错误!\n",code[k].chars);k++;j=-1;break;
}
if(j==-1)
n=0;
j++;
i++;
n++;
}
return 0;
}