自動機用的是原來的
[cpp]
include <stdio.h>
#include <ctype.h>
#include <string.h>
char opword[]="+-*/=";
char bordword[]="()";
FILE *fin,*fout;
int main()
{
char ch,token[40];
int es=0,j,state,brp;
if((fin=fopen("I1.txt","r"))==NULL)
{
printf("\n打開詞法分析輸入文件出錯!\n");
return(1);
}
if((fout=fopen("O1.txt","w"))==NULL)
{
printf("\n創建詞法分析輸出文件出錯!\n");
return(2);
}
ch=getc(fin);
while(ch!=EOF)
{
while(ch==' '||ch=='\n'||ch=='\t')
ch=getc(fin);
j=0;
state=0;
brp=1;
while(brp)
{
switch(state)
{
case 0:
if(ch=='.')
{
token[j++]=ch;
state=1;
ch=getc(fin);
}
else if(isdigit(ch))
{
token[j++]=ch;
state=6;
ch=getc(fin);
}
else if(isalpha(ch))
{
token[j++]=ch;
ch=getc(fin);
while(isalnum(ch))
{
token[j++]=ch;
ch=getc(fin);
}
state=7;
brp=0;
}
else if(strchr(bordword,ch)>0)
{
token[j++]=ch;
state=8;
brp=0;
ch=getc(fin);
}
else if(strchr(opword,ch)>0)
{
token[j++]=ch;
state=9;
brp=0;
ch=getc(fin);
}
else state=-1;
break;
case 1:
if(isdigit(ch))
{
token[j++]=ch;
ch=getc(fin);
state=2;
}
else brp=0;
break;
case 2:
while(isdigit(ch))
{
token[j++]=ch;
ch=getc(fin);
}
if(ch=='E'||ch=='e')
{
token[j++]=ch;
ch=getc(fin);
state=3;
}
else brp=0;
break;
case 3:
if(ch=='+'||ch=='-')
{
token[j++]=ch;
state=4;
ch=getc(fin);
}
else if(isdigit(ch))
{
token[j++]=ch;
state=5;
ch=getc(fin);
}
else brp=0;
break;
case 4:
if(isdigit(ch))
{
token[j++]=ch;
ch=getc(fin);
state=5;
}
else brp=0;
break;
case 5:
brp=0;
break;
case 6:
while(isdigit(ch))
{
token[j++]=ch;
ch=getc(fin);
}
if(ch=='.')
{
token[j++]=ch;
ch=getc(fin);
state=1;
}
else brp=0;
break;
default:
state=-1;
break;
}
}
//printf("%d\n",state);
token[j]='\0';
if(state==6||state==5||state==2)
fprintf(fout,"(%s,%s)\n","40",token);
else if(state==7)
fprintf(fout,"(%s,%s)\n","10",token);
else if(state==8)
fprintf(fout,"(%s,%s)\n","30",token);
else if(state==9)
fprintf(fout,"(%s,%s)\n","20",token);
else
fprintf(fout,"(%s,%s)\n","ERROR",token);
}
fclose(fin);
fclose(fout);
printf("%d\n",es);
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char opword[]="+-*/=";
char bordword[]="()";
FILE *fin,*fout;
int main()
{
char ch,token[40];
int es=0,j,state,brp;
if((fin=fopen("I1.txt","r"))==NULL)
{
printf("\n打開詞法分析輸入文件出錯!\n");
return(1);
}
if((fout=fopen("O1.txt","w"))==NULL)
{
printf("\n創建詞法分析輸出文件出錯!\n");
return(2);
}
ch=getc(fin);
while(ch!=EOF)
{
while(ch==' '||ch=='\n'||ch=='\t')
ch=getc(fin);
j=0;
state=0;
brp=1;
while(brp)
{
switch(state)
{
case 0:
if(ch=='.')
{
token[j++]=ch;
state=1;
ch=getc(fin);
}
else if(isdigit(ch))
{
token[j++]=ch;
state=6;
ch=getc(fin);
}
else if(isalpha(ch))
{
token[j++]=ch;
ch=getc(fin);
while(isalnum(ch))
{
token[j++]=ch;
ch=getc(fin);
}
state=7;
brp=0;
}
else if(strchr(bordword,ch)>0)
{
token[j++]=ch;
state=8;
brp=0;
ch=getc(fin);
}
else if(strchr(opword,ch)>0)
{
token[j++]=ch;
state=9;
brp=0;
ch=getc(fin);
}
else state=-1;
break;
case 1:
if(isdigit(ch))
{
token[j++]=ch;
ch=getc(fin);
state=2;
}
else brp=0;
break;
case 2:
while(isdigit(ch))
{
token[j++]=ch;
ch=getc(fin);
}
if(ch=='E'||ch=='e')
{
token[j++]=ch;
ch=getc(fin);
state=3;
}
else brp=0;
break;
case 3:
if(ch=='+'||ch=='-')
{
token[j++]=ch;
state=4;
ch=getc(fin);
}
else if(isdigit(ch))
{
token[j++]=ch;
state=5;
ch=getc(fin);
}
else brp=0;
break;
case 4:
if(isdigit(ch))
{
token[j++]=ch;
ch=getc(fin);
state=5;
}
else brp=0;
break;
case 5:
brp=0;
break;
case 6:
while(isdigit(ch))
{
token[j++]=ch;
ch=getc(fin);
}
if(ch=='.')
{
token[j++]=ch;
ch=getc(fin);
state=1;
}
else brp=0;
break;
default:
state=-1;
break;
}
}
//printf("%d\n",state);
token[j]='\0';
if(state==6||state==5||state==2)
fprintf(fout,"(%s,%s)\n","40",token);
else if(state==7)
fprintf(fout,"(%s,%s)\n","10",token);
else if(state==8)
fprintf(fout,"(%s,%s)\n","30",token);
else if(state==9)
fprintf(fout,"(%s,%s)\n","20",token);
else
fprintf(fout,"(%s,%s)\n","ERROR",token);
}
fclose(fin);
fclose(fout);
printf("%d\n",es);
return 0;
}