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
| #include<iostream> #include <cstring>
using namespace std;
char cBuffer; const char *key[8] = {"if", "else", "for", "while", "int", "return", "break", "continue"}; const char *border[6] = {",", ";", "{", "}", "(", ")"}; const char *arithmetic[5] = {"+", "-", "*", "/", "="}; const char *relations[6] = {"<", "<=", "==", ">", ">=", "!="}; char *consts[20]; char *label[20]; int constNum = 0, labelNum = 0;
int search(char searchChar[], int wordType) { int i ; switch (wordType) { case 1: for (i = 0; i <= 7; i++) if (strcmp(key[i], searchChar) == 0) return (i + 1); return 0; case 2: for (i = 0; i <= 5; i++) if (strcmp(border[i], searchChar) == 0) return (i + 1); return (0); case 3: for (i = 0; i <= 4; i++) if (strcmp(arithmetic[i], searchChar) == 0) return (i + 1); return (0); case 4: for (i = 0; i <= 5; i++) if (strcmp(relations[i], searchChar) == 0) return (i + 1); return (0); case 5: for (i = 0; i < constNum; i++) if (strcmp(consts[i], searchChar) == 0) return (i + 1); consts[i] = (char *) malloc(sizeof(searchChar)); strcpy(consts[i], searchChar); constNum++; return (i + 1); case 6: for (i = 0; i < labelNum; i++) if (strcmp(label[i], searchChar) == 0) return (i + 1); label[i] = (char *) malloc(sizeof(searchChar)); strcpy(label[i], searchChar); labelNum++; return (i + 1); default: return (0); } }
char alphaProcess(char buffer)//字母开头串的处理(可能是保留字或标识符) { int aType; int i = -1; char alphaTP[20]; while ((isalpha(buffer)) (isdigit(buffer))) { alphaTP[++i] = buffer; buffer = getchar(); } alphaTP[i + 1] = '\0'; if ((aType = search(alphaTP, 1))) printf("(%s,1,%d)\n", alphaTP, aType); else { aType = search(alphaTP, 6); printf("(%s,6,%d)\n", alphaTP, aType); } return (buffer); }
char digitProcess(char buffer) //常量串 { int i = -1; char digittp[20]; int dType; while ((isdigit(buffer))) { digittp[++i] = buffer; buffer = getchar(); } digittp[i + 1] = '\0'; dType = search(digittp, 5); printf("(%s,5,%d)\n", digittp, dType); return (buffer); }
char otherProcess(char buffer) //其它符号的处理 {
char othertp[20]; int otype, otypetp; othertp[0] = buffer; othertp[1] = '\0'; if ((otype = search(othertp, 3))) { printf("(%s,3,%d)\n", othertp, otype); buffer = getchar(); goto out; } if ((otype = search(othertp, 4))) { buffer = getchar(); othertp[1] = buffer; othertp[2] = '\0'; if ((otypetp = search(othertp, 4))) { printf("(%s,4,%d)\n", othertp, otypetp); goto out; } else { othertp[1] = '\0'; printf("(%s,4,%d)\n", othertp, otype); goto out; } } if (buffer == ':') { buffer = getchar(); if (buffer == '=') printf(":= (2,2)\n"); buffer = getchar(); goto out; } else if ((otype = search(othertp, 2))) { printf("(%s,2,%d)\n", othertp, otype); buffer = getchar(); goto out; }
if ((buffer != '\n') && (buffer != ' ')) printf("%c error,not a word\n", buffer); buffer = getchar(); out: return (buffer); }
int main() { int i; for (i = 0; i <= 20; i++) { label[i] = nullptr; consts[i] = nullptr; } cBuffer = getchar(); while (cBuffer != EOF) { if (isalpha(cBuffer)) cBuffer = alphaProcess(cBuffer); else if (isdigit(cBuffer)) cBuffer = digitProcess(cBuffer); else cBuffer = otherProcess(cBuffer); } printf("Anysis End\n"); getchar(); return 0; }
|