How To Implement DFA in C Code
Implementing DFA in C Program
If You need the .Exe File i will provide it..
2: *PROGRAM NAME: DFA.C
3: *AIM : TO IMPLEMENT DFA
4: *DATE : 07/28/2011
5: */
6: #include<stdio.h>
7: //nt a[10],b[10];
8: char letters[10];
9: int letter_count,count,final;
10: int final_states[5];
11: void DEFINE_DFA();
12: int MOVE_DFA(int,char);
13: int dfa[10][10];
14: int main()
15: {
16: int s,i,accepted;
17: char line[10];
18: printf("\t\tDFA SIMULATOR\n\t******************************\n");
19: DEFINE_DFA();
20: do
21: {
22: s=0;
23: i=0;
24: accepted=0;
25: printf("\n\nEnter Input String.. ");
26: scanf("%s",line);
27: while(line[i]!='\0')
28: if((s=MOVE_DFA(s,line[i++]))<0)
29: break;
30: for(i=0;i<final;i++)
31: if(final_states[i]==s)
32: accepted=1;
33: (accepted)?printf("\a\n\tYES..!!!") : printf("\a\n\tNO...!!");
34: getch();
35: printf("\n\nDo you want to continue..??(y/n)");
36: }
37: while(getch()=='y');
38: return 1;
39: }
40: void DEFINE_DFA()
41: {
42: int i,j;
43: char c;
44: printf("Enter the no. of states.. ");
45: scanf("%d",&count);
46: printf("Enter the no. of input letters.. ");
47: scanf("%d",&letter_count);
48: printf("Enter the no. of final states.. ");
49: scanf("%d",&final);
50: for(i=0;i<final;i++)
51: {
52: printf("Final state %d : ",i+1);
53: scanf("%d",&final_states[i]);
54: }
55: for(i=0;i<letter_count;i++)
56: {
57: printf("\nLetter %d :",i+1);
58: printf("%c",letters[i]=getch());
59: }
60: printf("\n\nFill transition diagram..");
61: for(i=0;i<count;i++)
62: {
63: printf("\n\n\t _________\n\t|STATE| %d |",i);
64: printf("\n\t __________\n");
65: for(j=0;j<letter_count;j++)
66: {
67: printf("\n\t| %c | |\b\b\b",letters[j]);
68: scanf("%d",&dfa[i][j]);
69: }
70: printf("\n\t ----------\n");
71: }
72: }
73: int MOVE_DFA(int s,char c)
74: {
75: int j;
76: for(j=0;j<letter_count;j++)
77: if(c==letters[j])
78: return(dfa[s][j]);
79: return -1;
80: }
If You need the .Exe File i will provide it..
1: /*
2: *PROGRAM NAME: DFA.C
3: *AIM : TO IMPLEMENT DFA
4: *DATE : 07/28/2011
5: */
6: #include<stdio.h>
7: //nt a[10],b[10];
8: char letters[10];
9: int letter_count,count,final;
10: int final_states[5];
11: void DEFINE_DFA();
12: int MOVE_DFA(int,char);
13: int dfa[10][10];
14: int main()
15: {
16: int s,i,accepted;
17: char line[10];
18: printf("\t\tDFA SIMULATOR\n\t******************************\n");
19: DEFINE_DFA();
20: do
21: {
22: s=0;
23: i=0;
24: accepted=0;
25: printf("\n\nEnter Input String.. ");
26: scanf("%s",line);
27: while(line[i]!='\0')
28: if((s=MOVE_DFA(s,line[i++]))<0)
29: break;
30: for(i=0;i<final;i++)
31: if(final_states[i]==s)
32: accepted=1;
33: (accepted)?printf("\a\n\tYES..!!!") : printf("\a\n\tNO...!!");
34: getch();
35: printf("\n\nDo you want to continue..??(y/n)");
36: }
37: while(getch()=='y');
38: return 1;
39: }
40: void DEFINE_DFA()
41: {
42: int i,j;
43: char c;
44: printf("Enter the no. of states.. ");
45: scanf("%d",&count);
46: printf("Enter the no. of input letters.. ");
47: scanf("%d",&letter_count);
48: printf("Enter the no. of final states.. ");
49: scanf("%d",&final);
50: for(i=0;i<final;i++)
51: {
52: printf("Final state %d : ",i+1);
53: scanf("%d",&final_states[i]);
54: }
55: for(i=0;i<letter_count;i++)
56: {
57: printf("\nLetter %d :",i+1);
58: printf("%c",letters[i]=getch());
59: }
60: printf("\n\nFill transition diagram..");
61: for(i=0;i<count;i++)
62: {
63: printf("\n\n\t _________\n\t|STATE| %d |",i);
64: printf("\n\t __________\n");
65: for(j=0;j<letter_count;j++)
66: {
67: printf("\n\t| %c | |\b\b\b",letters[j]);
68: scanf("%d",&dfa[i][j]);
69: }
70: printf("\n\t ----------\n");
71: }
72: }
73: int MOVE_DFA(int s,char c)
74: {
75: int j;
76: for(j=0;j<letter_count;j++)
77: if(c==letters[j])
78: return(dfa[s][j]);
79: return -1;
80: }
Comments
Post a Comment