#include"stdlib.h"
#include"stdio.h"
#include"ctype.h"
#include"string.h"
FILE*fp;
void fun()
{
int i;
int s=0;
char ch;
int t=0;
char p[80],ch1[80];
while(1)
{
switch(s)
{
case 0:ch=fgetc(fp);
if(ch=='0')s=4;
else if(ch=='
else if(ch=='=')s=9;
else if(ch=='>')s=10;
else if(ch=='+')s=14;
else if(ch=='-')s=15;
else if(ch=='*')s=16;
else if(ch=='/')s=17;
else if(ch=='=')s=18;
else if(ch==',')s=19;
else if(ch==';')s=20;
else if(ch=='(')s=21;
else if(ch1=="begin")s=24;
else if(ch1=="end")s=25;
else if(ch1=="if")s=26;
else if(ch1=="else")s=27;
else if (ch1=="then")s=28;
if(isalpha(ch))
{
s=1;
for(i=0;i<80;i++)
{p[i]=i;}
}
else
{printf("词法错误");
break;
}
if(isdigit(ch))
{s=3;
t=ch-'0';
}
else
{
printf("词法错误");
return ;
break;
}
case 1:ch=fgetc(fp);
if(isalnum(ch))
{s=1;
p[i]=i;
i++;
}
else s=2;
break;
case 2:printf("(0,内部字符串),%c",p[i]);
s=0;
fseek(fp,-1,1);
break;
case 3:ch=fgetc(fp);
if(isdigit(ch))
{s=3;
t=t*10+(ch-'0');
}
break;
case 4:printf("(1,整数值),%d",t);
s=0;
fseek(fp,-1,1);
break;
case 5:ch=fgetc(fp);
if(ch=='=')s=6;
else if(ch=='>')s=7;
else s=8;
break;
case 6:printf("(2,LE)");
s=0;
break;
case 7:printf("(2,NE)");
s=0;
break;
case 8:printf("(2,LT)");
s=0;
fseek(fp,-1,1);
break;
case 9:ch=fgetc(fp);
if(ch=='=')
s=11;
break;
case 10:ch=fgetc(fp);
if(ch=='=')
s=12;
else s=13;
break;
case 11:printf("(2,EQ)");
s=0;
break;
case 12:printf("(2,GE)");
s=0;
break;
case 13:printf("(2,GT)");
s=0;
fseek(fp,-1,1);
break;
case 14:printf("(3, )");
s=0;
break;
case 15:printf("(4, )");
s=0;
break;
case 16:printf("(5, )");
s=0;
break;
case 17:printf("(6, )");
s=0;
break;
case 18:printf("(7, )");
s=0;
break;
case 19:printf("(9, 分界符)");
s=0;
break;
case 20:printf("(9, 分界符)");
s=0;
break;
case 21:ch=fgetc(fp);
if(ch==')')
s=22;
else s=23;
break;
case 22:printf("(9,分界符)");
s=0;break;
case 23:printf("(9,分界符)");
s=0;
fseek(fp,-1,1);
break;
case 24:printf("(8,begin)");
s=0;
break;
case 25:printf("(8,end)");
s=0;
break;
case 26:printf("(8,if)");
s=0;
break;
case 27:printf("(8,then)");
s=0;break;
case 28:printf("(8,else)");
s=0;
break;
}
}
}
void main()
{
if((fp=fopen("file.txt","r"))==NULL)
{printf("can not open the file!\n");
exit(0);
}
fun();
printf("\n");
}