-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNFA.cpp
More file actions
99 lines (89 loc) · 2.09 KB
/
NFA.cpp
File metadata and controls
99 lines (89 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<stdio.h>
#include<conio.h>
int Fa[10][10][10],states[2][10],curr,row=0,col=0,sr=0,sc=0,th=0,in;
char *str;
int nfa(char *string,int state)
{
int i,j;
for(i=0;i<=row;i++)
{
if(*string)
{
curr=Fa[state][*string-97][i];
if (curr==-1)
break;
if(nfa(string+1,curr))
return 1;
}
else
{
if(states[1][i]==-1)
break;
if(state==states[1][i])
return 1;
}
}
return 0;
}
int main()
{
FILE *fp;
int i,j,k,flag=0;
char c,ch;
// clrscr();
fp=fopen("Nfa_ip.txt","r");
for(i=0;i<2;i++)
for(j=0;j<10;j++)
states[i][j]=-1;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
for(k=0;k<10;k++)
Fa[i][j][k]=-1;
while(fscanf(fp,"%d",&in)!=EOF)
{
fscanf(fp,"%c",&c);
if(flag)
{
states[sr][sc++]=in;
if(c=='\n')
{
sr++;
sc=0;
}
}
else if(c=='#')
{
flag=1;
Fa[row][col][th]=in;
printf("\nFa[%d][%d][%d]=%d",row,col,th,Fa[row][col][th]);
}
else if(!flag)
{
Fa[row][col][th]=in;
printf("\nFa[%d][%d][%d]=%d",row,col,th,Fa[row][col][th]);
if(c==',')
{
th++;
}
else if(c=='\n')
{
col=0;
row++;
th=0;
}
else if(c!=',')
{
col++;
th=0;
}
}
}
printf("\n\nEnter the string : \n");
scanf("%s",str);
if(nfa(str,states[0][0]))
printf("\nString Is Accepted");
else
printf("\nString Not Accepted");
getch();
return 0;
}