根据自动机的原理,我参考了一些资料,做了一个简易的c程序实现。
#include<stdio.h> #include<string.h> #define MAX 20 int n=1; void tackBack(char sh[],int start,int End,int Begain,int Fin);//进行判定操作 int main(){ char sh[MAX]; int size; scanf("%s",sh); size=strlen(sh); tackBack(sh,0,size,0,1); return 0; } void tackBack(char sh[],int start,int End,int Begain,int Fin) { int i=start; while(!(sh[i] == '|') && i<End)//如果遇到||或者到了结束 i++; if(sh[i]=='|'){ tackBack(sh,start,i-1,Begain,Fin);//递归判定遇到|| tackBack(sh,i+1,End,Begain,Fin); } i=start; if(sh[i]>='a' && sh[i]<='z')//遇到的a.....z之间 { if(sh[i+1]=='.')//遇到. { n++; printf("f<%d,%c>=%d ",Begain,sh[i],n); } else if(sh[i+1]=='*')//遇到* { n++; printf("f(%d,~)=%d ",Begain,n); printf("f(%d,~)=%d ",n,Fin); printf("f(%d,%c)=%d ",n,sh[i],n); } } i+=2; while(i<End) { if(sh[i]>='a' && sh[i]<='z') { if(sh[i+1]=='.') { printf("f<%d,%c>=%d ",n,sh[i],n+1); n++; } else if(sh[i+1]=='*') { n++; printf("f(%d,~)=%d ",Begain,n); printf("f(%d,~)=%d ",n,Fin); printf("f(%d,%c)=%d ",n,sh[i],n); } } i++; } if(sh[i]>='a' && sh[i]<='z') { if(sh[i+1]=='*') { printf("f(%d,%c)=%d ",n,sh[i],Fin); } else { printf("f<%d,%c>=%d ",n,sh[i],Fin); } } }