Advertisement
kartikkukreja

DFA simulation

Apr 24th, 2013
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. /*
  2.  * Author : Kartik Kukreja
  3.  * Description :    Program to simulate a given DFA on an input string. It reads in the
  4.             DFA from "DFA.txt" and the input strings from the console and prints out
  5.             whether the strings are accepted or rejected on the console.
  6.                    
  7.             "DFA.txt" must have the following format:
  8.                 N M
  9.                 F a1 a2 ... af
  10.                 s1 y1 t1
  11.                 s2 y2 y2
  12.                 :
  13.                 :
  14.             Here,   N -> No. of states in the DFA (states are numbered 0 ... N-1)
  15.                 M -> Size of input alphabet (input symbols are numbered 1 ... M)
  16.                 F -> No. of final states followed by F states ( 0 <= ai <= N-1)
  17.                 si -> Previous state
  18.                 yi -> Input symbol
  19.                 ti -> Next state
  20.             Each line until the end of file contains one transition ( si yi ti ).
  21.  *          '0' is the start state.
  22.  *          The input strings contain space-separated input symbols (1 ... M).
  23.  */
  24.  
  25. #include <cstdio>
  26. #include <iostream>
  27. #include <fstream>
  28. #include <cstdlib>
  29. #include <cstring>
  30. #define MAX_DFA_STATES 10
  31. #define MAX_ALPHABET_SIZE 10
  32. using namespace std;
  33.  
  34. int transitions[MAX_DFA_STATES][MAX_ALPHABET_SIZE];
  35. bool finalStates[MAX_DFA_STATES];
  36. char input_string[100000];
  37.  
  38. int main()  {
  39.     int N, M, F, X, Y, A, state, symbol;
  40.     char* p;
  41.  
  42.     // read in the underlying DFA
  43.     ifstream fin("DFA.txt");
  44.     fin >> N >> M >> F;
  45.     for(i=0; i<F; i++)  {
  46.         fin >> X;
  47.         finalStates[X] = true;
  48.     }
  49.     memset(transitions, -1, MAX_DFA_STATES * MAX_ALPHABET_SIZE * sizeof(int));
  50.     while(!fin.eof())   {
  51.         fin >> X >> A >> Y;
  52.         transitions[X][A] = Y;
  53.     }
  54.     fin.close();
  55.  
  56.     // check if the DFA is well defined
  57.     for(i=0; i<N; i++)
  58.         for(j=1; j<=M; j++)
  59.             if(transitions[i][j] < 0 || transitions[i][j] >= N) {
  60.                 printf("DFA not defined properly.\n");
  61.                 return -1;
  62.             }
  63.  
  64.     // simulate the DFA
  65.     printf("Enter a string ('.' to exit) : ");
  66.     scanf("%[^\n]%*c", input_string);
  67.     while(input_string[0] != '.')   {
  68.         state = 0;
  69.         p = strtok(input_string, " ");
  70.         while(p)    {
  71.             symbol = atoi(p);
  72.             if(symbol <= 0 || symbol > M)   {
  73.                 printf("Invalid input symbol %d.\n", symbol);
  74.                 return -1;
  75.             } else {
  76.                 state = transitions[state][symbol];
  77.             }
  78.             p = strtok(NULL, " ");
  79.         }
  80.         if(finalStates[state])
  81.             printf("String accepted.\n");
  82.         else
  83.             printf("String rejected.\n");
  84.  
  85.         printf("Enter a string ('.' to exit) : ");
  86.         scanf("%[^\n]%*c", input_string);
  87.     }
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement