#include <iostream> using namespace std; #include <string.h> char M[1000], Gjz[6][10] = {"begin", "if", "then", "while", "do", "end"}; void Set() { char T[50]; int i, j, now; i = now = 0; while (1) { switch (M[now]) { case 'b': for (j = 0, i = now; i <= now + 4; i++, j++) T[j] = M[i]; T[j] = '\0'; if (strcmp(T, Gjz[0]) == 0) { cout << "(" << 1 << "," << "begin" << ")"; now = i; } else goto loop; break; case 'i': for (j = 0, i = now; i <= now + 1; i++, j++) T[j] = M[i]; T[j] = '\0'; if (strcmp(T, Gjz[1]) == 0) { cout << "(" << 2 << "," << "if" << ")"; now = i; } else goto loop; break; case 't': for (j = 0, i = now; i <= now + 3; i++, j++) T[j] = M[i]; T[j] = '\0'; if (strcmp(T, Gjz[2]) == 0) { cout << "(" << 3 << "," << "then" << ")"; now = i; } else goto loop; break; case 'w': for (j = 0, i = now; i <= now + 4; i++, j++) T[j] = M[i]; T[j] = '\0'; if (strcmp(T, Gjz[3]) == 0) { cout << "(" << 4 << "," << "while" << ")"; now = i; } else goto loop; break; case 'd': for (j = 0, i = now; i <= now + 1; i++, j++) T[j] = M[i]; T[j] = '\0'; if (strcmp(T, Gjz[4]) == 0) { cout << "(" << 5 << "," << "do" << ")"; now = i; } else goto loop; break; case 'e': for (j = 0, i = now; i <= now + 2; i++, j++) T[j] = M[i]; T[j] = '\0'; if (strcmp(T, Gjz[5]) == 0) { cout << "(" << 6 << "," << "end" << ")"; cout << "(" << 0 << "," << '#' << ")"; exit(0); } else goto loop; break; case '+': cout << "(" << 13 << "," << '+' << ")"; break; case '-': cout << "(" << 14 << "," << '-' << ")"; break; case '*': cout << "(" << 15 << "," << '*' << ")"; break; case '/': cout << "(" << 16 << "," << '/' << ")"; break; case '=': cout << "(" << 25 << "," << '=' << ")"; break; case ';': cout << "(" << 26 << "," << ';' << ")"; break; case '(': cout << "(" << 27 << "," << '(' << ")"; break; case ')': cout << "(" << 28 << "," << ')' << ")"; break; case '<': if (M[now + 1] == '>') { cout << "(" << 21 << "," << "<>" << ")"; now++; } else if (M[now + 1] == '=') { cout << "(" << 22 << "," << "<=" << ")"; now++; } else cout << "(" << 20 << "," << "<" << ")"; break; case '>': if (M[now + 1] == '=') { cout << "(" << 24 << "," << ">=" << ")"; now++; } else cout << "(" << 23 << "," << ">" << ")"; break; case ':': if (M[now + 1] == '=') { cout << "(" << 18 << "," << ":=" << ")"; now++; } else cout << "(" << 17 << "," << ":" << ")"; break; } loop: if (((M[now] >= 'a' && M[now] <= 'z') || (M[now] >= 'A' && M[now] <= 'Z'))) { i = 0; while ((M[now] >= 'a' && M[now] <= 'z') || (M[now] >= 'A' && M[now] <= 'Z') || (M[now] >= '0' && M[now] <= '9')) { T[i] = M[now]; i++; now++; } T[i] = '\0'; now--; cout << "(" << 10 << "," << T << ")"; } else if (M[now] >= '0' && M[now] <= '9') { i = 0; while (M[now] >= '0' && M[now] <= '9') //和上一步判斷id10同樣,讓while浪費一步,保證指針的正常移動 { T[i] = M[now]; i++; now++; } T[i] = '\0'; now--; cout << "(" << 11 << "," << T << ")"; } now++; } } int main() { int i = 0; cin >> M[i]; while (M[i] != '#') { i++; cin >> M[i]; } M[++i] = '\0'; cout << M << endl; Set(); return 0; }