Advertisement
kartikkukreja

NFA simulation

May 24th, 2013
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. /*
  2.  * Author : Kartik Kukreja
  3.  * Description :    Program to simulate a given NFA on an input string. It reads in the
  4.             NFA from "NFA.txt" and the input strings from the console and prints out
  5.             whether the strings are accepted or rejected on the console.
  6.  
  7.             "NFA.txt" must have the following format:
  8.                 N M
  9.                 F a1 a2 ... af
  10.                 T
  11.                 s1 y1 T1 t1 t2 ... tt1
  12.                 s2 y2 T2 t1 t2 ... tt2
  13.                 :
  14.                 :
  15.             Here,   N -> No. of states (states are numbered 0 ... N-1). 0 is the start state
  16.                 M -> Size of input alphabet (input symbols are
  17.                     numbered 1 ... M and 0 is reserved for epsilon)
  18.                 F -> No. of final states, followed by F states ( 0 <= ai <= N-1)
  19.                 T -> No. of transitions, followed by T lines
  20.                 si -> Previous state ( 0 <= si <= N-1)
  21.                 yi -> Input symbol or epsilon ( 0 <= yi <= M)
  22.                 Ti -> No. of states the NFA moves from si on input yi, followed by Ti states
  23.  */
  24.  
  25. #include <cstdio>
  26. #include <fstream>
  27. #include <cstdlib>
  28. #include <set>
  29. #include <bitset>
  30. #include <cstring>
  31. #define MAX_NFA_STATES 10
  32. #define MAX_ALPHABET_SIZE 10
  33. using namespace std;
  34.  
  35. // Representation of an NFA state
  36. class NFAstate {
  37.     public:
  38.         int transitions[MAX_ALPHABET_SIZE][MAX_NFA_STATES];
  39.         NFAstate()  {
  40.             for(int i=0; i<MAX_ALPHABET_SIZE; i++)
  41.                 for(int j=0; j<MAX_NFA_STATES; j++)
  42.                     transitions[i][j] = -1;
  43.         }
  44. } *NFAstates;
  45.  
  46. set <int> NFA_finalStates;
  47. bitset <MAX_NFA_STATES> currentStates;
  48. char input_string[100000];
  49. int N;
  50.  
  51. // finds the epsilon closure of the NFA state "state" and stores it into "closure"
  52. void epsilonClosure(int state, bitset<MAX_NFA_STATES> &closure )    {
  53.     for(int i=0; i<N && NFAstates[state].transitions[0][i] != -1; i++)
  54.         if(closure[NFAstates[state].transitions[0][i]] == 0)    {
  55.             closure[NFAstates[state].transitions[0][i]] = 1;
  56.             epsilonClosure(NFAstates[state].transitions[0][i], closure);
  57.         }
  58. }
  59.  
  60. // finds the epsilon closure of a set of NFA states "state" and stores it into "closure"
  61. void epsilonClosure(bitset<MAX_NFA_STATES> state, bitset<MAX_NFA_STATES> &closure) {
  62.     for(int i=0; i<N; i++)
  63.         if(state[i] == 1)
  64.             epsilonClosure(i, closure);
  65. }
  66.  
  67. // returns a bitset representing the set of states the NFA could be in after moving
  68. // from state X on input symbol A
  69. void NFAmove(int X, int A, bitset<MAX_NFA_STATES> &Y)   {
  70.     for(int i=0; i<N && NFAstates[X].transitions[A][i] != -1; i++)
  71.         Y[NFAstates[X].transitions[A][i]] = 1;
  72. }
  73.  
  74. // returns a bitset representing the set of states the NFA could be in after moving
  75. // from the set of states X on input symbol A
  76. void NFAmove(bitset<MAX_NFA_STATES> X, int A, bitset<MAX_NFA_STATES> &Y)   {
  77.     bitset<MAX_NFA_STATES> tmp;
  78.     for(int i=0; i<N; i++)
  79.         if(X[i] == 1)
  80.             NFAmove(i, A, tmp);
  81.     Y = tmp;
  82. }
  83.  
  84. int main()  {
  85.     int M, F, X, Y, A, i, j, T, symbol;
  86.     char* p;
  87.  
  88.     // read in the underlying NFA
  89.     ifstream fin("NFA.txt");
  90.     fin >> N >> M;
  91.     NFAstates = new NFAstate[N];
  92.     fin >> F;
  93.     for(i=0; i<F; i++)    {
  94.         fin >> X;
  95.         NFA_finalStates.insert(X);
  96.     }
  97.     fin >> T;
  98.     while(T--)   {
  99.         fin >> X >> A >> Y;
  100.         for(i=0; i<Y; i++)  {
  101.             fin >> j;
  102.             NFAstates[X].transitions[A][i] = j;
  103.         }
  104.     }
  105.     fin.close();
  106.  
  107.     // simulate the NFA
  108.     printf("Enter a string ('.' to exit) : ");
  109.     scanf("%[^\n]%*c", input_string);
  110.     while(input_string[0] != '.')   {
  111.         currentStates[0] = 1;
  112.         epsilonClosure(currentStates, currentStates);
  113.  
  114.         p = strtok(input_string, " ");
  115.         while(p)    {
  116.             symbol = atoi(p);
  117.             if(symbol <= 0 || symbol > M)   {
  118.                 printf("Invalid input symbol %d.\n", symbol);
  119.                 return -1;
  120.             } else {
  121.                 NFAmove(currentStates, symbol, currentStates);
  122.                 epsilonClosure(currentStates, currentStates);
  123.             }
  124.             p = strtok(NULL, " ");
  125.         }
  126.  
  127.         for(i = 0; i < N; i++)
  128.             if(currentStates[i] == 1 && NFA_finalStates.find(i) != NFA_finalStates.end())
  129.                 break;
  130.  
  131.         if(i < N)
  132.             printf("String accepted.\n");
  133.         else
  134.             printf("String rejected.\n");
  135.  
  136.         printf("Enter a string ('.' to exit) : ");
  137.         scanf("%[^\n]%*c", input_string);
  138.     }
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement