forked from niyasc/Compiler-Design-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfa.c
More file actions
101 lines (95 loc) · 1.67 KB
/
nfa.c
File metadata and controls
101 lines (95 loc) · 1.67 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
100
101
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
int itable[10][10][10];
char isymbols[10];
int fstates[10];
int fcount;
int m,n;
bool check(char string[],int current_state,int index)
{
//find index of current_symbol
printf("Current state %d current symbol -%c-\n",current_state,string[index]);
int i,j,k;
for(i=0;i<n;i++)
if(string[index]==isymbols[i])
break;
if(i==n)
return false;
if(itable[current_state][i][1]=='-')
{
printf("Returning invalid movement\n");
return false;
}
else
for(j=1;j<itable[current_state][i][0];j++)
{
if(index<strlen(string)-1)
{
printf("next state %d\n",itable[current_state][i][j]-48);
if(check(string,itable[current_state][i][j]-48,index+1)==true)
return true;
}
else
{
//check whether current state is final state
for(k=0;k<fcount;k++)
{
if(current_state==fstates[k])
return true;
}
printf("string completed but not reached in final state");
return false;
}
}
return false;
}
int main()
{
char c;
int i,j,k;
FILE *ptr=fopen("nfa.txt","r");
k=0;
while(1)
{
fscanf(ptr,"%c",&c);
if(c==' '){}
else if(c=='\n')
break;
else
isymbols[k++]=c;
}
n=k;
i=0,j=0,k=1;
while(fscanf(ptr,"%c",&c)!=EOF)
{
if(c==','){}
else if(c==' ')
{
itable[i][j][0]=k;
k=1;
j++;
}
else if(c=='\n')
{
itable[i][j][0]=k;
k=1;
j=0;
i++;
}
else
itable[i][j][k++]=c;
}
m=i-1;
fcount=0;
for(i=1;i<itable[i][0][0];i++)
fstates[fcount++]=itable[m][0][i]-48;
char string[10];
printf("Enter string : ");
scanf("%s",string);
if(check(string,0,0)==true)
printf("String accepted \n");
else
printf("String rejected \n");
return 0;
}