canezzy

finiteStateMachine

Mar 27th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 KB | None | 0 0
  1. #include <sstream>
  2.  
  3. #include "FiniteStateMachine.h"
  4.  
  5. using namespace std;
  6.  
  7. const TokenType FiniteStateMachine::stateToTokenTable[NUM_STATE] = {
  8.     /*state 0*/     T_NO_TYPE,
  9.     /*state 1*/     T_NO_TYPE,
  10.     /*state 2*/     T_ID,
  11.     /*state 3*/     T_IF,
  12.     /*state 4*/     T_ID,
  13.     /*state 5*/     T_ID,
  14.     /*state 6*/     T_ID,
  15.     /*state 7*/     T_THEN,
  16.     /*state 8*/     T_ID,
  17.     /*state 9*/     T_ID,
  18.     /*state A*/     T_ID,
  19.     /*state B*/     T_ELSE,
  20.     /*state C*/     T_ID,
  21.     /*state D*/     T_MINUS,
  22.     /*state E*/     T_PLUS,
  23.     /*state F*/     T_EQ,
  24.     /*state G*/     T_SEMI,
  25.     /*state H*/     T_WHITE_SPACE,
  26.     /*state I*/     T_ERROR,
  27.     /*state J*/     T_NUM,
  28.     /*state K*/     T_REAL,
  29.     /*state L*/     T_COMMENT,
  30.     /*state M*/     T_EQEQ,
  31.     ////////////////
  32.     /*state N*/     T_ID,
  33.     /*state O*/     T_ID,
  34.     /*state P*/     T_DEC,
  35.     /////////////
  36. };
  37.  
  38. const char FiniteStateMachine::supportedCharacters[NUM_OF_CHARACTERS] =
  39. {
  40.     '0','1','2','3','4','5','6','7','8','9',
  41.     'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
  42.     '.','-',' ','\t','\n','\r',';','=','+','#'
  43. };
  44.  
  45. /*
  46.     S - space,
  47.     T - tab,
  48.     E - enter,
  49.     C - clear line
  50. */
  51. const int FiniteStateMachine::stateMatrix[NUM_STATE][NUM_OF_CHARACTERS] =
  52. {
  53.                 // 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z . - S T E C ; = + #
  54.     /* state 0 */ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},        // IDLE_STATE
  55.    
  56.     /* state 1 */ {J,J,J,J,J,J,J,J,J,J,C,C,C,N,8,C,C,C,2,C,C,C,C,C,C,C,C,C,C,4,C,C,C,C,C,C,K,D,H,H,H,H,G,F,E,R},        // START_STATE
  57.  
  58.     /* state 2 */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,3,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},
  59.     /* state 3 */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},        // T_IF
  60.  
  61.                 // 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z . - S T E C ; = + #
  62.     /* state 4 */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,5,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},
  63.     /* state 5 */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,6,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},
  64.     /* state 6 */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,7,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},
  65.     /* state 7 */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},        // T_THEN
  66.  
  67.                 // 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z . - S T E C ; = + #
  68.     /* state 8 */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,9,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},
  69.     /* state 9 */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,A,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},
  70.     /* state A */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,B,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},
  71.     /* state B */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},        // T_ELSE
  72.  
  73.     /* state C */ {C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0},        // T_ID
  74.  
  75.     /* state D */ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,I,0,0,0,0,0,0,0,0},        // T_MINUS
  76.  
  77.     /* state E */ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,I,0},        // T_PLUS
  78.  
  79.     /* state F */ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,M,0,0},        // T_EQ
  80.  
  81.     /* state G */ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},        // T_SEMI
  82.  
  83.     /* state H */ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,H,H,H,H,0,0,0,0},        // T_WHITE_SPACE
  84.  
  85.     /* state I */ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},        // T_ERROR
  86.  
  87.     /* state J */ {J,J,J,J,J,J,J,J,J,J,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,K,0,0,0,0,0,0,0,0,0},        // T_NUM
  88.  
  89.     /* state K */ {K,K,K,K,K,K,K,K,K,K,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,0,0,0,0,0,0,0,0,0},        // T_REAL
  90.  
  91.     /* state L */ {R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,R,0,0,R,R,R,R},        // T_COMMENT
  92.  
  93.     /* state M */ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},        // T_EQEQ
  94.  
  95.                 // 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z . - S T E C ; = + #
  96.     /* state N */{ C,C,C,C,C,C,C,C,C,C,C,C,C,C,O,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0 },       // T_ID
  97.     /* state O */{ C,C,C,C,C,C,C,C,C,C,C,C,P,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0 },       // T_ID
  98.     /* state P */{ C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,C,I,0,0,0,0,0,0,0,0,0 },       // T_DEC
  99. };
  100.  
  101.  
  102. TokenType FiniteStateMachine::getTokenType(int stateNumber)
  103. {
  104.     return stateToTokenTable[stateNumber];
  105. }
  106.  
  107.  
  108. void FiniteStateMachine::initStateMachine()
  109. {
  110.     for (int i = 0; i < NUM_STATE; i++)
  111.     {
  112.         map<char, int> stateTransitions;
  113.         for (int j = 0; j < NUM_OF_CHARACTERS; j++)
  114.         {
  115.             stateTransitions[supportedCharacters[j]] = stateMatrix[i][j];
  116.         }
  117.         stateMachine[i] = stateTransitions;
  118.     }
  119. }
  120.  
  121.  
  122. int FiniteStateMachine::getNextState(int currentState, char transitionLetter)
  123. {
  124.     STATE_MACHINE::iterator it = stateMachine.find(currentState);
  125.    
  126.     if (it == stateMachine.end())
  127.     {
  128.         string strCurrentState;
  129.         stringstream ss;
  130.         ss << currentState;
  131.         ss >> strCurrentState;
  132.         string errMessage = "\nEXCEPTION: currentState = " + strCurrentState + " is not a valid state!";
  133.         throw runtime_error(errMessage.c_str());
  134.     }
  135.  
  136.     map<char, int>::iterator cit = it->second.find(transitionLetter);
  137.  
  138.     if (cit == it->second.end())
  139.     {
  140.         return INVALID_STATE;
  141.     }
  142.     else
  143.     {
  144.         return cit->second;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment