Advertisement
PSYCHAMERON

NFA

Feb 25th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<string> automata[100];
  5. vector<string> symbols[100];
  6. string start, finish, nfaState;
  7. int numOfStates;
  8.  
  9. void takeInput();
  10. string canReachUsingSymbol(string from, string symbol);
  11. bool isValidCommand(string command);
  12. int getNfaPosition(string command);
  13. bool check(string command);
  14.  
  15. int main()
  16. {
  17.     takeInput();
  18.     string command;
  19.  
  20.     while(true)
  21.     {
  22.         cout << "Enter command: ";
  23.         cin >> command;
  24.  
  25.         if(check(command))
  26.         {
  27.             cout << "Valid command!\n";
  28.         }
  29.         else
  30.         {
  31.             cout << "Invalid command!\n";
  32.         }
  33.     } // end of while
  34.  
  35.     return 0;
  36. }
  37.  
  38. void takeInput()
  39. {
  40.     cout << "How many states: ";
  41.     cin >> numOfStates;
  42.  
  43.     cout << "Input starting state: ";
  44.     cin >> start;
  45.  
  46.     cout << "Input ending state: ";
  47.     cin >> finish;
  48.  
  49.     cout << "Input NFA state"
  50.     << " (we assume that 0, 1 to go from this state to itself): ";
  51.     cin >> nfaState; // 0, 1 to go to itself
  52.  
  53.     string temp;
  54.  
  55.     for(int i = 0; i < numOfStates; i++)
  56.     {
  57.         cout << "Enter name of the #" << i+1 << " state: ";
  58.         cin >> temp;
  59.         automata[i].push_back(temp); // first element is the state itself
  60.  
  61.         cout << "Which state can be accessed from " << temp
  62.         << " (NULL if not any): ";
  63.         cin >> temp;
  64.  
  65.         if(temp == "NULL") continue;
  66.  
  67.         automata[i].push_back(temp);
  68.  
  69.         symbols[i].push_back("NULL");
  70.  
  71.         cout << "Which is the symbol to go from " << automata[i][0]
  72.         << " to " << temp << ": ";
  73.         cin >> temp;
  74.         symbols[i].push_back(temp);
  75.     } // end of for
  76. }
  77.  
  78. string canReachUsingSymbol(string from, string symbol)
  79. {
  80.     int stateIndex = -1;
  81.     string reachable = "NULL";
  82.  
  83.     for(int i = 0; i < numOfStates; i++)
  84.     {
  85.         if(automata[i][0] == from)
  86.         {
  87.             stateIndex = i;
  88.             break;
  89.         }
  90.     } // end of for
  91.  
  92.     if(stateIndex == -1) return reachable;
  93.  
  94.     for(int j = 1; j < automata[stateIndex].size(); j++)
  95.     {
  96.         if(symbols[stateIndex][j] == symbol)
  97.         {
  98.             return automata[stateIndex][j];
  99.         }
  100.     } // end of for
  101.  
  102.     return reachable;
  103. } //////////////////////////////////////
  104.  
  105. bool isValidCommand(string command)
  106. {
  107.     string currentState = nfaState;
  108.     string temp;
  109.  
  110.     for(int i = 0; i < command.size(); i++)
  111.     {
  112.         temp = "";
  113.         temp += command[i];
  114.  
  115.         currentState = canReachUsingSymbol(currentState, temp);
  116.     } // end of for
  117.  
  118.     return (currentState == finish);
  119. } /////////////////////////////////////
  120.  
  121. int getNfaPosition(string command)
  122. {
  123.     int ans = -2;
  124.     string currentState = start;
  125.     string temp;
  126.  
  127.     // if the starting state is the NFA state
  128.     if(start == nfaState) return 0;
  129.  
  130.     for(int i = 0; i < command.size(); i++)
  131.     {
  132.         temp = "";
  133.         temp += command[i];
  134.  
  135.         currentState = canReachUsingSymbol(currentState, temp);
  136.         if(currentState == nfaState)
  137.         {
  138.             ans = i + 1;
  139.             break;
  140.         } // end of if
  141.     } // end of for
  142.  
  143.     return ans;
  144. }
  145.  
  146. bool check(string command)
  147. {
  148.     int pos;
  149.  
  150.     pos = getNfaPosition(command);
  151.     if(pos == -2) return false;
  152.     if(nfaState == finish) return true;
  153.  
  154.     //cout << "pos: " << pos << endl;
  155.  
  156.     for(int i = pos ; i < command.size(); i++)
  157.     {
  158.         //cout << string(command.begin() + i, command.end() ) << endl;
  159.  
  160.         if( isValidCommand( string(command.begin() + i, command.end()) ) )
  161.         {
  162.             return true;
  163.         }
  164.     }
  165.  
  166.     return false;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement