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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
| #include <cstdio> #include <cctype> #include <set> #include <string> #include <vector> #include <iostream> #include <algorithm>
using namespace std;
string TYPE[] = { "RESERVED", "NUMBER", "STRING", "SYMBOL", "NAME", "EOL", "COMMENT" };
struct SYMBOL { string all[26] = { "+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=", "<", ">", "=", "(", ")", "{", "}", "[", "]", ";", ":", ",", ".", "..", "..." };
set<string> all_symbol;
SYMBOL () { all_symbol.clear(); for(int i = 0; i < 26; ++i) all_symbol.insert(all[i]); }
inline bool is_symbol(string s) { return all_symbol.count(s); } } symb;
struct RESERVED { string all[21] = { "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while" };
set<string> all_reserved;
RESERVED () { all_reserved.clear(); for(int i = 0; i < 21; ++i) all_reserved.insert(all[i]); }
inline bool is_reserved(string s) { return all_reserved.count(s); } } rese;
struct Token { int typ; string s;
Token (int tp = 6, string ss = "") { typ = tp, s = ss; } };
struct NUMBER { inline bool is_zf(char c) { return c == '+' || c == '-'; }
inline bool is_10_int(const register string& s) { unsigned l = 0; while(l < s.length() && is_zf(s[l])) l++; if(l >= s.length()) return false; while(l < s.length() && isdigit(s[l])) l++; return l == s.length(); }
inline bool is_0x_char(char c) { return isdigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'); }
inline bool is_0x_int(const register string& s) { unsigned l = 0; while(l < s.length() && is_zf(s[l])) l++; if(l + 1 >= s.length()) return false; if(s[l] != '0' || (s[l + 1] != 'x' && s[l + 1] != 'X')) return false; l += 2; while(l < s.length() && is_0x_char(s[l])) l++; return l == s.length(); }
inline bool is_float(const register string& s) { unsigned l = 0; while(l < s.length() && is_zf(s[l])) l++; if(l + 1 >= s.length()) return false; int hav_e = -1, hav_d = -1; for(unsigned i = l; i < s.length(); ++i) { if(s[i] == '.') { if(~hav_d) return false; else hav_d = i + 1; } else if(s[i] == 'E' || s[i] == 'e') { if(~hav_e) return false; else hav_e = i + 1; } else if(s[i] != '-' && s[i] != '+' && !isdigit(s[i])) return false; } if((~hav_e) && (~hav_d)) { if(hav_e < hav_d || (hav_d == 1 && hav_e - hav_d == 1)) return false; return ((hav_d == 1) || is_10_int(s.substr(0, hav_d - 1))) && (is_10_int(s.substr(hav_d, hav_e - hav_d - 1)) ||(hav_e - hav_d == 1)) && is_10_int(s.substr(hav_e)); } else if(~hav_e) { return is_10_int(s.substr(0, hav_e - 1)) && is_10_int(s.substr(hav_e)); } else if(~hav_d) { return ((hav_d == 1) || is_10_int(s.substr(0, hav_d - 1))) && is_10_int(s.substr(hav_d)); } else return false; }
inline int is_number(const register string& s) { if(s[0] == '+' || s[0] == '-') return 0; if(is_10_int(s)) return 1; else if(is_0x_int(s)) return 2; else if(is_float(s)) return 3; else return 0; } } numb;
class BUF { private: string s; unsigned nowl;
inline bool can_be_a_name(char c) { return isalpha(c) || c == '_' || isdigit(c); }
inline bool can_be_a_number(char c) { return isdigit(c) || c == '.' || c == '-' || c == '+' || c == 'E' || c == 'e' || c == 'x' || c == 'X' || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'); }
public: BUF() { nowl = 0; s = ""; }
inline Token read() { Token ans; if(nowl == s.length()) { if(!getline(cin, s)) return Token(-1); nowl = 0; s.push_back('\n'); } while(s[nowl] == ' ') nowl++; if(s[nowl] == '\n') { nowl++; return ans; } if(s[nowl] == '-' && s[nowl + 1] == '-') { nowl = s.length(); return ans; } if(s[nowl] == '"') { bool zy = false; ans.s.push_back(s[nowl++]); while(zy || s[nowl] != '"') { if(!zy && s[nowl] == '\\') zy = true; else zy = false; ans.s.push_back(s[nowl++]); } ans.s.push_back(s[nowl++]); ans.typ = 3; return ans; } if(s[nowl] == '\'') { bool zy = false; ans.s.push_back(s[nowl++]); while(zy || s[nowl] != '\'') { if(!zy && s[nowl] == '\\') zy = true; else zy = false; ans.s.push_back(s[nowl++]); } ans.typ = 3; return ans; } if(isalpha(s[nowl])) { while(can_be_a_name(s[nowl])) ans.s.push_back(s[nowl++]); if(rese.is_reserved(ans.s)) ans.typ = 1; else ans.typ = 5; return ans; } else { int r = 0; if(s[nowl] != '+' && s[nowl] != '-') { while(can_be_a_number(s[nowl + r])) r++; while(r && !numb.is_number(s.substr(nowl, r))) r--; } if(r) { ans.s = s.substr(nowl, r); nowl += r; ans.typ = 2; } else { string lst = ans.s = ""; lst.push_back(s[nowl++]); while(symb.is_symbol(lst)) { ans.s.push_back(s[nowl - 1]); lst.push_back(s[nowl++]); } nowl--; ans.typ = 4; } return ans; } } } buf;
int main() { Token s; while(s = buf.read(), ~s.typ) { s.typ--; if(s.typ == 5) cout << '[' << TYPE[s.typ] << ']' << '\n'; else cout << '[' << TYPE[s.typ] << ']' << ' ' << s.s << '\n'; } return 0; }
|