Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. c program for dfa (deterministic finite automata). Let us first know what is DFA or let us again revise the concept of DFA? Automation are basically language acceptor or language recognizer. A finite automata is a collection of 5-tuple(Q,∑,∂,q0,F). Where
  2. Q=finite set of states
  3. ∑=input symbol
  4. ∂=transition function
  5. q0=initial state
  6. F=set of final state
  7.  
  8. c program for dfa
  9.  
  10. #include<stdio.h>
  11. #include<conio.h>
  12.  
  13. int ninputs;
  14.  
  15. int check(char,int ); //function declaration
  16. int dfa[10][10];
  17. char c[10], string[10];
  18.  
  19. int main()
  20. {
  21. int nstates, nfinals;
  22. int f[10];
  23. int i,j,s=0,final=0;
  24. printf("enter the number of states that your dfa consist of \n");
  25. scanf("%d",&nstates);
  26. printf("enter the number of input symbol that dfa have \n");
  27. scanf("%d",&ninputs);
  28. printf("\nenter input symbols\t");
  29.  
  30. for(i=0; i<ninputs; i++)
  31. {
  32. printf("\n\n %d input\t", i+1);
  33. printf("%c",c[i]=getch());
  34. }
  35.  
  36. printf("\n\nenter number of final states\t");
  37. scanf("%d",&nfinals);
  38.  
  39. for(i=0;i<nfinals;i++)
  40. {
  41. printf("\n\nFinal state %d : q",i+1);
  42. scanf("%d",&f[i]);
  43. }
  44.  
  45. printf("-----------------------------------------------------------------------");
  46. printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");
  47.  
  48. for(i=0; i<ninputs; i++)
  49. {
  50. for(j=0; j<nstates; j++)
  51. {
  52. printf("\n(q%d , %c ) = q",j,c[i]);
  53. scanf("%d",&dfa[i][j]);
  54. }
  55. }
  56.  
  57. do
  58. {
  59. i=0;
  60. printf("\n\nEnter Input String.. ");
  61. scanf("%s",string);
  62.  
  63. while(string[i]!='\0')
  64. if((s=check(string[i++],s))<0)
  65. break;
  66. for(i=0 ;i<nfinals ;i++)
  67. if(f[i] ==s )
  68. final=1;
  69. if(final==1)
  70. printf("\n valid string");
  71. else
  72. printf("invalid string");
  73. getch();
  74.  
  75. printf("\nDo you want to continue.? \n(y/n) ");
  76. }
  77. while(getch()=='y');
  78.  
  79. getch();
  80. }
  81. int check(char b,int d)
  82. {
  83. int j;
  84. for(j=0; j<ninputs; j++)
  85. if(b==c[j])
  86. return(dfa[d][j]);
  87. return -1;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement