1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
| #include <algorithm> #include <iostream> #include <string> #include <queue> #include <map> #include <stack>
using namespace std; map<char, int> form;
void init_form() { form.insert(pair<char, int>('(', 0)); form.insert(pair<char, int>(')', 0)); form.insert(pair<char, int>('*', 3)); form.insert(pair<char, int>('.', 2)); form.insert(pair<char, int>('+', 1)); }
bool isleagal(char op) { if (op == '(' || op == ')' || op == '.' || op == '*' || op == '+') {
return true; }
return false; }
bool myIsSupper(char c) { return ('0' <= c && c <= '1'); }
string init_expre(string expre) { string res = ""; char last_char = '$'; for (int i = 0; i < expre.length(); i++) { if (0 == i) { last_char = expre[i]; res += expre[i]; } else { if (myIsSupper(last_char)) { if (myIsSupper(expre[i]) || expre[i] == '(') { res += '.'; } } else if (last_char == ')') { if (myIsSupper(expre[i]) || '(' == expre[i]) { res += '.'; } } else if (last_char == '*') { if (myIsSupper(expre[i]) || expre[i] == '(') { res += '.'; } } last_char = expre[i]; res += expre[i]; } } return res; }
queue<char>* infix_to_postfix(string expre) { init_form(); queue<char>* postfix = new queue<char>(); stack<char>* op = new stack<char>();
for (int i = 0; i < expre.length(); i++) { if ('0' <= expre[i] && expre[i] <= '1') { postfix->push(expre[i]); } else { if (!isleagal(expre[i])) { return nullptr; } else { if (expre[i] == '(') { op->push(expre[i]); } else if (expre[i] == ')') { while (!op->empty() && op->top() != '(') { postfix->push(op->top()); op->pop();
} if (op->empty()) { return nullptr; } op->pop(); } else if (op->empty() || form[expre[i]] > form[op->top()]) { op->push(expre[i]); } else { char tp = op->top(); while (!op->empty() && form[expre[i]] <= form[tp]) { postfix->push(tp); op->pop(); if (!op->empty()) { tp = op->top(); } } op->push(expre[i]); } } } } while (!op->empty()) { postfix->push(op->top()); op->pop(); }
return postfix; }
vector<vector<pair<int, char>>>* make_e_NFA(string expre, pair<int, int>& begin_end, int& maxStates) { vector<vector<pair<int, char>>>* NFA_map = new vector<vector<pair<int, char>>>();
int stateNum = 0; stack<pair<int, int>>* st = new stack<pair<int, int>>();
expre = init_expre(expre); queue<char>* postfix = infix_to_postfix(expre);
while (!postfix->empty()) { char tc = postfix->front(); postfix->pop(); if ('0' <= tc && tc <= '1') { int state_begin = stateNum++; NFA_map->push_back(vector<pair<int, char>>()); int state_end = stateNum++; NFA_map->push_back(vector<pair<int, char>>()); st->push(pair<int, int>(state_begin, state_end)); NFA_map->at(state_begin).push_back(make_pair(state_end, tc)); } else { if (tc == '*') { pair<int, int> t_NFA = st->top(); st->pop(); int state_begin = stateNum++; NFA_map->push_back(vector<pair<int, char>>()); int state_end = stateNum++; NFA_map->push_back(vector<pair<int, char>>()); st->push(pair<int, int>(state_begin, state_end)); NFA_map->at(state_begin).push_back(make_pair(t_NFA.first, '$')); NFA_map->at(state_begin).push_back(make_pair(state_end, '$')); NFA_map->at(t_NFA.second).push_back(make_pair(state_end, '$')); NFA_map->at(t_NFA.second).push_back(make_pair(t_NFA.first, '$')); } else if (tc == '.') { pair<int, int> t_NFAy = st->top(); st->pop(); pair<int, int> t_NFAx = st->top(); st->pop();
NFA_map->at(t_NFAx.second).push_back(make_pair(t_NFAy.first, '$')); st->push(pair<int, int>(t_NFAx.first, t_NFAy.second)); } else { pair<int, int> t_NFAy = st->top(); st->pop(); pair<int, int> t_NFAx = st->top(); st->pop();
int state_begin = stateNum++; NFA_map->push_back(vector<pair<int, char>>()); int state_end = stateNum++; NFA_map->push_back(vector<pair<int, char>>());
NFA_map->at(state_begin).push_back(make_pair(t_NFAx.first, '$')); NFA_map->at(state_begin).push_back(make_pair(t_NFAy.first, '$')); NFA_map->at(t_NFAx.second).push_back(make_pair(state_end, '$')); NFA_map->at(t_NFAy.second).push_back(make_pair(state_end, '$')); st->push(pair<int, int>(state_begin, state_end)); } } } maxStates = stateNum; begin_end = st->top(); return NFA_map; }
|