├── 文法解读作业 ├── testin2.txt ├── test.zip ├── testout3.txt ├── testout5.txt ├── testout2.txt ├── testout1.txt ├── testout4.txt ├── testfile4.txt ├── testfile1.txt ├── testfile5.txt ├── testfile2.txt └── testfile3.txt ├── readme.md ├── 词法分析.cpp ├── 语法分析.cpp ├── 代码生成1.cpp └── 错误处理.cpp /文法解读作业/testin2.txt: -------------------------------------------------------------------------------- 1 | 10 -------------------------------------------------------------------------------- /文法解读作业/test.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjc20000323/buaa-compile/HEAD/文法解读作业/test.zip -------------------------------------------------------------------------------- /文法解读作业/testout3.txt: -------------------------------------------------------------------------------- 1 | %d90 2 | %d90 3 | %d90 4 | %d9 5 | 18373750 6 | %d100 7 | %d100 8 | %d800 9 | %d200 -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # 2020-BUAA-编译原理实验 2 | 3 | 我所完成的是一个单文件单遍编译器(非常垃圾是吧。。。) 4 | 5 | 每一个文件都代表了一次作业。 6 | 7 | 仅此算是个纪念。 -------------------------------------------------------------------------------- /文法解读作业/testout5.txt: -------------------------------------------------------------------------------- 1 | 18373750 2 | %d6 3 | You and I 4 | %d8 5 | %d8 6 | %d1 7 | %d2 8 | %d10 9 | b 10 | a -------------------------------------------------------------------------------- /文法解读作业/testout2.txt: -------------------------------------------------------------------------------- 1 | 18373750 2 | %d20 3 | %d66 4 | %d6 5 | %d46 6 | %d0 7 | %d10 8 | %ca 9 | %cb 10 | %d47 -------------------------------------------------------------------------------- /文法解读作业/testout1.txt: -------------------------------------------------------------------------------- 1 | 18373750 2 | 1a+-*/!@ 3 | %d1 4 | %d4 5 | %cC 6 | %cc 7 | This is my %d test.1 8 | 3 9 | %d1 10 | %d3 11 | -------------------------------------------------------------------------------- /文法解读作业/testout4.txt: -------------------------------------------------------------------------------- 1 | 18373750 2 | cccccc 3 | abcdefghijklmnopqrstuvwxyz 4 | ABCDEFGHIJKLMNOPQRSTUVWXYZ 5 | 123456789 6 | 987654321 7 | aaaaaa 8 | bbbbbb 9 | yyyyyy 10 | xxxxxx -------------------------------------------------------------------------------- /文法解读作业/testfile4.txt: -------------------------------------------------------------------------------- 1 | void main() 2 | { 3 | printf("18373750"); 4 | printf("cccccc"); 5 | printf("abcdefghijklmnopqrstuvwxyz"); 6 | printf("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 7 | printf("123456789"); 8 | printf("987654321"); 9 | printf("aaaaaa"); 10 | printf("bbbbbb"); 11 | printf("yyyyyy"); 12 | printf("xxxxxx"); 13 | } -------------------------------------------------------------------------------- /文法解读作业/testfile1.txt: -------------------------------------------------------------------------------- 1 | const int NumStudent = 100; 2 | const char cc = 'C'; 3 | int a = 1; 4 | int b = 3; 5 | char c = 'c'; 6 | int aa[10]; 7 | void main() 8 | { 9 | printf("18373750"); 10 | printf("1a+-*/!@"); 11 | printf("%d", a); 12 | printf("%d", a + b); 13 | printf("%c", cc); 14 | printf("%c", c); 15 | printf("This is my %d test.", a); 16 | printf(b); 17 | printf("%d", a * b - b + a); 18 | printf("%d", a * b + a / b); 19 | } -------------------------------------------------------------------------------- /文法解读作业/testfile5.txt: -------------------------------------------------------------------------------- 1 | const int sum = 100, num = 200; 2 | int aum = 300; 3 | int bum = 400; 4 | char c1 = '+'; 5 | char c2 = '*'; 6 | int a[3] = { 1, 2, 3 }; 7 | int b[2][3] = { {1, 2, 3}, {4, 5, 6} }; 8 | char map[10][10]; 9 | char str[10]; 10 | void get_sum() {} 11 | 12 | char get_char() 13 | { 14 | char c = 'a'; 15 | return (c); 16 | } 17 | void main() 18 | { 19 | const char aaa = '-'; 20 | const char bbb = '-'; 21 | const int ccc = 9; 22 | int a1; 23 | int b1; 24 | char c; 25 | a1 = +8; 26 | b1 = -8; 27 | a1 = -(b1); 28 | b1 = +(a1); 29 | if (a1 > b1) 30 | a1 = b1; 31 | if (a1 > b1) 32 | { 33 | 34 | } 35 | printf("18373750"); 36 | printf("%d", a[0] + b[1][1]); 37 | printf("You and I"); 38 | printf("%d", a1); 39 | printf("%d", b1); 40 | printf("%d", b[0][0]); 41 | printf("%d", a[1]); 42 | printf("%d", a[1] * b[1][1]); 43 | printf("b"); 44 | printf("a"); 45 | } -------------------------------------------------------------------------------- /文法解读作业/testfile2.txt: -------------------------------------------------------------------------------- 1 | int array[100]; 2 | int map[10][10]; 3 | void test_for_add_sub() 4 | { 5 | int i, j; 6 | int a = +8; 7 | int b = -9; 8 | for (i = 0; i < 10; i = i + 1) 9 | { 10 | for (j = 9; j >= 0; j = j - 1) 11 | { 12 | map[i][j] = a; 13 | } 14 | } 15 | } 16 | void test_while() 17 | { 18 | int i = 0; 19 | while (i <= 99) 20 | { 21 | array[i] = i; 22 | i = i + 1; 23 | } 24 | } 25 | int test_scanf(int b) 26 | { 27 | return (b+b); 28 | } 29 | void main() 30 | { 31 | const int aaa = 100; 32 | int b; 33 | int a; 34 | char c[100]; 35 | test_for_add_sub(); 36 | test_while(); 37 | scanf(b); 38 | a = test_scanf(b); 39 | c[0] = 'a'; 40 | c[1] = 'b'; 41 | c[2] = 'c'; 42 | printf("18373750"); 43 | printf("%d", a); 44 | printf("%d", (a + 2) * 3); 45 | printf("%d", (a - 2) / 3); 46 | printf("%d", 2 * (a + 3)); 47 | printf("%d", 2 / (a - 3)); 48 | printf("%d", b); 49 | printf("%c", c[0]); 50 | printf("%c", c[1]); 51 | printf("%d", array[47]); 52 | } -------------------------------------------------------------------------------- /文法解读作业/testfile3.txt: -------------------------------------------------------------------------------- 1 | const int sum = 100; 2 | int get_sum() 3 | { 4 | return (100); 5 | } 6 | void test_if(int a, int b, char c) 7 | { 8 | if (a >= 60) 9 | printf("%d", a); 10 | if (a >= 10) 11 | printf("%d", a); 12 | else 13 | printf("%d", b); 14 | if (b != 90) 15 | { 16 | b = 10; 17 | a = 90; 18 | } 19 | else 20 | { 21 | b = 10; 22 | a = 90; 23 | } 24 | if (b == 10) 25 | b = 9; 26 | printf("%d", a); 27 | printf("%d", b); 28 | return; 29 | } 30 | void test_switch() 31 | { 32 | int b = 200; 33 | int result; 34 | switch (b * 2) 35 | { 36 | case 300: { 37 | result = get_sum() + 300; 38 | } 39 | case 400: { 40 | result = 400 + get_sum(); 41 | } 42 | case 500: { 43 | result = 500 + get_sum() / 2 * 2; 44 | } 45 | default: { 46 | result = 200; 47 | } 48 | } 49 | switch (b) 50 | { 51 | case 200: { 52 | result = (600 - 200) * get_sum() / (101 - 1 + 1 - 1) + (200 + 4); 53 | } 54 | default: { 55 | ; 56 | } 57 | } 58 | } 59 | void main() 60 | { 61 | const int num = 100; 62 | const char c = '5'; 63 | const char cc = '6'; 64 | const int a = 400; 65 | test_if(90, 10, '5'); 66 | printf("18373750"); 67 | printf("%d", num); 68 | printf("%d", get_sum()); 69 | printf("%d", a * 2); 70 | printf("%d", a / 2); 71 | } -------------------------------------------------------------------------------- /词法分析.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | FILE* in = freopen("testfile.txt", "r", stdin); 7 | FILE* out = freopen("output.txt", "w", stdout); 8 | enum Code //类别码 9 | { 10 | IDENFR, 11 | INTCON, 12 | CHARCON, 13 | STRCON, 14 | CONSTTK, 15 | INTTK, 16 | CHARTK, 17 | VOIDTK, 18 | MAINTK, 19 | IFTK, 20 | ELSETK, 21 | SWITCHTK, 22 | CASETK, 23 | DEFAULTTK, 24 | WHILETK, 25 | FORTK, 26 | SCANFTK, 27 | PRINTFTK, 28 | RETURNTK, 29 | PLUS, 30 | MINU, 31 | MULT, 32 | DIV, 33 | LSS, 34 | LEQ, 35 | GRE, 36 | GEQ, 37 | EQL, 38 | NEQ, 39 | COLON, 40 | ASSIGN, 41 | SEMICN, 42 | COMMA, 43 | LPARENT, 44 | RPARENT, 45 | LBRACK, 46 | RBRACK, 47 | LBRACE, 48 | RBRACE 49 | }; 50 | void Code_map(Code symbol) //映射函数 51 | { 52 | switch (symbol) { 53 | case 0: printf("IDENFR"); break; 54 | case 1: printf("INTCON"); break; 55 | case 2: printf("CHARCON"); break; 56 | case 3: printf("STRCON"); break; 57 | case 4: printf("CONSTTK"); break; 58 | case 5: printf("INTTK"); break; 59 | case 6: printf("CHARTK"); break; 60 | case 7: printf("VOIDTK"); break; 61 | case 8: printf("MAINTK"); break; 62 | case 9: printf("IFTK"); break; 63 | case 10: printf("ELSETK"); break; 64 | case 11: printf("SWITCHTK"); break; 65 | case 12: printf("CASETK"); break; 66 | case 13: printf("DEFAULTTK"); break; 67 | case 14: printf("WHILETK"); break; 68 | case 15: printf("FORTK"); break; 69 | case 16: printf("SCANFTK"); break; 70 | case 17: printf("PRINTFTK"); break; 71 | case 18: printf("RETURNTK"); break; 72 | case 19: printf("PLUS"); break; 73 | case 20: printf("MINU"); break; 74 | case 21: printf("MULT"); break; 75 | case 22: printf("DIV"); break; 76 | case 23: printf("LSS"); break; 77 | case 24: printf("LEQ"); break; 78 | case 25: printf("GRE"); break; 79 | case 26: printf("GEQ"); break; 80 | case 27: printf("EQL"); break; 81 | case 28: printf("NEQ"); break; 82 | case 29: printf("COLON"); break; 83 | case 30: printf("ASSIGN"); break; 84 | case 31: printf("SEMICN"); break; 85 | case 32: printf("COMMA"); break; 86 | case 33: printf("LPARENT"); break; 87 | case 34: printf("RPARENT"); break; 88 | case 35: printf("LBRACK"); break; 89 | case 36: printf("RBRACK"); break; 90 | case 37: printf("LBRACE"); break; 91 | case 38: printf("RBRACE"); break; 92 | } 93 | } 94 | void deal_token(char c) 95 | { 96 | if (isalpha(c) || c == '_') 97 | { 98 | char s[1000]; 99 | char s_low[1000]; 100 | int i = 0; 101 | s[i] = c; 102 | if (c >= 'A' && c <= 'Z') { 103 | c = c - 'A' + 'a'; 104 | } 105 | s_low[i++] = c; 106 | while ((c = getchar())) { 107 | if (!isalnum(c) && c != '_') { 108 | fseek(in, -1, SEEK_CUR); 109 | break; 110 | } 111 | s[i] = c; 112 | if (c >= 'A' && c <= 'Z') { 113 | c = c - 'A' + 'a'; 114 | } 115 | s_low[i++] = c; 116 | } 117 | s[i] = '\0'; 118 | s_low[i] = '\0'; 119 | if (strcmp(s_low, "const") == 0) { 120 | Code symbol = CONSTTK; 121 | Code_map(symbol); 122 | printf(" %s\n", s); 123 | } 124 | else if (strcmp(s_low, "int") == 0) { 125 | Code symbol = INTTK; 126 | Code_map(symbol); 127 | printf(" %s\n", s); 128 | } 129 | else if (strcmp(s_low, "char") == 0) { 130 | Code symbol = CHARTK; 131 | Code_map(symbol); 132 | printf(" %s\n", s); 133 | } 134 | else if (strcmp(s_low, "void") == 0) { 135 | Code symbol = VOIDTK; 136 | Code_map(symbol); 137 | printf(" %s\n", s); 138 | } 139 | else if (strcmp(s_low, "main") == 0) { 140 | Code symbol = MAINTK; 141 | Code_map(symbol); 142 | printf(" %s\n", s); 143 | } 144 | else if (strcmp(s_low, "if") == 0) { 145 | Code symbol = IFTK; 146 | Code_map(symbol); 147 | printf(" %s\n", s); 148 | } 149 | else if (strcmp(s_low, "else") == 0) { 150 | Code symbol = ELSETK; 151 | Code_map(symbol); 152 | printf(" %s\n", s); 153 | } 154 | else if (strcmp(s_low, "switch") == 0) { 155 | Code symbol = SWITCHTK; 156 | Code_map(symbol); 157 | printf(" %s\n", s); 158 | } 159 | else if (strcmp(s_low, "case") == 0) { 160 | Code symbol = CASETK; 161 | Code_map(symbol); 162 | printf(" %s\n", s); 163 | } 164 | else if (strcmp(s_low, "default") == 0) { 165 | Code symbol = DEFAULTTK; 166 | Code_map(symbol); 167 | printf(" %s\n", s); 168 | } 169 | else if (strcmp(s_low, "while") == 0) { 170 | Code symbol = WHILETK; 171 | Code_map(symbol); 172 | printf(" %s\n", s); 173 | } 174 | else if (strcmp(s_low, "for") == 0) { 175 | Code symbol = FORTK; 176 | Code_map(symbol); 177 | printf(" %s\n", s); 178 | } 179 | else if (strcmp(s_low, "scanf") == 0) { 180 | Code symbol = SCANFTK; 181 | Code_map(symbol); 182 | printf(" %s\n", s); 183 | } 184 | else if (strcmp(s_low, "printf") == 0) { 185 | Code symbol = PRINTFTK; 186 | Code_map(symbol); 187 | printf(" %s\n", s); 188 | } 189 | else if (strcmp(s_low, "return") == 0) { 190 | Code symbol = RETURNTK; 191 | Code_map(symbol); 192 | printf(" %s\n", s); 193 | } 194 | else { 195 | Code symbol = IDENFR; 196 | Code_map(symbol); 197 | printf(" %s\n", s); 198 | } 199 | } 200 | else if (isdigit(c)) { 201 | char s[1000]; 202 | int i = 0; 203 | s[i++] = c; 204 | while ((c = getchar())) { 205 | if (!isdigit(c)) { 206 | fseek(in, -1, SEEK_CUR); 207 | break; 208 | } 209 | s[i++] = c; 210 | } 211 | s[i] = '\0'; 212 | Code symbol = INTCON; 213 | Code_map(symbol); 214 | printf(" %s\n", s); 215 | } 216 | else if (c == '\'') 217 | { 218 | char s[1000]; 219 | int i = 0; 220 | c = getchar(); 221 | s[i++] = c; 222 | s[i] = '\0'; 223 | c = getchar(); 224 | Code symbol = CHARCON; 225 | Code_map(symbol); 226 | printf(" %s\n", s); 227 | } 228 | else if (c == '"') 229 | { 230 | char s[1000]; 231 | int i = 0; 232 | while ((c = getchar())) { 233 | if (c == '"') { 234 | break; 235 | } 236 | s[i++] = c; 237 | } 238 | s[i] = '\0'; 239 | Code symbol = STRCON; 240 | Code_map(symbol); 241 | printf(" %s\n", s); 242 | } 243 | else if (c == '+') { 244 | Code symbol = PLUS; 245 | Code_map(symbol); 246 | printf(" +\n"); 247 | } 248 | else if (c == '-') { 249 | Code symbol = MINU; 250 | Code_map(symbol); 251 | printf(" -\n"); 252 | } 253 | else if (c == '*') { 254 | Code symbol = MULT; 255 | Code_map(symbol); 256 | printf(" *\n"); 257 | } 258 | else if (c == '/') { 259 | Code symbol = DIV; 260 | Code_map(symbol); 261 | printf(" /\n"); 262 | } 263 | else if (c == '<') { 264 | c = getchar(); 265 | if (c == '=') { 266 | Code symbol = LEQ; 267 | Code_map(symbol); 268 | printf(" <=\n"); 269 | } 270 | else { 271 | fseek(in, -1, SEEK_CUR); 272 | Code symbol = LSS; 273 | Code_map(symbol); 274 | printf(" <\n"); 275 | } 276 | } 277 | else if (c == '>') { 278 | c = getchar(); 279 | if (c == '=') { 280 | Code symbol = GEQ; 281 | Code_map(symbol); 282 | printf(" >=\n"); 283 | } 284 | else { 285 | fseek(in, -1, SEEK_CUR); 286 | Code symbol = GRE; 287 | Code_map(symbol); 288 | printf(" >\n"); 289 | } 290 | } 291 | else if (c == '=') { 292 | c = getchar(); 293 | if (c == '=') { 294 | Code symbol = EQL; 295 | Code_map(symbol); 296 | printf(" ==\n"); 297 | } 298 | else { 299 | fseek(in, -1, SEEK_CUR); 300 | Code symbol = ASSIGN; 301 | Code_map(symbol); 302 | printf(" =\n"); 303 | } 304 | } 305 | else if (c == '!') { 306 | c = getchar(); 307 | if (c == '=') { 308 | Code symbol = NEQ; 309 | Code_map(symbol); 310 | printf(" !=\n"); 311 | } 312 | } 313 | else if (c == ':') { 314 | Code symbol = COLON; 315 | Code_map(symbol); 316 | printf(" :\n"); 317 | } 318 | else if (c == ';') { 319 | Code symbol = SEMICN; 320 | Code_map(symbol); 321 | printf(" ;\n"); 322 | } 323 | else if (c == ',') { 324 | Code symbol = COMMA; 325 | Code_map(symbol); 326 | printf(" ,\n"); 327 | } 328 | else if (c == '(') { 329 | Code symbol = LPARENT; 330 | Code_map(symbol); 331 | printf(" (\n"); 332 | } 333 | else if (c == ')') { 334 | Code symbol = RPARENT; 335 | Code_map(symbol); 336 | printf(" )\n"); 337 | } 338 | else if (c == '[') { 339 | Code symbol = LBRACK; 340 | Code_map(symbol); 341 | printf(" [\n"); 342 | } 343 | else if (c == ']') { 344 | Code symbol = RBRACK; 345 | Code_map(symbol); 346 | printf(" ]\n"); 347 | } 348 | else if (c == '{') { 349 | Code symbol = LBRACE; 350 | Code_map(symbol); 351 | printf(" {\n"); 352 | } 353 | else if (c == '}') { 354 | Code symbol = RBRACE; 355 | Code_map(symbol); 356 | printf(" }\n"); 357 | } 358 | } 359 | int main() 360 | { 361 | char c = getchar(); 362 | while (1) 363 | { 364 | while (isspace(c)) 365 | c = getchar(); 366 | if (c == EOF) 367 | break; 368 | deal_token(c); 369 | c = getchar(); 370 | } 371 | } -------------------------------------------------------------------------------- /语法分析.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void print_code(); 5 | void print_code_pre1(); 6 | void print_code_pre2(); 7 | //void Code_map(Code symbol); 8 | void deal_token(char c); 9 | void getsym(); 10 | int find_return_func(); 11 | int find_no_return_func(); 12 | int judge_string(); 13 | int judge_program(); 14 | int judge_const_inform(); 15 | int judge_const_define(); 16 | int judge_unsigned_integer(); 17 | int judge_integer(); 18 | int judge_head(); 19 | int judge_const(); 20 | int judge_var_inform(); 21 | int judge_var_define(); 22 | int judge_function_has_return(); 23 | int judge_function_no_return(); 24 | int judge_mix_statement(); 25 | int judge_parameter_list(); 26 | int judge_main_function(); 27 | int judge_expression(); 28 | int judge_item(); 29 | int judge_factor(); 30 | int judge_statement(); 31 | int judge_value_statement(); 32 | int judge_condition_statement(); 33 | int judge_condition(); 34 | int judge_circulation_statement(); 35 | int judge_stepsize(); 36 | int judge_case_statement(); 37 | int judge_case_list(); 38 | int judge_case_son_statement(); 39 | int judge_default(); 40 | int judge_returnfunc_use(); 41 | int judge_noreturnfunc_use(); 42 | int judge_value_parameter_list(); 43 | int judge_statement_list(); 44 | int judge_scanf(); 45 | int judge_printf(); 46 | int judge_return_statement(); 47 | 48 | FILE* in = freopen("testfile.txt", "r", stdin); 49 | FILE* out = freopen("output.txt", "w", stdout); 50 | char c; 51 | char s[1000]; 52 | char s_low[1000]; 53 | 54 | enum Code 55 | { 56 | IDENFR, 57 | INTCON, 58 | CHARCON, 59 | STRCON, 60 | CONSTTK, 61 | INTTK, 62 | CHARTK, 63 | VOIDTK, 64 | MAINTK, 65 | IFTK, 66 | ELSETK, 67 | SWITCHTK, 68 | CASETK, 69 | DEFAULTTK, 70 | WHILETK, 71 | FORTK, 72 | SCANFTK, 73 | PRINTFTK, 74 | RETURNTK, 75 | PLUS, 76 | MINU, 77 | MULT, 78 | DIV, 79 | LSS, 80 | LEQ, 81 | GRE, 82 | GEQ, 83 | EQL, 84 | NEQ, 85 | COLON, 86 | ASSIGN, 87 | SEMICN, 88 | COMMA, 89 | LPARENT, 90 | RPARENT, 91 | LBRACK, 92 | RBRACK, 93 | LBRACE, 94 | RBRACE 95 | }; 96 | 97 | Code symbol, pre1, pre2; 98 | char content1[1000], content2[1000]; 99 | int flag1 = 0, flag2 = 0; 100 | char no_return_func[1000][1000]; 101 | char has_return_func[1000][1000]; 102 | int no_return_func_sum = 0, has_return_func_sum = 0; 103 | void Code_map(Code symbol) 104 | { 105 | switch (symbol) { 106 | case 0: printf("IDENFR"); break; 107 | case 1: printf("INTCON"); break; 108 | case 2: printf("CHARCON"); break; 109 | case 3: printf("STRCON"); break; 110 | case 4: printf("CONSTTK"); break; 111 | case 5: printf("INTTK"); break; 112 | case 6: printf("CHARTK"); break; 113 | case 7: printf("VOIDTK"); break; 114 | case 8: printf("MAINTK"); break; 115 | case 9: printf("IFTK"); break; 116 | case 10: printf("ELSETK"); break; 117 | case 11: printf("SWITCHTK"); break; 118 | case 12: printf("CASETK"); break; 119 | case 13: printf("DEFAULTTK"); break; 120 | case 14: printf("WHILETK"); break; 121 | case 15: printf("FORTK"); break; 122 | case 16: printf("SCANFTK"); break; 123 | case 17: printf("PRINTFTK"); break; 124 | case 18: printf("RETURNTK"); break; 125 | case 19: printf("PLUS"); break; 126 | case 20: printf("MINU"); break; 127 | case 21: printf("MULT"); break; 128 | case 22: printf("DIV"); break; 129 | case 23: printf("LSS"); break; 130 | case 24: printf("LEQ"); break; 131 | case 25: printf("GRE"); break; 132 | case 26: printf("GEQ"); break; 133 | case 27: printf("EQL"); break; 134 | case 28: printf("NEQ"); break; 135 | case 29: printf("COLON"); break; 136 | case 30: printf("ASSIGN"); break; 137 | case 31: printf("SEMICN"); break; 138 | case 32: printf("COMMA"); break; 139 | case 33: printf("LPARENT"); break; 140 | case 34: printf("RPARENT"); break; 141 | case 35: printf("LBRACK"); break; 142 | case 36: printf("RBRACK"); break; 143 | case 37: printf("LBRACE"); break; 144 | case 38: printf("RBRACE"); break; 145 | } 146 | } 147 | 148 | void print_code() 149 | { 150 | Code_map(symbol); 151 | printf(" %s\n", s); 152 | } 153 | 154 | void print_code_pre1() 155 | { 156 | Code_map(pre1); 157 | printf(" %s\n", content1); 158 | } 159 | 160 | void print_code_pre2() 161 | { 162 | Code_map(pre2); 163 | printf(" %s\n", content2); 164 | } 165 | 166 | void deal_token(char c) 167 | { 168 | if (isalpha(c) || c == '_') 169 | { 170 | int i = 0; 171 | s[i] = c; 172 | if (c >= 'A' && c <= 'Z') { 173 | c = c - 'A' + 'a'; 174 | } 175 | s_low[i++] = c; 176 | while ((c = getchar())) { 177 | if (!isalnum(c) && c != '_') { 178 | fseek(in, -1, SEEK_CUR); 179 | break; 180 | } 181 | s[i] = c; 182 | if (c >= 'A' && c <= 'Z') { 183 | c = c - 'A' + 'a'; 184 | } 185 | s_low[i++] = c; 186 | } 187 | s[i] = '\0'; 188 | s_low[i] = '\0'; 189 | if (strcmp(s_low, "const") == 0) { 190 | symbol = CONSTTK; 191 | } 192 | else if (strcmp(s_low, "int") == 0) { 193 | symbol = INTTK; 194 | } 195 | else if (strcmp(s_low, "char") == 0) { 196 | symbol = CHARTK; 197 | } 198 | else if (strcmp(s_low, "void") == 0) { 199 | symbol = VOIDTK; 200 | } 201 | else if (strcmp(s_low, "main") == 0) { 202 | symbol = MAINTK; 203 | } 204 | else if (strcmp(s_low, "if") == 0) { 205 | symbol = IFTK; 206 | } 207 | else if (strcmp(s_low, "else") == 0) { 208 | symbol = ELSETK; 209 | } 210 | else if (strcmp(s_low, "switch") == 0) { 211 | symbol = SWITCHTK; 212 | } 213 | else if (strcmp(s_low, "case") == 0) { 214 | symbol = CASETK; 215 | } 216 | else if (strcmp(s_low, "default") == 0) { 217 | symbol = DEFAULTTK; 218 | } 219 | else if (strcmp(s_low, "while") == 0) { 220 | symbol = WHILETK; 221 | } 222 | else if (strcmp(s_low, "for") == 0) { 223 | symbol = FORTK; 224 | } 225 | else if (strcmp(s_low, "scanf") == 0) { 226 | symbol = SCANFTK; 227 | } 228 | else if (strcmp(s_low, "printf") == 0) { 229 | symbol = PRINTFTK; 230 | } 231 | else if (strcmp(s_low, "return") == 0) { 232 | symbol = RETURNTK; 233 | } 234 | else { 235 | symbol = IDENFR; 236 | } 237 | } 238 | else if (isdigit(c)) { 239 | int i = 0; 240 | s[i++] = c; 241 | while ((c = getchar())) { 242 | if (!isdigit(c)) { 243 | fseek(in, -1, SEEK_CUR); 244 | break; 245 | } 246 | s[i++] = c; 247 | } 248 | s[i] = '\0'; 249 | symbol = INTCON; 250 | } 251 | else if (c == '\'') 252 | { 253 | int i = 0; 254 | c = getchar(); 255 | s[i++] = c; 256 | s[i] = '\0'; 257 | c = getchar(); 258 | symbol = CHARCON; 259 | } 260 | else if (c == '"') 261 | { 262 | int i = 0; 263 | while ((c = getchar())) { 264 | if (c == '"') { 265 | break; 266 | } 267 | s[i++] = c; 268 | } 269 | s[i] = '\0'; 270 | symbol = STRCON; 271 | } 272 | else if (c == '+') { 273 | symbol = PLUS; 274 | strcpy(s, "+"); 275 | } 276 | else if (c == '-') { 277 | symbol = MINU; 278 | strcpy(s, "-"); 279 | } 280 | else if (c == '*') { 281 | symbol = MULT; 282 | strcpy(s, "*"); 283 | } 284 | else if (c == '/') { 285 | symbol = DIV; 286 | strcpy(s, "/"); 287 | } 288 | else if (c == '<') { 289 | c = getchar(); 290 | if (c == '=') { 291 | symbol = LEQ; 292 | strcpy(s, "<="); 293 | } 294 | else { 295 | fseek(in, -1, SEEK_CUR); 296 | symbol = LSS; 297 | strcpy(s, "<"); 298 | } 299 | } 300 | else if (c == '>') { 301 | c = getchar(); 302 | if (c == '=') { 303 | symbol = GEQ; 304 | strcpy(s, ">="); 305 | } 306 | else { 307 | fseek(in, -1, SEEK_CUR); 308 | symbol = GRE; 309 | strcpy(s, ">"); 310 | } 311 | } 312 | else if (c == '=') { 313 | c = getchar(); 314 | if (c == '=') { 315 | symbol = EQL; 316 | strcpy(s, "=="); 317 | } 318 | else { 319 | fseek(in, -1, SEEK_CUR); 320 | symbol = ASSIGN; 321 | strcpy(s, "="); 322 | } 323 | } 324 | else if (c == '!') { 325 | c = getchar(); 326 | if (c == '=') { 327 | symbol = NEQ; 328 | strcpy(s, "!="); 329 | } 330 | } 331 | else if (c == ':') { 332 | symbol = COLON; 333 | strcpy(s, ":"); 334 | } 335 | else if (c == ';') { 336 | symbol = SEMICN; 337 | strcpy(s, ";"); 338 | } 339 | else if (c == ',') { 340 | symbol = COMMA; 341 | strcpy(s, ","); 342 | } 343 | else if (c == '(') { 344 | symbol = LPARENT; 345 | strcpy(s, "("); 346 | } 347 | else if (c == ')') { 348 | symbol = RPARENT; 349 | strcpy(s, ")"); 350 | } 351 | else if (c == '[') { 352 | symbol = LBRACK; 353 | strcpy(s, "["); 354 | } 355 | else if (c == ']') { 356 | symbol = RBRACK; 357 | strcpy(s, "]"); 358 | } 359 | else if (c == '{') { 360 | symbol = LBRACE; 361 | strcpy(s, "{"); 362 | } 363 | else if (c == '}') { 364 | symbol = RBRACE; 365 | strcpy(s, "}"); 366 | } 367 | } 368 | 369 | void getsym() { 370 | c = getchar(); 371 | while (isspace(c)) { 372 | c = getchar(); 373 | } 374 | if (c == EOF) { 375 | return; 376 | } 377 | deal_token(c); 378 | } 379 | 380 | int find_return_func(char* name) 381 | { 382 | int i; 383 | for (i = 0; i < has_return_func_sum; i++) 384 | { 385 | if (strcmp(has_return_func[i], name) == 0) 386 | return 1; 387 | } 388 | return 0; 389 | } 390 | 391 | int find_no_return_func(char* name) 392 | { 393 | int i; 394 | for (i = 0; i < no_return_func_sum; i++) 395 | { 396 | if (strcmp(no_return_func[i], name) == 0) 397 | return 1; 398 | } 399 | return 0; 400 | } 401 | 402 | int judge_string() 403 | { 404 | if (symbol != STRCON) 405 | return -1; 406 | print_code(); 407 | getsym(); 408 | printf("<字符串>\n"); 409 | return 0; 410 | } 411 | 412 | int judge_program() 413 | { 414 | int r; 415 | r = judge_const_inform(); 416 | if (r < 0) 417 | return r; 418 | r = judge_var_inform(); 419 | if (r < 0) 420 | return r; 421 | while (1) 422 | { 423 | if (judge_function_has_return() < 0) 424 | { 425 | if (judge_function_no_return() < 0) 426 | { 427 | break; 428 | } 429 | } 430 | } 431 | r = judge_main_function(); 432 | if (r < 0) 433 | return r; 434 | printf("<程序>\n"); 435 | return 0; 436 | } 437 | 438 | int judge_const_inform() 439 | { 440 | int r; 441 | if (symbol != CONSTTK) { 442 | return 0; //这里不应该返回负数,否则就直接退了 443 | } 444 | while (symbol == CONSTTK) 445 | { 446 | print_code(); 447 | getsym(); 448 | r = judge_const_define(); 449 | if (r < 0) 450 | return r; 451 | print_code(); 452 | getsym(); 453 | } 454 | printf("<常量说明>\n"); 455 | return 0; 456 | } 457 | 458 | int judge_const_define() 459 | { 460 | int r; 461 | if (symbol == INTTK) 462 | { 463 | print_code(); 464 | getsym(); //读标识符 465 | print_code(); 466 | getsym(); //读等号 467 | print_code(); 468 | getsym(); //读整数 469 | r = judge_integer(); 470 | if (r < 0) 471 | return r; 472 | while (symbol == COMMA) //不出意外的话,逗号在前面读了 473 | { 474 | print_code(); 475 | getsym(); //读标识符 476 | print_code(); 477 | getsym(); //读等号 478 | print_code(); 479 | getsym(); //读整数 480 | r = judge_integer(); 481 | if (r < 0) 482 | return r; 483 | } 484 | } 485 | else if (symbol == CHARTK) //???可否有else 486 | { 487 | print_code(); 488 | getsym(); //读标识符 489 | print_code(); 490 | getsym(); //读等号 491 | print_code(); 492 | getsym(); //读字符 493 | print_code(); 494 | getsym(); //读逗号,因为并没有处理逗号的函数 495 | while (symbol == COMMA) //不出意外的话,逗号在前面读了 496 | { 497 | print_code(); 498 | getsym(); //读标识符 499 | print_code(); 500 | getsym(); //读等号 501 | print_code(); 502 | getsym(); //读字符 503 | print_code(); 504 | getsym(); //读逗号,因为并没有处理逗号的函数,若不是逗号,那么读sym并跳出 505 | } 506 | } 507 | printf("<常量定义>\n"); 508 | return 0; 509 | } 510 | 511 | int judge_unsigned_integer() 512 | { 513 | if (symbol != INTCON) { 514 | return -1; 515 | } 516 | print_code(); //输出数 517 | getsym(); //读下一个 518 | printf("<无符号整数>\n"); 519 | return 0; 520 | } 521 | 522 | int judge_integer() 523 | { 524 | int r; 525 | if (symbol == PLUS || symbol == MINU) //???是否需要判断是不是char常量 526 | { 527 | print_code(); //输出符号 528 | getsym(); //读数 529 | } 530 | r = judge_unsigned_integer(); 531 | if (r < 0) 532 | return r; 533 | printf("<整数>\n"); 534 | return 0; 535 | } 536 | 537 | int judge_head() 538 | { 539 | if ((flag1 == 1 && pre1 != INTTK && pre1 != CHARTK) 540 | || (flag1 == 0 && symbol != INTTK && symbol != CHARTK)) { 541 | return -1; 542 | } 543 | if (flag1 == 1) { 544 | flag1 = 0; 545 | print_code_pre1(); 546 | } 547 | else { 548 | print_code(); 549 | getsym(); 550 | } 551 | if (flag2 == 1) { 552 | flag2 = 0; 553 | strcpy(has_return_func[has_return_func_sum++], content2); 554 | print_code_pre2(); //输出标识符 555 | } 556 | else { 557 | strcpy(has_return_func[has_return_func_sum++], s); 558 | print_code(); //输出标识符 559 | getsym(); 560 | } 561 | printf("<声明头部>\n"); 562 | return 0; 563 | } 564 | 565 | int judge_const() 566 | { 567 | if (judge_integer() == 0) 568 | { 569 | printf("<常量>\n"); 570 | return 0; 571 | } 572 | else if (s[0] == '+' || s[0] == '-' || s[0] == '*' || s[0] == '/' || (s[0] >= '0' && s[0] <= '9') 573 | || (s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z') || s[0] == '_') 574 | { 575 | print_code(); //输出这个字符 576 | getsym(); 577 | printf("<常量>\n"); 578 | return 0; 579 | } 580 | else 581 | return -1; 582 | } 583 | 584 | int judge_var_inform() 585 | { 586 | int r; 587 | int sum = 0; 588 | while (1) 589 | { 590 | if (symbol != INTTK && symbol != CHARTK) 591 | { 592 | break; 593 | } 594 | pre1 = symbol; 595 | strcpy(content1, s); 596 | flag1 = 1; 597 | getsym(); 598 | pre2 = symbol; 599 | strcpy(content2, s); 600 | flag2 = 1; 601 | getsym(); //注意,下面接的有可能是无返回值函数定义,也有可能是有返回值函数定义,也有可能是个变量,也有可能是主函数 602 | if (symbol != LBRACK && symbol != COMMA && symbol != SEMICN && symbol != ASSIGN) 603 | { 604 | break; 605 | } 606 | r = judge_var_define(); 607 | if (r < 0) 608 | return r; 609 | sum++; 610 | print_code(); 611 | getsym(); 612 | } 613 | if (sum == 0) 614 | return 0; //如果为0,说明没有变量说明 615 | printf("<变量说明>\n"); 616 | return 0; 617 | } 618 | 619 | int judge_var_define() 620 | { 621 | int r; 622 | int flag = 0; 623 | print_code_pre1(); //输出类型标识符 624 | flag1 = 0; 625 | print_code_pre2(); //输出标识符 626 | flag2 = 0; 627 | if (symbol == LBRACK) 628 | { 629 | print_code(); //输出[ 630 | getsym(); 631 | r = judge_unsigned_integer(); 632 | if (r < 0) 633 | return r; 634 | print_code(); //输出] 635 | getsym(); 636 | if (symbol == LBRACK) 637 | { 638 | print_code(); //输出[ 639 | getsym(); 640 | r = judge_unsigned_integer(); 641 | if (r < 0) 642 | return r; 643 | print_code(); //输出] 644 | getsym(); 645 | if (symbol == ASSIGN) 646 | { 647 | flag = 1; 648 | print_code(); //输出= 649 | getsym(); 650 | print_code(); //输出{ 651 | getsym(); 652 | print_code(); //输出{ 653 | getsym(); 654 | r = judge_const(); 655 | if (r < 0) 656 | return r; 657 | while (symbol == COMMA) 658 | { 659 | print_code(); //输出, 660 | getsym(); 661 | r = judge_const(); 662 | if (r < 0) 663 | return r; 664 | } 665 | print_code(); //输出} 666 | getsym(); 667 | while (symbol == COMMA) 668 | { 669 | print_code(); //输出, 670 | getsym(); 671 | print_code(); //输出{ 672 | getsym(); 673 | r = judge_const(); 674 | if (r < 0) 675 | return r; 676 | while (symbol == COMMA) 677 | { 678 | print_code(); //输出, 679 | getsym(); 680 | r = judge_const(); 681 | if (r < 0) 682 | return r; 683 | } 684 | print_code(); //输出} 685 | getsym(); 686 | } 687 | print_code(); //输出} 688 | getsym(); 689 | } 690 | } 691 | else if(symbol == ASSIGN) 692 | { 693 | flag = 1; 694 | print_code(); //输出= 695 | getsym(); 696 | print_code(); //输出{ 697 | getsym(); 698 | r = judge_const(); 699 | if (r < 0) 700 | return r; 701 | while (symbol == COMMA) 702 | { 703 | print_code(); //输出, 704 | getsym(); 705 | r = judge_const(); 706 | if (r < 0) 707 | return r; 708 | } 709 | print_code(); //输出} 710 | getsym(); 711 | } 712 | } 713 | else if (symbol == ASSIGN) 714 | { 715 | flag = 1; 716 | print_code(); //输出= 717 | getsym(); 718 | r = judge_const(); 719 | if (r < 0) 720 | return r; 721 | } 722 | while (symbol == COMMA) 723 | { 724 | print_code(); //输出, 725 | getsym(); 726 | print_code(); //输出标识符 727 | getsym(); 728 | if (symbol == LBRACK) 729 | { 730 | print_code(); //输出[ 731 | getsym(); 732 | r = judge_unsigned_integer(); 733 | if (r < 0) 734 | return r; 735 | print_code(); //输出] 736 | getsym(); 737 | if (symbol == LBRACK) 738 | { 739 | print_code(); //输出[ 740 | getsym(); 741 | r = judge_unsigned_integer(); 742 | if (r < 0) 743 | return r; 744 | print_code(); //输出] 745 | getsym(); 746 | if (symbol == ASSIGN) 747 | { 748 | flag = 1; 749 | print_code(); //输出= 750 | getsym(); 751 | print_code(); //输出{ 752 | getsym(); 753 | print_code(); //输出{ 754 | getsym(); 755 | r = judge_const(); 756 | if (r < 0) 757 | return r; 758 | while (symbol == COMMA) 759 | { 760 | print_code(); //输出, 761 | getsym(); 762 | r = judge_const(); 763 | if (r < 0) 764 | return r; 765 | } 766 | print_code(); //输出} 767 | getsym(); 768 | while (symbol == COMMA) 769 | { 770 | print_code(); //输出, 771 | getsym(); 772 | print_code(); //输出{ 773 | getsym(); 774 | r = judge_const(); 775 | if (r < 0) 776 | return r; 777 | while (symbol == COMMA) 778 | { 779 | print_code(); //输出, 780 | getsym(); 781 | r = judge_const(); 782 | if (r < 0) 783 | return r; 784 | } 785 | print_code(); //输出} 786 | getsym(); 787 | } 788 | print_code(); //输出} 789 | getsym(); 790 | } 791 | } 792 | else if (symbol == ASSIGN) 793 | { 794 | flag = 1; 795 | print_code(); //输出= 796 | getsym(); 797 | print_code(); //输出{ 798 | getsym(); 799 | r = judge_const(); 800 | if (r < 0) 801 | return r; 802 | while (symbol == COMMA) 803 | { 804 | print_code(); //输出, 805 | getsym(); 806 | r = judge_const(); 807 | if (r < 0) 808 | return r; 809 | } 810 | print_code(); //输出} 811 | getsym(); 812 | } 813 | } 814 | else if (symbol == ASSIGN) 815 | { 816 | flag = 1; 817 | print_code(); //输出= 818 | getsym(); 819 | r = judge_const(); 820 | if (r < 0) 821 | return r; 822 | } 823 | //??? 824 | } 825 | if (flag) 826 | printf("<变量定义及初始化>\n"); 827 | else 828 | printf("<变量定义无初始化>\n"); 829 | printf("<变量定义>\n"); 830 | return 0; 831 | } 832 | 833 | int judge_function_has_return() 834 | { 835 | int r; 836 | r = judge_head(); 837 | if (r < 0) 838 | return r; 839 | print_code(); //输出( 840 | getsym(); 841 | r = judge_parameter_list(); 842 | if (r < 0) 843 | return r; 844 | print_code(); //输出) 845 | getsym(); 846 | print_code(); //输出{ 847 | getsym(); 848 | r = judge_mix_statement(); 849 | if (r < 0) 850 | return r; 851 | print_code(); //输出} 852 | getsym(); 853 | printf("<有返回值函数定义>\n"); 854 | return 0; 855 | } 856 | 857 | int judge_function_no_return() 858 | { 859 | int r; 860 | pre1 = symbol; 861 | strcpy(content1, s); 862 | flag1 = 1; 863 | getsym(); 864 | if (symbol == MAINTK) 865 | return -1; 866 | print_code_pre1(); //输出void 867 | flag1 = 0; 868 | strcpy(no_return_func[no_return_func_sum++], s); 869 | print_code(); //输出标识符 870 | getsym(); 871 | print_code(); //输出( 872 | getsym(); 873 | r = judge_parameter_list(); 874 | if (r < 0) 875 | return r; 876 | print_code(); //输出) 877 | getsym(); 878 | print_code(); //输出{ 879 | getsym(); 880 | r = judge_mix_statement(); 881 | if (r < 0) 882 | return r; 883 | print_code(); //输出} 884 | getsym(); 885 | printf("<无返回值函数定义>\n"); 886 | return 0; 887 | } 888 | 889 | int judge_mix_statement() 890 | { 891 | int r; 892 | r = judge_const_inform(); 893 | if (r < 0) 894 | return r; 895 | r = judge_var_inform(); 896 | if (r < 0) 897 | return r; 898 | r = judge_statement_list(); 899 | if (r < 0) 900 | return r; 901 | printf("<复合语句>\n"); 902 | return 0; 903 | } 904 | 905 | int judge_parameter_list() 906 | { 907 | if (symbol == RPARENT) //空的情况 908 | { 909 | printf("<参数表>\n"); 910 | return 0; 911 | } 912 | print_code(); //输出int或char 913 | getsym(); 914 | print_code(); //输出标识符 915 | getsym(); 916 | while (symbol == COMMA) 917 | { 918 | print_code(); //输出, 919 | getsym(); 920 | print_code(); //输出int或char 921 | getsym(); 922 | print_code(); //输出标识符 923 | getsym(); 924 | } 925 | printf("<参数表>\n"); 926 | return 0; 927 | } 928 | 929 | int judge_main_function() 930 | { 931 | int r; 932 | if (flag1 == 1) 933 | { 934 | flag1 = 0; 935 | print_code_pre1(); 936 | } 937 | else 938 | { 939 | print_code(); 940 | getsym(); 941 | }//两种情况决定输出void 942 | print_code(); //输出main 943 | getsym(); 944 | print_code(); //输出( 945 | getsym(); 946 | print_code(); //输出) 947 | getsym(); 948 | print_code(); //输出{ 949 | getsym(); 950 | r = judge_mix_statement(); 951 | if (r < 0) 952 | return r; 953 | print_code(); //输出} 954 | //到最后啦,不用读了 955 | printf("<主函数>\n"); 956 | return 0; 957 | } 958 | 959 | int judge_expression() 960 | { 961 | int r; 962 | if (symbol == PLUS || symbol == MINU) 963 | { 964 | print_code(); 965 | getsym(); 966 | } 967 | r = judge_item(); 968 | if (r < 0) 969 | return r; 970 | while (symbol == PLUS || symbol == MINU) 971 | { 972 | print_code(); 973 | getsym(); 974 | r = judge_item(); 975 | if (r < 0) 976 | return r; 977 | } 978 | printf("<表达式>\n"); 979 | return 0; 980 | } 981 | 982 | int judge_item() 983 | { 984 | int r; 985 | r = judge_factor(); 986 | if (r < 0) 987 | return r; 988 | while (symbol == MULT || symbol == DIV) 989 | { 990 | print_code(); 991 | getsym(); 992 | r = judge_factor(); 993 | if (r < 0) 994 | return r; 995 | } 996 | printf("<项>\n"); 997 | return 0; 998 | } 999 | 1000 | int judge_factor() 1001 | { 1002 | int r; 1003 | if (judge_returnfunc_use() == 0) 1004 | { 1005 | //应该不用干什么 1006 | } 1007 | else if (symbol == IDENFR) 1008 | { 1009 | print_code(); //输出标识符 1010 | getsym(); 1011 | while (symbol == LBRACK) 1012 | { 1013 | print_code(); //输出[ 1014 | getsym(); 1015 | r = judge_expression(); 1016 | if (r < 0) 1017 | return r; 1018 | print_code(); //输出] 1019 | getsym(); 1020 | } 1021 | } 1022 | else if (symbol == LPARENT) 1023 | { 1024 | print_code(); //输出( 1025 | getsym(); 1026 | r = judge_expression(); 1027 | if (r < 0) 1028 | return r; 1029 | print_code(); //输出) 1030 | getsym(); 1031 | } 1032 | else if (symbol == CHARCON) 1033 | { 1034 | print_code(); //输出字符 1035 | getsym(); 1036 | } 1037 | else if (judge_integer() == 0) 1038 | { 1039 | //应该不用干什么 1040 | } 1041 | printf("<因子>\n"); 1042 | return 0; 1043 | } 1044 | 1045 | int judge_statement() 1046 | { 1047 | int r; 1048 | if (symbol == SEMICN) 1049 | { //空的情况 1050 | print_code(); //输出; 1051 | getsym(); 1052 | } 1053 | else if (judge_circulation_statement() == 0) 1054 | { 1055 | //应该不用干什么 1056 | } 1057 | else if (judge_condition_statement() == 0) 1058 | { 1059 | //应该不用干什么 1060 | } 1061 | else if (judge_return_statement() == 0) 1062 | { 1063 | print_code(); //输出; 1064 | getsym(); 1065 | } 1066 | else if (judge_returnfunc_use() == 0) 1067 | { 1068 | print_code(); //输出; 1069 | getsym(); 1070 | } 1071 | else if (judge_noreturnfunc_use() == 0) 1072 | { 1073 | print_code(); //输出; 1074 | getsym(); 1075 | } 1076 | else if (judge_value_statement() == 0) 1077 | { 1078 | print_code(); //输出; 1079 | getsym(); 1080 | } 1081 | else if (judge_scanf() == 0) 1082 | { 1083 | print_code(); //输出; 1084 | getsym(); 1085 | } 1086 | else if (judge_printf() == 0) 1087 | { 1088 | print_code(); //输出; 1089 | getsym(); 1090 | } 1091 | else if (judge_case_statement() == 0) 1092 | { 1093 | //应该不用干什么 1094 | } 1095 | else if (symbol == LBRACE) 1096 | { 1097 | print_code(); //输出{ 1098 | getsym(); 1099 | r = judge_statement_list(); 1100 | if (r < 0) 1101 | return r; 1102 | print_code(); //输出} 1103 | getsym(); 1104 | } 1105 | else 1106 | return -1; 1107 | printf("<语句>\n"); 1108 | return 0; 1109 | } 1110 | 1111 | int judge_value_statement() 1112 | { 1113 | int r; 1114 | if (symbol != IDENFR) 1115 | { 1116 | return -1; 1117 | } 1118 | print_code(); //输出标识符 1119 | getsym(); 1120 | while (symbol == LBRACK) 1121 | { 1122 | print_code(); //输出[ 1123 | getsym(); 1124 | r = judge_expression(); 1125 | if (r < 0) 1126 | return r; 1127 | print_code(); //输出] 1128 | getsym(); 1129 | } 1130 | print_code(); //输出= 1131 | getsym(); 1132 | r = judge_expression(); 1133 | if (r < 0) 1134 | return r; 1135 | printf("<赋值语句>\n"); 1136 | return 0; 1137 | } 1138 | 1139 | int judge_condition_statement() 1140 | { 1141 | int r; 1142 | if (symbol != IFTK) 1143 | return -1; 1144 | print_code(); //输出if 1145 | getsym(); 1146 | print_code(); //输出( 1147 | getsym(); 1148 | r = judge_condition(); 1149 | if (r < 0) 1150 | return r; 1151 | print_code(); //输出) 1152 | getsym(); 1153 | r = judge_statement(); 1154 | if (r < 0) 1155 | return r; 1156 | if (symbol == ELSETK) 1157 | { 1158 | print_code();//输出else 1159 | getsym(); 1160 | r = judge_statement(); 1161 | if (r < 0) 1162 | return r; 1163 | } 1164 | printf("<条件语句>\n"); 1165 | return 0; 1166 | } 1167 | 1168 | int judge_condition() 1169 | { 1170 | int r; 1171 | r = judge_expression(); 1172 | if (r < 0) 1173 | return r; 1174 | print_code(); //输出关系运算符 1175 | getsym(); 1176 | r = judge_expression(); 1177 | if (r < 0) 1178 | return r; 1179 | printf("<条件>\n"); 1180 | return 0; 1181 | } 1182 | 1183 | int judge_circulation_statement() 1184 | { 1185 | int r; 1186 | if (symbol != WHILETK && symbol != FORTK) 1187 | return -1; 1188 | if (symbol == WHILETK) 1189 | { 1190 | print_code();//输出while 1191 | getsym(); 1192 | print_code(); //输出( 1193 | getsym(); 1194 | r = judge_condition(); 1195 | if (r < 0) 1196 | return r; 1197 | print_code(); //输出) 1198 | getsym(); 1199 | r = judge_statement(); 1200 | if (r < 0) 1201 | return r; 1202 | } 1203 | else if (symbol == FORTK) 1204 | { 1205 | print_code(); //输出for 1206 | getsym(); 1207 | print_code(); //输出( 1208 | getsym(); 1209 | print_code(); //输出标识符 1210 | getsym(); 1211 | print_code(); //输出= 1212 | getsym(); 1213 | r = judge_expression(); 1214 | if (r < 0) 1215 | return r; 1216 | print_code(); //输出; 1217 | getsym(); 1218 | r = judge_condition(); 1219 | if (r < 0) 1220 | return r; 1221 | print_code(); //输出; 1222 | getsym(); 1223 | print_code(); //输出标识符 1224 | getsym(); 1225 | print_code(); //输出= 1226 | getsym(); 1227 | print_code(); //输出标识符 1228 | getsym(); 1229 | print_code(); //输出+或- 1230 | getsym(); 1231 | r = judge_stepsize(); 1232 | if (r < 0) 1233 | return r; 1234 | print_code(); //输出) 1235 | getsym(); 1236 | r = judge_statement(); 1237 | if (r < 0) 1238 | return r; 1239 | } 1240 | printf("<循环语句>\n"); 1241 | return 0; 1242 | } 1243 | 1244 | int judge_stepsize() 1245 | { 1246 | int r; 1247 | r = judge_unsigned_integer(); 1248 | if (r < 0) 1249 | return r; 1250 | printf("<步长>\n"); 1251 | return 0; 1252 | } 1253 | 1254 | int judge_case_statement() 1255 | { 1256 | int r; 1257 | if (symbol != SWITCHTK) 1258 | return -1; 1259 | print_code(); //输出switch 1260 | getsym(); 1261 | print_code(); //输出( 1262 | getsym(); 1263 | r = judge_expression(); 1264 | if (r < 0) 1265 | return r; 1266 | print_code(); //输出) 1267 | getsym(); 1268 | print_code(); //输出{ 1269 | getsym(); 1270 | r = judge_case_list(); 1271 | if (r < 0) 1272 | return r; 1273 | r = judge_default(); 1274 | if (r < 0) 1275 | return r; 1276 | print_code(); //输出} 1277 | getsym(); 1278 | printf("<情况语句>\n"); 1279 | return 0; 1280 | } 1281 | 1282 | int judge_case_list() 1283 | { 1284 | int r; 1285 | r = judge_case_son_statement(); 1286 | if (r < 0) 1287 | return r; 1288 | while (judge_case_son_statement() == 0) 1289 | { 1290 | //应该不需要干啥。。。 1291 | } 1292 | printf("<情况表>\n"); 1293 | return 0; 1294 | } 1295 | 1296 | int judge_case_son_statement() 1297 | { 1298 | int r; 1299 | if (symbol != CASETK) 1300 | return -1; 1301 | print_code(); //输出case 1302 | getsym(); 1303 | r = judge_const(); 1304 | if (r < 0) 1305 | return r; 1306 | print_code();//输出: 1307 | getsym(); 1308 | r = judge_statement(); 1309 | if (r < 0) 1310 | return r; 1311 | printf("<情况子语句>\n"); 1312 | return 0; 1313 | } 1314 | 1315 | int judge_default() 1316 | { 1317 | int r; 1318 | if (symbol != DEFAULTTK) 1319 | return -1; 1320 | print_code(); //输出default 1321 | getsym(); 1322 | print_code(); //输出: 1323 | getsym(); 1324 | r = judge_statement(); 1325 | if (r < 0) 1326 | return r; 1327 | printf("<缺省>\n"); 1328 | return 0; 1329 | } 1330 | 1331 | int judge_returnfunc_use() 1332 | { 1333 | int r; 1334 | char tmp[1000]; 1335 | if (symbol != IDENFR) 1336 | return -1; 1337 | strcpy(tmp, s); 1338 | if (find_return_func(tmp) == 0) 1339 | return -1; 1340 | print_code(); //输出标识符 1341 | getsym(); 1342 | print_code(); //输出( 1343 | getsym(); 1344 | r = judge_value_parameter_list(); 1345 | if (r < 0) 1346 | return r; 1347 | print_code(); //输出) 1348 | getsym(); 1349 | printf("<有返回值函数调用语句>\n"); 1350 | return 0; 1351 | } 1352 | 1353 | int judge_noreturnfunc_use() 1354 | { 1355 | int r; 1356 | char tmp[1000]; 1357 | if (symbol != IDENFR) 1358 | return -1; 1359 | strcpy(tmp, s); 1360 | if (find_no_return_func(tmp) == 0) 1361 | return -1; 1362 | print_code(); //输出标识符 1363 | getsym(); 1364 | print_code(); //输出( 1365 | getsym(); 1366 | r = judge_value_parameter_list(); 1367 | if (r < 0) 1368 | return -1; 1369 | print_code(); //输出) 1370 | getsym(); 1371 | printf("<无返回值函数调用语句>\n"); 1372 | return 0; 1373 | } 1374 | 1375 | int judge_value_parameter_list() 1376 | { 1377 | int r; 1378 | if (symbol == RPARENT) 1379 | { //空的情况 1380 | printf("<值参数表>\n"); 1381 | return 0; 1382 | } 1383 | r = judge_expression(); 1384 | if (r < 0) 1385 | return r; 1386 | while (symbol == COMMA) 1387 | { 1388 | print_code(); 1389 | getsym(); 1390 | r = judge_expression(); 1391 | if (r < 0) 1392 | return r; 1393 | } 1394 | printf("<值参数表>\n"); 1395 | return 0; 1396 | } 1397 | 1398 | int judge_statement_list() 1399 | { 1400 | while (judge_statement() == 0) 1401 | { 1402 | //应该不用干什么。。。 1403 | } 1404 | printf("<语句列>\n"); 1405 | return 0; 1406 | } 1407 | 1408 | int judge_scanf() 1409 | { 1410 | if (symbol != SCANFTK) 1411 | return -1; 1412 | print_code(); //输出scanf 1413 | getsym(); 1414 | print_code(); //输出( 1415 | getsym(); 1416 | print_code(); //输出标识符 1417 | getsym(); 1418 | print_code(); //输出) 1419 | getsym(); 1420 | printf("<读语句>\n"); 1421 | return 0; 1422 | } 1423 | 1424 | int judge_printf() 1425 | { 1426 | int r; 1427 | if (symbol != PRINTFTK) 1428 | return -1; 1429 | print_code(); //输出printf 1430 | getsym(); 1431 | print_code(); //输出( 1432 | getsym(); 1433 | if (judge_string() == 0) 1434 | { 1435 | //发现有字符串,判断有没有逗号来看有没有表达式 1436 | if (symbol == COMMA) 1437 | { 1438 | print_code(); 1439 | getsym(); 1440 | r = judge_expression(); 1441 | if (r < 0) 1442 | return r; 1443 | } 1444 | } 1445 | else if (judge_expression() == 0) 1446 | { 1447 | 1448 | } 1449 | print_code(); 1450 | getsym(); 1451 | printf("<写语句>\n"); 1452 | return 0; 1453 | } 1454 | 1455 | int judge_return_statement() 1456 | { 1457 | int r; 1458 | if (symbol != RETURNTK) 1459 | return -1; 1460 | print_code(); //输出return 1461 | getsym(); 1462 | if (symbol == LPARENT) 1463 | { 1464 | print_code(); //输出( 1465 | getsym(); 1466 | r = judge_expression(); 1467 | if (r < 0) 1468 | return r; 1469 | print_code(); //输出) 1470 | getsym(); 1471 | } 1472 | printf("<返回语句>\n"); 1473 | return 0; 1474 | } 1475 | int main() 1476 | { 1477 | getsym(); 1478 | judge_program(); 1479 | } -------------------------------------------------------------------------------- /代码生成1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | void deal_token(char c); 7 | void getsym(); 8 | int find_return_func(char* name); 9 | int find_no_return_func(char* name); 10 | void print_mips(char* mips_code); 11 | void mips_generate(); 12 | void mips_print_statement1(int loc); 13 | void mips_print_statement2(int loc); 14 | void mips_print_statement3(int loc); 15 | void mips_scanf_statement(int loc); 16 | void store_var(char* name, char* reg, int type); 17 | void mips_calculate(int loc); 18 | void mips_calculate(int loc); 19 | void mips_value_statement(int loc); 20 | struct return_info judge_string(); 21 | struct return_info judge_program(); 22 | struct return_info judge_const_inform(); 23 | struct return_info judge_const_define(); 24 | struct return_info judge_unsigned_integer(); 25 | struct return_info judge_integer(); 26 | struct return_info judge_head(); 27 | struct return_info judge_const(); 28 | struct return_info judge_var_inform(); 29 | struct return_info judge_var_define(); 30 | struct return_info judge_function_has_return(); 31 | struct return_info judge_function_no_return(); 32 | struct return_info judge_mix_statement(); 33 | struct return_info judge_parameter_list(); 34 | struct return_info judge_main_function(); 35 | struct return_info judge_expression(); 36 | struct return_info judge_item(); 37 | struct return_info judge_factor(); 38 | struct return_info judge_statement(); 39 | struct return_info judge_value_statement(); 40 | struct return_info judge_condition_statement(); 41 | struct return_info judge_condition(); 42 | struct return_info judge_circulation_statement(); 43 | struct return_info judge_stepsize(); 44 | struct return_info judge_case_statement(); 45 | struct return_info judge_case_list(); 46 | struct return_info judge_case_son_statement(); 47 | struct return_info judge_default(); 48 | struct return_info judge_returnfunc_use(); 49 | struct return_info judge_noreturnfunc_use(); 50 | struct return_info judge_value_parameter_list(); 51 | struct return_info judge_statement_list(); 52 | struct return_info judge_scanf(); 53 | struct return_info judge_printf(); 54 | struct return_info judge_return_statement(); 55 | 56 | FILE* in = freopen("testfile.txt", "r", stdin); 57 | FILE* out = freopen("mips.txt", "w", stdout); 58 | char c; 59 | char s[1000]; 60 | char s_low[1000]; 61 | 62 | enum Code 63 | { 64 | IDENFR, 65 | INTCON, 66 | CHARCON, 67 | STRCON, 68 | CONSTTK, 69 | INTTK, 70 | CHARTK, 71 | VOIDTK, 72 | MAINTK, 73 | IFTK, 74 | ELSETK, 75 | SWITCHTK, 76 | CASETK, 77 | DEFAULTTK, 78 | WHILETK, 79 | FORTK, 80 | SCANFTK, 81 | PRINTFTK, 82 | RETURNTK, 83 | PLUS, 84 | MINU, 85 | MULT, 86 | DIV, 87 | LSS, 88 | LEQ, 89 | GRE, 90 | GEQ, 91 | EQL, 92 | NEQ, 93 | COLON, 94 | ASSIGN, 95 | SEMICN, 96 | COMMA, 97 | LPARENT, 98 | RPARENT, 99 | LBRACK, 100 | RBRACK, 101 | LBRACE, 102 | RBRACE 103 | }; 104 | 105 | Code symbol, pre1, pre2; 106 | char content1[1000], content2[1000]; 107 | int flag1 = 0, flag2 = 0; 108 | char no_return_func[1000][1000]; 109 | char has_return_func[1000][1000]; 110 | int no_return_func_sum = 0, has_return_func_sum = 0; 111 | 112 | enum Middle_type { 113 | VARIABLE_DECLARATIONS, //变量声明 114 | CONSTANT_DECLARATIONS, //常量声明 115 | SCANF_STATEMENT, //读语句 116 | VALUE_STATEMENT, //赋值语句 117 | PRINT_STATEMENT1, //printf '(' <字符串>,<表达式> ')' 118 | PRINT_STATEMENT2,//printf '('<字符串> ')' 119 | PRINT_STATEMENT3,//printf '('<表达式> ')' 120 | CALCULATE, //涉及到计算结果的语句 121 | MAIN, //主函数调用 122 | }; 123 | 124 | int reg_use[35]; //寄存器是否被使用过,0代表没有,1代表有 125 | 126 | int choose_reg_s() { 127 | int i; 128 | for (i = 16; i <= 23; i++) { 129 | if (reg_use[i] == 0) { 130 | return i; 131 | } 132 | } 133 | return -1; 134 | } 135 | 136 | int choose_reg_t() { 137 | int i; 138 | for (i = 8; i <= 15; i++) { 139 | if (reg_use[i] == 0) { 140 | return i; 141 | } 142 | } //t8和t9先保留着不用 143 | return -1; 144 | } 145 | 146 | struct Middle_code { 147 | Middle_type type; //这一组中间代码是哪一种语句的 148 | int num; //code的数量 149 | int kind; //0:int, 1:char 150 | char code[10][100]; //中间代码的内容 151 | }; 152 | 153 | struct name_info { 154 | char name[100]; //名字 155 | int type; //0:int, 1:char 156 | int use; 157 | int new_type; //0:int, 1:char 158 | char new_name[100]; //新的名字 159 | }; 160 | 161 | struct return_info { 162 | int return_num; 163 | int num; 164 | int type;//0:int, 1:char 165 | char code[10][100]; 166 | }; 167 | 168 | struct return_info zero = { 0 }; 169 | struct return_info fail = { -1 }; 170 | struct return_info one = { 1 }; 171 | 172 | struct name_info name_list[1000]; //所有名字都保存在这里,包括变量,常量,函数名 173 | int name_sum = 0; //现在一共声明了多少个名字 174 | 175 | struct Middle_code middle_code_list[10000]; //中间代码的列表 176 | int middle_code_sum = 0; //中间代码列表的指针 177 | int main_location; 178 | 179 | int reg_index = 0; 180 | 181 | int find_name(char* name) { 182 | int i; 183 | for (i = 0; i < name_sum; i++) { 184 | if (strcmp(name, name_list[i].name) == 0) { 185 | return i; 186 | } 187 | } 188 | return -1; 189 | } 190 | 191 | Middle_code middle_code_generate(Middle_type type, int kind, int num, ...) { 192 | Middle_code middle_code; 193 | middle_code.type = type; 194 | middle_code.num = num; 195 | middle_code.kind = kind; 196 | va_list ap; 197 | va_start(ap, num); 198 | int i; 199 | for (i = 0; i < num; i++) { 200 | strcpy(middle_code.code[i], va_arg(ap, char*)); 201 | } 202 | va_end(ap); 203 | return middle_code; 204 | } 205 | 206 | int find_const(char* name) { 207 | int i; 208 | for (i = 0; i < middle_code_sum; i++) { 209 | if (middle_code_list[i].type == CONSTANT_DECLARATIONS && 210 | strcmp(middle_code_list[i].code[2], name) == 0) { 211 | return i; 212 | } 213 | } 214 | return -1; 215 | } 216 | 217 | int mips_string_sum = 0; 218 | int depth = 0; 219 | int stack_pointer = 0x7fffeffc; //??? 220 | int global_pointer = 0x10008000; //??? 221 | int base_pointer = 0x10008000; 222 | 223 | struct store_info { 224 | char name[100]; 225 | int type; 226 | int reg_num; 227 | int stack_position; 228 | }; 229 | 230 | struct store_info store_info_list[1000]; 231 | int store_info_sum = 0; 232 | 233 | int find_var(char* name) { 234 | int i; 235 | for (i = 0; i < store_info_sum; i++) { 236 | if (strcmp(store_info_list[i].name, name) == 0 && 237 | store_info_list[i].reg_num != -2) { 238 | return i; 239 | } 240 | } 241 | return -1; 242 | } 243 | 244 | void print_mips(char* mips_code) { 245 | int i; 246 | for (i = 0; i < depth; i++) { 247 | printf(" "); 248 | } 249 | printf("%s\n", mips_code); 250 | } 251 | 252 | void store_string_mips() { 253 | int i; 254 | char code[100]; 255 | for (i = 0; i < middle_code_sum; i++) { 256 | if (middle_code_list[i].type == PRINT_STATEMENT1 257 | || middle_code_list[i].type == PRINT_STATEMENT2) { 258 | sprintf(code, "str%d: .asciiz \"%s\"", mips_string_sum, middle_code_list[i].code[1]); 259 | sprintf(middle_code_list[i].code[1], "str%d", mips_string_sum++); 260 | print_mips(code); 261 | } 262 | } 263 | sprintf(code, "changeline: .asciiz \"\\n\""); 264 | print_mips(code); 265 | } 266 | 267 | void store_var1() { 268 | int i, rs, rt; 269 | char code[100]; 270 | for (i = 0; middle_code_list[i].type != MAIN; i++) { //注意,未来可能增加对于函数调用的处理 271 | if (middle_code_list[i].type == VARIABLE_DECLARATIONS) { 272 | rs = choose_reg_s(); 273 | rt = choose_reg_t(); 274 | if (rs > 0) { 275 | strcpy(store_info_list[store_info_sum].name, middle_code_list[i].code[2]); 276 | store_info_list[store_info_sum].type = middle_code_list[i].kind; 277 | store_info_list[store_info_sum++].reg_num = rs; 278 | if (middle_code_list[i].num == 5) { 279 | if (middle_code_list[i].kind == 0) { 280 | sprintf(code, "li $s%d, 0x%x", rs - 16, atoi(middle_code_list[i].code[4])); 281 | } 282 | else { 283 | sprintf(code, "li $s%d, 0x%x", rs - 16, middle_code_list[i].code[4][0]); 284 | } 285 | print_mips(code); 286 | } 287 | reg_use[rs] = 1; 288 | } 289 | else if (rt > 0) { 290 | strcpy(store_info_list[store_info_sum].name, middle_code_list[i].code[2]); 291 | store_info_list[store_info_sum].type = middle_code_list[i].kind; 292 | store_info_list[store_info_sum++].reg_num = rt; 293 | if (middle_code_list[i].num == 5) { 294 | if (middle_code_list[i].kind == 0) { 295 | sprintf(code, "li $t%d, 0x%x", rt - 8, atoi(middle_code_list[i].code[4])); 296 | } 297 | else { 298 | sprintf(code, "li $t%d, 0x%x", rt - 8, middle_code_list[i].code[4][0]); 299 | } 300 | print_mips(code); 301 | } 302 | reg_use[rt] = 1; 303 | } 304 | else { 305 | strcpy(store_info_list[store_info_sum].name, middle_code_list[i].code[2]); 306 | store_info_list[store_info_sum].type = middle_code_list[i].kind; 307 | store_info_list[store_info_sum].reg_num = -1; 308 | global_pointer -= 4; 309 | if (middle_code_list[i].num == 5) { 310 | if (middle_code_list[i].kind == 0) { 311 | sprintf(code, "li $t8, 0x%x", atoi(middle_code_list[i].code[4])); 312 | print_mips(code); 313 | sprintf(code, "sw $t8, %d($gp)", global_pointer - base_pointer); 314 | print_mips(code); 315 | } 316 | else { 317 | sprintf(code, "li $t8, 0x%x", middle_code_list[i].code[4][0]); 318 | print_mips(code); 319 | sprintf(code, "sw $t8, %d($gp)", global_pointer - base_pointer); 320 | print_mips(code); 321 | } 322 | } 323 | store_info_list[store_info_sum++].stack_position = global_pointer; 324 | } 325 | } 326 | } 327 | } 328 | 329 | void mips_generate() { 330 | int i, rs, rt; 331 | char code[100]; 332 | sprintf(code, ".data"); 333 | print_mips(code); 334 | depth++; 335 | store_string_mips(); 336 | depth--; 337 | sprintf(code, ".text"); 338 | print_mips(code); 339 | depth++; 340 | store_var1(); //注意后续的数组改动 341 | sprintf(code, "main:"); 342 | print_mips(code); 343 | depth++; 344 | for (i = main_location + 1; i < middle_code_sum; i++) { 345 | if (middle_code_list[i].type == SCANF_STATEMENT) { 346 | mips_scanf_statement(i); 347 | } 348 | else if (middle_code_list[i].type == PRINT_STATEMENT1) { 349 | mips_print_statement1(i); 350 | sprintf(code, "li $v0, 4"); 351 | print_mips(code); 352 | sprintf(code, "la $a0, changeline"); 353 | print_mips(code); 354 | sprintf(code, "syscall"); 355 | print_mips(code); 356 | } 357 | else if (middle_code_list[i].type == PRINT_STATEMENT2) { 358 | mips_print_statement2(i); 359 | sprintf(code, "li $v0, 4"); 360 | print_mips(code); 361 | sprintf(code, "la $a0, changeline"); 362 | print_mips(code); 363 | sprintf(code, "syscall"); 364 | print_mips(code); 365 | } 366 | else if (middle_code_list[i].type == PRINT_STATEMENT3) { 367 | mips_print_statement3(i); 368 | sprintf(code, "li $v0, 4"); 369 | print_mips(code); 370 | sprintf(code, "la $a0, changeline"); 371 | print_mips(code); 372 | sprintf(code, "syscall"); 373 | print_mips(code); 374 | } 375 | else if (middle_code_list[i].type == CALCULATE) { 376 | mips_calculate(i); 377 | } 378 | else if (middle_code_list[i].type == VALUE_STATEMENT) { 379 | mips_value_statement(i); 380 | } 381 | else if (middle_code_list[i].type == VARIABLE_DECLARATIONS) { 382 | rs = choose_reg_s(); 383 | rt = choose_reg_t(); 384 | if (rs > 0) { 385 | strcpy(store_info_list[store_info_sum].name, middle_code_list[i].code[2]); 386 | store_info_list[store_info_sum].type = middle_code_list[i].kind; 387 | store_info_list[store_info_sum++].reg_num = rs; 388 | if (middle_code_list[i].num == 5) { 389 | if (middle_code_list[i].kind == 0) { 390 | sprintf(code, "li $s%d, 0x%x", rs - 16, atoi(middle_code_list[i].code[4])); 391 | } 392 | else { 393 | sprintf(code, "li $s%d, 0x%x", rs - 16, middle_code_list[i].code[4][0]); 394 | } 395 | print_mips(code); 396 | } 397 | reg_use[rs] = 1; 398 | } 399 | else if (rt > 0) { 400 | strcpy(store_info_list[store_info_sum].name, middle_code_list[i].code[2]); 401 | store_info_list[store_info_sum].type = middle_code_list[i].kind; 402 | store_info_list[store_info_sum++].reg_num = rt; 403 | if (middle_code_list[i].num == 5) { 404 | if (middle_code_list[i].kind == 0) { 405 | sprintf(code, "li $t%d, 0x%x", rt - 8, atoi(middle_code_list[i].code[4])); 406 | } 407 | else { 408 | sprintf(code, "li $t%d, 0x%x", rt - 8, middle_code_list[i].code[4][0]); 409 | } 410 | print_mips(code); 411 | } 412 | reg_use[rt] = 1; 413 | } 414 | else { 415 | strcpy(store_info_list[store_info_sum].name, middle_code_list[i].code[2]); 416 | store_info_list[store_info_sum].type = middle_code_list[i].kind; 417 | store_info_list[store_info_sum].reg_num = -1; 418 | global_pointer -= 4; 419 | if (middle_code_list[i].num == 5) { 420 | if (middle_code_list[i].kind == 0) { 421 | sprintf(code, "li $t8, 0x%x", atoi(middle_code_list[i].code[4])); 422 | print_mips(code); 423 | sprintf(code, "sw $t8, %d($gp)", global_pointer - base_pointer); 424 | print_mips(code); 425 | } 426 | else { 427 | sprintf(code, "li $t8, 0x%x", middle_code_list[i].code[4][0]); 428 | print_mips(code); 429 | sprintf(code, "sw $t8, %d($gp)", global_pointer - base_pointer); 430 | print_mips(code); 431 | } 432 | } 433 | store_info_list[store_info_sum++].stack_position = global_pointer; 434 | } 435 | } 436 | } 437 | sprintf(code, "li $v0, 10"); 438 | print_mips(code); 439 | sprintf(code, "syscall"); 440 | print_mips(code); 441 | depth--; 442 | } 443 | 444 | void mips_print_statement1(int loc) { 445 | char code[100]; 446 | int pos; 447 | mips_print_statement2(loc); 448 | if (middle_code_list[loc].code[2][0] == '\'') { 449 | sprintf(code, "li $v0, 11"); 450 | print_mips(code); 451 | sprintf(code, "li $a0, %d", middle_code_list[loc].code[2][1]); 452 | print_mips(code); 453 | sprintf(code, "syscall"); 454 | print_mips(code); 455 | } 456 | else if (middle_code_list[loc].code[2][0] == '+' || 457 | middle_code_list[loc].code[2][0] == '-' || 458 | isdigit(middle_code_list[loc].code[2][0])) { 459 | sprintf(code, "li $v0, 1"); 460 | print_mips(code); 461 | sprintf(code, "li $a0, 0x%x", atoi(middle_code_list[loc].code[2])); 462 | print_mips(code); 463 | sprintf(code, "syscall"); 464 | print_mips(code); 465 | } 466 | else if ((pos = find_const(middle_code_list[loc].code[2])) >= 0) { 467 | if (strcmp(middle_code_list[pos].code[1], "int") == 0) { 468 | sprintf(code, "li $v0, 1"); 469 | print_mips(code); 470 | sprintf(code, "li $a0, 0x%x", atoi(middle_code_list[pos].code[4])); 471 | print_mips(code); 472 | sprintf(code, "syscall"); 473 | print_mips(code); 474 | } 475 | else { 476 | sprintf(code, "li $v0, 11"); 477 | print_mips(code); 478 | sprintf(code, "li $a0, 0x%x", middle_code_list[pos].code[4][0]); 479 | print_mips(code); 480 | sprintf(code, "syscall"); 481 | print_mips(code); 482 | } 483 | } 484 | else { 485 | pos = find_var(middle_code_list[loc].code[2]); 486 | if (store_info_list[pos].type == 0) { 487 | sprintf(code, "li $v0, 1"); 488 | print_mips(code); 489 | } 490 | else { 491 | sprintf(code, "li $v0, 11"); 492 | print_mips(code); 493 | } 494 | pos = store_info_list[pos].reg_num >= 0 ? 495 | store_info_list[pos].reg_num : store_info_list[pos].stack_position; 496 | if (pos <= 31) { 497 | if (pos >= 16 && pos <= 25) { 498 | sprintf(code, "move $a0, $s%d", pos - 16); 499 | } 500 | else { 501 | sprintf(code, "move $a0, $t%d", pos - 8); 502 | } 503 | print_mips(code); 504 | sprintf(code, "syscall"); 505 | print_mips(code); 506 | } 507 | else { 508 | sprintf(code, "lw $a0, %d($gp)", pos - base_pointer); 509 | print_mips(code); 510 | sprintf(code, "syscall"); 511 | print_mips(code); 512 | } 513 | } 514 | } 515 | 516 | void mips_print_statement2(int loc) { 517 | char code[100]; 518 | sprintf(code, "li $v0, 4"); 519 | print_mips(code); 520 | sprintf(code, "la $a0, %s", middle_code_list[loc].code[1]); 521 | print_mips(code); 522 | sprintf(code, "syscall"); 523 | print_mips(code); 524 | } 525 | 526 | void mips_print_statement3(int loc) { 527 | char code[100]; 528 | int pos; 529 | if (middle_code_list[loc].code[1][0] == '\'') { 530 | sprintf(code, "li $v0, 11"); 531 | print_mips(code); 532 | sprintf(code, "li $a0, %d", middle_code_list[loc].code[1][1]); 533 | print_mips(code); 534 | sprintf(code, "syscall"); 535 | print_mips(code); 536 | } 537 | else if (middle_code_list[loc].code[1][0] == '+' || 538 | middle_code_list[loc].code[1][0] == '-' || 539 | isdigit(middle_code_list[loc].code[1][0])) { 540 | sprintf(code, "li $v0, 1"); 541 | print_mips(code); 542 | sprintf(code, "li $a0, 0x%x", atoi(middle_code_list[loc].code[1])); 543 | print_mips(code); 544 | sprintf(code, "syscall"); 545 | print_mips(code); 546 | } 547 | else if ((pos = find_const(middle_code_list[loc].code[1])) >= 0) { 548 | if (strcmp(middle_code_list[pos].code[1], "int") == 0) { 549 | sprintf(code, "li $v0, 1"); 550 | print_mips(code); 551 | sprintf(code, "li $a0, 0x%x", atoi(middle_code_list[pos].code[4])); 552 | print_mips(code); 553 | sprintf(code, "syscall"); 554 | print_mips(code); 555 | } 556 | else { 557 | sprintf(code, "li $v0, 11"); 558 | print_mips(code); 559 | sprintf(code, "li $a0, 0x%x", middle_code_list[pos].code[4][0]); 560 | print_mips(code); 561 | sprintf(code, "syscall"); 562 | print_mips(code); 563 | } 564 | } 565 | else { 566 | pos = find_var(middle_code_list[loc].code[1]); 567 | if (store_info_list[pos].type == 0) { 568 | sprintf(code, "li $v0, 1"); 569 | print_mips(code); 570 | } 571 | else { 572 | sprintf(code, "li $v0, 11"); 573 | print_mips(code); 574 | } 575 | pos = store_info_list[pos].reg_num >= 0 ? 576 | store_info_list[pos].reg_num : store_info_list[pos].stack_position; 577 | if (pos <= 31) { 578 | if (pos >= 16 && pos <= 25) { 579 | sprintf(code, "move $a0, $s%d", pos - 16); 580 | } 581 | else { 582 | sprintf(code, "move $a0, $t%d", pos - 8); 583 | } 584 | print_mips(code); 585 | sprintf(code, "syscall"); 586 | print_mips(code); 587 | } 588 | else { 589 | sprintf(code, "lw $a0, %d($gp)", pos - base_pointer); 590 | print_mips(code); 591 | sprintf(code, "syscall"); 592 | print_mips(code); 593 | } 594 | } 595 | } 596 | 597 | void mips_scanf_statement(int loc) { 598 | int pos; 599 | char code[100]; 600 | char reg[100]; 601 | if (middle_code_list[loc].kind == 0) { 602 | sprintf(code, "li $v0, 5"); 603 | print_mips(code); 604 | sprintf(code, "syscall"); 605 | print_mips(code); 606 | } 607 | else if (middle_code_list[loc].kind == 1) { 608 | sprintf(code, "li $v0, 12"); 609 | print_mips(code); 610 | sprintf(code, "syscall"); 611 | print_mips(code); 612 | } 613 | pos = find_var(middle_code_list[loc].code[1]); 614 | if (pos >= 0) { 615 | pos = store_info_list[pos].reg_num >= 0 ? 616 | store_info_list[pos].reg_num : store_info_list[pos].stack_position; 617 | } 618 | if (pos >= 0 && pos <= 31) { 619 | if (pos >= 16 && pos <= 25) 620 | sprintf(code, "move $s%d, $v0", pos - 16); 621 | else 622 | sprintf(code, "move $t%d, $v0", pos - 8); 623 | print_mips(code); 624 | } 625 | else if (pos > 31) { 626 | sprintf(code, "sw $v0, %d($gp)", pos - base_pointer); 627 | print_mips(code); 628 | } 629 | else { 630 | sprintf(reg, "$v0"); 631 | store_var(middle_code_list[loc].code[1], reg, middle_code_list[loc].kind); 632 | } 633 | } 634 | 635 | void store_var(char* name, char* reg, int type) { 636 | char code[100]; 637 | int rs, rt, loc; 638 | loc = find_var(name); 639 | if (loc < 0) { 640 | rs = choose_reg_s(); 641 | rt = choose_reg_t(); 642 | if (rs > 0) { 643 | strcpy(store_info_list[store_info_sum].name, name); 644 | store_info_list[store_info_sum].type = type; 645 | store_info_list[store_info_sum++].reg_num = rs; 646 | sprintf(code, "move $s%d, %s", rs - 16, reg); 647 | print_mips(code); 648 | reg_use[rs] = 1; 649 | } 650 | else if (rt > 0) { 651 | strcpy(store_info_list[store_info_sum].name, name); 652 | store_info_list[store_info_sum].type = type; 653 | store_info_list[store_info_sum++].reg_num = rt; 654 | sprintf(code, "move $t%d, %s", rt - 8, reg); 655 | print_mips(code); 656 | reg_use[rt] = 1; 657 | } 658 | else { 659 | strcpy(store_info_list[store_info_sum].name, name); 660 | store_info_list[store_info_sum].type = type; 661 | store_info_list[store_info_sum].reg_num = -1; 662 | global_pointer -= 4; 663 | sprintf(code, "sw %s, %d($gp)", reg, global_pointer - base_pointer); 664 | print_mips(code); 665 | store_info_list[store_info_sum++].stack_position = global_pointer; 666 | } 667 | } 668 | else { 669 | loc = store_info_list[loc].reg_num >= 0 ? 670 | store_info_list[loc].reg_num : store_info_list[loc].stack_position; 671 | if (loc <= 31) { 672 | if (loc >= 16 && loc <= 25) 673 | sprintf(code, "move $s%d, %s", loc - 16, reg); 674 | else 675 | sprintf(code, "move $t%d, %s", loc - 8, reg); 676 | print_mips(code); 677 | } 678 | else { 679 | sprintf(code, "sw %s, %d($gp)", reg, loc - base_pointer); 680 | print_mips(code); 681 | } 682 | } 683 | } 684 | 685 | void mips_oprand(int loc, char* reg, char* op, int count) { 686 | char code[100]; 687 | int position; 688 | if (middle_code_list[loc].code[count][0] == '+' || 689 | middle_code_list[loc].code[count][0] == '-' || 690 | isdigit(middle_code_list[loc].code[count][0])) { 691 | sprintf(code, "li %s, 0x%x", reg, atoi(middle_code_list[loc].code[count])); //存入数 692 | strcpy(op, reg); 693 | print_mips(code); 694 | } 695 | else if (middle_code_list[loc].code[count][0] == '\'') { 696 | sprintf(code, "li %s, 0x%x", reg, middle_code_list[loc].code[count][1]); //存入字符的ASC码 697 | strcpy(op, reg); 698 | print_mips(code); 699 | } 700 | else if ((position = find_const(middle_code_list[loc].code[count])) >= 0) { 701 | if (strcmp(middle_code_list[position].code[1], "int") == 0) { 702 | sprintf(code, "li %s, 0x%x", reg, atoi(middle_code_list[position].code[4])); 703 | } 704 | else { 705 | sprintf(code, "li %s, 0x%x", reg, middle_code_list[position].code[4][0]); 706 | } 707 | strcpy(op, reg); 708 | print_mips(code); 709 | } 710 | else { 711 | position = find_var(middle_code_list[loc].code[count]); 712 | if (position >= 0) { 713 | position = store_info_list[position].reg_num >= 0 ? 714 | store_info_list[position].reg_num : store_info_list[position].stack_position; 715 | } 716 | if (position < 31) { 717 | if (position >= 16 && position <= 23) { 718 | sprintf(op, "$s%d", position - 16);//注意这里会不会改动 719 | } 720 | else { 721 | sprintf(op, "$t%d", position - 8); //注意这里会不会改动 722 | } 723 | } 724 | else { 725 | sprintf(code, "lw %s, %d($gp)", reg, position - base_pointer); 726 | strcpy(op, reg); 727 | print_mips(code); 728 | } 729 | } 730 | } 731 | 732 | void mips_calculate(int loc) { 733 | int position; 734 | char code[100], op1[100], op2[100]; 735 | char reg[100]; 736 | position = find_var(middle_code_list[loc].code[0]); 737 | if (position >= 0) { //注意,未来增加对于数组的操作 738 | position = store_info_list[position].reg_num >= 0 ? 739 | store_info_list[position].reg_num : store_info_list[position].stack_position; 740 | } 741 | sprintf(reg, "$t8"); 742 | mips_oprand(loc, reg, op1, 2); 743 | sprintf(reg, "$t9"); 744 | mips_oprand(loc, reg, op2, 4); 745 | if (strcmp(middle_code_list[loc].code[3], "+") == 0) { 746 | if (position <= 31 && position >= 0) { 747 | if (position >= 16 && position <= 25) { 748 | position -= 16; 749 | sprintf(code, "add $s%d, %s, %s", position, op1, op2); 750 | } 751 | else { 752 | position -= 8; 753 | sprintf(code, "add $t%d, %s, %s", position, op1, op2); 754 | } 755 | print_mips(code); 756 | } 757 | else if (position > 31) { 758 | sprintf(code, "add $t8, %s, %s", op1, op2); 759 | print_mips(code); 760 | sprintf(code, "sw $t8, %d($gp)", position - base_pointer); 761 | print_mips(code); 762 | } 763 | else { 764 | sprintf(code, "add $t8, %s, %s", op1, op2); 765 | print_mips(code); 766 | sprintf(reg, "$t8"); 767 | store_var(middle_code_list[loc].code[0], reg, 0); 768 | } 769 | } 770 | else if (strcmp(middle_code_list[loc].code[3], "-") == 0) { 771 | if (position <= 31 && position >= 0) { 772 | if (position >= 16 && position <= 25) { 773 | position -= 16; 774 | sprintf(code, "sub $s%d, %s, %s", position, op1, op2); 775 | } 776 | else { 777 | position -= 8; 778 | sprintf(code, "sub $t%d, %s, %s", position, op1, op2); 779 | } 780 | print_mips(code); 781 | } 782 | else if (position > 31) { 783 | sprintf(code, "sub $t8, %s, %s", op1, op2); 784 | print_mips(code); 785 | sprintf(code, "sw $t8, %d($gp)", position - base_pointer); 786 | print_mips(code); 787 | } 788 | else { 789 | sprintf(code, "sub $t8, %s, %s", op1, op2); 790 | print_mips(code); 791 | sprintf(reg, "$t8"); 792 | store_var(middle_code_list[loc].code[0], reg, 0); 793 | } 794 | } 795 | else if (strcmp(middle_code_list[loc].code[3], "*") == 0) { 796 | sprintf(code, "mult %s, %s", op1, op2); 797 | print_mips(code); 798 | if (position <= 31 && position >= 0) { 799 | if (position >= 16 && position <= 25) { 800 | sprintf(code, "mflo $s%d", position - 16); 801 | } 802 | else { 803 | sprintf(code, "mflo $t%d", position - 8); 804 | } 805 | print_mips(code); 806 | } 807 | else if (position > 31) { 808 | sprintf(code, "mflo $t8"); 809 | print_mips(code); 810 | sprintf(code, "sw $t8, %d($gp)", position - base_pointer); 811 | print_mips(code); 812 | } 813 | else { 814 | sprintf(code, "mflo $t8"); 815 | print_mips(code); 816 | sprintf(reg, "$t8"); 817 | store_var(middle_code_list[loc].code[0], reg, 0); 818 | } 819 | } 820 | else if (strcmp(middle_code_list[loc].code[3], "/") == 0) { 821 | sprintf(code, "div %s, %s", op1, op2); 822 | print_mips(code); 823 | if (position <= 31 && position >= 0) { 824 | if (position >= 16 && position <= 25) { 825 | sprintf(code, "mflo $s%d", position - 16); 826 | } 827 | else { 828 | sprintf(code, "mflo $t%d", position - 8); 829 | } 830 | print_mips(code); 831 | } 832 | else if (position > 31) { 833 | sprintf(code, "mflo $t8"); 834 | print_mips(code); 835 | sprintf(code, "sw $t8, %d($gp)", position - base_pointer); 836 | print_mips(code); 837 | } 838 | else { 839 | sprintf(code, "mflo $t8"); 840 | print_mips(code); 841 | sprintf(reg, "$t8"); 842 | store_var(middle_code_list[loc].code[0], reg, 0); 843 | } 844 | } 845 | } 846 | 847 | void mips_value_statement(int loc) { 848 | int position; 849 | char code[100], op[100]; 850 | char reg[100]; 851 | position = find_var(middle_code_list[loc].code[0]); 852 | if (position >= 0) { //注意,未来增加对于数组的操作 853 | position = store_info_list[position].reg_num >= 0 ? 854 | store_info_list[position].reg_num : store_info_list[position].stack_position; 855 | } 856 | sprintf(reg, "$t8"); 857 | mips_oprand(loc, reg, op, 2); 858 | if (position >= 0 && position <= 31) { 859 | if (position >= 16 && position <= 25) { 860 | sprintf(code, "move $s%d, %s", position - 16, op); 861 | } 862 | else { 863 | sprintf(code, "move $t%d, %s", position - 8, op); 864 | } 865 | print_mips(code); 866 | } 867 | else if (position > 31) { 868 | sprintf(code, "sw %s, %d($gp)", op, position - base_pointer); 869 | print_mips(code); 870 | } 871 | else { 872 | store_var(middle_code_list[loc].code[0], op, middle_code_list[loc].kind); 873 | } 874 | } 875 | 876 | void deal_token(char c) 877 | { 878 | if (isalpha(c) || c == '_') 879 | { 880 | int i = 0; 881 | s[i] = c; 882 | if (c >= 'A' && c <= 'Z') { 883 | c = c - 'A' + 'a'; 884 | } 885 | s_low[i++] = c; 886 | while ((c = getchar())) { 887 | if (!isalnum(c) && c != '_') { 888 | fseek(in, -1, SEEK_CUR); 889 | break; 890 | } 891 | s[i] = c; 892 | if (c >= 'A' && c <= 'Z') { 893 | c = c - 'A' + 'a'; 894 | } 895 | s_low[i++] = c; 896 | } 897 | s[i] = '\0'; 898 | s_low[i] = '\0'; 899 | if (strcmp(s_low, "const") == 0) { 900 | symbol = CONSTTK; 901 | } 902 | else if (strcmp(s_low, "int") == 0) { 903 | symbol = INTTK; 904 | } 905 | else if (strcmp(s_low, "char") == 0) { 906 | symbol = CHARTK; 907 | } 908 | else if (strcmp(s_low, "void") == 0) { 909 | symbol = VOIDTK; 910 | } 911 | else if (strcmp(s_low, "main") == 0) { 912 | symbol = MAINTK; 913 | } 914 | else if (strcmp(s_low, "if") == 0) { 915 | symbol = IFTK; 916 | } 917 | else if (strcmp(s_low, "else") == 0) { 918 | symbol = ELSETK; 919 | } 920 | else if (strcmp(s_low, "switch") == 0) { 921 | symbol = SWITCHTK; 922 | } 923 | else if (strcmp(s_low, "case") == 0) { 924 | symbol = CASETK; 925 | } 926 | else if (strcmp(s_low, "default") == 0) { 927 | symbol = DEFAULTTK; 928 | } 929 | else if (strcmp(s_low, "while") == 0) { 930 | symbol = WHILETK; 931 | } 932 | else if (strcmp(s_low, "for") == 0) { 933 | symbol = FORTK; 934 | } 935 | else if (strcmp(s_low, "scanf") == 0) { 936 | symbol = SCANFTK; 937 | } 938 | else if (strcmp(s_low, "printf") == 0) { 939 | symbol = PRINTFTK; 940 | } 941 | else if (strcmp(s_low, "return") == 0) { 942 | symbol = RETURNTK; 943 | } 944 | else { 945 | symbol = IDENFR; 946 | } 947 | } 948 | else if (isdigit(c)) { 949 | int i = 0; 950 | s[i++] = c; 951 | while ((c = getchar())) { 952 | if (!isdigit(c)) { 953 | fseek(in, -1, SEEK_CUR); 954 | break; 955 | } 956 | s[i++] = c; 957 | } 958 | s[i] = '\0'; 959 | symbol = INTCON; 960 | } 961 | else if (c == '\'') 962 | { 963 | int i = 0; 964 | c = getchar(); 965 | s[i++] = c; 966 | s[i] = '\0'; 967 | c = getchar(); 968 | symbol = CHARCON; 969 | } 970 | else if (c == '"') 971 | { 972 | int i = 0; 973 | while ((c = getchar())) { 974 | if (c == '"') { 975 | break; 976 | } 977 | s[i++] = c; 978 | } 979 | s[i] = '\0'; 980 | symbol = STRCON; 981 | } 982 | else if (c == '+') { 983 | symbol = PLUS; 984 | strcpy(s, "+"); 985 | } 986 | else if (c == '-') { 987 | symbol = MINU; 988 | strcpy(s, "-"); 989 | } 990 | else if (c == '*') { 991 | symbol = MULT; 992 | strcpy(s, "*"); 993 | } 994 | else if (c == '/') { 995 | symbol = DIV; 996 | strcpy(s, "/"); 997 | } 998 | else if (c == '<') { 999 | c = getchar(); 1000 | if (c == '=') { 1001 | symbol = LEQ; 1002 | strcpy(s, "<="); 1003 | } 1004 | else { 1005 | fseek(in, -1, SEEK_CUR); 1006 | symbol = LSS; 1007 | strcpy(s, "<"); 1008 | } 1009 | } 1010 | else if (c == '>') { 1011 | c = getchar(); 1012 | if (c == '=') { 1013 | symbol = GEQ; 1014 | strcpy(s, ">="); 1015 | } 1016 | else { 1017 | fseek(in, -1, SEEK_CUR); 1018 | symbol = GRE; 1019 | strcpy(s, ">"); 1020 | } 1021 | } 1022 | else if (c == '=') { 1023 | c = getchar(); 1024 | if (c == '=') { 1025 | symbol = EQL; 1026 | strcpy(s, "=="); 1027 | } 1028 | else { 1029 | fseek(in, -1, SEEK_CUR); 1030 | symbol = ASSIGN; 1031 | strcpy(s, "="); 1032 | } 1033 | } 1034 | else if (c == '!') { 1035 | c = getchar(); 1036 | if (c == '=') { 1037 | symbol = NEQ; 1038 | strcpy(s, "!="); 1039 | } 1040 | } 1041 | else if (c == ':') { 1042 | symbol = COLON; 1043 | strcpy(s, ":"); 1044 | } 1045 | else if (c == ';') { 1046 | symbol = SEMICN; 1047 | strcpy(s, ";"); 1048 | } 1049 | else if (c == ',') { 1050 | symbol = COMMA; 1051 | strcpy(s, ","); 1052 | } 1053 | else if (c == '(') { 1054 | symbol = LPARENT; 1055 | strcpy(s, "("); 1056 | } 1057 | else if (c == ')') { 1058 | symbol = RPARENT; 1059 | strcpy(s, ")"); 1060 | } 1061 | else if (c == '[') { 1062 | symbol = LBRACK; 1063 | strcpy(s, "["); 1064 | } 1065 | else if (c == ']') { 1066 | symbol = RBRACK; 1067 | strcpy(s, "]"); 1068 | } 1069 | else if (c == '{') { 1070 | symbol = LBRACE; 1071 | strcpy(s, "{"); 1072 | } 1073 | else if (c == '}') { 1074 | symbol = RBRACE; 1075 | strcpy(s, "}"); 1076 | } 1077 | } 1078 | 1079 | void getsym() { 1080 | c = getchar(); 1081 | while (isspace(c)) { 1082 | c = getchar(); 1083 | } 1084 | if (c == EOF) { 1085 | return; 1086 | } 1087 | deal_token(c); 1088 | } 1089 | 1090 | int find_return_func(char* name) 1091 | { 1092 | int i; 1093 | for (i = 0; i < has_return_func_sum; i++) 1094 | { 1095 | if (strcmp(has_return_func[i], name) == 0) 1096 | return 1; 1097 | } 1098 | return 0; 1099 | } 1100 | 1101 | int find_no_return_func(char* name) 1102 | { 1103 | int i; 1104 | for (i = 0; i < no_return_func_sum; i++) 1105 | { 1106 | if (strcmp(no_return_func[i], name) == 0) 1107 | return 1; 1108 | } 1109 | return 0; 1110 | } 1111 | 1112 | struct return_info judge_string() 1113 | { 1114 | struct return_info tmp = { 0, 1 }; 1115 | if (symbol != STRCON) 1116 | return fail; 1117 | strcpy(tmp.code[0], s); 1118 | getsym(); 1119 | //printf("<字符串>\n"); 1120 | return tmp; 1121 | } 1122 | 1123 | struct return_info judge_program() 1124 | { 1125 | struct return_info r; 1126 | r = judge_const_inform(); 1127 | if (r.return_num < 0) 1128 | return r; 1129 | r = judge_var_inform(); 1130 | if (r.return_num < 0) 1131 | return r; 1132 | while (1) 1133 | { 1134 | if (judge_function_has_return().return_num < 0) 1135 | { 1136 | if (judge_function_no_return().return_num < 0) 1137 | { 1138 | break; 1139 | } 1140 | } 1141 | } 1142 | r = judge_main_function(); 1143 | if (r.return_num < 0) 1144 | return r; 1145 | //printf("<程序>\n"); 1146 | return zero; 1147 | } 1148 | 1149 | struct return_info judge_const_inform() 1150 | { 1151 | struct return_info r; 1152 | if (symbol != CONSTTK) { 1153 | return zero; //这里不应该返回负数,否则就直接退了 1154 | } 1155 | while (symbol == CONSTTK) 1156 | { 1157 | //print_code(); 1158 | getsym(); 1159 | r = judge_const_define(); 1160 | if (r.return_num < 0) 1161 | return r; 1162 | //print_code(); 1163 | getsym(); 1164 | } 1165 | //printf("<常量说明>\n"); 1166 | return zero; 1167 | } 1168 | 1169 | struct return_info judge_const_define() 1170 | { 1171 | struct return_info r; 1172 | int loc; 1173 | char identify[100]; 1174 | if (symbol == INTTK) 1175 | { 1176 | //print_code(); 1177 | getsym(); //读标识符 1178 | if ((loc = find_name(s_low)) == -1) { 1179 | strcpy(name_list[name_sum].name, s_low); 1180 | strcpy(identify, s_low); //记录下标识符 1181 | name_list[name_sum].type = 0; 1182 | name_list[name_sum++].use = 0; 1183 | } 1184 | else { 1185 | name_list[loc].new_type = 0; 1186 | name_list[loc].use = 1; 1187 | sprintf(name_list[loc].new_name, "def%d", reg_index++); 1188 | strcpy(identify, name_list[loc].new_name); //记录下标识符 1189 | //注意,这种情况目前不确定是否要改动 1190 | } 1191 | //print_code(); 1192 | getsym(); //读等号 1193 | //print_code(); 1194 | getsym(); //读整数 1195 | r = judge_integer(); 1196 | if (r.return_num < 0) 1197 | return r; 1198 | middle_code_list[middle_code_sum++] = middle_code_generate( 1199 | CONSTANT_DECLARATIONS, 0, 5, "const", "int", identify, "=", r.code[0]); 1200 | while (symbol == COMMA) //不出意外的话,逗号在前面读了 1201 | { 1202 | //print_code(); 1203 | getsym(); //读标识符 1204 | if ((loc = find_name(s_low)) == -1) { 1205 | strcpy(name_list[name_sum].name, s_low); 1206 | strcpy(identify, s_low); //记录下标识符 1207 | name_list[name_sum].type = 0; 1208 | name_list[name_sum++].use = 0; 1209 | } 1210 | else { 1211 | name_list[loc].new_type = 0; 1212 | name_list[loc].use = 1; 1213 | sprintf(name_list[loc].new_name, "def%d", reg_index++); 1214 | strcpy(identify, name_list[loc].new_name); //记录下标识符 1215 | //注意,这种情况目前不确定是否要改动 1216 | } 1217 | //print_code(); 1218 | getsym(); //读等号 1219 | //print_code(); 1220 | getsym(); //读整数 1221 | r = judge_integer(); 1222 | if (r.return_num < 0) 1223 | return r; 1224 | middle_code_list[middle_code_sum++] = middle_code_generate( 1225 | CONSTANT_DECLARATIONS, 0, 5, "const", "int", identify, "=", r.code[0]); 1226 | } 1227 | } 1228 | else if (symbol == CHARTK) //???可否有else 1229 | { 1230 | //print_code(); 1231 | getsym(); //读标识符 1232 | if ((loc = find_name(s_low)) == -1) { 1233 | strcpy(name_list[name_sum].name, s_low); 1234 | strcpy(identify, s_low); //记录下标识符 1235 | name_list[name_sum].type = 1; 1236 | name_list[name_sum++].use = 0; 1237 | } 1238 | else { 1239 | name_list[loc].new_type = 1; 1240 | name_list[loc].use = 1; 1241 | sprintf(name_list[loc].new_name, "def%d", reg_index++); 1242 | strcpy(identify, name_list[loc].new_name); //记录下标识符 1243 | //注意,这种情况目前不确定是否要改动 1244 | } 1245 | //print_code(); 1246 | getsym(); //读等号 1247 | //print_code(); 1248 | getsym(); //读字符 1249 | middle_code_list[middle_code_sum++] = middle_code_generate( 1250 | CONSTANT_DECLARATIONS, 1, 5, "const", "char", identify, "=", s); 1251 | //print_code(); 1252 | getsym(); //读逗号,因为并没有处理逗号的函数 1253 | while (symbol == COMMA) //不出意外的话,逗号在前面读了 1254 | { 1255 | //print_code(); 1256 | getsym(); //读标识符 1257 | if ((loc = find_name(s_low)) == -1) { 1258 | strcpy(name_list[name_sum].name, s_low); 1259 | strcpy(identify, s_low); //记录下标识符 1260 | name_list[name_sum].type = 1; 1261 | name_list[name_sum++].use = 0; 1262 | } 1263 | else { 1264 | name_list[loc].new_type = 1; 1265 | name_list[loc].use = 1; 1266 | sprintf(name_list[loc].new_name, "def%d", reg_index++); 1267 | strcpy(identify, name_list[loc].new_name); //记录下标识符 1268 | //注意,这种情况目前不确定是否要改动 1269 | } 1270 | //print_code(); 1271 | getsym(); //读等号 1272 | //print_code(); 1273 | getsym(); //读字符 1274 | middle_code_list[middle_code_sum++] = middle_code_generate( 1275 | CONSTANT_DECLARATIONS, 1, 5, "const", "char", identify, "=", s); 1276 | //print_code(); 1277 | getsym(); //读逗号,因为并没有处理逗号的函数,若不是逗号,那么读sym并跳出 1278 | } 1279 | } 1280 | //printf("<常量定义>\n"); 1281 | return zero; 1282 | } 1283 | 1284 | struct return_info judge_unsigned_integer() 1285 | { 1286 | if (symbol != INTCON) { 1287 | return fail; 1288 | } 1289 | struct return_info tmp = { 0, 1 }; 1290 | strcpy(tmp.code[0], s); 1291 | //print_code(); //输出数 1292 | getsym(); //读下一个 1293 | //printf("<无符号整数>\n"); 1294 | return tmp; 1295 | } 1296 | 1297 | struct return_info judge_integer() 1298 | { 1299 | struct return_info r, tmp = { 0, 1 }; 1300 | 1301 | if (symbol == PLUS || symbol == MINU) //???是否需要判断是不是char常量 1302 | { 1303 | //print_code(); //输出符号 1304 | strcpy(tmp.code[0], s); 1305 | getsym(); //读数 1306 | } 1307 | r = judge_unsigned_integer(); 1308 | if (r.return_num < 0) 1309 | return r; 1310 | strcat(tmp.code[0], r.code[0]); 1311 | return tmp; 1312 | //printf("<整数>\n"); 1313 | 1314 | } 1315 | 1316 | struct return_info judge_head() 1317 | { 1318 | if ((flag1 == 1 && pre1 != INTTK && pre1 != CHARTK) 1319 | || (flag1 == 0 && symbol != INTTK && symbol != CHARTK)) { 1320 | return fail; 1321 | } 1322 | if (flag1 == 1) { 1323 | flag1 = 0; 1324 | //print_code_pre1(); 1325 | } 1326 | else { 1327 | //print_code(); 1328 | getsym(); 1329 | } 1330 | if (flag2 == 1) { 1331 | flag2 = 0; 1332 | strcpy(has_return_func[has_return_func_sum++], content2); 1333 | //print_code_pre2(); //输出标识符 1334 | } 1335 | else { 1336 | strcpy(has_return_func[has_return_func_sum++], s); 1337 | //print_code(); //输出标识符 1338 | getsym(); 1339 | } 1340 | //printf("<声明头部>\n"); 1341 | return zero; 1342 | } 1343 | 1344 | struct return_info judge_const() 1345 | { 1346 | struct return_info r, tmp = { 0, 1, 0 }; 1347 | if ((r = judge_integer()).return_num == 0) 1348 | { 1349 | //printf("<常量>\n"); 1350 | strcpy(tmp.code[0], r.code[0]); 1351 | return tmp; 1352 | } 1353 | else if (s[0] == '+' || s[0] == '-' || s[0] == '*' || s[0] == '/' || (s[0] >= '0' && s[0] <= '9') 1354 | || (s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z') || s[0] == '_') 1355 | { 1356 | //print_code(); //输出这个字符 1357 | strcpy(tmp.code[0], s); 1358 | getsym(); 1359 | //printf("<常量>\n"); 1360 | return tmp; 1361 | } 1362 | else 1363 | return fail; 1364 | } 1365 | 1366 | struct return_info judge_var_inform() 1367 | { 1368 | struct return_info r; 1369 | int sum = 0; 1370 | while (1) 1371 | { 1372 | if (symbol != INTTK && symbol != CHARTK) 1373 | { 1374 | break; 1375 | } 1376 | pre1 = symbol; 1377 | strcpy(content1, s_low); 1378 | flag1 = 1; 1379 | getsym(); 1380 | pre2 = symbol; 1381 | strcpy(content2, s_low); 1382 | flag2 = 1; 1383 | getsym(); //注意,下面接的有可能是无返回值函数定义,也有可能是有返回值函数定义,也有可能是个变量,也有可能是主函数 1384 | if (symbol != LBRACK && symbol != COMMA && symbol != SEMICN && symbol != ASSIGN) 1385 | { 1386 | break; 1387 | } 1388 | r = judge_var_define(); 1389 | if (r.return_num < 0) 1390 | return r; 1391 | sum++; 1392 | //print_code(); 1393 | getsym(); 1394 | } 1395 | if (sum == 0) 1396 | return zero; //如果为0,说明没有变量说明 1397 | //printf("<变量说明>\n"); 1398 | return zero; 1399 | } 1400 | 1401 | struct return_info judge_var_define() 1402 | { 1403 | struct return_info r; 1404 | int loc; 1405 | int flag = 0; 1406 | int type = (pre1 == INTTK) ? 0 : 1; 1407 | char identity[100], type_string[100]; 1408 | strcpy(type_string, content1); 1409 | //print_code_pre1(); //输出类型标识符 1410 | flag1 = 0; 1411 | //print_code_pre2(); //输出标识符 1412 | if ((loc = find_name(content2)) == -1) { 1413 | strcpy(name_list[name_sum].name, content2); 1414 | name_list[name_sum].use = 0; 1415 | name_list[name_sum++].type = type; 1416 | strcpy(identity, content2); 1417 | } 1418 | else { 1419 | name_list[loc].new_type = type; 1420 | name_list[loc].use = 1; 1421 | sprintf(name_list[loc].new_name, "def%d", reg_index++); 1422 | strcpy(identity, name_list[loc].new_name); 1423 | //注意,这里可能要进行改动 1424 | } 1425 | flag2 = 0; 1426 | if (symbol == LBRACK) 1427 | { 1428 | //print_code(); //输出[ 1429 | getsym(); 1430 | r = judge_unsigned_integer(); 1431 | if (r.return_num < 0) 1432 | return r; 1433 | //print_code(); //输出] 1434 | getsym(); 1435 | if (symbol == LBRACK) 1436 | { 1437 | //print_code(); //输出[ 1438 | getsym(); 1439 | r = judge_unsigned_integer(); 1440 | if (r.return_num < 0) 1441 | return r; 1442 | //print_code(); //输出] 1443 | getsym(); 1444 | if (symbol == ASSIGN) 1445 | { 1446 | flag = 1; 1447 | //print_code(); //输出= 1448 | getsym(); 1449 | //print_code(); //输出{ 1450 | getsym(); 1451 | //print_code(); //输出{ 1452 | getsym(); 1453 | r = judge_const(); 1454 | if (r.return_num < 0) 1455 | return r; 1456 | while (symbol == COMMA) 1457 | { 1458 | //print_code(); //输出, 1459 | getsym(); 1460 | r = judge_const(); 1461 | if (r.return_num < 0) 1462 | return r; 1463 | } 1464 | //print_code(); //输出} 1465 | getsym(); 1466 | while (symbol == COMMA) 1467 | { 1468 | //print_code(); //输出, 1469 | getsym(); 1470 | //print_code(); //输出{ 1471 | getsym(); 1472 | r = judge_const(); 1473 | if (r.return_num < 0) 1474 | return r; 1475 | while (symbol == COMMA) 1476 | { 1477 | //print_code(); //输出, 1478 | getsym(); 1479 | r = judge_const(); 1480 | if (r.return_num < 0) 1481 | return r; 1482 | } 1483 | //print_code(); //输出} 1484 | getsym(); 1485 | } 1486 | //print_code(); //输出} 1487 | getsym(); 1488 | } 1489 | } 1490 | else if (symbol == ASSIGN) 1491 | { 1492 | flag = 1; 1493 | //print_code(); //输出= 1494 | getsym(); 1495 | //print_code(); //输出{ 1496 | getsym(); 1497 | r = judge_const(); 1498 | if (r.return_num < 0) 1499 | return r; 1500 | while (symbol == COMMA) 1501 | { 1502 | //print_code(); //输出, 1503 | getsym(); 1504 | r = judge_const(); 1505 | if (r.return_num < 0) 1506 | return r; 1507 | } 1508 | //print_code(); //输出} 1509 | getsym(); 1510 | } 1511 | } 1512 | else if (symbol == ASSIGN) 1513 | { 1514 | flag = 1; 1515 | //print_code(); //输出= 1516 | getsym(); 1517 | r = judge_const(); 1518 | if (r.return_num < 0) 1519 | return r; 1520 | } 1521 | if (flag == 0) 1522 | middle_code_list[middle_code_sum++] = middle_code_generate( 1523 | VARIABLE_DECLARATIONS, type, 3, "var", type_string, identity); 1524 | else 1525 | middle_code_list[middle_code_sum++] = middle_code_generate( 1526 | VARIABLE_DECLARATIONS, type, 5, "var", type_string, identity, "=", r.code[0]); 1527 | while (symbol == COMMA) 1528 | { 1529 | //print_code(); //输出, 1530 | getsym(); 1531 | //print_code(); //输出标识符 1532 | if ((loc = find_name(s_low)) == -1) { 1533 | strcpy(name_list[name_sum].name, s_low); 1534 | name_list[name_sum].use = 0; 1535 | name_list[name_sum++].type = type; 1536 | strcpy(identity, s_low); 1537 | } 1538 | else { 1539 | name_list[loc].new_type = type; 1540 | name_list[loc].use = 1; 1541 | sprintf(name_list[loc].new_name, "def%d", reg_index++); 1542 | strcpy(identity, name_list[loc].new_name); 1543 | //注意,这里可能要进行改动 1544 | } 1545 | getsym(); 1546 | if (symbol == LBRACK) 1547 | { 1548 | //print_code(); //输出[ 1549 | getsym(); 1550 | r = judge_unsigned_integer(); 1551 | if (r.return_num < 0) 1552 | return r; 1553 | //print_code(); //输出] 1554 | getsym(); 1555 | if (symbol == LBRACK) 1556 | { 1557 | //print_code(); //输出[ 1558 | getsym(); 1559 | r = judge_unsigned_integer(); 1560 | if (r.return_num < 0) 1561 | return r; 1562 | //print_code(); //输出] 1563 | getsym(); 1564 | if (symbol == ASSIGN) 1565 | { 1566 | flag = 1; 1567 | //print_code(); //输出= 1568 | getsym(); 1569 | //print_code(); //输出{ 1570 | getsym(); 1571 | //print_code(); //输出{ 1572 | getsym(); 1573 | r = judge_const(); 1574 | if (r.return_num < 0) 1575 | return r; 1576 | while (symbol == COMMA) 1577 | { 1578 | //print_code(); //输出, 1579 | getsym(); 1580 | r = judge_const(); 1581 | if (r.return_num < 0) 1582 | return r; 1583 | } 1584 | //print_code(); //输出} 1585 | getsym(); 1586 | while (symbol == COMMA) 1587 | { 1588 | //print_code(); //输出, 1589 | getsym(); 1590 | //print_code(); //输出{ 1591 | getsym(); 1592 | r = judge_const(); 1593 | if (r.return_num < 0) 1594 | return r; 1595 | while (symbol == COMMA) 1596 | { 1597 | //print_code(); //输出, 1598 | getsym(); 1599 | r = judge_const(); 1600 | if (r.return_num < 0) 1601 | return r; 1602 | } 1603 | //print_code(); //输出} 1604 | getsym(); 1605 | } 1606 | //print_code(); //输出} 1607 | getsym(); 1608 | } 1609 | } 1610 | else if (symbol == ASSIGN) 1611 | { 1612 | flag = 1; 1613 | //print_code(); //输出= 1614 | getsym(); 1615 | //print_code(); //输出{ 1616 | getsym(); 1617 | r = judge_const(); 1618 | if (r.return_num < 0) 1619 | return r; 1620 | while (symbol == COMMA) 1621 | { 1622 | //print_code(); //输出, 1623 | getsym(); 1624 | r = judge_const(); 1625 | if (r.return_num < 0) 1626 | return r; 1627 | } 1628 | //print_code(); //输出} 1629 | getsym(); 1630 | } 1631 | } 1632 | else if (symbol == ASSIGN) 1633 | { 1634 | flag = 1; 1635 | //print_code(); //输出= 1636 | getsym(); 1637 | r = judge_const(); 1638 | if (r.return_num < 0) 1639 | return r; 1640 | } 1641 | if (flag == 0) 1642 | middle_code_list[middle_code_sum++] = middle_code_generate( 1643 | VARIABLE_DECLARATIONS, type, 3, "var", type_string, identity); 1644 | else 1645 | middle_code_list[middle_code_sum++] = middle_code_generate( 1646 | VARIABLE_DECLARATIONS, type, 5, "var", type_string, identity, "=", r.code[0]); 1647 | //??? 1648 | } 1649 | /*if (flag) 1650 | printf("<变量定义及初始化>\n"); 1651 | else 1652 | printf("<变量定义无初始化>\n"); 1653 | printf("<变量定义>\n");*/ 1654 | return zero; 1655 | } 1656 | 1657 | struct return_info judge_function_has_return() 1658 | { 1659 | struct return_info r; 1660 | r = judge_head(); 1661 | if (r.return_num < 0) 1662 | return r; 1663 | //print_code(); //输出( 1664 | getsym(); 1665 | r = judge_parameter_list(); 1666 | if (r.return_num < 0) 1667 | return r; 1668 | //print_code(); //输出) 1669 | getsym(); 1670 | //print_code(); //输出{ 1671 | getsym(); 1672 | r = judge_mix_statement(); 1673 | if (r.return_num < 0) 1674 | return r; 1675 | //print_code(); //输出} 1676 | getsym(); 1677 | //printf("<有返回值函数定义>\n"); 1678 | return zero; 1679 | } 1680 | 1681 | struct return_info judge_function_no_return() 1682 | { 1683 | struct return_info r; 1684 | pre1 = symbol; 1685 | strcpy(content1, s_low); 1686 | flag1 = 1; 1687 | getsym(); 1688 | if (symbol == MAINTK) 1689 | return fail; 1690 | //print_code_pre1(); //输出void 1691 | flag1 = 0; 1692 | strcpy(no_return_func[no_return_func_sum++], s_low); 1693 | //print_code(); //输出标识符 1694 | getsym(); 1695 | //print_code(); //输出( 1696 | getsym(); 1697 | r = judge_parameter_list(); 1698 | if (r.return_num < 0) 1699 | return r; 1700 | //print_code(); //输出) 1701 | getsym(); 1702 | //print_code(); //输出{ 1703 | getsym(); 1704 | r = judge_mix_statement(); 1705 | if (r.return_num < 0) 1706 | return r; 1707 | //print_code(); //输出} 1708 | getsym(); 1709 | //printf("<无返回值函数定义>\n"); 1710 | return zero; 1711 | } 1712 | 1713 | struct return_info judge_mix_statement() 1714 | { 1715 | struct return_info r; 1716 | r = judge_const_inform(); 1717 | if (r.return_num < 0) 1718 | return r; 1719 | r = judge_var_inform(); 1720 | if (r.return_num < 0) 1721 | return r; 1722 | r = judge_statement_list(); 1723 | if (r.return_num < 0) 1724 | return r; 1725 | //printf("<复合语句>\n"); 1726 | return zero; 1727 | } 1728 | 1729 | struct return_info judge_parameter_list() 1730 | { 1731 | if (symbol == RPARENT) //空的情况 1732 | { 1733 | //printf("<参数表>\n"); 1734 | return zero; 1735 | } 1736 | //print_code(); //输出int或char 1737 | getsym(); 1738 | //print_code(); //输出标识符 1739 | getsym(); 1740 | while (symbol == COMMA) 1741 | { 1742 | //print_code(); //输出, 1743 | getsym(); 1744 | //print_code(); //输出int或char 1745 | getsym(); 1746 | //print_code(); //输出标识符 1747 | getsym(); 1748 | } 1749 | //printf("<参数表>\n"); 1750 | return zero; 1751 | } 1752 | 1753 | struct return_info judge_main_function() 1754 | { 1755 | struct return_info r; 1756 | if (flag1 == 1) 1757 | { 1758 | flag1 = 0; 1759 | //print_code_pre1(); 1760 | } 1761 | else 1762 | { 1763 | //print_code(); 1764 | getsym(); 1765 | }//两种情况决定输出void 1766 | //print_code(); //输出main 1767 | getsym(); 1768 | //print_code(); //输出( 1769 | getsym(); 1770 | //print_code(); //输出) 1771 | getsym(); 1772 | main_location = middle_code_sum; 1773 | middle_code_list[middle_code_sum++] = 1774 | middle_code_generate(MAIN, 0, 1, "main"); 1775 | //print_code(); //输出{ 1776 | getsym(); 1777 | r = judge_mix_statement(); 1778 | if (r.return_num < 0) 1779 | return r; 1780 | //print_code(); //输出} 1781 | //到最后啦,不用读了 1782 | //printf("<主函数>\n"); 1783 | return zero; 1784 | } 1785 | 1786 | struct return_info judge_expression() 1787 | { 1788 | struct return_info r, ret = { 0, 0, 0 }; 1789 | char y[100], z[100], op[10], x[100]; 1790 | int flag = 0; 1791 | if (symbol == PLUS || symbol == MINU) 1792 | { 1793 | strcpy(op, s); 1794 | flag = 1; 1795 | //print_code(); 1796 | getsym(); 1797 | } 1798 | r = judge_item(); 1799 | if (r.return_num < 0) 1800 | return r; 1801 | ret.return_num = r.return_num; 1802 | if (flag) { 1803 | ret.return_num = 1; 1804 | strcpy(z, r.code[0]); 1805 | sprintf(x, "reg%d", reg_index++); 1806 | middle_code_list[middle_code_sum++] = middle_code_generate( 1807 | CALCULATE, 0, 5, x, "=", "0", op, z); 1808 | strcpy(y, x); 1809 | } 1810 | else { 1811 | strcpy(y, r.code[0]); 1812 | ret.type = r.type; 1813 | } 1814 | while (symbol == PLUS || symbol == MINU) 1815 | { 1816 | ret.return_num = 1; 1817 | ret.type = 0; 1818 | strcpy(op, s); 1819 | //print_code(); 1820 | getsym(); 1821 | r = judge_item(); 1822 | if (r.return_num < 0) 1823 | return r; 1824 | strcpy(z, r.code[0]); 1825 | sprintf(x, "reg%d", reg_index++); 1826 | middle_code_list[middle_code_sum++] = middle_code_generate( 1827 | CALCULATE, 0, 5, x, "=", y, op, z); 1828 | strcpy(y, x); 1829 | } 1830 | strcpy(ret.code[0], y); 1831 | //printf("<表达式>\n"); 1832 | return ret; 1833 | } 1834 | 1835 | struct return_info judge_item() 1836 | { 1837 | struct return_info r, ret = { 0 }; 1838 | char y[100], z[100], op[10], x[100]; 1839 | r = judge_factor(); 1840 | if (r.return_num < 0) 1841 | return r; 1842 | strcpy(y, r.code[0]); 1843 | ret.type = r.type; 1844 | ret.return_num = r.return_num; 1845 | while (symbol == MULT || symbol == DIV) 1846 | { 1847 | //print_code(); 1848 | ret.return_num = 1; 1849 | ret.type = 0; 1850 | strcpy(op, s); 1851 | getsym(); 1852 | r = judge_factor(); 1853 | if (r.return_num < 0) 1854 | return r; 1855 | strcpy(z, r.code[0]); 1856 | sprintf(x, "reg%d", reg_index++); 1857 | middle_code_list[middle_code_sum++] = middle_code_generate( 1858 | CALCULATE, 0, 5, x, "=", y, op, z); 1859 | strcpy(y, x); 1860 | } 1861 | strcpy(ret.code[0], y); 1862 | //printf("<项>\n"); 1863 | return ret; 1864 | } 1865 | 1866 | struct return_info judge_factor() 1867 | { 1868 | struct return_info r, ret = { 0, 1, 0 }; 1869 | int loc; 1870 | char identify[100]; 1871 | if (judge_returnfunc_use().return_num == 0) 1872 | { 1873 | //应该不用干什么 1874 | } 1875 | else if (symbol == IDENFR) 1876 | { 1877 | //print_code(); //输出标识符 1878 | loc = find_name(s_low); 1879 | if (name_list[loc].use == 1) { 1880 | strcpy(identify, name_list[loc].new_name); 1881 | ret.type = name_list[loc].new_type; 1882 | } 1883 | else { 1884 | strcpy(identify, s_low); 1885 | ret.type = name_list[loc].type; 1886 | } 1887 | getsym(); 1888 | while (symbol == LBRACK) 1889 | { 1890 | //print_code(); //输出[ 1891 | getsym(); 1892 | r = judge_expression(); 1893 | if (r.return_num < 0) 1894 | return r; 1895 | //print_code(); //输出] 1896 | getsym(); 1897 | } 1898 | strcpy(ret.code[0], identify); //注意,目前不需要判断数组的情况 1899 | } 1900 | else if (symbol == LPARENT) 1901 | { 1902 | //print_code(); //输出( 1903 | getsym(); 1904 | r = judge_expression(); 1905 | if (r.return_num < 0) 1906 | return r; 1907 | //print_code(); //输出) 1908 | strcpy(ret.code[0], r.code[0]); 1909 | getsym(); 1910 | } 1911 | else if (symbol == CHARCON) 1912 | { 1913 | //print_code(); //输出字符 1914 | char charcon[10]; 1915 | sprintf(charcon, "\'%s\'", s_low); 1916 | strcpy(ret.code[0], charcon); 1917 | ret.type = 1; 1918 | getsym(); 1919 | } 1920 | else if ((r = judge_integer()).return_num == 0) 1921 | { 1922 | strcpy(ret.code[0], r.code[0]); 1923 | //应该不用干什么 1924 | } 1925 | else { 1926 | return fail; 1927 | } 1928 | //printf("<因子>\n"); 1929 | return ret; 1930 | } 1931 | 1932 | struct return_info judge_statement() 1933 | { 1934 | struct return_info r; 1935 | if (symbol == SEMICN) 1936 | { //空的情况 1937 | //print_code(); //输出; 1938 | getsym(); 1939 | } 1940 | else if (judge_circulation_statement().return_num == 0) 1941 | { 1942 | //应该不用干什么 1943 | } 1944 | else if (judge_condition_statement().return_num == 0) 1945 | { 1946 | //应该不用干什么 1947 | } 1948 | else if (judge_return_statement().return_num == 0) 1949 | { 1950 | //print_code(); //输出; 1951 | getsym(); 1952 | } 1953 | else if (judge_returnfunc_use().return_num == 0) 1954 | { 1955 | //print_code(); //输出; 1956 | getsym(); 1957 | } 1958 | else if (judge_noreturnfunc_use().return_num == 0) 1959 | { 1960 | //print_code(); //输出; 1961 | getsym(); 1962 | } 1963 | else if (judge_value_statement().return_num == 0) 1964 | { 1965 | //print_code(); //输出; 1966 | getsym(); 1967 | } 1968 | else if (judge_scanf().return_num == 0) 1969 | { 1970 | //print_code(); //输出; 1971 | getsym(); 1972 | } 1973 | else if (judge_printf().return_num == 0) 1974 | { 1975 | //print_code(); //输出; 1976 | getsym(); 1977 | } 1978 | else if (judge_case_statement().return_num == 0) 1979 | { 1980 | //应该不用干什么 1981 | } 1982 | else if (symbol == LBRACE) 1983 | { 1984 | //print_code(); //输出{ 1985 | getsym(); 1986 | r = judge_statement_list(); 1987 | if (r.return_num < 0) 1988 | return r; 1989 | //print_code(); //输出} 1990 | getsym(); 1991 | } 1992 | else 1993 | return fail; 1994 | //printf("<语句>\n"); 1995 | return zero; 1996 | } 1997 | 1998 | struct return_info judge_value_statement() 1999 | { 2000 | struct return_info r; 2001 | char identity[100], x[100], y[100]; 2002 | int loc; 2003 | if (symbol != IDENFR) 2004 | { 2005 | return fail; 2006 | } 2007 | loc = find_name(s_low); 2008 | if (name_list[loc].use == 1) { 2009 | strcpy(identity, name_list[loc].new_name); 2010 | } 2011 | else { 2012 | strcpy(identity, name_list[loc].name); 2013 | } 2014 | //print_code(); //输出标识符 2015 | getsym(); 2016 | while (symbol == LBRACK) 2017 | { 2018 | //print_code(); //输出[ 2019 | getsym(); 2020 | r = judge_expression(); 2021 | if (r.return_num < 0) 2022 | return r; 2023 | //print_code(); //输出] 2024 | getsym(); 2025 | } 2026 | //print_code(); //输出= 2027 | getsym(); 2028 | r = judge_expression(); 2029 | if (r.return_num < 0) 2030 | return r; 2031 | strcpy(y, r.code[0]); 2032 | if (r.return_num == 1) { 2033 | strcpy(middle_code_list[middle_code_sum - 1].code[0], identity); 2034 | } 2035 | else { 2036 | middle_code_list[middle_code_sum++] = middle_code_generate( 2037 | VALUE_STATEMENT, r.type, 3, identity, "=", y); 2038 | } 2039 | //printf("<赋值语句>\n"); 2040 | return zero; 2041 | } 2042 | 2043 | struct return_info judge_condition_statement() 2044 | { 2045 | struct return_info r; 2046 | if (symbol != IFTK) 2047 | return fail; 2048 | //print_code(); //输出if 2049 | getsym(); 2050 | //print_code(); //输出( 2051 | getsym(); 2052 | r = judge_condition(); 2053 | if (r.return_num < 0) 2054 | return r; 2055 | //print_code(); //输出) 2056 | getsym(); 2057 | r = judge_statement(); 2058 | if (r.return_num < 0) 2059 | return r; 2060 | if (symbol == ELSETK) 2061 | { 2062 | //print_code();//输出else 2063 | getsym(); 2064 | r = judge_statement(); 2065 | if (r.return_num < 0) 2066 | return r; 2067 | } 2068 | //printf("<条件语句>\n"); 2069 | return zero; 2070 | } 2071 | 2072 | struct return_info judge_condition() 2073 | { 2074 | struct return_info r; 2075 | r = judge_expression(); 2076 | if (r.return_num < 0) 2077 | return r; 2078 | //print_code(); //输出关系运算符 2079 | getsym(); 2080 | r = judge_expression(); 2081 | if (r.return_num < 0) 2082 | return r; 2083 | //printf("<条件>\n"); 2084 | return zero; 2085 | } 2086 | 2087 | struct return_info judge_circulation_statement() 2088 | { 2089 | struct return_info r; 2090 | if (symbol != WHILETK && symbol != FORTK) 2091 | return fail; 2092 | if (symbol == WHILETK) 2093 | { 2094 | //print_code();//输出while 2095 | getsym(); 2096 | //print_code(); //输出( 2097 | getsym(); 2098 | r = judge_condition(); 2099 | if (r.return_num < 0) 2100 | return r; 2101 | //print_code(); //输出) 2102 | getsym(); 2103 | r = judge_statement(); 2104 | if (r.return_num < 0) 2105 | return r; 2106 | } 2107 | else if (symbol == FORTK) 2108 | { 2109 | //print_code(); //输出for 2110 | getsym(); 2111 | //print_code(); //输出( 2112 | getsym(); 2113 | //print_code(); //输出标识符 2114 | getsym(); 2115 | //print_code(); //输出= 2116 | getsym(); 2117 | r = judge_expression(); 2118 | if (r.return_num < 0) 2119 | return r; 2120 | //print_code(); //输出; 2121 | getsym(); 2122 | r = judge_condition(); 2123 | if (r.return_num < 0) 2124 | return r; 2125 | //print_code(); //输出; 2126 | getsym(); 2127 | //print_code(); //输出标识符 2128 | getsym(); 2129 | //print_code(); //输出= 2130 | getsym(); 2131 | //print_code(); //输出标识符 2132 | getsym(); 2133 | //print_code(); //输出+或- 2134 | getsym(); 2135 | r = judge_stepsize(); 2136 | if (r.return_num < 0) 2137 | return r; 2138 | //print_code(); //输出) 2139 | getsym(); 2140 | r = judge_statement(); 2141 | if (r.return_num < 0) 2142 | return r; 2143 | } 2144 | //printf("<循环语句>\n"); 2145 | return zero; 2146 | } 2147 | 2148 | struct return_info judge_stepsize() 2149 | { 2150 | struct return_info r; 2151 | r = judge_unsigned_integer(); 2152 | if (r.return_num < 0) 2153 | return r; 2154 | //printf("<步长>\n"); 2155 | return zero; 2156 | } 2157 | 2158 | struct return_info judge_case_statement() 2159 | { 2160 | struct return_info r; 2161 | if (symbol != SWITCHTK) 2162 | return fail; 2163 | //print_code(); //输出switch 2164 | getsym(); 2165 | //print_code(); //输出( 2166 | getsym(); 2167 | r = judge_expression(); 2168 | if (r.return_num < 0) 2169 | return r; 2170 | //print_code(); //输出) 2171 | getsym(); 2172 | //print_code(); //输出{ 2173 | getsym(); 2174 | r = judge_case_list(); 2175 | if (r.return_num < 0) 2176 | return r; 2177 | r = judge_default(); 2178 | if (r.return_num < 0) 2179 | return r; 2180 | //print_code(); //输出} 2181 | getsym(); 2182 | //printf("<情况语句>\n"); 2183 | return zero; 2184 | } 2185 | 2186 | struct return_info judge_case_list() 2187 | { 2188 | struct return_info r; 2189 | r = judge_case_son_statement(); 2190 | if (r.return_num < 0) 2191 | return r; 2192 | while (judge_case_son_statement().return_num == 0) 2193 | { 2194 | //应该不需要干啥。。。 2195 | } 2196 | //printf("<情况表>\n"); 2197 | return zero; 2198 | } 2199 | 2200 | struct return_info judge_case_son_statement() 2201 | { 2202 | struct return_info r; 2203 | if (symbol != CASETK) 2204 | return fail; 2205 | //print_code(); //输出case 2206 | getsym(); 2207 | r = judge_const(); 2208 | if (r.return_num < 0) 2209 | return r; 2210 | //print_code();//输出: 2211 | getsym(); 2212 | r = judge_statement(); 2213 | if (r.return_num < 0) 2214 | return r; 2215 | //printf("<情况子语句>\n"); 2216 | return zero; 2217 | } 2218 | 2219 | struct return_info judge_default() 2220 | { 2221 | struct return_info r; 2222 | if (symbol != DEFAULTTK) 2223 | return fail; 2224 | //print_code(); //输出default 2225 | getsym(); 2226 | //print_code(); //输出: 2227 | getsym(); 2228 | r = judge_statement(); 2229 | if (r.return_num < 0) 2230 | return r; 2231 | //printf("<缺省>\n"); 2232 | return zero; 2233 | } 2234 | 2235 | struct return_info judge_returnfunc_use() 2236 | { 2237 | struct return_info r; 2238 | char tmp[1000]; 2239 | if (symbol != IDENFR) 2240 | return fail; 2241 | strcpy(tmp, s); 2242 | if (find_return_func(tmp) == 0) 2243 | return fail; 2244 | //print_code(); //输出标识符 2245 | getsym(); 2246 | //print_code(); //输出( 2247 | getsym(); 2248 | r = judge_value_parameter_list(); 2249 | if (r.return_num < 0) 2250 | return r; 2251 | //print_code(); //输出) 2252 | getsym(); 2253 | //printf("<有返回值函数调用语句>\n"); 2254 | return zero; 2255 | } 2256 | 2257 | struct return_info judge_noreturnfunc_use() 2258 | { 2259 | struct return_info r; 2260 | char tmp[1000]; 2261 | if (symbol != IDENFR) 2262 | return fail; 2263 | strcpy(tmp, s); 2264 | if (find_no_return_func(tmp) == 0) 2265 | return fail; 2266 | //print_code(); //输出标识符 2267 | getsym(); 2268 | //print_code(); //输出( 2269 | getsym(); 2270 | r = judge_value_parameter_list(); 2271 | if (r.return_num < 0) 2272 | return fail; 2273 | //print_code(); //输出) 2274 | getsym(); 2275 | //printf("<无返回值函数调用语句>\n"); 2276 | return zero; 2277 | } 2278 | 2279 | struct return_info judge_value_parameter_list() 2280 | { 2281 | struct return_info r; 2282 | if (symbol == RPARENT) 2283 | { //空的情况 2284 | //printf("<值参数表>\n"); 2285 | return zero; 2286 | } 2287 | r = judge_expression(); 2288 | if (r.return_num < 0) 2289 | return r; 2290 | while (symbol == COMMA) 2291 | { 2292 | //print_code(); 2293 | getsym(); 2294 | r = judge_expression(); 2295 | if (r.return_num < 0) 2296 | return r; 2297 | } 2298 | //printf("<值参数表>\n"); 2299 | return zero; 2300 | } 2301 | 2302 | struct return_info judge_statement_list() 2303 | { 2304 | while (judge_statement().return_num == 0) 2305 | { 2306 | //应该不用干什么。。。 2307 | } 2308 | //printf("<语句列>\n"); 2309 | return zero; 2310 | } 2311 | 2312 | struct return_info judge_scanf() 2313 | { 2314 | int loc; 2315 | char identify[100]; 2316 | int type; 2317 | if (symbol != SCANFTK) 2318 | return fail; 2319 | //print_code(); //输出scanf 2320 | getsym(); 2321 | //print_code(); //输出( 2322 | getsym(); 2323 | //print_code(); //输出标识符 2324 | loc = find_name(s_low); 2325 | if (name_list[loc].use == 1) { 2326 | strcpy(identify, name_list[loc].new_name); 2327 | type = name_list[loc].new_type; 2328 | } 2329 | else { 2330 | strcpy(identify, name_list[loc].name); 2331 | type = name_list[loc].type; 2332 | } 2333 | middle_code_list[middle_code_sum++] = middle_code_generate( 2334 | SCANF_STATEMENT, type, 2, "scanf", identify); 2335 | getsym(); 2336 | //print_code(); //输出) 2337 | getsym(); 2338 | //printf("<读语句>\n"); 2339 | return zero; 2340 | } 2341 | 2342 | struct return_info judge_printf() 2343 | { 2344 | struct return_info r; 2345 | char string[1000]; 2346 | if (symbol != PRINTFTK) 2347 | return fail; 2348 | //print_code(); //输出printf 2349 | getsym(); 2350 | //print_code(); //输出( 2351 | getsym(); 2352 | if ((r = judge_string()).return_num == 0) 2353 | { 2354 | strcpy(string, r.code[0]); 2355 | //发现有字符串,判断有没有逗号来看有没有表达式 2356 | if (symbol == COMMA) 2357 | { 2358 | //print_code(); 2359 | getsym(); 2360 | r = judge_expression(); 2361 | if (r.return_num < 0) 2362 | return r; 2363 | middle_code_list[middle_code_sum++] = middle_code_generate( 2364 | PRINT_STATEMENT1, r.type, 3, "print", string, r.code[0]); 2365 | } 2366 | else { 2367 | middle_code_list[middle_code_sum++] = middle_code_generate( 2368 | PRINT_STATEMENT2, 0, 2, "print", string); 2369 | } 2370 | } 2371 | else if ((r = judge_expression()).return_num >= 0) 2372 | { 2373 | middle_code_list[middle_code_sum++] = middle_code_generate( 2374 | PRINT_STATEMENT3, r.type, 2, "print", r.code[0]); 2375 | } 2376 | //print_code(); 2377 | getsym(); 2378 | //printf("<写语句>\n"); 2379 | return zero; 2380 | } 2381 | 2382 | struct return_info judge_return_statement() 2383 | { 2384 | struct return_info r; 2385 | if (symbol != RETURNTK) 2386 | return fail; 2387 | //print_code(); //输出return 2388 | getsym(); 2389 | if (symbol == LPARENT) 2390 | { 2391 | //print_code(); //输出( 2392 | getsym(); 2393 | r = judge_expression(); 2394 | if (r.return_num < 0) 2395 | return r; 2396 | //print_code(); //输出) 2397 | getsym(); 2398 | } 2399 | //printf("<返回语句>\n"); 2400 | return zero; 2401 | } 2402 | 2403 | int main() 2404 | { 2405 | getsym(); 2406 | judge_program(); 2407 | mips_generate(); 2408 | } -------------------------------------------------------------------------------- /错误处理.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | void delete_name_list(int depth); 6 | void add_name_list(char* n, int deep, int type); 7 | int checknow_name_list(char* n, int deep); 8 | int search_name_list(char* n, int type); 9 | void print_code(); 10 | void print_code_pre1(); 11 | void print_code_pre2(); 12 | void print_error(struct error_info error); 13 | void deal_token(char c); 14 | void getsym(); 15 | int find_return_func(char* name); 16 | int find_no_return_func(char* name); 17 | int judge_string(); 18 | int judge_program(); 19 | int judge_const_inform(); 20 | int judge_const_define(); 21 | int judge_unsigned_integer(); 22 | int judge_integer(); 23 | int judge_head(); 24 | int judge_const(); 25 | int judge_var_inform(); 26 | int judge_var_define(); 27 | int judge_function_has_return(); 28 | int judge_function_no_return(); 29 | int judge_mix_statement(); 30 | int judge_parameter_list(int type); 31 | int judge_main_function(); 32 | int judge_expression(); 33 | int judge_item(); 34 | int judge_factor(); 35 | int judge_statement(); 36 | int judge_value_statement(); 37 | int judge_condition_statement(); 38 | int judge_condition(); 39 | int judge_circulation_statement(); 40 | int judge_stepsize(); 41 | int judge_case_statement(); 42 | int judge_case_list(int type); 43 | int judge_case_son_statement(int type); 44 | int judge_default(); 45 | int judge_returnfunc_use(); 46 | int judge_noreturnfunc_use(); 47 | int judge_func_call(); 48 | int judge_value_parameter_list(char* name); 49 | int judge_statement_list(); 50 | int judge_scanf(); 51 | int judge_printf(); 52 | int judge_return_statement(); 53 | 54 | FILE* in = freopen("testfile.txt", "r", stdin); 55 | FILE* out = freopen("error.txt", "w", stdout); 56 | char c; 57 | char s[1000]; 58 | char s_low[1000]; 59 | 60 | enum Code 61 | { 62 | IDENFR, 63 | INTCON, 64 | CHARCON, 65 | STRCON, 66 | CONSTTK, 67 | INTTK, 68 | CHARTK, 69 | VOIDTK, 70 | MAINTK, 71 | IFTK, 72 | ELSETK, 73 | SWITCHTK, 74 | CASETK, 75 | DEFAULTTK, 76 | WHILETK, 77 | FORTK, 78 | SCANFTK, 79 | PRINTFTK, 80 | RETURNTK, 81 | PLUS, 82 | MINU, 83 | MULT, 84 | DIV, 85 | LSS, 86 | LEQ, 87 | GRE, 88 | GEQ, 89 | EQL, 90 | NEQ, 91 | COLON, 92 | ASSIGN, 93 | SEMICN, 94 | COMMA, 95 | LPARENT, 96 | RPARENT, 97 | LBRACK, 98 | RBRACK, 99 | LBRACE, 100 | RBRACE 101 | }; 102 | 103 | Code symbol, pre1, pre2; 104 | char content1[1000], content2[1000]; 105 | int flag1 = 0, flag2 = 0; 106 | void Code_map(Code symbol) 107 | { 108 | switch (symbol) { 109 | case 0: printf("IDENFR"); break; 110 | case 1: printf("INTCON"); break; 111 | case 2: printf("CHARCON"); break; 112 | case 3: printf("STRCON"); break; 113 | case 4: printf("CONSTTK"); break; 114 | case 5: printf("INTTK"); break; 115 | case 6: printf("CHARTK"); break; 116 | case 7: printf("VOIDTK"); break; 117 | case 8: printf("MAINTK"); break; 118 | case 9: printf("IFTK"); break; 119 | case 10: printf("ELSETK"); break; 120 | case 11: printf("SWITCHTK"); break; 121 | case 12: printf("CASETK"); break; 122 | case 13: printf("DEFAULTTK"); break; 123 | case 14: printf("WHILETK"); break; 124 | case 15: printf("FORTK"); break; 125 | case 16: printf("SCANFTK"); break; 126 | case 17: printf("PRINTFTK"); break; 127 | case 18: printf("RETURNTK"); break; 128 | case 19: printf("PLUS"); break; 129 | case 20: printf("MINU"); break; 130 | case 21: printf("MULT"); break; 131 | case 22: printf("DIV"); break; 132 | case 23: printf("LSS"); break; 133 | case 24: printf("LEQ"); break; 134 | case 25: printf("GRE"); break; 135 | case 26: printf("GEQ"); break; 136 | case 27: printf("EQL"); break; 137 | case 28: printf("NEQ"); break; 138 | case 29: printf("COLON"); break; 139 | case 30: printf("ASSIGN"); break; 140 | case 31: printf("SEMICN"); break; 141 | case 32: printf("COMMA"); break; 142 | case 33: printf("LPARENT"); break; 143 | case 34: printf("RPARENT"); break; 144 | case 35: printf("LBRACK"); break; 145 | case 36: printf("RBRACK"); break; 146 | case 37: printf("LBRACE"); break; 147 | case 38: printf("RBRACE"); break; 148 | } 149 | } 150 | 151 | int linenum = 1;//当前的行数 152 | int mark_newline = 0; //判断前一个符号是不是换行符的标记,0不是1是 153 | 154 | struct error_info { 155 | int line; 156 | char type; 157 | }; 158 | 159 | struct name_info { 160 | char name[1000]; //名字 161 | int type; //类型,0:const_int; 1:const_char; 2:var_int; 3:var_char; 4:func; 162 | int depth; //所在层次 163 | }; 164 | 165 | struct func_info { 166 | char name[1000]; //函数名 167 | int type; //0:void, 1:int, 2:char,表示函数返回值的种类 168 | int parameter_type[1000]; //0:int, 1:char,按顺序表示参数的种类 169 | int parameter_sum; //表示参数的数量 170 | }; 171 | 172 | struct name_info name_list[1000]; //所有名字都保存在这里,包括变量,常量,函数名 173 | int name_sum = 0; //现在一共声明了多少个名字 174 | int depth = 0; //这表明了某个声明所在的层次 175 | 176 | struct func_info no_return_func[1000]; 177 | struct func_info has_return_func[1000]; 178 | int no_return_func_sum = 0, has_return_func_sum = 0; 179 | int func_type = 0; 180 | 181 | struct func_info search_function(char* name) 182 | { 183 | int i; 184 | struct func_info tmp; 185 | tmp.type = 3; 186 | for (i = 0; i < has_return_func_sum; i++) { 187 | if (strcmp(has_return_func[i].name, name) == 0) { 188 | return has_return_func[i]; 189 | } 190 | } 191 | for (i = 0; i < no_return_func_sum; i++) { 192 | if (strcmp(no_return_func[i].name, name) == 0) { 193 | return no_return_func[i]; 194 | } 195 | } 196 | return tmp; 197 | } 198 | 199 | void delete_name_list(int deep) //把当前层次的所有名字全部删掉 200 | { 201 | while (name_list[name_sum - 1].depth == deep) { 202 | name_sum--; 203 | } 204 | } 205 | 206 | void add_name_list(char* n, int deep, int type) 207 | { 208 | strcpy(name_list[name_sum].name, n); 209 | name_list[name_sum].depth = deep; 210 | name_list[name_sum].type = type; 211 | name_sum++; 212 | } 213 | 214 | int checknow_name_list(char* n, int deep) //检查当前层次是否有重名的 215 | { 216 | int i; 217 | for (i = name_sum - 1; name_list[i].depth == deep; i--) 218 | { 219 | if (strcmp(name_list[i].name, n) == 0) 220 | { 221 | return 1; 222 | } 223 | 224 | } 225 | return 0; 226 | } 227 | 228 | int search_name_list(char* n, int type) 229 | { 230 | int i; 231 | for (i = 0; i < name_sum; i++) { 232 | if (strcmp(name_list[i].name, n) == 0) { 233 | if ((name_list[i].type == 4 && type == 4) || type == 0) { 234 | return i; 235 | } 236 | else { 237 | return -1; 238 | } 239 | } 240 | } 241 | return -1; 242 | } 243 | 244 | struct name_info find_name_list(char* name) 245 | { 246 | int i; 247 | struct name_info a; 248 | for (i = 0; i < name_sum; i++) { 249 | if (strcmp(name_list[i].name, name) == 0) { 250 | return name_list[i]; 251 | } 252 | } 253 | return a; 254 | } 255 | 256 | void print_code() 257 | { 258 | Code_map(symbol); 259 | printf(" %s\n", s); 260 | } 261 | 262 | void print_code_pre1() 263 | { 264 | Code_map(pre1); 265 | printf(" %s\n", content1); 266 | } 267 | 268 | void print_code_pre2() 269 | { 270 | Code_map(pre2); 271 | printf(" %s\n", content2); 272 | } 273 | 274 | void print_error(struct error_info error) 275 | { 276 | printf("%d %c\n", error.line, error.type); 277 | } 278 | 279 | void deal_token(char c) 280 | { 281 | if (isalpha(c) || c == '_') 282 | { 283 | int i = 0; 284 | s[i] = c; 285 | if (c >= 'A' && c <= 'Z') { 286 | c = c - 'A' + 'a'; 287 | } 288 | s_low[i++] = c; 289 | while ((c = getchar())) { 290 | if (!isalnum(c) && c != '_') { 291 | fseek(in, -1, SEEK_CUR); 292 | break; 293 | } 294 | s[i] = c; 295 | if (c >= 'A' && c <= 'Z') { 296 | c = c - 'A' + 'a'; 297 | } 298 | s_low[i++] = c; 299 | } 300 | s[i] = '\0'; 301 | s_low[i] = '\0'; 302 | if (strcmp(s_low, "const") == 0) { 303 | symbol = CONSTTK; 304 | } 305 | else if (strcmp(s_low, "int") == 0) { 306 | symbol = INTTK; 307 | } 308 | else if (strcmp(s_low, "char") == 0) { 309 | symbol = CHARTK; 310 | } 311 | else if (strcmp(s_low, "void") == 0) { 312 | symbol = VOIDTK; 313 | } 314 | else if (strcmp(s_low, "main") == 0) { 315 | symbol = MAINTK; 316 | } 317 | else if (strcmp(s_low, "if") == 0) { 318 | symbol = IFTK; 319 | } 320 | else if (strcmp(s_low, "else") == 0) { 321 | symbol = ELSETK; 322 | } 323 | else if (strcmp(s_low, "switch") == 0) { 324 | symbol = SWITCHTK; 325 | } 326 | else if (strcmp(s_low, "case") == 0) { 327 | symbol = CASETK; 328 | } 329 | else if (strcmp(s_low, "default") == 0) { 330 | symbol = DEFAULTTK; 331 | } 332 | else if (strcmp(s_low, "while") == 0) { 333 | symbol = WHILETK; 334 | } 335 | else if (strcmp(s_low, "for") == 0) { 336 | symbol = FORTK; 337 | } 338 | else if (strcmp(s_low, "scanf") == 0) { 339 | symbol = SCANFTK; 340 | } 341 | else if (strcmp(s_low, "printf") == 0) { 342 | symbol = PRINTFTK; 343 | } 344 | else if (strcmp(s_low, "return") == 0) { 345 | symbol = RETURNTK; 346 | } 347 | else { 348 | symbol = IDENFR; 349 | } 350 | } 351 | else if (isdigit(c)) { 352 | int i = 0; 353 | s[i++] = c; 354 | while ((c = getchar())) { 355 | if (!isdigit(c)) { 356 | fseek(in, -1, SEEK_CUR); 357 | break; 358 | } 359 | s[i++] = c; 360 | } 361 | s[i] = '\0'; 362 | symbol = INTCON; 363 | } 364 | else if (c == '\'') 365 | { 366 | int i = 0; 367 | c = getchar(); 368 | s[i++] = c; 369 | s[i] = '\0'; 370 | c = getchar(); 371 | symbol = CHARCON; 372 | } 373 | else if (c == '"') 374 | { 375 | int i = 0; 376 | while ((c = getchar())) { 377 | if (c == '"') { 378 | break; 379 | } 380 | s[i++] = c; 381 | } 382 | s[i] = '\0'; 383 | symbol = STRCON; 384 | } 385 | else if (c == '+') { 386 | symbol = PLUS; 387 | strcpy(s, "+"); 388 | } 389 | else if (c == '-') { 390 | symbol = MINU; 391 | strcpy(s, "-"); 392 | } 393 | else if (c == '*') { 394 | symbol = MULT; 395 | strcpy(s, "*"); 396 | } 397 | else if (c == '/') { 398 | symbol = DIV; 399 | strcpy(s, "/"); 400 | } 401 | else if (c == '<') { 402 | c = getchar(); 403 | if (c == '=') { 404 | symbol = LEQ; 405 | strcpy(s, "<="); 406 | } 407 | else { 408 | fseek(in, -1, SEEK_CUR); 409 | symbol = LSS; 410 | strcpy(s, "<"); 411 | } 412 | } 413 | else if (c == '>') { 414 | c = getchar(); 415 | if (c == '=') { 416 | symbol = GEQ; 417 | strcpy(s, ">="); 418 | } 419 | else { 420 | fseek(in, -1, SEEK_CUR); 421 | symbol = GRE; 422 | strcpy(s, ">"); 423 | } 424 | } 425 | else if (c == '=') { 426 | c = getchar(); 427 | if (c == '=') { 428 | symbol = EQL; 429 | strcpy(s, "=="); 430 | } 431 | else { 432 | fseek(in, -1, SEEK_CUR); 433 | symbol = ASSIGN; 434 | strcpy(s, "="); 435 | } 436 | } 437 | else if (c == '!') { 438 | c = getchar(); 439 | if (c == '=') { 440 | symbol = NEQ; 441 | strcpy(s, "!="); 442 | } 443 | } 444 | else if (c == ':') { 445 | symbol = COLON; 446 | strcpy(s, ":"); 447 | } 448 | else if (c == ';') { 449 | symbol = SEMICN; 450 | strcpy(s, ";"); 451 | } 452 | else if (c == ',') { 453 | symbol = COMMA; 454 | strcpy(s, ","); 455 | } 456 | else if (c == '(') { 457 | symbol = LPARENT; 458 | strcpy(s, "("); 459 | } 460 | else if (c == ')') { 461 | symbol = RPARENT; 462 | strcpy(s, ")"); 463 | } 464 | else if (c == '[') { 465 | symbol = LBRACK; 466 | strcpy(s, "["); 467 | } 468 | else if (c == ']') { 469 | symbol = RBRACK; 470 | strcpy(s, "]"); 471 | } 472 | else if (c == '{') { 473 | symbol = LBRACE; 474 | strcpy(s, "{"); 475 | } 476 | else if (c == '}') { 477 | symbol = RBRACE; 478 | strcpy(s, "}"); 479 | } 480 | } 481 | 482 | void getsym() { 483 | mark_newline = 0; 484 | c = getchar(); 485 | while (isspace(c)) { 486 | //TODO 487 | if (c == '\n') 488 | { 489 | linenum++; 490 | mark_newline = 1; 491 | } 492 | c = getchar(); 493 | } 494 | if (c == EOF) { 495 | return; 496 | } 497 | deal_token(c); 498 | } 499 | 500 | int find_return_func(char* name) 501 | { 502 | int i; 503 | for (i = 0; i < has_return_func_sum; i++) 504 | { 505 | if (strcmp(has_return_func[i].name, name) == 0) 506 | return 1; 507 | } 508 | return 0; 509 | } 510 | 511 | int find_no_return_func(char* name) 512 | { 513 | int i; 514 | for (i = 0; i < no_return_func_sum; i++) 515 | { 516 | if (strcmp(no_return_func[i].name, name) == 0) 517 | return 1; 518 | } 519 | return 0; 520 | } 521 | 522 | int judge_string() 523 | { 524 | if (symbol != STRCON) 525 | return -1; 526 | //print_code(); 527 | int i, biaoji = 0; 528 | for (i = 0; i < strlen(s); i++) 529 | { 530 | if (s[i] != 32 && s[i] != 33 && !(s[i] >= 35 && s[i] <= 126)) 531 | { 532 | biaoji = 1; 533 | break; 534 | } 535 | } 536 | if (biaoji == 1 || strlen(s) == 0) 537 | { 538 | struct error_info error; 539 | error.line = linenum; 540 | error.type = 'a'; 541 | print_error(error); 542 | } 543 | getsym(); 544 | //printf("<字符串>\n"); 545 | return 0; 546 | } 547 | 548 | int judge_program() 549 | { 550 | int r; 551 | r = judge_const_inform(); 552 | if (r < 0) 553 | return r; 554 | r = judge_var_inform(); 555 | if (r < 0) 556 | return r; 557 | while (1) 558 | { 559 | if (judge_function_has_return() < 0) 560 | { 561 | if (judge_function_no_return() < 0) 562 | { 563 | break; 564 | } 565 | } 566 | } 567 | r = judge_main_function(); 568 | if (r < 0) 569 | return r; 570 | //printf("<程序>\n"); 571 | return 0; 572 | } 573 | 574 | int judge_const_inform() 575 | { 576 | int r; 577 | if (symbol != CONSTTK) { 578 | return 0; //这里不应该返回负数,否则就直接退了 579 | } 580 | while (symbol == CONSTTK) 581 | { 582 | //print_code(); 583 | getsym(); 584 | r = judge_const_define(); 585 | if (r < 0) 586 | return r; 587 | //print_code(); //输出; 588 | if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号 589 | { 590 | struct error_info error; 591 | if (mark_newline) 592 | { 593 | error.line = linenum - 1; 594 | mark_newline = 0; 595 | } 596 | else 597 | error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1 598 | error.type = 'k'; 599 | print_error(error); 600 | } 601 | else 602 | { 603 | getsym(); 604 | } //如果没错,那么还要继续往下读一个 605 | } 606 | //printf("<常量说明>\n"); 607 | return 0; 608 | } 609 | 610 | int judge_const_define() 611 | { 612 | int r; 613 | if (symbol == INTTK) 614 | { 615 | //print_code(); 616 | getsym(); //读标识符 617 | if (checknow_name_list(s_low, depth)) 618 | { 619 | struct error_info error; 620 | error.line = linenum; 621 | error.type = 'b'; 622 | print_error(error); 623 | } 624 | else 625 | { 626 | add_name_list(s_low, depth, 0); 627 | } 628 | //print_code(); 629 | getsym(); //读等号 630 | //print_code(); 631 | getsym(); //读整数 632 | r = judge_integer(); 633 | if (r < 0) 634 | return r; 635 | while (symbol == COMMA) //不出意外的话,逗号在前面读了 636 | { 637 | //print_code(); 638 | getsym(); //读标识符 639 | if (checknow_name_list(s_low, depth)) 640 | { 641 | struct error_info error; 642 | error.line = linenum; 643 | error.type = 'b'; 644 | print_error(error); 645 | } 646 | else 647 | { 648 | add_name_list(s_low, depth, 0); 649 | } 650 | //print_code(); 651 | getsym(); //读等号 652 | //print_code(); 653 | getsym(); //读整数 654 | r = judge_integer(); 655 | if (r < 0) 656 | return r; 657 | } 658 | } 659 | else if (symbol == CHARTK) //???可否有else 660 | { 661 | //print_code(); 662 | getsym(); //读标识符 663 | if (checknow_name_list(s_low, depth)) 664 | { 665 | struct error_info error; 666 | error.line = linenum; 667 | error.type = 'b'; 668 | print_error(error); 669 | } 670 | else 671 | { 672 | add_name_list(s_low, depth, 1); 673 | } 674 | //print_code(); 675 | getsym(); //读等号 676 | //print_code(); 677 | getsym(); //读字符 678 | if (s[0] != '_' && !(s[0] <= 'z' && s[0] >= 'a') 679 | && !(s[0] <= 'Z' && s[0] >= 'A') && !(s[0] >= '0' && s[0] <= '9') 680 | && s[0] != '+' && s[0] != '-' && s[0] != '*' && s[0] != '/') 681 | { 682 | struct error_info error; 683 | error.line = linenum; 684 | error.type = 'a'; 685 | print_error(error); 686 | } 687 | //print_code(); 688 | getsym(); //读逗号,因为并没有处理逗号的函数 689 | while (symbol == COMMA) //不出意外的话,逗号在前面读了 690 | { 691 | //print_code(); 692 | getsym(); //读标识符 693 | if (checknow_name_list(s_low, depth)) 694 | { 695 | struct error_info error; 696 | error.line = linenum; 697 | error.type = 'b'; 698 | print_error(error); 699 | } 700 | else 701 | { 702 | add_name_list(s_low, depth, 1); 703 | } 704 | //print_code(); 705 | getsym(); //读等号 706 | //print_code(); 707 | getsym(); //读字符 708 | if (s[0] != '_' && !(s[0] <= 'z' && s[0] >= 'a') 709 | && !(s[0] <= 'Z' && s[0] >= 'A') && !(s[0] >= '0' && s[0] <= '9') 710 | && s[0] != '+' && s[0] != '-' && s[0] != '*' && s[0] != '/') 711 | { 712 | struct error_info error; 713 | error.line = linenum; 714 | error.type = 'a'; 715 | print_error(error); 716 | } 717 | //print_code(); 718 | getsym(); //读逗号,因为并没有处理逗号的函数,若不是逗号,那么读sym并跳出 719 | } 720 | } 721 | //printf("<常量定义>\n"); 722 | return 0; 723 | } 724 | 725 | int judge_unsigned_integer() 726 | { 727 | if (symbol != INTCON) { 728 | return -1; 729 | } 730 | //print_code(); //输出数 731 | int tmp = atoi(s); 732 | getsym(); //读下一个 733 | //printf("<无符号整数>\n"); 734 | return tmp; 735 | } 736 | 737 | int judge_integer() 738 | { 739 | int r; 740 | if (symbol == PLUS || symbol == MINU) //???是否需要判断是不是char常量 741 | { 742 | //print_code(); //输出符号 743 | getsym(); //读数 744 | } 745 | r = judge_unsigned_integer(); 746 | if (r < 0) 747 | return r; 748 | //printf("<整数>\n"); 749 | return 0; 750 | } 751 | 752 | int judge_head() 753 | { 754 | int type; 755 | if ((flag1 == 1 && pre1 != INTTK && pre1 != CHARTK) 756 | || (flag1 == 0 && symbol != INTTK && symbol != CHARTK)) { 757 | return -1; 758 | } 759 | if (flag1 == 1) { 760 | flag1 = 0; 761 | //print_code_pre1(); 762 | if (pre1 == INTTK) 763 | { 764 | type = 1; 765 | has_return_func[has_return_func_sum].type = 1; 766 | } 767 | else if (pre1 == CHARTK) 768 | { 769 | type = 2; 770 | has_return_func[has_return_func_sum].type = 2; 771 | } 772 | } 773 | else { 774 | //print_code(); 775 | if (symbol == INTTK) 776 | { 777 | type = 1; 778 | has_return_func[has_return_func_sum].type = 1; 779 | } 780 | else if (symbol == CHARTK) 781 | { 782 | type = 2; 783 | has_return_func[has_return_func_sum].type = 2; 784 | } 785 | getsym(); 786 | } 787 | if (flag2 == 1) { 788 | flag2 = 0; 789 | if (checknow_name_list(content2, depth)) 790 | { 791 | struct error_info error; 792 | error.line = linenum; 793 | error.type = 'b'; 794 | print_error(error); 795 | } 796 | else 797 | { 798 | add_name_list(content2, depth, 4); 799 | } 800 | strcpy(has_return_func[has_return_func_sum].name, content2); 801 | //print_code_pre2(); //输出标识符 802 | } 803 | else { 804 | if (checknow_name_list(s_low, depth)) 805 | { 806 | struct error_info error; 807 | error.line = linenum; 808 | error.type = 'b'; 809 | print_error(error); 810 | } 811 | else 812 | { 813 | add_name_list(s_low, depth, 4); 814 | } 815 | strcpy(has_return_func[has_return_func_sum].name, s_low); 816 | //print_code(); //输出标识符 817 | getsym(); 818 | } 819 | //printf("<声明头部>\n"); 820 | return type; 821 | } 822 | 823 | int judge_const() 824 | { 825 | if (judge_integer() == 0) 826 | { 827 | //printf("<常量>\n"); 828 | return 0; 829 | } 830 | else if (s[0] == '+' || s[0] == '-' || s[0] == '*' || s[0] == '/' || (s[0] >= '0' && s[0] <= '9') 831 | || (s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z') || s[0] == '_') 832 | { 833 | //print_code(); //输出这个字符 834 | getsym(); 835 | //printf("<常量>\n"); 836 | return 1; 837 | } 838 | else if (s[0] != '_' && !(s[0] <= 'z' && s[0] >= 'a') 839 | && !(s[0] <= 'Z' && s[0] >= 'A') && !(s[0] >= '0' && s[0] <= '9') 840 | && s[0] != '+' && s[0] != '-' && s[0] != '*' && s[0] != '/') 841 | { 842 | struct error_info error; 843 | error.line = linenum; 844 | error.type = 'a'; 845 | print_error(error); 846 | getsym(); //这里应该是要读一个,因为不存在缺少,只有错误的情况 847 | return 1; //不应返回负数 848 | } 849 | else 850 | return -1; 851 | } 852 | 853 | int judge_var_inform() 854 | { 855 | int r; 856 | int sum = 0; 857 | while (1) 858 | { 859 | if (symbol != INTTK && symbol != CHARTK) 860 | { 861 | break; 862 | } 863 | pre1 = symbol; 864 | strcpy(content1, s_low); //注意这里和下面做出了改动,因为不需要输出内容,为了方便判断,将其改为小写存储 865 | flag1 = 1; 866 | getsym(); 867 | pre2 = symbol; 868 | strcpy(content2, s_low); 869 | flag2 = 1; 870 | getsym(); //注意,下面接的有可能是无返回值函数定义,也有可能是有返回值函数定义,也有可能是个变量,也有可能是主函数 871 | if (symbol != LBRACK && symbol != COMMA && symbol != SEMICN && symbol != ASSIGN) 872 | { 873 | break; 874 | } 875 | r = judge_var_define(); 876 | if (r < 0) 877 | return r; 878 | sum++; 879 | //print_code(); 880 | if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号 881 | { 882 | struct error_info error; 883 | if (mark_newline) 884 | { 885 | error.line = linenum - 1; 886 | mark_newline = 0; 887 | } 888 | else 889 | error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1 890 | error.type = 'k'; 891 | print_error(error); 892 | } 893 | else 894 | { 895 | getsym(); 896 | } //如果没错,那么还要继续往下读一个 897 | } 898 | if (sum == 0) 899 | return 0; //如果为0,说明没有变量说明 900 | //printf("<变量说明>\n"); 901 | return 0; 902 | } 903 | 904 | int judge_var_define() 905 | { 906 | int r; 907 | int flag = 0; 908 | int type; 909 | int a1 = 0, a2 = 0; 910 | int sum1 = 0, sum2 = 0; 911 | //print_code_pre1(); //输出类型标识符 912 | if (pre1 == INTTK) 913 | { 914 | type = 2; 915 | } 916 | else if (pre1 == CHARTK) 917 | { 918 | type = 3; 919 | } 920 | flag1 = 0; 921 | //print_code_pre2(); //输出标识符 922 | if (checknow_name_list(content2, depth)) 923 | { 924 | struct error_info error; 925 | error.line = linenum; 926 | error.type = 'b'; 927 | print_error(error); 928 | } 929 | else 930 | { 931 | add_name_list(content2, depth, type); 932 | } 933 | flag2 = 0; 934 | if (symbol == LBRACK) 935 | { 936 | sum1 = 0; 937 | //print_code(); //输出[ 938 | getsym(); 939 | r = judge_unsigned_integer(); 940 | if (r < 0) 941 | return r; 942 | a1 = r; //这是第一个中括号里面的数 943 | //print_code(); //输出] 944 | if (strcmp(s, "]") != 0) 945 | { 946 | struct error_info error; 947 | error.line = linenum; 948 | error.type = 'm'; 949 | print_error(error); 950 | } 951 | else 952 | { 953 | getsym(); 954 | } 955 | if (symbol == LBRACK) 956 | { 957 | sum2 = 0; 958 | //print_code(); //输出[ 959 | getsym(); 960 | r = judge_unsigned_integer(); 961 | if (r < 0) 962 | return r; 963 | a2 = r; 964 | //print_code(); //输出] 965 | if (strcmp(s, "]") != 0) 966 | { 967 | struct error_info error; 968 | error.line = linenum; 969 | error.type = 'm'; 970 | print_error(error); 971 | } 972 | else 973 | { 974 | getsym(); 975 | } 976 | if (symbol == ASSIGN) 977 | { 978 | flag = 1; 979 | //print_code(); //输出= 980 | getsym(); 981 | //print_code(); //输出{ 982 | getsym(); 983 | //print_code(); //输出{ 984 | getsym(); 985 | r = judge_const(); 986 | if (r < 0) 987 | return r; 988 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 989 | { 990 | struct error_info error; 991 | error.line = linenum; 992 | error.type = 'o'; 993 | print_error(error); 994 | } 995 | sum2++; 996 | while (symbol == COMMA) 997 | { 998 | //print_code(); //输出, 999 | getsym(); 1000 | r = judge_const(); 1001 | if (r < 0) 1002 | return r; 1003 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1004 | { 1005 | struct error_info error; 1006 | error.line = linenum; 1007 | error.type = 'o'; 1008 | print_error(error); 1009 | } 1010 | sum2++; 1011 | } 1012 | if (sum2 != a2) 1013 | { 1014 | struct error_info error; 1015 | error.line = linenum; 1016 | error.type = 'n'; 1017 | print_error(error); 1018 | } 1019 | //print_code(); //输出} 1020 | sum1++; 1021 | getsym(); 1022 | while (symbol == COMMA) 1023 | { 1024 | sum2 = 0; 1025 | //print_code(); //输出, 1026 | getsym(); 1027 | //print_code(); //输出{ 1028 | getsym(); 1029 | r = judge_const(); 1030 | if (r < 0) 1031 | return r; 1032 | sum2++; 1033 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1034 | { 1035 | struct error_info error; 1036 | error.line = linenum; 1037 | error.type = 'o'; 1038 | print_error(error); 1039 | } 1040 | while (symbol == COMMA) 1041 | { 1042 | //print_code(); //输出, 1043 | getsym(); 1044 | r = judge_const(); 1045 | if (r < 0) 1046 | return r; 1047 | sum2++; 1048 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1049 | { 1050 | struct error_info error; 1051 | error.line = linenum; 1052 | error.type = 'o'; 1053 | print_error(error); 1054 | } 1055 | } 1056 | if (sum2 != a2) 1057 | { 1058 | struct error_info error; 1059 | error.line = linenum; 1060 | error.type = 'n'; 1061 | print_error(error); 1062 | } 1063 | //print_code(); //输出} 1064 | sum1++; 1065 | getsym(); 1066 | } 1067 | //print_code(); //输出} 1068 | if (sum1 != a1) 1069 | { 1070 | struct error_info error; 1071 | error.line = linenum; 1072 | error.type = 'n'; 1073 | print_error(error); 1074 | } 1075 | getsym(); 1076 | } 1077 | } 1078 | else if (symbol == ASSIGN) 1079 | { 1080 | flag = 1; 1081 | //print_code(); //输出= 1082 | getsym(); 1083 | //print_code(); //输出{ 1084 | getsym(); 1085 | r = judge_const(); 1086 | if (r < 0) 1087 | return r; 1088 | sum1++; 1089 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1090 | { 1091 | struct error_info error; 1092 | error.line = linenum; 1093 | error.type = 'o'; 1094 | print_error(error); 1095 | } 1096 | while (symbol == COMMA) 1097 | { 1098 | //print_code(); //输出, 1099 | getsym(); 1100 | r = judge_const(); 1101 | if (r < 0) 1102 | return r; 1103 | sum1++; 1104 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1105 | { 1106 | struct error_info error; 1107 | error.line = linenum; 1108 | error.type = 'o'; 1109 | print_error(error); 1110 | } 1111 | } 1112 | //print_code(); //输出} 1113 | if (sum1 != a1) 1114 | { 1115 | struct error_info error; 1116 | error.line = linenum; 1117 | error.type = 'n'; 1118 | print_error(error); 1119 | } 1120 | getsym(); 1121 | } 1122 | } 1123 | else if (symbol == ASSIGN) 1124 | { 1125 | flag = 1; 1126 | //print_code(); //输出= 1127 | getsym(); 1128 | r = judge_const(); 1129 | if (r < 0) 1130 | return r; 1131 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1132 | { 1133 | struct error_info error; 1134 | error.line = linenum; 1135 | error.type = 'o'; 1136 | print_error(error); 1137 | } 1138 | } 1139 | while (symbol == COMMA) 1140 | { 1141 | //print_code(); //输出, 1142 | getsym(); 1143 | if (checknow_name_list(s_low, depth)) 1144 | { 1145 | struct error_info error; 1146 | error.line = linenum; 1147 | error.type = 'b'; 1148 | print_error(error); 1149 | } 1150 | else 1151 | { 1152 | add_name_list(s_low, depth, type); 1153 | } 1154 | //print_code(); //输出标识符 1155 | getsym(); 1156 | if (symbol == LBRACK) 1157 | { 1158 | sum1 = 0; 1159 | sum2 = 0; 1160 | //print_code(); //输出[ 1161 | getsym(); 1162 | r = judge_unsigned_integer(); 1163 | if (r < 0) 1164 | return r; 1165 | a1 = r; 1166 | //print_code(); //输出] 1167 | if (strcmp(s, "]") != 0) 1168 | { 1169 | struct error_info error; 1170 | error.line = linenum; 1171 | error.type = 'm'; 1172 | print_error(error); 1173 | } 1174 | else 1175 | { 1176 | getsym(); 1177 | } 1178 | if (symbol == LBRACK) 1179 | { 1180 | //print_code(); //输出[ 1181 | getsym(); 1182 | r = judge_unsigned_integer(); 1183 | if (r < 0) 1184 | return r; 1185 | a2 = r; 1186 | //print_code(); //输出] 1187 | if (strcmp(s, "]") != 0) 1188 | { 1189 | struct error_info error; 1190 | error.line = linenum; 1191 | error.type = 'm'; 1192 | print_error(error); 1193 | } 1194 | else 1195 | { 1196 | getsym(); 1197 | } 1198 | if (symbol == ASSIGN) 1199 | { 1200 | flag = 1; 1201 | //print_code(); //输出= 1202 | getsym(); 1203 | //print_code(); //输出{ 1204 | getsym(); 1205 | //print_code(); //输出{ 1206 | getsym(); 1207 | r = judge_const(); 1208 | if (r < 0) 1209 | return r; 1210 | sum2++; 1211 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1212 | { 1213 | struct error_info error; 1214 | error.line = linenum; 1215 | error.type = 'o'; 1216 | print_error(error); 1217 | } 1218 | while (symbol == COMMA) 1219 | { 1220 | //print_code(); //输出, 1221 | getsym(); 1222 | r = judge_const(); 1223 | if (r < 0) 1224 | return r; 1225 | sum2++; 1226 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1227 | { 1228 | struct error_info error; 1229 | error.line = linenum; 1230 | error.type = 'o'; 1231 | print_error(error); 1232 | } 1233 | } 1234 | //print_code(); //输出} 1235 | sum1++; 1236 | if (sum2 != a2) 1237 | { 1238 | struct error_info error; 1239 | error.line = linenum; 1240 | error.type = 'n'; 1241 | print_error(error); 1242 | } 1243 | getsym(); 1244 | while (symbol == COMMA) 1245 | { 1246 | sum2 = 0; 1247 | //print_code(); //输出, 1248 | getsym(); 1249 | //print_code(); //输出{ 1250 | getsym(); 1251 | r = judge_const(); 1252 | if (r < 0) 1253 | return r; 1254 | sum2++; 1255 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1256 | { 1257 | struct error_info error; 1258 | error.line = linenum; 1259 | error.type = 'o'; 1260 | print_error(error); 1261 | } 1262 | while (symbol == COMMA) 1263 | { 1264 | //print_code(); //输出, 1265 | getsym(); 1266 | r = judge_const(); 1267 | if (r < 0) 1268 | return r; 1269 | sum2++; 1270 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1271 | { 1272 | struct error_info error; 1273 | error.line = linenum; 1274 | error.type = 'o'; 1275 | print_error(error); 1276 | } 1277 | } 1278 | //print_code(); //输出} 1279 | sum1++; 1280 | if (sum2 != a2) 1281 | { 1282 | struct error_info error; 1283 | error.line = linenum; 1284 | error.type = 'n'; 1285 | print_error(error); 1286 | } 1287 | getsym(); 1288 | } 1289 | //print_code(); //输出} 1290 | if (sum1 != a1) 1291 | { 1292 | struct error_info error; 1293 | error.line = linenum; 1294 | error.type = 'n'; 1295 | print_error(error); 1296 | } 1297 | getsym(); 1298 | } 1299 | } 1300 | else if (symbol == ASSIGN) 1301 | { 1302 | flag = 1; 1303 | //print_code(); //输出= 1304 | getsym(); 1305 | //print_code(); //输出{ 1306 | getsym(); 1307 | r = judge_const(); 1308 | if (r < 0) 1309 | return r; 1310 | sum1++; 1311 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1312 | { 1313 | struct error_info error; 1314 | error.line = linenum; 1315 | error.type = 'o'; 1316 | print_error(error); 1317 | } 1318 | while (symbol == COMMA) 1319 | { 1320 | //print_code(); //输出, 1321 | getsym(); 1322 | r = judge_const(); 1323 | if (r < 0) 1324 | return r; 1325 | sum1++; 1326 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1327 | { 1328 | struct error_info error; 1329 | error.line = linenum; 1330 | error.type = 'o'; 1331 | print_error(error); 1332 | } 1333 | } 1334 | //print_code(); //输出} 1335 | if (sum1 != a1) 1336 | { 1337 | struct error_info error; 1338 | error.line = linenum; 1339 | error.type = 'n'; 1340 | print_error(error); 1341 | } 1342 | getsym(); 1343 | } 1344 | } 1345 | else if (symbol == ASSIGN) 1346 | { 1347 | flag = 1; 1348 | //print_code(); //输出= 1349 | getsym(); 1350 | r = judge_const(); 1351 | if (r < 0) 1352 | return r; 1353 | if ((r == 0 && type == 3) || (r == 1 && type == 2)) 1354 | { 1355 | struct error_info error; 1356 | error.line = linenum; 1357 | error.type = 'o'; 1358 | print_error(error); 1359 | } 1360 | } 1361 | //??? 1362 | } 1363 | //if (flag) 1364 | //printf("<变量定义及初始化>\n"); 1365 | //else 1366 | //printf("<变量定义无初始化>\n"); 1367 | //printf("<变量定义>\n"); 1368 | return 0; 1369 | } 1370 | 1371 | int judge_function_has_return() 1372 | { 1373 | int r, type; 1374 | r = judge_head(); 1375 | if (r < 0) 1376 | return r; 1377 | //print_code(); //输出( 1378 | type = r; 1379 | if (type == 1) 1380 | { 1381 | func_type = 1; 1382 | } 1383 | else if (type == 2) 1384 | { 1385 | func_type = 2; 1386 | } 1387 | depth++; //注意,在这里加depth可能有问题,一会改一改看看 1388 | getsym(); 1389 | r = judge_parameter_list(1); 1390 | if (r < 0) 1391 | return r; 1392 | has_return_func_sum++; 1393 | //print_code(); //输出) 1394 | if (strcmp(s, ")") != 0) 1395 | { 1396 | struct error_info error; 1397 | if (mark_newline) 1398 | { 1399 | error.line = linenum - 1; 1400 | mark_newline = 0; 1401 | } 1402 | else 1403 | error.line = linenum; 1404 | error.type = 'l'; 1405 | print_error(error); 1406 | } 1407 | else 1408 | { 1409 | getsym(); 1410 | } 1411 | //print_code(); //输出{ 1412 | getsym(); 1413 | r = judge_mix_statement(); //TODO注意当前位置可能要修改 1414 | if (r < 0) 1415 | return r; 1416 | //print_code(); //输出} 1417 | delete_name_list(depth); 1418 | depth--; //将当前层次的名字都删除 1419 | getsym(); 1420 | //printf("<有返回值函数定义>\n"); 1421 | return 0; 1422 | } 1423 | 1424 | int judge_function_no_return() 1425 | { 1426 | func_type = 0; 1427 | int r; 1428 | pre1 = symbol; 1429 | strcpy(content1, s_low); 1430 | flag1 = 1; 1431 | getsym(); 1432 | if (symbol == MAINTK) 1433 | return -1; 1434 | //print_code_pre1(); //输出void 1435 | flag1 = 0; 1436 | if (checknow_name_list(s_low, depth)) 1437 | { 1438 | struct error_info error; 1439 | error.line = linenum; 1440 | error.type = 'b'; 1441 | print_error(error); 1442 | } 1443 | else 1444 | { 1445 | add_name_list(s_low, depth, 4); 1446 | } 1447 | strcpy(no_return_func[no_return_func_sum].name, s_low); 1448 | no_return_func[no_return_func_sum].type = 0; 1449 | //print_code(); //输出标识符 1450 | getsym(); 1451 | //print_code(); //输出( 1452 | depth++; //进入高一级层次 1453 | getsym(); 1454 | r = judge_parameter_list(0); 1455 | if (r < 0) 1456 | return r; 1457 | //print_code(); //输出) 1458 | no_return_func_sum++; 1459 | if (strcmp(s, ")") != 0) 1460 | { 1461 | struct error_info error; 1462 | if (mark_newline) 1463 | { 1464 | error.line = linenum - 1; 1465 | mark_newline = 0; 1466 | } 1467 | else 1468 | error.line = linenum; 1469 | error.type = 'l'; 1470 | print_error(error); 1471 | } 1472 | else 1473 | { 1474 | getsym(); 1475 | } 1476 | //print_code(); //输出{ 1477 | getsym(); 1478 | r = judge_mix_statement(); 1479 | if (r < 0) 1480 | return r; 1481 | delete_name_list(depth); 1482 | depth--; 1483 | //print_code(); //输出} 1484 | getsym(); 1485 | //printf("<无返回值函数定义>\n"); 1486 | return 0; 1487 | } 1488 | 1489 | int judge_mix_statement() 1490 | { 1491 | int r; 1492 | r = judge_const_inform(); 1493 | if (r < 0) 1494 | return r; 1495 | r = judge_var_inform(); 1496 | if (r < 0) 1497 | return r; 1498 | r = judge_statement_list(); 1499 | if (r < 0) 1500 | return r; 1501 | if ((func_type == 1 || func_type == 2) && r == 0) 1502 | { 1503 | struct error_info error; 1504 | error.line = linenum; 1505 | error.type = 'h'; 1506 | print_error(error); 1507 | } 1508 | //printf("<复合语句>\n"); 1509 | return 0; 1510 | } 1511 | 1512 | int judge_parameter_list(int type) 1513 | { 1514 | int type1; 1515 | if (symbol == RPARENT) //空的情况 1516 | { 1517 | //printf("<参数表>\n"); 1518 | return 0; 1519 | } 1520 | has_return_func[has_return_func_sum].parameter_sum = 0; //从0开始计数 1521 | if (type == 1) { 1522 | if (symbol == INTTK) { 1523 | type1 = 2; 1524 | has_return_func[has_return_func_sum].parameter_type[has_return_func[has_return_func_sum].parameter_sum++] = 0; 1525 | } 1526 | else { 1527 | type1 = 3; 1528 | has_return_func[has_return_func_sum].parameter_type[has_return_func[has_return_func_sum].parameter_sum++] = 1; 1529 | } 1530 | } 1531 | else 1532 | { 1533 | if (symbol == INTTK) { 1534 | type1 = 2; 1535 | no_return_func[no_return_func_sum].parameter_type[no_return_func[no_return_func_sum].parameter_sum++] = 0; 1536 | } 1537 | else { 1538 | type1 = 3; 1539 | no_return_func[no_return_func_sum].parameter_type[no_return_func[no_return_func_sum].parameter_sum++] = 1; 1540 | } 1541 | } 1542 | //print_code(); //输出int或char 1543 | getsym(); 1544 | if (checknow_name_list(s_low, depth)) 1545 | { 1546 | struct error_info error; 1547 | error.line = linenum; 1548 | error.type = 'b'; 1549 | print_error(error); 1550 | } 1551 | else 1552 | { 1553 | add_name_list(s_low, depth, type1); 1554 | } 1555 | //print_code(); //输出标识符 1556 | getsym(); 1557 | while (symbol == COMMA) 1558 | { 1559 | //print_code(); //输出, 1560 | getsym(); 1561 | if (type == 1) { 1562 | if (symbol == INTTK) { 1563 | type1 = 2; 1564 | has_return_func[has_return_func_sum].parameter_type[has_return_func[has_return_func_sum].parameter_sum++] = 0; 1565 | } 1566 | else { 1567 | type1 = 3; 1568 | has_return_func[has_return_func_sum].parameter_type[has_return_func[has_return_func_sum].parameter_sum++] = 1; 1569 | } 1570 | } 1571 | else 1572 | { 1573 | if (symbol == INTTK) { 1574 | type1 = 2; 1575 | no_return_func[no_return_func_sum].parameter_type[no_return_func[no_return_func_sum].parameter_sum++] = 0; 1576 | } 1577 | else { 1578 | type1 = 3; 1579 | no_return_func[no_return_func_sum].parameter_type[no_return_func[no_return_func_sum].parameter_sum++] = 1; 1580 | } 1581 | } 1582 | //print_code(); //输出int或char 1583 | getsym(); 1584 | if (checknow_name_list(s_low, depth)) 1585 | { 1586 | struct error_info error; 1587 | error.line = linenum; 1588 | error.type = 'b'; 1589 | print_error(error); 1590 | } 1591 | else 1592 | { 1593 | add_name_list(s_low, depth, type1); 1594 | } 1595 | //print_code(); //输出标识符 1596 | getsym(); 1597 | } 1598 | //printf("<参数表>\n"); 1599 | return 0; 1600 | } 1601 | 1602 | int judge_main_function() 1603 | { 1604 | func_type = 0; 1605 | int r; 1606 | if (flag1 == 1) 1607 | { 1608 | flag1 = 0; 1609 | //print_code_pre1(); 1610 | } 1611 | else 1612 | { 1613 | //print_code(); 1614 | getsym(); 1615 | }//两种情况决定输出void 1616 | //print_code(); //输出main 1617 | getsym(); 1618 | //print_code(); //输出( 1619 | getsym(); 1620 | //print_code(); //输出) 1621 | if (strcmp(s, ")") != 0) 1622 | { 1623 | struct error_info error; 1624 | if (mark_newline) 1625 | { 1626 | error.line = linenum - 1; 1627 | mark_newline = 0; 1628 | } 1629 | else 1630 | error.line = linenum; 1631 | error.type = 'l'; 1632 | print_error(error); 1633 | } 1634 | else 1635 | { 1636 | getsym(); 1637 | } 1638 | depth++; 1639 | //print_code(); //输出{ 1640 | getsym(); 1641 | r = judge_mix_statement(); 1642 | if (r < 0) 1643 | return r; 1644 | delete_name_list(depth); 1645 | depth--; 1646 | //print_code(); //输出} 1647 | //到最后啦,不用读了 1648 | //printf("<主函数>\n"); 1649 | return 0; 1650 | } 1651 | 1652 | int judge_expression() 1653 | { 1654 | int r; 1655 | int mark = 0; 1656 | if (symbol == PLUS || symbol == MINU) 1657 | { 1658 | //print_code(); 1659 | mark = 1; 1660 | getsym(); 1661 | } 1662 | r = judge_item(); 1663 | if (r < 0) 1664 | return r; 1665 | while (symbol == PLUS || symbol == MINU) 1666 | { 1667 | //print_code(); 1668 | mark = 1; 1669 | getsym(); 1670 | r = judge_item(); 1671 | if (r < 0) 1672 | return r; 1673 | } 1674 | //printf("<表达式>\n"); 1675 | if (mark == 1) 1676 | { 1677 | return 0; 1678 | } 1679 | else 1680 | { 1681 | return r; 1682 | } 1683 | return 0; 1684 | } 1685 | 1686 | int judge_item() 1687 | { 1688 | int r, mark = 0; 1689 | r = judge_factor(); 1690 | if (r < 0) 1691 | return r; 1692 | while (symbol == MULT || symbol == DIV) 1693 | { 1694 | mark = 1; 1695 | //print_code(); 1696 | getsym(); 1697 | r = judge_factor(); 1698 | if (r < 0) 1699 | return r; 1700 | } 1701 | if (mark) 1702 | { 1703 | return 0; 1704 | } 1705 | else 1706 | { 1707 | return r; 1708 | } 1709 | //printf("<项>\n"); 1710 | return 0; 1711 | } 1712 | 1713 | int judge_factor() 1714 | { 1715 | int r; 1716 | if (symbol == LPARENT) 1717 | { 1718 | //print_code(); //输出( 1719 | getsym(); 1720 | r = judge_expression(); 1721 | if (r < 0) 1722 | return r; 1723 | //print_code(); //输出) 1724 | if (strcmp(s, ")") != 0) 1725 | { 1726 | struct error_info error; 1727 | if (mark_newline) 1728 | { 1729 | error.line = linenum - 1; 1730 | mark_newline = 0; 1731 | } 1732 | else 1733 | error.line = linenum; 1734 | error.type = 'l'; 1735 | print_error(error); 1736 | } 1737 | else 1738 | { 1739 | getsym(); 1740 | } 1741 | return 0; 1742 | } 1743 | else if (symbol == CHARCON) 1744 | { 1745 | //print_code(); //输出字符 1746 | if (s[0] != '_' && !(s[0] <= 'z' && s[0] >= 'a') 1747 | && !(s[0] <= 'Z' && s[0] >= 'A') && !(s[0] >= '0' && s[0] <= '9') 1748 | && s[0] != '+' && s[0] != '-' && s[0] != '*' && s[0] != '/') 1749 | { 1750 | struct error_info error; 1751 | error.line = linenum; 1752 | error.type = 'a'; 1753 | print_error(error); 1754 | } 1755 | getsym(); 1756 | return 1; 1757 | } 1758 | else if (judge_integer() == 0) 1759 | { 1760 | //应该不用干什么 1761 | return 0; 1762 | } 1763 | else if (symbol == IDENFR) 1764 | { 1765 | //print_code(); //输出标识符 1766 | int loc = search_name_list(s_low, 0); 1767 | if (search_name_list(s_low, 0) < 0) 1768 | { 1769 | struct error_info error; 1770 | error.line = linenum; 1771 | error.type = 'c'; 1772 | print_error(error); 1773 | } 1774 | strcpy(content1, s_low); 1775 | pre1 = symbol; 1776 | flag1 = 1; 1777 | getsym(); 1778 | if (symbol == LPARENT) 1779 | { 1780 | if (judge_func_call() == 0) 1781 | { 1782 | return 0; 1783 | } 1784 | else 1785 | { 1786 | return 1; 1787 | } 1788 | } 1789 | while (symbol == LBRACK) 1790 | { 1791 | //print_code(); //输出[ 1792 | getsym(); 1793 | r = judge_expression(); 1794 | if (r < 0) 1795 | return r; 1796 | if (r != 0) 1797 | { 1798 | struct error_info error; 1799 | error.line = linenum; 1800 | error.type = 'i'; 1801 | print_error(error); 1802 | } 1803 | //print_code(); //输出] 1804 | if (strcmp(s, "]") != 0) 1805 | { 1806 | struct error_info error; 1807 | error.line = linenum; 1808 | error.type = 'm'; 1809 | print_error(error); 1810 | } 1811 | else 1812 | { 1813 | getsym(); 1814 | } 1815 | } 1816 | if (flag1 == 1) { 1817 | flag1 = 0; 1818 | } 1819 | if (name_list[loc].type == 1 || name_list[loc].type == 3) { 1820 | return 1; 1821 | } 1822 | else { 1823 | return 0; 1824 | } 1825 | } 1826 | else 1827 | { 1828 | return -1; 1829 | } 1830 | //printf("<因子>\n"); 1831 | return 0; 1832 | } 1833 | 1834 | int judge_statement() 1835 | { 1836 | int r, mark = 0; 1837 | if (symbol == SEMICN) 1838 | { //空的情况 1839 | //print_code(); //输出; 1840 | if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号 1841 | { 1842 | struct error_info error; 1843 | if (mark_newline) 1844 | { 1845 | error.line = linenum - 1; 1846 | mark_newline = 0; 1847 | } 1848 | else 1849 | error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1 1850 | error.type = 'k'; 1851 | print_error(error); 1852 | } 1853 | else 1854 | { 1855 | getsym(); 1856 | } //如果没错,那么还要继续往下读一个 1857 | } 1858 | else if ((r = judge_circulation_statement()) >= 0) 1859 | { 1860 | if (r == 1) 1861 | { 1862 | mark = 1; 1863 | } 1864 | //应该不用干什么 1865 | } 1866 | else if ((r = judge_condition_statement()) >= 0) 1867 | { 1868 | if (r == 1) 1869 | { 1870 | mark = 1; 1871 | } 1872 | //应该不用干什么 1873 | } 1874 | else if ((r = judge_return_statement()) >= 0) 1875 | { 1876 | mark = 1; 1877 | //print_code(); //输出; 1878 | if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号 1879 | { 1880 | struct error_info error; 1881 | if (mark_newline) 1882 | { 1883 | error.line = linenum - 1; 1884 | mark_newline = 0; 1885 | } 1886 | else 1887 | error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1 1888 | error.type = 'k'; 1889 | print_error(error); 1890 | } 1891 | else 1892 | { 1893 | getsym(); 1894 | } //如果没错,那么还要继续往下读一个 1895 | } 1896 | else if (judge_scanf() >= 0) 1897 | { 1898 | //print_code(); //输出; 1899 | if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号 1900 | { 1901 | struct error_info error; 1902 | if (mark_newline) 1903 | { 1904 | error.line = linenum - 1; 1905 | mark_newline = 0; 1906 | } 1907 | else 1908 | error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1 1909 | error.type = 'k'; 1910 | print_error(error); 1911 | } 1912 | else 1913 | { 1914 | getsym(); 1915 | } //如果没错,那么还要继续往下读一个 1916 | } 1917 | else if (judge_printf() >= 0) 1918 | { 1919 | //print_code(); //输出; 1920 | if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号 1921 | { 1922 | struct error_info error; 1923 | if (mark_newline) 1924 | { 1925 | error.line = linenum - 1; 1926 | mark_newline = 0; 1927 | } 1928 | else 1929 | error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1 1930 | error.type = 'k'; 1931 | print_error(error); 1932 | } 1933 | else 1934 | { 1935 | getsym(); 1936 | } //如果没错,那么还要继续往下读一个 1937 | } 1938 | else if (judge_case_statement() >= 0) 1939 | { 1940 | //应该不用干什么 1941 | } 1942 | else if (symbol == LBRACE) 1943 | { 1944 | //print_code(); //输出{ 1945 | getsym(); 1946 | r = judge_statement_list(); 1947 | if (r < 0) 1948 | return r; 1949 | //print_code(); //输出} 1950 | getsym(); 1951 | } 1952 | else if (judge_value_statement() >= 0) 1953 | { 1954 | //print_code(); //输出; 1955 | if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号 1956 | { 1957 | struct error_info error; 1958 | if (mark_newline) 1959 | { 1960 | error.line = linenum - 1; 1961 | mark_newline = 0; 1962 | } 1963 | else 1964 | error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1 1965 | error.type = 'k'; 1966 | print_error(error); 1967 | } 1968 | else 1969 | { 1970 | getsym(); 1971 | } //如果没错,那么还要继续往下读一个 1972 | } 1973 | else if (judge_func_call() >= 0) 1974 | { 1975 | //print_code(); //输出; 1976 | if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号 1977 | { 1978 | struct error_info error; 1979 | if (mark_newline) 1980 | { 1981 | error.line = linenum - 1; 1982 | mark_newline = 0; 1983 | } 1984 | else 1985 | error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1 1986 | error.type = 'k'; 1987 | print_error(error); 1988 | } 1989 | else 1990 | { 1991 | getsym(); 1992 | } //如果没错,那么还要继续往下读一个 1993 | } 1994 | else 1995 | return -1; 1996 | //printf("<语句>\n"); 1997 | if (mark) 1998 | return 1; 1999 | return 0; 2000 | } 2001 | 2002 | int judge_value_statement() 2003 | { 2004 | int r; 2005 | struct name_info name; 2006 | if (symbol != IDENFR) 2007 | { 2008 | return -1; 2009 | } 2010 | //print_code(); //输出标识符 2011 | pre1 = symbol; 2012 | strcpy(content1, s_low); 2013 | flag1 = 1; 2014 | getsym(); 2015 | if (symbol == LPARENT) 2016 | { 2017 | return -1; 2018 | } 2019 | if (search_name_list(content1, 0) < 0) //似乎是除了函数其他的名字随便填一个数就行,不管能不能匹配,找出来不是函数的就行 2020 | { 2021 | struct error_info error; 2022 | error.line = linenum; 2023 | error.type = 'c'; 2024 | print_error(error); 2025 | } 2026 | name = find_name_list(content1); 2027 | if (name.type == 0 || name.type == 1) 2028 | { 2029 | struct error_info error; 2030 | error.line = linenum; 2031 | error.type = 'j'; 2032 | print_error(error); 2033 | } 2034 | while (symbol == LBRACK) 2035 | { 2036 | //print_code(); //输出[ 2037 | flag1 = 0; 2038 | getsym(); 2039 | r = judge_expression(); 2040 | if (r < 0) 2041 | return r; 2042 | if (r != 0) 2043 | { 2044 | struct error_info error; 2045 | error.line = linenum; 2046 | error.type = 'i'; 2047 | print_error(error); 2048 | } 2049 | //print_code(); //输出] 2050 | if (strcmp(s, "]") != 0) 2051 | { 2052 | struct error_info error; 2053 | error.line = linenum; 2054 | error.type = 'm'; 2055 | print_error(error); 2056 | } 2057 | else 2058 | { 2059 | getsym(); 2060 | } 2061 | } 2062 | if (flag1) 2063 | flag1 = 0; 2064 | //print_code(); //输出= 2065 | getsym(); 2066 | r = judge_expression(); 2067 | if (r < 0) 2068 | return r; 2069 | //printf("<赋值语句>\n"); 2070 | return 0; 2071 | } 2072 | 2073 | int judge_condition_statement() 2074 | { 2075 | int r, mark = 0; 2076 | if (symbol != IFTK) 2077 | return -1; 2078 | //print_code(); //输出if 2079 | getsym(); 2080 | //print_code(); //输出( 2081 | getsym(); 2082 | r = judge_condition(); 2083 | if (r < 0) 2084 | return r; 2085 | //print_code(); //输出) 2086 | if (strcmp(s, ")") != 0) 2087 | { 2088 | struct error_info error; 2089 | if (mark_newline) 2090 | { 2091 | error.line = linenum - 1; 2092 | mark_newline = 0; 2093 | } 2094 | else 2095 | error.line = linenum; 2096 | error.type = 'l'; 2097 | print_error(error); 2098 | } 2099 | else 2100 | { 2101 | getsym(); 2102 | } 2103 | r = judge_statement(); 2104 | if (r < 0) 2105 | return r; 2106 | if (r == 1) 2107 | { 2108 | mark = 1; 2109 | } 2110 | if (symbol == ELSETK) 2111 | { 2112 | //print_code();//输出else 2113 | getsym(); 2114 | r = judge_statement(); 2115 | if (r < 0) 2116 | return r; 2117 | if (r == 1) 2118 | { 2119 | mark = 1; 2120 | } 2121 | } 2122 | //printf("<条件语句>\n"); 2123 | if (mark == 1) 2124 | return 1; 2125 | return 0; 2126 | } 2127 | 2128 | int judge_condition() 2129 | { 2130 | int r; 2131 | r = judge_expression(); 2132 | if (r < 0) 2133 | return r; 2134 | if (r != 0) 2135 | { 2136 | struct error_info error; 2137 | error.line = linenum; 2138 | error.type = 'f'; 2139 | print_error(error); 2140 | } 2141 | //print_code(); //输出关系运算符 2142 | getsym(); 2143 | r = judge_expression(); 2144 | if (r < 0) 2145 | return r; 2146 | if (r != 0) 2147 | { 2148 | struct error_info error; 2149 | error.line = linenum; 2150 | error.type = 'f'; 2151 | print_error(error); 2152 | } 2153 | //printf("<条件>\n"); 2154 | return 0; 2155 | } 2156 | 2157 | int judge_circulation_statement() 2158 | { 2159 | int r, mark = 0; 2160 | struct name_info name; 2161 | if (symbol != WHILETK && symbol != FORTK) 2162 | return -1; 2163 | if (symbol == WHILETK) 2164 | { 2165 | //print_code();//输出while 2166 | getsym(); 2167 | //print_code(); //输出( 2168 | getsym(); 2169 | r = judge_condition(); 2170 | if (r < 0) 2171 | return r; 2172 | //print_code(); //输出) 2173 | if (strcmp(s, ")") != 0) 2174 | { 2175 | struct error_info error; 2176 | if (mark_newline) 2177 | { 2178 | error.line = linenum - 1; 2179 | mark_newline = 0; 2180 | } 2181 | else 2182 | error.line = linenum; 2183 | error.type = 'l'; 2184 | print_error(error); 2185 | } 2186 | else 2187 | { 2188 | getsym(); 2189 | } 2190 | r = judge_statement(); 2191 | if (r < 0) 2192 | return r; 2193 | if (r == 1) 2194 | { 2195 | mark = 1; 2196 | } 2197 | } 2198 | else if (symbol == FORTK) 2199 | { 2200 | //print_code(); //输出for 2201 | getsym(); 2202 | //print_code(); //输出( 2203 | getsym(); 2204 | //print_code(); //输出标识符 2205 | if (search_name_list(s_low, 0) < 0) 2206 | { 2207 | struct error_info error; 2208 | error.line = linenum; 2209 | error.type = 'c'; 2210 | print_error(error); 2211 | } 2212 | name = find_name_list(s_low); 2213 | if (name.type == 0 || name.type == 1) 2214 | { 2215 | struct error_info error; 2216 | error.line = linenum; 2217 | error.type = 'j'; 2218 | print_error(error); 2219 | } 2220 | getsym(); 2221 | //print_code(); //输出= 2222 | getsym(); 2223 | r = judge_expression(); 2224 | if (r < 0) 2225 | return r; 2226 | //print_code(); //输出; 2227 | if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号 2228 | { 2229 | struct error_info error; 2230 | if (mark_newline) 2231 | { 2232 | error.line = linenum - 1; 2233 | mark_newline = 0; 2234 | } 2235 | else 2236 | error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1 2237 | error.type = 'k'; 2238 | print_error(error); 2239 | } 2240 | else 2241 | { 2242 | getsym(); 2243 | } //如果没错,那么还要继续往下读一个 2244 | r = judge_condition(); 2245 | if (r < 0) 2246 | return r; 2247 | //print_code(); //输出; 2248 | if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号 2249 | { 2250 | struct error_info error; 2251 | if (mark_newline) 2252 | { 2253 | error.line = linenum - 1; 2254 | mark_newline = 0; 2255 | } 2256 | else 2257 | error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1 2258 | error.type = 'k'; 2259 | print_error(error); 2260 | } 2261 | else 2262 | { 2263 | getsym(); 2264 | } //如果没错,那么还要继续往下读一个 2265 | //print_code(); //输出标识符 2266 | if (search_name_list(s_low, 0) < 0) 2267 | { 2268 | struct error_info error; 2269 | error.line = linenum; 2270 | error.type = 'c'; 2271 | print_error(error); 2272 | } 2273 | name = find_name_list(s_low); 2274 | if (name.type == 0 || name.type == 1) 2275 | { 2276 | struct error_info error; 2277 | error.line = linenum; 2278 | error.type = 'j'; 2279 | print_error(error); 2280 | } 2281 | getsym(); 2282 | //print_code(); //输出= 2283 | getsym(); 2284 | //print_code(); //输出标识符 2285 | if (search_name_list(s_low, 0) < 0) 2286 | { 2287 | struct error_info error; 2288 | error.line = linenum; 2289 | error.type = 'c'; 2290 | print_error(error); 2291 | } 2292 | name = find_name_list(s_low); 2293 | if (name.type == 0 || name.type == 1) 2294 | { 2295 | struct error_info error; 2296 | error.line = linenum; 2297 | error.type = 'j'; 2298 | print_error(error); 2299 | } 2300 | getsym(); 2301 | //print_code(); //输出+或- 2302 | getsym(); 2303 | r = judge_stepsize(); 2304 | if (r < 0) 2305 | return r; 2306 | //print_code(); //输出) 2307 | if (strcmp(s, ")") != 0) 2308 | { 2309 | struct error_info error; 2310 | if (mark_newline) 2311 | { 2312 | error.line = linenum - 1; 2313 | mark_newline = 0; 2314 | } 2315 | else 2316 | error.line = linenum; 2317 | error.type = 'l'; 2318 | print_error(error); 2319 | } 2320 | else 2321 | { 2322 | getsym(); 2323 | } 2324 | r = judge_statement(); 2325 | if (r < 0) 2326 | return r; 2327 | if (r == 1) 2328 | { 2329 | mark = 1; 2330 | } 2331 | } 2332 | //printf("<循环语句>\n"); 2333 | if (mark) 2334 | return 1; 2335 | return 0; 2336 | } 2337 | 2338 | int judge_stepsize() 2339 | { 2340 | int r; 2341 | r = judge_unsigned_integer(); 2342 | if (r < 0) 2343 | return r; 2344 | //printf("<步长>\n"); 2345 | return 0; 2346 | } 2347 | 2348 | int judge_case_statement() 2349 | { 2350 | int r, type; 2351 | if (symbol != SWITCHTK) 2352 | return -1; 2353 | //print_code(); //输出switch 2354 | getsym(); 2355 | //print_code(); //输出( 2356 | getsym(); 2357 | r = judge_expression(); 2358 | if (r < 0) 2359 | return r; 2360 | type = r; 2361 | //print_code(); //输出) 2362 | if (strcmp(s, ")") != 0) 2363 | { 2364 | struct error_info error; 2365 | if (mark_newline) 2366 | { 2367 | error.line = linenum - 1; 2368 | mark_newline = 0; 2369 | } 2370 | else 2371 | error.line = linenum; 2372 | error.type = 'l'; 2373 | print_error(error); 2374 | } 2375 | else 2376 | { 2377 | getsym(); 2378 | } 2379 | //print_code(); //输出{ 2380 | getsym(); 2381 | r = judge_case_list(type); 2382 | if (r < 0) 2383 | return r; 2384 | r = judge_default(); 2385 | if (r < 0) 2386 | return r; 2387 | //print_code(); //输出} 2388 | getsym(); //这里应该暂时不需要进行关于错误p的额外的判断,因为如果没有错误必定当前是},往下读就行了 2389 | //printf("<情况语句>\n"); 2390 | return 0; 2391 | } 2392 | 2393 | int judge_case_list(int type) 2394 | { 2395 | int r; 2396 | r = judge_case_son_statement(type); 2397 | if (r < 0) 2398 | return r; 2399 | while (judge_case_son_statement(type) == 0) 2400 | { 2401 | //应该不需要干啥。。。 2402 | } 2403 | //printf("<情况表>\n"); 2404 | return 0; 2405 | } 2406 | 2407 | int judge_case_son_statement(int type) 2408 | { 2409 | int r; 2410 | if (symbol != CASETK) 2411 | return -1; 2412 | //print_code(); //输出case 2413 | getsym(); 2414 | r = judge_const(); 2415 | if (r < 0) 2416 | return r; 2417 | if ((r == 0 && type == 1) || (r == 1 && type == 0)) 2418 | { 2419 | struct error_info error; 2420 | error.line = linenum; 2421 | error.type = 'o'; 2422 | print_error(error); 2423 | } 2424 | //print_code();//输出: 2425 | getsym(); 2426 | r = judge_statement(); 2427 | if (r < 0) 2428 | return r; 2429 | //printf("<情况子语句>\n"); 2430 | return 0; 2431 | } 2432 | 2433 | int judge_default() 2434 | { 2435 | int r; 2436 | if (symbol != DEFAULTTK) 2437 | { 2438 | //TODO 2439 | struct error_info error; 2440 | error.line = linenum; 2441 | error.type = 'p'; 2442 | print_error(error); 2443 | return 1; //应该是不返回-1不然就判断错误不往下走了,注意该想法有可能有错误 2444 | } 2445 | //print_code(); //输出default 2446 | getsym(); 2447 | //print_code(); //输出: 2448 | getsym(); 2449 | r = judge_statement(); 2450 | if (r < 0) 2451 | return r; 2452 | //printf("<缺省>\n"); 2453 | return 0; 2454 | } 2455 | 2456 | /*int judge_returnfunc_use() 2457 | { 2458 | int r; 2459 | char tmp[1000]; 2460 | if ((flag1 == 0 && symbol != IDENFR) || (flag1 == 1 && pre1 != IDENFR)) { 2461 | return -1; 2462 | } 2463 | if (flag1) 2464 | { 2465 | if (symbol != LPARENT) 2466 | { 2467 | return -1; 2468 | } 2469 | //print_code_pre1(); 2470 | strcpy(tmp, content1); 2471 | if (find_no_return_func(tmp) == 1) 2472 | { 2473 | return -1; 2474 | } 2475 | flag1 = 0; 2476 | } 2477 | else 2478 | { 2479 | //print_code(); 2480 | getsym(); //读入( 2481 | if (symbol != LPARENT) 2482 | { 2483 | return -1; 2484 | } 2485 | strcpy(tmp, s_low); 2486 | if (find_no_return_func(tmp) == 1) 2487 | { 2488 | return -1; 2489 | } 2490 | } 2491 | if (search_name_list(tmp, 4) < 0) 2492 | { 2493 | struct error_info error; 2494 | error.line = linenum; 2495 | error.type = 'c'; 2496 | print_error(error); 2497 | } 2498 | //print_code(); //输出( 2499 | getsym(); 2500 | r = judge_value_parameter_list(); 2501 | if (r < 0) 2502 | return r; 2503 | //print_code(); //输出) 2504 | if (strcmp(s, ")") != 0) 2505 | { 2506 | struct error_info error; 2507 | if (mark_newline) 2508 | { 2509 | error.line = linenum - 1; 2510 | mark_newline = 0; 2511 | } 2512 | else 2513 | error.line = linenum; 2514 | error.type = 'l'; 2515 | print_error(error); 2516 | } 2517 | else 2518 | { 2519 | getsym(); 2520 | } 2521 | //printf("<有返回值函数调用语句>\n"); 2522 | return 0; 2523 | } 2524 | 2525 | int judge_noreturnfunc_use() 2526 | { 2527 | int r; 2528 | char tmp[1000]; 2529 | if ((flag1 == 0 && symbol != IDENFR) || (flag1 == 1 && pre1 != IDENFR)) { 2530 | return -1; 2531 | } 2532 | if (flag1) 2533 | { 2534 | if (symbol != LPARENT) 2535 | { 2536 | return -1; 2537 | } 2538 | flag1 = 0; 2539 | strcpy(tmp, content1); 2540 | } 2541 | else 2542 | { 2543 | 2544 | strcpy(tmp, s_low); 2545 | } 2546 | if (find_no_return_func(tmp) == 0) 2547 | return -1; 2548 | //print_code(); //输出标识符 2549 | if (search_name_list(s_low, 4) < 0) 2550 | { 2551 | struct error_info error; 2552 | error.line = linenum; 2553 | error.type = 'c'; 2554 | print_error(error); 2555 | } 2556 | getsym(); 2557 | //print_code(); //输出( 2558 | getsym(); 2559 | r = judge_value_parameter_list(); 2560 | if (r < 0) 2561 | return -1; 2562 | //print_code(); //输出) 2563 | if (strcmp(s, ")") != 0) 2564 | { 2565 | struct error_info error; 2566 | if (mark_newline) 2567 | { 2568 | error.line = linenum - 1; 2569 | mark_newline = 0; 2570 | } 2571 | else 2572 | error.line = linenum; 2573 | error.type = 'l'; 2574 | print_error(error); 2575 | } 2576 | else 2577 | { 2578 | getsym(); 2579 | } 2580 | //printf("<无返回值函数调用语句>\n"); 2581 | return 0; 2582 | }*/ 2583 | 2584 | int judge_func_call() 2585 | { 2586 | int r; 2587 | char tmp[1000]; 2588 | if ((flag1 == 0 && symbol != IDENFR) || (flag1 == 1 && pre1 != IDENFR)) { 2589 | return -1; 2590 | } 2591 | if (flag1) 2592 | { 2593 | //print_code_pre1(); 2594 | strcpy(tmp, content1); 2595 | flag1 = 0; 2596 | } 2597 | else 2598 | { 2599 | //print_code(); 2600 | getsym(); //读入( 2601 | strcpy(tmp, s_low); 2602 | } 2603 | if (search_name_list(tmp, 4) < 0) 2604 | { 2605 | struct error_info error; 2606 | error.line = linenum; 2607 | error.type = 'c'; 2608 | print_error(error); 2609 | } 2610 | if (symbol != LPARENT) 2611 | { 2612 | return -1; 2613 | } 2614 | //print_code(); //输出( 2615 | getsym(); 2616 | r = judge_value_parameter_list(tmp); 2617 | if (r < 0) 2618 | return r; 2619 | //print_code(); //输出) 2620 | if (strcmp(s, ")") != 0) 2621 | { 2622 | struct error_info error; 2623 | if (mark_newline) 2624 | { 2625 | error.line = linenum - 1; 2626 | mark_newline = 0; 2627 | } 2628 | else 2629 | error.line = linenum; 2630 | error.type = 'l'; 2631 | print_error(error); 2632 | } 2633 | else 2634 | { 2635 | getsym(); 2636 | } 2637 | struct func_info func = search_function(tmp); 2638 | if (func.type == 1) 2639 | { 2640 | return 0; 2641 | } 2642 | else 2643 | { 2644 | return 1; 2645 | } 2646 | return 0; 2647 | } 2648 | 2649 | int judge_value_parameter_list(char* name) 2650 | { 2651 | int r, sum = 0; 2652 | struct func_info func = search_function(name); 2653 | if (symbol == RPARENT || symbol == SEMICN) 2654 | { //空的情况 2655 | //printf("<值参数表>\n"); 2656 | if (sum != func.parameter_sum) 2657 | { 2658 | struct error_info error; 2659 | error.line = linenum; 2660 | error.type = 'd'; 2661 | print_error(error); 2662 | } 2663 | return 0; 2664 | } 2665 | r = judge_expression(); 2666 | if (r < 0) 2667 | return r; 2668 | if (r != func.parameter_type[sum] && func.parameter_sum > 0) 2669 | { 2670 | struct error_info error; 2671 | error.line = linenum; 2672 | error.type = 'e'; 2673 | print_error(error); 2674 | } 2675 | sum++; 2676 | while (symbol == COMMA) 2677 | { 2678 | //print_code(); 2679 | getsym(); 2680 | r = judge_expression(); 2681 | if (r < 0) 2682 | return r; 2683 | if (r != func.parameter_type[sum] && func.parameter_sum > 0) 2684 | { 2685 | struct error_info error; 2686 | error.line = linenum; 2687 | error.type = 'e'; 2688 | print_error(error); 2689 | } 2690 | sum++; 2691 | } 2692 | if (sum != func.parameter_sum) 2693 | { 2694 | struct error_info error; 2695 | error.line = linenum; 2696 | error.type = 'd'; 2697 | print_error(error); 2698 | } 2699 | //printf("<值参数表>\n"); 2700 | return 0; 2701 | } 2702 | 2703 | int judge_statement_list() 2704 | { 2705 | int r, mark = 0; 2706 | while ((r = judge_statement()) >= 0) 2707 | { 2708 | if (r == 1) 2709 | { 2710 | mark = 1; 2711 | } 2712 | //应该不用干什么。。。 2713 | } 2714 | if (mark) 2715 | { 2716 | return 1; 2717 | } 2718 | //printf("<语句列>\n"); 2719 | return 0; 2720 | } 2721 | 2722 | int judge_scanf() 2723 | { 2724 | struct name_info name; 2725 | if (symbol != SCANFTK) 2726 | return -1; 2727 | //print_code(); //输出scanf 2728 | getsym(); 2729 | //print_code(); //输出( 2730 | getsym(); 2731 | //print_code(); //输出标识符 2732 | if (search_name_list(s_low, 0) < 0) 2733 | { 2734 | struct error_info error; 2735 | error.line = linenum; 2736 | error.type = 'c'; 2737 | print_error(error); 2738 | } 2739 | name = find_name_list(s_low); 2740 | if (name.type == 0 || name.type == 1) 2741 | { 2742 | struct error_info error; 2743 | error.line = linenum; 2744 | error.type = 'j'; 2745 | print_error(error); 2746 | } 2747 | getsym(); 2748 | //print_code(); //输出) 2749 | if (strcmp(s, ")") != 0) 2750 | { 2751 | struct error_info error; 2752 | if (mark_newline) 2753 | { 2754 | error.line = linenum - 1; 2755 | mark_newline = 0; 2756 | } 2757 | else 2758 | error.line = linenum; 2759 | error.type = 'l'; 2760 | print_error(error); 2761 | } 2762 | else 2763 | { 2764 | getsym(); 2765 | } 2766 | //printf("<读语句>\n"); 2767 | return 0; 2768 | } 2769 | 2770 | int judge_printf() 2771 | { 2772 | int r; 2773 | if (symbol != PRINTFTK) 2774 | return -1; 2775 | //print_code(); //输出printf 2776 | getsym(); 2777 | //print_code(); //输出( 2778 | getsym(); 2779 | if (judge_string() == 0) 2780 | { 2781 | //发现有字符串,判断有没有逗号来看有没有表达式 2782 | if (symbol == COMMA) 2783 | { 2784 | //print_code(); //输出, 2785 | getsym(); 2786 | r = judge_expression(); 2787 | if (r < 0) 2788 | return r; 2789 | } 2790 | } 2791 | else if (judge_expression() == 0) 2792 | { 2793 | 2794 | } 2795 | //print_code(); //输出) 2796 | if (strcmp(s, ")") != 0) 2797 | { 2798 | struct error_info error; 2799 | if (mark_newline) 2800 | { 2801 | error.line = linenum - 1; 2802 | mark_newline = 0; 2803 | } 2804 | else 2805 | error.line = linenum; 2806 | error.type = 'l'; 2807 | print_error(error); 2808 | } 2809 | else 2810 | { 2811 | getsym(); 2812 | } 2813 | //printf("<写语句>\n"); 2814 | return 0; 2815 | } 2816 | 2817 | int judge_return_statement() 2818 | { 2819 | int r, mark = 0, flag = 0; 2820 | if (symbol != RETURNTK) 2821 | return -1; 2822 | //print_code(); //输出return 2823 | getsym(); 2824 | if (symbol == LPARENT) 2825 | { 2826 | mark = 1; 2827 | //print_code(); //输出( 2828 | getsym(); 2829 | r = judge_expression(); 2830 | if (r < 0) 2831 | { 2832 | flag = 1; 2833 | } 2834 | //print_code(); //输出) 2835 | if (strcmp(s, ")") != 0) 2836 | { 2837 | struct error_info error; 2838 | if (mark_newline) 2839 | { 2840 | error.line = linenum - 1; 2841 | mark_newline = 0; 2842 | } 2843 | else 2844 | error.line = linenum; 2845 | error.type = 'l'; 2846 | print_error(error); 2847 | } 2848 | else 2849 | { 2850 | getsym(); 2851 | } 2852 | } 2853 | if (mark == 1 && func_type == 0) 2854 | { 2855 | struct error_info error; 2856 | error.line = linenum; 2857 | error.type = 'g'; 2858 | print_error(error); 2859 | } 2860 | else if ((func_type == 1 && (mark == 0 || r == 1)) || (func_type == 2 && (mark == 0 || r == 0))) 2861 | { 2862 | struct error_info error; 2863 | error.line = linenum; 2864 | error.type = 'h'; 2865 | print_error(error); 2866 | } 2867 | if (flag && func_type == 1) 2868 | { 2869 | struct error_info error; 2870 | error.line = linenum; 2871 | error.type = 'h'; 2872 | print_error(error); 2873 | } 2874 | 2875 | //printf("<返回语句>\n"); 2876 | return 0; 2877 | } 2878 | int main() 2879 | { 2880 | getsym(); 2881 | judge_program(); 2882 | } --------------------------------------------------------------------------------