自动机

#include<stdio.h>
#define N 100
main()
{
	int j=0;
	int sum=0;
	int i;
	char a[N];
	printf("请输入一个文法(以#号结束):\n");
	for(i=0;i<10;i++)
	{
		scanf("%c",&a[i]);
		j++;
		if(a[i]=='#')
			break;
	}
	/*for(i=0;i<j-1;i++)
	{
		printf("%c",a[i]);
	}*/
	for(i=0;i<100;i++)
	{
		if(a[i]=='|')
		{
			printf("\nf(%d,%c)=%d",sum,a[i-1],sum+1);
			printf("\nf(%d,%c)=%d",sum,a[i+1],sum+1);
			sum=sum+1;
		}
		else if(a[i]=='*')
		{
			printf("\nf(%d,$)=%d",sum,sum+1);
			sum=sum+1;
			printf("\nf(%d,%c)=%d",sum,a[i-1],sum);
			printf("\nf(%d,$)=%d",sum,sum+1);
			sum=sum+1;
		}
		else if((a[i]<='z')&&(a[i]>='a')&&(a[i+1]<='z')&&(a[i+1]>='a'))
		{
			if(a[i+2]=='|')
			{
				printf("\nf(%d,%c)=%d",sum,a[i],sum+1);
				sum=sum+1;
				printf("\nf(%d,%c)=%d",sum,a[i+1],sum+1);
				printf("\nf(%d,%c)=%d",sum,a[i+3],sum+1);
				sum=sum+1;
				i=i+2;
			}
			else if(a[i+2]=='*')
			{
				printf("\nf(%d,%c)=%d",sum,a[i],sum+1);
				sum=sum+1;
				printf("\nf(%d,$)=%d",sum,sum+1);
				sum=sum+1;
				printf("\nf(%d,%c)=%d",sum,a[i+1],sum);
				printf("\nf(%d,$)=%d",sum,sum+1);
				sum=sum+1;
				i=i+2;
			}
			else
			{
				printf("\nf(%d,%c)=%d",sum,a[i],sum+1);
				sum=sum+1;
				printf("\nf(%d,%c)=%d",sum,a[i+1],sum+1);
				sum=sum+1;
			}
		}



	}


}
posted @ 2015-12-14 12:32  39谭宇森  阅读(139)  评论(0编辑  收藏  举报