-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydriver.c
176 lines (130 loc) · 3.43 KB
/
mydriver.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* ****** error code declaration is remains *** */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXSIZE 20
struct dfaDriver
{
int states;
int symbols;
int transitions[20][20];
int final[10];
};
char str[MAXSIZE];
char c;
int source,destination,i,unique,finals,length, currstate, startstate = 0, endstate, nextstate;
// above variable minimization is remaining....
int asci = 97, k = 0;
int dfaExecution(struct dfaDriver dfa)
{
fgets(str,MAXSIZE,stdin);
fprintf(stdout,"\n");
fprintf(stdout,"This is Your string\n");
fprintf(stdout,"%s\n",str);
length = strlen(str) - 1;
for(i = 0; i < length; i++)
if(isalpha(str[i]) == 0 )
{
fprintf(stdout,"Incorrect String\n");
exit(0);
}
currstate = startstate;
i = 0;
while(i < length)
{
c = str[i];
endstate = c - 'a';
nextstate = (dfa.transitions[currstate][endstate]);
currstate = nextstate;
i++;
}
for(i = 0; i < finals; i++)
{
if((dfa.final[i]) == currstate)
{
fprintf(stdout,"Input String Is In Language \n !...ACCEPTED...!\n");
exit(0);
}
}
fprintf(stdout,"Input String Is Not In Language \n !....REJECTED...!\n");
return 0;
}
void displayDfaDetails(struct dfaDriver dfa)
{
fprintf(stdout,"*****------Dispaly The Dfa Details-----******\n\n");
fprintf(stdout,"Number ofd states is : %d\n",dfa.states);
fprintf(stdout,"Number ofd Symbols is : %d\n",dfa.symbols);
fprintf(stdout,"Transitions Table\n");
for(source = 0; source < dfa.states; source ++)
{
fprintf(stdout,"{");
for(unique = 0; unique < dfa.symbols; unique ++)
if(unique == dfa.symbols - 1)
fprintf(stdout," %d ",dfa.transitions[source][unique]);
else
fprintf(stdout," %d ,",dfa.transitions[source][unique]);
fprintf(stdout,"}\n");
}
fprintf(stdout,"The Final State\n");
for(i = 0; i < finals; i++)
fprintf(stdout,"%d\n",dfa.final[i]);
}
void acceptDfaDetails()
{
struct dfaDriver dfa;
fprintf(stdout,"How Many States in Your Dfa\n");
scanf("%d",&dfa.states);
fprintf(stdout,"How Many Symbols in Your Dfa\n");
scanf("%d",&dfa.symbols);
if(((dfa.states) < 0 ) || (dfa.symbols) < 0 || (dfa.symbols) > 26 || (isalpha(dfa.states)) != 0)
{
fprintf(stdout,"Invalid states / symbols count\n");
exit(0);
}
fprintf(stdout,"** Accept Transitions For Every State Over Every Alphabet **\n");
for(source = 0; source < (dfa.states); source ++)
{
fprintf(stdout,"For State %d ",source);
for(unique = 0; unique < (dfa.symbols); unique ++)
{
fprintf(stdout,"Over Symbol %c \n",(asci + unique));
scanf("%d",&(dfa.transitions[source][unique]));
/* ***** if(isalpha(dfa.transitions[source][unique]) != 0)
{
fprintf(stdout,"Invaliid State Transition\n");
exit(0);
}
***** */
if((dfa.transitions[source][unique]) > (dfa.states)-1 )
{
fprintf(stdout,"Invaliid State (Enter The Valid State)\n");
source --;
break;
}
}
}
fprintf(stdout,"Define Array Of Final States\n");
fprintf(stdout,"How Many Final States ");
scanf("%d",&finals);
if(finals > (dfa.states) || (finals < 0))
{
fprintf(stdout,"Incorrect Final states\n");
exit(0);
}
for(i = 0; i < finals; i++)
scanf("%d\n",&(dfa.final[i]));
if((dfa.final[i]) < 0)
{
fprintf(stdout,"Incorrect Final states\n");
exit(0);
}
dfaExecution(dfa);
fprintf(stdout,"\n");
displayDfaDetails(dfa);
}
int main(void)
{
acceptDfaDetails();
return 0;
}