实验四 DFA程序实现

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验三 DFA程序实现
一、实验目的
通过DFA程序的实现理解自动机的原理。

二、实验重难点
DFA编码实现
三、实验内容与要求
1、D FA的程序表示;
四、实验学时
2课时
五、实验设备与环境
Visual C++ 6.0
六、实验过程
1.There is an FA=({0,1,2,3}, {a,b}, M, 0, {3})
M:
M(0,a)=1 M(0,b)=2 M(1,a)=3 M(1,b)=2
M(2,a)=1 M(2,b)=3 M(3,a)=3 M(3,b)=3
The question is how to judge whether the string “abbb” could be identified or accepted by the FA? 0 1 2 3 3
参考代码:
2. 以教材P72页习题3为例,确定化后的DFA 为:
构造该自动机的C语言程序表示;
#include<stdio.h>
int in(char s,char c,char e,char f)
{
if(s==c)
{
printf(" C\nlook!the last status belongs to C!");
return 1;
}
else if(s==e)
{
printf(" E\nlook!the last status belongs to E!");
return 1;
}
else if(s==f)
{
printf(" F\nlook!the last status belongs to F!");
return 1;
}
else
{
return 0;
}
}
char step(char s,int t)
{
if( t == '0')//判断数字经0转换成另一个数字
switch(s)
{
case 's':return 'a';
case 'a':return 'c';
case 'b':return 'd';
case 'c':return 'f';
case 'd':return 'f';
case 'e':return 'c';
case 'f':return 'f';
}
else if( t == '1')//判断数字经1转换成另一个数字
switch(s)
{
case 's':return 'b';
case 'a':return 'b';
case 'b':return 'e';
case 'c':return 'f';
case 'e':return 'e';
case 'f':return 'f';
}
}
int realize(char *input)
{
char c='c';
char e='e';
char f='f';
int i;
char s;
s='s';
for(i=0;input[i]!='\n';i++)
{
printf("%2c",s);
s=step(s,input[i]);
}
if(in(s,c,e,f))
return 1;
else return 0;
}
main()
{
int i;
int a;
char input[40];
printf("FA=({S,A,B,C,D,E,F},{0,1},M,S,(C,F))\n");//文法五元组printf("M:\n");
printf(" M(S,0)=A M(S,1)=B\n");
printf(" M(A,0)=C M(A,1)=B\n");
printf(" M(B,0)=D M(B,1)=E\n");
printf(" M(C,0)=F M(C,1)=F\n");
printf(" M(D,0)=F M(D,1)=null\n");
printf(" M(E,0)=C M(E,1)=E\n");
printf(" M(F,0)=F M(F,1)=F\n");
printf("please enter your string which is to be checked:\n");
lop: for(i=0;input[i-1]!='\n';i++)//输入0,1中的一个或多个字符组合。

{
scanf("%c",&input[i]);
}
for(i=0;input[i-1]!='\n';i++)
if(input[i]!='0' &&input[i]!='1' &&input[i]!='\n')
{
printf("input error,enter again please:\n");
goto lop;
}
printf("the status sequence is :\n");
a=realize(input);
if(a==1)
printf("\nSo this string can be identified\n");
else
printf("\nthis string can'n be identified\n");
printf("press error to exit the program\n");
getchar();
}
3. 写出与该自动机相等价的正规式。

R = (0|1)0*1*
4. 根据正规式,构造一个由0,1构成的数字串,并作为输入,通过运行该自动机程序判断该字符串能否被该自动机所识别。

此处给出测试数据及运行结果。

测试数据:0010
测试结果:s a c f f。

相关文档
最新文档