├── .gitignore ├── GramOrigin ├── function.cpp ├── function.h ├── grammer.cpp ├── grammer.h ├── lexical.cpp ├── lexical.h ├── main.cpp ├── midCode.cpp ├── midCode.h ├── mipsCode.cpp ├── mipsCode.h └── symbolItem.h ├── README.md ├── function.cpp ├── function.h ├── grammer.cpp ├── grammer.h ├── lexical.cpp ├── lexical.h ├── main.cpp ├── midCode.cpp ├── midCode.h ├── mipsCode.cpp ├── mipsCode.h ├── optimize.cpp ├── optimize.h ├── symbolItem.h ├── test ├── error-CALL.txt ├── error-PUTARRAY.txt ├── error-比较运算符.txt ├── sRegTest.txt ├── sRegTestRes.txt ├── test │ ├── 16061160_test1.txt │ ├── 16061160_test10.txt │ ├── 16061160_test11.txt │ ├── 16061160_test12.txt │ ├── 16061160_test13.txt │ ├── 16061160_test2.txt │ ├── 16061160_test3.txt │ ├── 16061160_test4.txt │ ├── 16061160_test5.txt │ ├── 16061160_test6.txt │ ├── 16061160_test7.txt │ ├── 16061160_test8.txt │ ├── 16061160_test9.txt │ ├── 16231246.txt │ ├── czy.txt │ ├── fc.txt │ ├── mee │ │ ├── testfile1.txt │ │ ├── testfile2.txt │ │ ├── testfile3.txt │ │ ├── testfile4.txt │ │ ├── testfile5.txt │ │ └── testfile6.txt │ ├── mzy.txt │ ├── old16231246.txt │ ├── qys.txt │ ├── testFibnaci.txt │ ├── testarray.txt │ ├── testbigparatable.txt │ ├── testclang.txt │ ├── testcompute.txt │ ├── testdeeppara.txt │ ├── testfile1.txt │ ├── testfile2.txt │ ├── testfile3.txt │ ├── testfile4.txt │ ├── testfile6.txt │ ├── testfile7.txt │ ├── testfile8.txt │ ├── testfile9.txt │ ├── testfunction.txt │ ├── testglobalandlocal.txt │ ├── testio.txt │ ├── testloop.txt │ ├── testmergesort.txt │ ├── testprime.txt │ ├── testpublic3.txt │ ├── testsmallfunction.txt │ └── teststoreload.txt ├── use-def错误.txt ├── 代码生成样例.txt ├── 文法.txt ├── 新建文本文档.txt ├── 竞速.txt ├── 竞速排序输出.txt ├── 错误处理前导0.txt └── 错误处理样例.txt ├── 编译申优文档.md └── 编译申优文档.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | /*.txt 2 | /Debug 3 | Gram.* 4 | *.zip 5 | *.rar -------------------------------------------------------------------------------- /GramOrigin/function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "symbolItem.h" 5 | #include "function.h" 6 | using namespace std; 7 | 8 | extern map globalSymbolTable; 9 | extern map localSymbolTable; 10 | extern map> allLocalSymbolTable; //保存所有的局部符号表 用于保留变量的地址 11 | extern vector stringList; //保存所有的字符串 12 | int labelId = 0; //标号的id 13 | int tmpVarId = 0; //中间变量的id 14 | 15 | void showGlobal() { 16 | cout << "----------------\n"; 17 | for (map::iterator it = globalSymbolTable.begin(); it != globalSymbolTable.end(); it++) { 18 | (*it).second.output(); 19 | } 20 | cout << "----------------\n"; 21 | } 22 | 23 | void showLocal() { 24 | cout << "----------------\n"; 25 | for (map::iterator it = localSymbolTable.begin(); it != localSymbolTable.end(); it++) { 26 | (*it).second.output(); 27 | } 28 | cout << "----------------\n"; 29 | } 30 | 31 | void showAll() { 32 | cout << "----------------\n"; 33 | for (map>::iterator it = allLocalSymbolTable.begin(); it != allLocalSymbolTable.end(); it++) { 34 | cout << "Func: " << (*it).first << "\n"; 35 | map ss = (*it).second; 36 | for (map::iterator i = ss.begin(); i != ss.end(); i++) { 37 | (*i).second.output(); 38 | } 39 | } 40 | cout << "----------------\n"; 41 | } 42 | 43 | void showString() { 44 | cout << "Show strings:\n"; 45 | for (int i = 0; i < stringList.size(); i++) { 46 | cout << stringList[i] << "\n"; 47 | } 48 | } 49 | 50 | string int2string(int t) { 51 | stringstream ss; 52 | ss << t; 53 | return ss.str(); 54 | } 55 | 56 | int string2int(string s) { 57 | stringstream ss; 58 | ss << s; 59 | int t; 60 | ss >> t; 61 | return t; 62 | } 63 | 64 | string genLabel() { 65 | labelId++; 66 | return "Label" + int2string(labelId); 67 | } 68 | 69 | string genTmp() { 70 | tmpVarId++; 71 | return "T" + int2string(tmpVarId); 72 | } -------------------------------------------------------------------------------- /GramOrigin/function.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | void showGlobal(); 7 | 8 | void showLocal(); 9 | 10 | void showAll(); 11 | 12 | void showString(); 13 | 14 | string int2string(int t); //修改 15 | 16 | int string2int(string s); //修改 17 | 18 | string genLabel(); 19 | 20 | string genTmp(); 21 | -------------------------------------------------------------------------------- /GramOrigin/grammer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef GRAMMER_H 3 | #define GRAMMER_H 4 | 5 | #include 6 | using namespace std; 7 | 8 | //<字符串>   ::=  "{十进制编码为32,33,35-126的ASCII字符}" 9 | bool strings(); 10 | //<程序> ::= [<常量说明>][<变量说明>]{<有返回值函数定义>|<无返回值函数定义>}<主函数> 11 | bool procedure(); 12 | //<常量说明> ::=  const<常量定义>;{ const<常量定义>;} 13 | bool constDeclaration(bool isglobal); 14 | //<常量定义> ::= int<标识符>=<整数>{,<标识符>=<整数>} 15 | //   | char<标识符>=<字符>{,<标识符>=<字符>} 16 | bool constDefinition(bool isglobal); 17 | //<无符号整数>  ::= <非零数字>{<数字>}| 0 18 | bool unsignedInteger(int& value); 19 | //<整数> ::= [+|-]<无符号整数> 20 | bool integer(int& value); 21 | //<声明头部>   ::=  int<标识符> |char<标识符> 22 | bool declarationHead(string& tmp, int& type); 23 | //<变量说明>  ::= <变量定义>;{<变量定义>;} 24 | bool variableDeclaration(bool isglobal); 25 | //<变量定义>  ::= <类型标识符>(<标识符>|<标识符>'['<无符号整数>']') 26 | // {,(<标识符>|<标识符>'['<无符号整数>']' )} 27 | bool variableDefinition(bool isglobal); 28 | //<有返回值函数定义>  ::=  <声明头部>'('<参数表>')' '{'<复合语句>'}’ 29 | bool haveReturnValueFunction(); 30 | //<无返回值函数定义>  ::= void<标识符>'('<参数表>')''{'<复合语句>'}’ 31 | bool noReturnValueFunction(); 32 | //<参数表>    ::=  <类型标识符><标识符>{,<类型标识符><标识符>}| <空> 33 | bool parameterTable(string funcName, bool isRedefine); 34 | //<复合语句> ::=  [<常量说明>][<变量说明>]<语句列> 35 | bool compoundStatement(); 36 | //<主函数>    ::= void main‘(’‘)’ ‘{’<复合语句>‘}’ 37 | bool mainFunction(); 38 | //<表达式>    ::= [+|-]<项>{<加法运算符><项>}   39 | bool expression(int& type, string& ansTmp); 40 | //<项>     ::= <因子>{<乘法运算符><因子>} 41 | bool item(int& type, string& ansTmp); 42 | //<因子>    ::= <标识符>|<标识符>'['<表达式>']'|'('<表达式>')'|<整数>|<字符>|<有返回值函数调用语句> 43 | bool factor(int& type, string& ansTmp); 44 | //<语句>    ::= <条件语句>|<循环语句>| '{'<语句列>'}'| <有返回值函数调用语句>;  45 | // |<无返回值函数调用语句>;|<赋值语句>;|<读语句>;|<写语句>;|<空>;|<返回语句>; 46 | bool statement(); 47 | //<赋值语句>   ::=  <标识符>=<表达式>|<标识符>'['<表达式>']'=<表达式> 48 | bool assignStatement(); 49 | //<条件语句>  ::= if '('<条件>')'<语句>[else<语句>] 50 | bool conditionStatement(); 51 | //<条件>  ::=  <表达式><关系运算符><表达式>|<表达式> 52 | bool condition(string& result); 53 | //<循环语句>   ::=  while '('<条件>')'<语句>| do<语句>while '('<条件>')' 54 | // |for'('<标识符>=<表达式>;<条件>;<标识符>=<标识符>(+|-)<步长>')'<语句> 55 | bool repeatStatement(); 56 | //<步长>::= <无符号整数> 57 | bool step(int& value); 58 | //<有返回值函数调用语句> ::= <标识符>'('<值参数表>')’ 59 | bool callHaveReturnValueFunction(); 60 | //<无返回值函数调用语句> ::= <标识符>'('<值参数表>')’ 61 | bool callNoReturnValueFunction(); 62 | //<值参数表>  ::= <表达式>{,<表达式>}|<空> 63 | bool valueParameterTable(string funcName); 64 | //<语句列>   ::= {<语句>} 65 | bool statementList(); 66 | //<读语句>    ::=  scanf '('<标识符>{,<标识符>}')’ 67 | bool readStatement(); 68 | //<写语句>    ::= printf '(' <字符串>,<表达式> ')'| printf '('<字符串> ')'| printf '('<表达式>')’ 69 | bool writeStatement(); 70 | //<返回语句>   ::=  return['('<表达式>')']   71 | bool returnStatement(); 72 | 73 | 74 | #endif // !GRAMMER_H 75 | -------------------------------------------------------------------------------- /GramOrigin/lexical.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "lexical.h" 6 | using namespace std; 7 | 8 | char ch; 9 | char token[100000]; 10 | int tokenI = 0; 11 | int num; //记录整形常量 12 | char con_ch; //记录字符型常量 13 | char s[100000]; //记录字符串常量 14 | enum typeId symbol; 15 | 16 | int len_reservedWord = 13; 17 | char reservedWord[20][10] = { 18 | "const", "int", "char", "void", "main", "if", "else", "do", "while", "for", "scanf", "printf", "return", 19 | }; 20 | 21 | int indexs = 0; //文件的索引 22 | int oldIndex; //用于做恢复 23 | int line = 1; //记录行号 24 | string filecontent; //文件的内容 25 | 26 | extern ofstream outputfile; 27 | extern ofstream errorfile; 28 | 29 | bool isSpace() 30 | { 31 | return (ch == ' '); 32 | } 33 | 34 | bool isNewline() 35 | { 36 | return (ch == '\n'); 37 | } 38 | 39 | bool isBlank() 40 | { 41 | return (ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r' || ch == '\f' || ch == '\v'); 42 | } 43 | 44 | bool isLetter() 45 | { 46 | return ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch == '_')); 47 | } 48 | 49 | bool isDigit() 50 | { 51 | return (ch >= '0' && ch <= '9'); 52 | } 53 | 54 | bool isPlus() 55 | { 56 | return (ch == '+'); 57 | } 58 | 59 | bool isMinu() 60 | { 61 | return (ch == '-'); 62 | } 63 | 64 | bool isMult() 65 | { 66 | return (ch == '*'); 67 | } 68 | 69 | bool isDiv() 70 | { 71 | return (ch == '/'); 72 | } 73 | 74 | bool isChar() { 75 | return (isLetter() || isDigit() || isPlus() || isMinu() || isMult() || isDiv()); 76 | } 77 | 78 | bool isLss() 79 | { 80 | return (ch == '<'); 81 | } 82 | 83 | bool isGre() 84 | { 85 | return (ch == '>'); 86 | } 87 | 88 | bool isExcla() 89 | { 90 | return (ch == '!'); 91 | } 92 | 93 | bool isAssign() 94 | { 95 | return (ch == '='); 96 | } 97 | 98 | bool isSemicn() 99 | { 100 | return (ch == ';'); 101 | } 102 | 103 | bool isComma() 104 | { 105 | return (ch == ','); 106 | } 107 | 108 | bool isLparent() 109 | { 110 | return (ch == '('); 111 | } 112 | 113 | bool isRparent() 114 | { 115 | return (ch == ')'); 116 | } 117 | 118 | bool isLbrack() 119 | { 120 | return (ch == '['); 121 | } 122 | 123 | bool isRbrack() 124 | { 125 | return (ch == ']'); 126 | } 127 | 128 | bool isLbrace() 129 | { 130 | return (ch == '{'); 131 | } 132 | 133 | bool isRbrace() 134 | { 135 | return (ch == '}'); 136 | } 137 | 138 | bool isSquo() 139 | { 140 | return (ch == '\''); 141 | } 142 | 143 | bool isDquo() 144 | { 145 | return (ch == '\"'); 146 | } 147 | 148 | bool isEOF() 149 | { 150 | //return ch == EOF; 151 | return indexs >= filecontent.size(); 152 | } 153 | 154 | bool isStringChar() 155 | { 156 | return ((ch >= 35 && ch <= 126) || ch == 32 || ch == 33); 157 | } 158 | 159 | bool isFactorFellow() 160 | { 161 | return (isLss() || isGre() || isAssign() || isExcla() 162 | || isPlus() || isMinu() || isMult() || isDiv() 163 | || isRparent() || isRbrack() || isComma() || isSemicn()); 164 | } 165 | 166 | void clearToken() 167 | { //清空token 168 | tokenI = 0; 169 | } 170 | 171 | void catToken() 172 | { //把当前字符ch拼到token上 173 | token[tokenI++] = ch; 174 | } 175 | 176 | void get_ch() 177 | { //读一个字符 178 | ch = filecontent[indexs++]; 179 | if (ch == '\n') { 180 | line++; 181 | } 182 | } 183 | 184 | void retract() 185 | { //把字符写回到输入流 186 | if (ch == '\n') { 187 | line--; 188 | } 189 | indexs--; 190 | } 191 | 192 | void retractString(int old) { 193 | for (int i = old; i < indexs; i++) { 194 | if (filecontent[i] == '\n') { 195 | line--; 196 | } 197 | } 198 | indexs = old; 199 | } 200 | 201 | int reserver() 202 | { //查找保留字 返回-1代表是标识符 不是保留字 否则返回保留字编码 203 | for (int i = 0; i < len_reservedWord; i++) { 204 | if (strcmp(reservedWord[i], token) == 0) { 205 | return i; 206 | } 207 | } 208 | return -1; 209 | } 210 | 211 | int transNum() 212 | { 213 | int res = 0; 214 | for (int i = 0; i < tokenI; i++) { 215 | res = res * 10 + (token[i] - '0'); 216 | } 217 | return res; 218 | } 219 | 220 | int getsym(int out) 221 | { 222 | oldIndex = indexs; //记录没有读取之前的indexs为oldIndex 223 | clearToken(); 224 | get_ch(); 225 | while (isBlank()) { 226 | get_ch(); 227 | }// 一直读 直到ch不是空白符 228 | if (isEOF()) { 229 | return -1; 230 | } 231 | if (isLetter()) { // 'a'-'z' 'A'-'Z' '_' 232 | while (isLetter() || isDigit()) { //拼标识符 <标识符>::=<字母>{<字母>|<数字>} 233 | catToken(); //把ch连接到token中 234 | get_ch(); //读ch 235 | } 236 | //此时ch不再是字母或者数字了 说明标识符结束了 237 | retract(); //回退ch 238 | token[tokenI] = '\0'; 239 | int resultValue = reserver(); //查找保留字表 240 | if (resultValue == -1) { //标识符 241 | symbol = IDENFR; 242 | } 243 | else { //保留字 244 | symbol = (typeId)resultValue; 245 | } 246 | return 1; 247 | } 248 | else if (isDigit()) { // '0'-'9' 数字 249 | while (isDigit()) { 250 | catToken(); 251 | get_ch(); 252 | } 253 | //此时ch不再是数字了 254 | retract(); 255 | token[tokenI] = '\0'; 256 | num = transNum(); 257 | symbol = INTCON; //整型常量 258 | return 1; 259 | } 260 | else if (isSquo()) { // ' 261 | get_ch(); 262 | if (isChar()) { 263 | char tmp = ch; //暂时记录 264 | get_ch(); 265 | if (isSquo()) { 266 | con_ch = tmp; 267 | symbol = CHARCON; //字符常量 268 | } 269 | else { 270 | //wrong! retract(); 缺少右单引号 不符合词法 271 | if (out) { 272 | errorfile << line << " a\n"; //不符合词法 273 | } 274 | int old = indexs; 275 | while (1) { 276 | if (isSquo()) { 277 | break; 278 | } 279 | if (isNewline()) { 280 | retractString(old-1); //回到这个读错了的 本该是'的位置 281 | break; 282 | } 283 | if (isFactorFellow()) { //, ; + - * / > < ! = 284 | indexs--; //需要把,;退回去 285 | break; 286 | } 287 | get_ch(); 288 | } 289 | con_ch = tmp; 290 | symbol = CHARCON; //字符常量 291 | } 292 | } 293 | else { 294 | //wrong! retract(); 字符不是a-z A-Z _+-*/ 295 | if (out) { 296 | errorfile << line << " a\n"; //不符合词法 297 | } 298 | char tmp = ch; 299 | get_ch(); 300 | if (isSquo()) { 301 | con_ch = tmp; 302 | symbol = CHARCON; //字符常量 303 | } 304 | else { 305 | //wrong! retract(); 缺少右单引号 不符合词法 306 | int old = indexs; 307 | while (1) { 308 | if (isSquo()) { 309 | break; 310 | } 311 | if (isNewline()) { 312 | retractString(old - 1); //回到这个读错了的 本该是'的位置 313 | break; 314 | } 315 | if (isFactorFellow()) { //, ; + - * / > < ! = 316 | indexs--; //需要把,;退回去 317 | break; 318 | } 319 | get_ch(); 320 | } 321 | con_ch = tmp; 322 | symbol = CHARCON; //字符常量 323 | } 324 | } 325 | return 1; 326 | } 327 | else if (isDquo()) { // " 328 | get_ch(); 329 | while (isStringChar()) { 330 | catToken(); 331 | get_ch(); 332 | } 333 | //此时ch不再是可以组成字符串的字符 334 | if (isDquo()) { //遇到了结束的双引号 335 | symbol = STRCON; //字符串常量 336 | token[tokenI] = '\0'; 337 | strcpy(s, token); //存到s中 338 | } 339 | else { //wrong! retract(); 缺少右双引号 不符合词法 340 | if (isNewline()) { 341 | line--; 342 | if (out) { 343 | errorfile << line << " a\n"; //不符合词法 344 | } 345 | while (1) { 346 | indexs--; 347 | if (filecontent[indexs] == ')') { 348 | break; 349 | } 350 | tokenI--; 351 | } 352 | symbol = STRCON; //字符串常量 353 | token[tokenI] = '\0'; 354 | strcpy(s, token); //存到s中 355 | } 356 | } 357 | return 1; 358 | } 359 | else if (isPlus()) { // + 360 | symbol = PLUS; 361 | return 1; 362 | } 363 | else if (isMinu()) { // - 364 | symbol = MINU; 365 | return 1; 366 | } 367 | else if (isMult()) { // * 368 | symbol = MULT; 369 | return 1; 370 | } 371 | else if (isDiv()) { // / 372 | symbol = DIV; 373 | return 1; 374 | } 375 | else if (isLss()) { // < 376 | get_ch(); 377 | if (isAssign()) { //<= 378 | symbol = LEQ; 379 | } 380 | else { 381 | symbol = LSS; //< 382 | retract(); //回退 383 | } 384 | return 1; 385 | } 386 | else if (isGre()) { // > 387 | get_ch(); 388 | if (isAssign()) { //>= 389 | symbol = GEQ; 390 | } 391 | else { 392 | symbol = GRE; //> 393 | retract(); //回退 394 | } 395 | return 1; 396 | } 397 | else if (isExcla()) { // ! 398 | get_ch(); 399 | if (isAssign()) { // != 400 | symbol = NEQ; 401 | } 402 | else { 403 | //wrong! retract(); !后边缺少= 404 | retract(); 405 | if (out) { 406 | errorfile << line << " a\n"; //不符合词法 407 | } 408 | symbol = NEQ; 409 | } 410 | return 1; 411 | } 412 | else if (isAssign()) { // = 413 | get_ch(); 414 | if (isAssign()) { //== 415 | symbol = EQL; 416 | } 417 | else { 418 | symbol = ASSIGN; 419 | retract(); //回退 420 | } 421 | return 1; 422 | } 423 | else if (isSemicn()) { // ; 424 | symbol = SEMICN; 425 | return 1; 426 | } 427 | else if (isComma()) { // , 428 | symbol = COMMA; 429 | return 1; 430 | } 431 | else if (isLparent()) { // ( 432 | symbol = LPARENT; 433 | return 1; 434 | } 435 | else if (isRparent()) { // ) 436 | symbol = RPARENT; 437 | return 1; 438 | } 439 | else if (isLbrack()) { // [ 440 | symbol = LBRACK; 441 | return 1; 442 | } 443 | else if (isRbrack()) { // ] 444 | symbol = RBRACK; 445 | return 1; 446 | } 447 | else if (isLbrace()) { // { 448 | symbol = LBRACE; 449 | return 1; 450 | } 451 | else if (isRbrace()) { // } 452 | symbol = RBRACE; 453 | return 1; 454 | } 455 | else { 456 | //wrong! retract(); 457 | if (out) { 458 | errorfile << line << " a\n"; //不符合词法 459 | } 460 | return getsym(out); 461 | } 462 | } 463 | 464 | void doOutput() 465 | { 466 | switch (symbol) { 467 | case IDENFR: 468 | outputfile << "IDENFR " << token << endl; 469 | break; 470 | case INTCON: 471 | outputfile << "INTCON " << token << endl; 472 | break; 473 | case CHARCON: 474 | outputfile << "CHARCON " << con_ch << endl; 475 | break; 476 | case STRCON: 477 | outputfile << "STRCON " << s << endl; 478 | break; 479 | case CONSTTK: 480 | outputfile << "CONSTTK const" << endl; 481 | break; 482 | case INTTK: 483 | outputfile << "INTTK int" << endl; 484 | break; 485 | case CHARTK: 486 | outputfile << "CHARTK char" << endl; 487 | break; 488 | case VOIDTK: 489 | outputfile << "VOIDTK void" << endl; 490 | break; 491 | case MAINTK: 492 | outputfile << "MAINTK main" << endl; 493 | break; 494 | case IFTK: 495 | outputfile << "IFTK if" << endl; 496 | break; 497 | case ELSETK: 498 | outputfile << "ELSETK else" << endl; 499 | break; 500 | case DOTK: 501 | outputfile << "DOTK do" << endl; 502 | break; 503 | case WHILETK: 504 | outputfile << "WHILETK while" << endl; 505 | break; 506 | case FORTK: 507 | outputfile << "FORTK for" << endl; 508 | break; 509 | case SCANFTK: 510 | outputfile << "SCANFTK scanf" << endl; 511 | break; 512 | case PRINTFTK: 513 | outputfile << "PRINTFTK printf" << endl; 514 | break; 515 | case RETURNTK: 516 | outputfile << "RETURNTK return" << endl; 517 | break; 518 | case PLUS: 519 | outputfile << "PLUS +" << endl; 520 | break; 521 | case MINU: 522 | outputfile << "MINU -" << endl; 523 | break; 524 | case MULT: 525 | outputfile << "MULT *" << endl; 526 | break; 527 | case DIV: 528 | outputfile << "DIV /" << endl; 529 | break; 530 | case LSS: 531 | outputfile << "LSS <" << endl; 532 | break; 533 | case LEQ: 534 | outputfile << "LEQ <=" << endl; 535 | break; 536 | case GRE: 537 | outputfile << "GRE >" << endl; 538 | break; 539 | case GEQ: 540 | outputfile << "GEQ >=" << endl; 541 | break; 542 | case EQL: 543 | outputfile << "EQL ==" << endl; 544 | break; 545 | case NEQ: 546 | outputfile << "NEQ !=" << endl; 547 | break; 548 | case ASSIGN: 549 | outputfile << "ASSIGN =" << endl; 550 | break; 551 | case SEMICN: 552 | outputfile << "SEMICN ;" << endl; 553 | break; 554 | case COMMA: 555 | outputfile << "COMMA ," << endl; 556 | break; 557 | case LPARENT: 558 | outputfile << "LPARENT (" << endl; 559 | break; 560 | case RPARENT: 561 | outputfile << "RPARENT )" << endl; 562 | break; 563 | case LBRACK: 564 | outputfile << "LBRACK [" << endl; 565 | break; 566 | case RBRACK: 567 | outputfile << "RBRACK ]" << endl; 568 | break; 569 | case LBRACE: 570 | outputfile << "LBRACE {" << endl; 571 | break; 572 | case RBRACE: 573 | outputfile << "RBRACE }" << endl; 574 | break; 575 | default: 576 | break; 577 | } 578 | } -------------------------------------------------------------------------------- /GramOrigin/lexical.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef LEXICAL_H 3 | #define LEXICAL_H 4 | 5 | enum typeId { 6 | CONSTTK, INTTK, CHARTK, VOIDTK, MAINTK, IFTK, ELSETK, DOTK, WHILETK, FORTK, SCANFTK, PRINTFTK, RETURNTK, //保留字 7 | IDENFR, INTCON, CHARCON, STRCON, 8 | PLUS, MINU, MULT, DIV, LSS, LEQ, GRE, GEQ, EQL, NEQ, ASSIGN, SEMICN, COMMA, LPARENT, RPARENT, LBRACK, RBRACK, LBRACE, RBRACE 9 | }; 10 | 11 | bool isSpace(); 12 | 13 | bool isNewline(); 14 | 15 | bool isBlank(); 16 | 17 | bool isLetter(); 18 | 19 | bool isDigit(); 20 | 21 | bool isPlus(); 22 | 23 | bool isMinu(); 24 | 25 | bool isMult(); 26 | 27 | bool isDiv(); 28 | 29 | bool isChar(); 30 | 31 | bool isLss(); 32 | 33 | bool isGre(); 34 | 35 | bool isExcla(); 36 | 37 | bool isAssign(); 38 | 39 | bool isSemicn(); 40 | 41 | bool isComma(); 42 | 43 | bool isLparent(); 44 | 45 | bool isRparent(); 46 | 47 | bool isLbrack(); 48 | 49 | bool isRbrack(); 50 | 51 | bool isLbrace(); 52 | 53 | bool isRbrace(); 54 | 55 | bool isSquo(); 56 | 57 | bool isDquo(); 58 | 59 | bool isEOF(); 60 | 61 | bool isStringChar(); 62 | 63 | bool isValid(); 64 | 65 | bool isFactorFellow(); 66 | 67 | void clearToken(); 68 | 69 | void catToken(); 70 | 71 | void get_ch(); 72 | 73 | void retract(); 74 | 75 | void retractString(int oldIndex); 76 | 77 | int reserver(); 78 | 79 | int transNum(); 80 | 81 | int getsym(int out=1); 82 | 83 | void doOutput(); 84 | 85 | #endif // !LEXICAL_H 86 | 87 | -------------------------------------------------------------------------------- /GramOrigin/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lexical.h" 4 | #include "grammer.h" 5 | #include "midCode.h" 6 | #include "mipsCode.h" 7 | #include "function.h" 8 | using namespace std; 9 | 10 | extern string filecontent; //文件的内容 11 | ifstream inputfile; 12 | ofstream outputfile; 13 | ofstream errorfile; 14 | ofstream midCodefile; 15 | ofstream mipsCodefile; 16 | 17 | int main() { 18 | inputfile.open("testfile.txt", ios::in); 19 | outputfile.open("output.txt", ios::out); 20 | errorfile.open("error.txt", ios::out); 21 | midCodefile.open("midCode.txt", ios::out); 22 | mipsCodefile.open("mips.txt", ios::out); 23 | string tmpIn; 24 | while (getline(inputfile, tmpIn)) { //读取文件内容 25 | filecontent.append(tmpIn); 26 | filecontent.append("\n"); 27 | } 28 | int re = getsym(); 29 | if (re < 0) { 30 | //error() 31 | } 32 | else { 33 | if (procedure()) { 34 | //success 35 | //cout << "True!" << endl; 36 | } 37 | else { 38 | //error() 39 | } 40 | } 41 | outputMidCode(); 42 | genMips(); 43 | outputMipsCode(); 44 | inputfile.close(); 45 | outputfile.close(); 46 | errorfile.close(); 47 | midCodefile.close(); 48 | mipsCodefile.close(); 49 | showGlobal(); 50 | showAll(); 51 | showString(); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /GramOrigin/midCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "midCode.h" 5 | using namespace std; 6 | 7 | extern vector midCodeTable; 8 | extern ofstream midCodefile; 9 | 10 | void outputMidCode() { 11 | for (int i = 0; i < midCodeTable.size(); i++) { 12 | midCode mc = midCodeTable[i]; 13 | switch (mc.op) { 14 | case PLUSOP: 15 | midCodefile << mc.z << " = " << mc.x << " + " << mc.y << "\n"; 16 | break; 17 | case MINUOP: 18 | midCodefile << mc.z << " = " << mc.x << " - " << mc.y << "\n"; 19 | break; 20 | case MULTOP: 21 | midCodefile << mc.z << " = " << mc.x << " * " << mc.y << "\n"; 22 | break; 23 | case DIVOP: 24 | midCodefile << mc.z << " = " << mc.x << " / " << mc.y << "\n"; 25 | break; 26 | case LSSOP: 27 | midCodefile << mc.z << " = (" << mc.x << " < " << mc.y << ")\n"; 28 | break; 29 | case LEQOP: 30 | midCodefile << mc.z << " = (" << mc.x << " <= " << mc.y << ")\n"; 31 | break; 32 | case GREOP: 33 | midCodefile << mc.z << " = (" << mc.x << " > " << mc.y << ")\n"; 34 | break; 35 | case GEQOP: 36 | midCodefile << mc.z << " = (" << mc.x << " >= " << mc.y << ")\n"; 37 | break; 38 | case EQLOP: 39 | midCodefile << mc.z << " = (" << mc.x << " == " << mc.y << ")\n"; 40 | break; 41 | case NEQOP: 42 | midCodefile << mc.z << " = (" << mc.x << " != " << mc.y << ")\n"; 43 | break; 44 | case ASSIGNOP: 45 | midCodefile << mc.z << " = " << mc.x << "\n"; 46 | break; 47 | case GOTO: 48 | midCodefile << "GOTO " << mc.z << "\n"; 49 | break; 50 | case BZ: 51 | midCodefile << "BZ " << mc.z << "(" << mc.x << "=0)" << "\n"; 52 | break; 53 | case BNZ: 54 | midCodefile << "BNZ " << mc.z << "(" << mc.x << "=1)" << "\n"; 55 | break; 56 | case PUSH: 57 | midCodefile << "PUSH " << mc.z << "\n"; 58 | break; 59 | case CALL: 60 | midCodefile << "CALL " << mc.z << "\n"; 61 | break; 62 | case RET: 63 | midCodefile << "RET " << mc.z << "\n"; 64 | break; 65 | case RETVALUE: 66 | midCodefile << "RETVALUE " << mc.z << " = " << mc.x << "\n"; 67 | break; 68 | case SCAN: 69 | midCodefile << "SCAN " << mc.z << "\n"; 70 | break; 71 | case PRINT: 72 | midCodefile << "PRINT " << mc.z << " " << mc.x << "\n"; 73 | break; 74 | case LABEL: 75 | midCodefile << mc.z << ": \n"; 76 | break; 77 | /*case CONST: 78 | midCodefile << "CONST " << mc.z << " " << mc.x << " = " << mc.y << endl; 79 | break; 80 | case ARRAY: 81 | midCodefile << "ARRAY " << mc.z << " " << mc.x << "[" << mc.y << "]" << endl; 82 | break; 83 | case VAR: 84 | midCodefile << "VAR " << mc.z << " " << mc.x << endl; 85 | break;*/ 86 | case FUNC: 87 | midCodefile << "FUNC " << mc.z << " " << mc.x << "()" << endl; 88 | break; 89 | case PARAM: 90 | midCodefile << "PARA " << mc.z << " " << mc.x << endl; 91 | break; 92 | case GETARRAY: 93 | midCodefile << mc.z << " = " << mc.x << "[" << mc.y << "]\n"; 94 | break; 95 | case PUTARRAY: 96 | midCodefile << mc.z << "[" << mc.x << "]" << " = " << mc.y << "\n"; 97 | break; 98 | case EXIT: 99 | midCodefile << "EXIT\n"; 100 | break; 101 | default: 102 | break; 103 | } 104 | } 105 | } -------------------------------------------------------------------------------- /GramOrigin/midCode.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | enum operation { 7 | PLUSOP, //+ 8 | MINUOP, //- 9 | MULTOP, //* 10 | DIVOP, // / 11 | LSSOP, //< 12 | LEQOP, //<= 13 | GREOP, //> 14 | GEQOP, //>= 15 | EQLOP, //== 16 | NEQOP, //!= 17 | ASSIGNOP, //= 18 | GOTO, //无条件跳转 19 | BZ, //不满足条件跳转 20 | BNZ, //满足条件跳转 21 | PUSH, //函数调用时参数传递 22 | CALL, //函数调用 23 | RET, //函数返回语句 24 | RETVALUE, //有返回值函数返回的结果 25 | SCAN, //读 26 | PRINT, //写 27 | LABEL, //标号 28 | CONST, //常量 29 | ARRAY, //数组 30 | VAR, //变量 31 | FUNC, //函数定义 32 | PARAM, //函数参数 33 | GETARRAY, //取数组的值 t = a[] 34 | PUTARRAY, //给数组赋值 a[] = t 35 | EXIT, //退出 main最后 36 | }; 37 | 38 | class midCode { //z = x op y 39 | public: 40 | operation op; // 操作 41 | string z; // 结果 42 | string x; // 左操作数 43 | string y; // 右操作数 44 | midCode(operation o, string zz, string xx, string yy) : op(o), z(zz), x(xx), y(yy) {} 45 | }; 46 | 47 | void outputMidCode(); 48 | -------------------------------------------------------------------------------- /GramOrigin/mipsCode.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | enum mipsOperation { 7 | add, 8 | addi, 9 | sub, 10 | mult, 11 | divop, 12 | mflo, 13 | mfhi, 14 | sll, 15 | beq, 16 | bne, 17 | bgt, //扩展指令 相当于一条ALU类指令+一条branch指令 18 | bge, //扩展指令 相当于一条ALU类指令+一条branch指令 19 | blt, //扩展指令 相当于一条ALU类指令+一条branch指令 20 | ble, //扩展指令 相当于一条ALU类指令+一条branch指令 21 | j, 22 | jal, 23 | jr, 24 | lw, 25 | sw, 26 | syscall, 27 | li, 28 | la, 29 | moveop, 30 | dataSeg, //.data 31 | textSeg, //.text 32 | asciizSeg, //.asciiz 33 | globlSeg, //.globl 34 | label, //产生标号 35 | }; 36 | 37 | class mipsCode { 38 | public: 39 | mipsOperation op; // 操作 40 | string z; // 结果 41 | string x; // 左操作数 42 | string y; // 右操作数 43 | int imme; // 立即数 44 | mipsCode(mipsOperation o, string zz, string xx, string yy, int i = 0) : op(o), z(zz), x(xx), y(yy), imme(i) {} 45 | }; 46 | 47 | void genMips(); 48 | 49 | void outputMipsCode(); 50 | 51 | void loadValue(string name, string regName, bool gene, int& va, bool& get); 52 | 53 | void storeValue(string name, string regName); 54 | -------------------------------------------------------------------------------- /GramOrigin/symbolItem.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef SYMBOLITEM_H 3 | #define SYMBOLITEM_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | class symbolItem { 12 | public: 13 | string name; 14 | int kind; //var const function array 15 | int type; //int char void 16 | int constInt; 17 | char constChar; 18 | int length; //数组长度 对于函数用于记录这个函数有多少变量(参数+局部变量+临时) 19 | vector parameterTable; //参数类型 20 | int addr; //地址 21 | symbolItem(string s, int add = 0, int k = 0, int t = 0, int ci = 0, char cc = ' ', int l = 0) : 22 | name(s), kind(k), type(t), constInt(ci), constChar(cc), length(l), addr(add) { 23 | parameterTable = vector(); 24 | } 25 | symbolItem() {} 26 | void output() { 27 | cout << name << " "; 28 | switch (type) { 29 | case 1: 30 | cout << "int "; 31 | break; 32 | case 2: 33 | cout << "char "; 34 | break; 35 | case 3: 36 | cout << "void "; 37 | break; 38 | } 39 | switch (kind) { 40 | case 1: 41 | cout << "var "; 42 | break; 43 | case 2: 44 | cout << "const "; 45 | if (type == 1) { 46 | cout << constInt << " "; 47 | } 48 | else if (type == 2) { 49 | cout << constChar << " "; 50 | } 51 | break; 52 | case 3: 53 | cout << "func "; 54 | cout << "parameters: ("; 55 | for (int i = 0; i < parameterTable.size(); i++) { 56 | if (parameterTable[i] == 1) { 57 | cout << "int "; 58 | } 59 | else { 60 | cout << "char "; 61 | } 62 | } 63 | cout << ")" << " 参数个数: " << length << " "; 64 | break; 65 | case 4: 66 | cout << "array "; 67 | cout << length << " "; 68 | break; 69 | } 70 | cout << "addr: " << addr; 71 | cout << "\n"; 72 | } 73 | void insert(int t) { 74 | parameterTable.push_back(t); 75 | } 76 | }; 77 | 78 | #endif // !SYMBOLITEM_H 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BUAA Complier # 2 | 3 | ## 北航计算机学院编译技术课程设计 ## 4 | 5 | 简单C0文法的编译器 6 | 7 | 使用语言:C++ 8 | 9 | GramOrign/ 目录下为未优化的版本 10 | 11 | 根目录下为优化后的版本 12 | 13 | C0文法及其语义说明: 14 | 15 | ```c 16 | <加法运算符> ::= +|- 17 | <乘法运算符> ::= *|/ 18 | <关系运算符> ::= <|<=|>|>=|!=|== 19 | <字母> ::= _|a|...|z|A|...|Z 20 | <数字> ::= 0|<非零数字> 21 | <非零数字> ::= 1|...|9 22 | <字符> ::= '<加法运算符>'|'<乘法运算符>'|'<字母>'|'<数字>' 23 | <字符串> ::= "{十进制编码为32,33,35-126的ASCII字符}" 24 | <程序> ::= [<常量说明>][<变量说明>]{<有返回值函数定义>|<无返回值函数定义>}<主函数> 25 | <常量说明> ::= const<常量定义>;{ const<常量定义>;} 26 | <常量定义> ::= int<标识符>=<整数>{,<标识符>=<整数>} 27 | | char<标识符>=<字符>{,<标识符>=<字符>} 28 | <无符号整数> ::= <非零数字>{<数字>}| 0 29 | <整数> ::= [+|-]<无符号整数> 30 | <标识符> ::= <字母>{<字母>|<数字>} //标识符和保留字都区分大小写 31 | <声明头部> ::= int<标识符> |char<标识符> 32 | <变量说明> ::= <变量定义>;{<变量定义>;} 33 | <变量定义> ::= <类型标识符>(<标识符>|<标识符>'['<无符号整数>']'){,(<标识符>|<标识符>'['<无符号整数>']' )} 34 | //<无符号整数>表示数组元素的个数,其值需大于0 35 | //变量没有初始化的情况下没有初值 36 | <类型标识符> ::= int | char 37 | <有返回值函数定义> ::= <声明头部>'('<参数表>')' '{'<复合语句>'}' 38 | <无返回值函数定义> ::= void<标识符>'('<参数表>')''{'<复合语句>'}' 39 | <复合语句> ::= [<常量说明>][<变量说明>]<语句列> 40 | <参数表> ::= <类型标识符><标识符>{,<类型标识符><标识符>}| <空> 41 | <主函数> ::= void main‘(’‘)’ ‘{’<复合语句>‘}’ 42 | <表达式> ::= [+|-]<项>{<加法运算符><项>} //[+|-]只作用于第一个<项> 43 | <项> ::= <因子>{<乘法运算符><因子>} 44 | <因子> ::= <标识符>|<标识符>'['<表达式>']'|'('<表达式>')'|<整数>|<字符>|<有返回值函数调用语句> //char 类型的变量或常量,用字符的ASCII 码对应的整数参加运算 45 | //<标识符>'['<表达式>']'中的<表达式>只能是整型,下标从0开始 46 | //单个<标识符>不包括数组名,即数组不能整体参加运算,数组元素可以参加运算 47 | <语句> ::= <条件语句>|<循环语句>| '{'<语句列>'}'| <有返回值函数调用语句>; 48 | |<无返回值函数调用语句>;|<赋值语句>;|<读语句>;|<写语句>;|<空>;|<返回语句>; 49 | <赋值语句> ::= <标识符>=<表达式>|<标识符>'['<表达式>']'=<表达式> 50 | //<标识符>=<表达式>中的<标识符>不能为常量名和数组名 51 | <条件语句> ::= if '('<条件>')'<语句>[else<语句>] 52 | <条件> ::= <表达式><关系运算符><表达式>|<表达式> //表达式需均为整数类型才能进行比较,第二个侯选式中表达式为0条件为假,否则为真 53 | <循环语句> ::= while '('<条件>')'<语句>| do<语句>while '('<条件>')' |for'('<标识符>=<表达式>;<条件>;<标识符>=<标识符>(+|-)<步长>')'<语句> //for语句先进行条件判断,符合条件再进入循环体 54 | <步长>::= <无符号整数> 55 | <有返回值函数调用语句> ::= <标识符>'('<值参数表>')' 56 | <无返回值函数调用语句> ::= <标识符>'('<值参数表>')' 57 | <值参数表> ::= <表达式>{,<表达式>}|<空> 58 | //实参的表达式不能是数组名,可以是数组元素 59 | //实参的计算顺序,要求生成的目标码运行结果与Clang8.0.0 编译器运行的结果一致 60 | <语句列> ::= {<语句>} 61 | <读语句> ::= scanf '('<标识符>{,<标识符>}')' 62 | //从标准输入获取<标识符>的值,该标识符不能是常量名和数组名 63 | //生成PCODE代码的情况:需要处理为一个scanf语句中,若有多个<标识符>,无论标识符的类型是char还是int,每输入一项均需回车 64 | //生成MIPS汇编的情况:按照syscall指令的用法使用即可 65 | <写语句> ::= printf '(' <字符串>,<表达式> ')'| printf '('<字符串> ')'| printf '('<表达式>')' 66 | //printf '(' <字符串>,<表达式> ')' 67 | //printf '(' <字符串>,<表达式> ')'输出时,先输出字符串的内容,再输出表达式的值,两者之间无空格 68 | //表达式为字符型时,输出字符;为整型时输出整数 69 | //<字符串>原样输出(不存在转义) 70 | //每个printf语句的内容输出到一行,按结尾有换行符\n处理 71 | <返回语句> ::= return['('<表达式>')'] 72 | //无返回值的函数中可以没有return语句,也可以有形如return;的语句 73 | //有返回值的函数只要出现一条带返回值的return语句即可,不用检查每个分支是否有带返回值的return语句 74 | 75 | 另:关于类型和类型转换的约定: 76 | 1. 表达式类型为char型有以下三种情况: 77 | 1)表达式由<标识符>或<标识符>'['<表达式>']构成,且<标识符>的类型为char,即char类型的常量和变量、char类型的数组元素。 78 | 2)表达式仅由一个<字符>构成,即字符字面量。 79 | 3)表达式仅由一个有返回值的函数调用构成,且该被调用的函数返回值为char型 80 | 除此之外的所有情况,<表达式>的类型都是int 81 | 2. 只在表达式计算中有类型转换,字符型一旦参与运算则转换成整型,包括小括号括起来的字符型,也算参与了运算,例如(‘c’)的结果是整型。 82 | 3. 其他情况,例如赋值、函数传参、if/while条件语句中关系比较要求类型完全匹配,并且<条件>中的关系比较只能是整型之间比,不能是字符型,if ‘(’<条件>‘)’和while ‘(’<条件>‘)’里边,如果<条件>是单个表达式,则必须是整型。 83 | ``` 84 | 85 | -------------------------------------------------------------------------------- /function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "symbolItem.h" 6 | #include "function.h" 7 | #include "midCode.h" 8 | using namespace std; 9 | 10 | extern map globalSymbolTable; 11 | extern map localSymbolTable; 12 | extern map> allLocalSymbolTable; //保存所有的局部符号表 用于保留变量的地址 13 | extern vector stringList; //保存所有的字符串 14 | extern map > funcMidCodeTable; 15 | int labelId = 0; //标号的id 16 | int tmpVarId = 0; //中间变量的id 17 | int nameId = 0; //函数内联新产生的变量名常量名数组名 18 | 19 | void showGlobal() { 20 | cout << "----------------\n"; 21 | for (map::iterator it = globalSymbolTable.begin(); it != globalSymbolTable.end(); it++) { 22 | (*it).second.output(); 23 | } 24 | cout << "----------------\n"; 25 | } 26 | 27 | void showLocal() { 28 | cout << "----------------\n"; 29 | for (map::iterator it = localSymbolTable.begin(); it != localSymbolTable.end(); it++) { 30 | (*it).second.output(); 31 | } 32 | cout << "----------------\n"; 33 | } 34 | 35 | void showAll() { 36 | cout << "----------------\n"; 37 | for (map>::iterator it = allLocalSymbolTable.begin(); it != allLocalSymbolTable.end(); it++) { 38 | cout << "Func: " << (*it).first << "\n"; 39 | map ss = (*it).second; 40 | for (map::iterator i = ss.begin(); i != ss.end(); i++) { 41 | (*i).second.output(); 42 | } 43 | } 44 | cout << "----------------\n"; 45 | } 46 | 47 | void showString() { 48 | cout << "Show strings:\n"; 49 | for (int i = 0; i < stringList.size(); i++) { 50 | cout << stringList[i] << "\n"; 51 | } 52 | } 53 | 54 | string int2string(int t) { 55 | stringstream ss; 56 | ss << t; 57 | return ss.str(); 58 | } 59 | 60 | int string2int(string s) { 61 | stringstream ss; 62 | ss << s; 63 | int t; 64 | ss >> t; 65 | return t; 66 | } 67 | 68 | string genLabel(string app) { 69 | labelId++; 70 | return "Label" + int2string(labelId) + app; 71 | } 72 | 73 | string genTmp() { 74 | tmpVarId++; 75 | return "#T" + int2string(tmpVarId); //#开头 跟正常的变量区分开 76 | } 77 | 78 | string genName() { 79 | nameId++; 80 | return "%INLINE_" + int2string(nameId); //%开头 跟正常的变量区分开 81 | } 82 | 83 | void showFuncMidCode() { 84 | cout << "*********************\n"; 85 | for (map >::iterator it = funcMidCodeTable.begin(); it != funcMidCodeTable.end(); it++) { 86 | vector ve = (*it).second; 87 | for (int i = 0; i < ve.size(); i++) { 88 | midCode mc = ve[i]; 89 | switch (mc.op) { 90 | case PLUSOP: 91 | cout << mc.z << " = " << mc.x << " + " << mc.y << "\n"; 92 | break; 93 | case MINUOP: 94 | cout << mc.z << " = " << mc.x << " - " << mc.y << "\n"; 95 | break; 96 | case MULTOP: 97 | cout << mc.z << " = " << mc.x << " * " << mc.y << "\n"; 98 | break; 99 | case DIVOP: 100 | cout << mc.z << " = " << mc.x << " / " << mc.y << "\n"; 101 | break; 102 | case LSSOP: 103 | cout << mc.z << " = (" << mc.x << " < " << mc.y << ")\n"; 104 | break; 105 | case LEQOP: 106 | cout << mc.z << " = (" << mc.x << " <= " << mc.y << ")\n"; 107 | break; 108 | case GREOP: 109 | cout << mc.z << " = (" << mc.x << " > " << mc.y << ")\n"; 110 | break; 111 | case GEQOP: 112 | cout << mc.z << " = (" << mc.x << " >= " << mc.y << ")\n"; 113 | break; 114 | case EQLOP: 115 | cout << mc.z << " = (" << mc.x << " == " << mc.y << ")\n"; 116 | break; 117 | case NEQOP: 118 | cout << mc.z << " = (" << mc.x << " != " << mc.y << ")\n"; 119 | break; 120 | case ASSIGNOP: 121 | cout << mc.z << " = " << mc.x << "\n"; 122 | break; 123 | case GOTO: 124 | cout << "GOTO " << mc.z << "\n"; 125 | break; 126 | case BZ: 127 | cout << "BZ " << mc.z << "(" << mc.x << "=0)" << "\n"; 128 | break; 129 | case BNZ: 130 | cout << "BNZ " << mc.z << "(" << mc.x << "=1)" << "\n"; 131 | break; 132 | case PUSH: 133 | cout << "PUSH " << mc.z << "\n"; 134 | break; 135 | case CALL: 136 | cout << "CALL " << mc.z << "\n"; 137 | break; 138 | case RET: 139 | cout << "RET " << mc.z << "\n"; 140 | break; 141 | case INLINERET: 142 | cout << "INLINERET " << mc.z << "\n"; 143 | break; 144 | case RETVALUE: 145 | cout << "RETVALUE " << mc.z << " = " << mc.x << "\n"; 146 | break; 147 | case SCAN: 148 | cout << "SCAN " << mc.z << "\n"; 149 | break; 150 | case PRINT: 151 | cout << "PRINT " << mc.z << " " << mc.x << "\n"; 152 | break; 153 | case LABEL: 154 | cout << mc.z << ": \n"; 155 | break; 156 | case CONST: 157 | cout << "CONST " << mc.z << " " << mc.x << " = " << mc.y << endl; 158 | break; 159 | case ARRAY: 160 | cout << "ARRAY " << mc.z << " " << mc.x << "[" << mc.y << "]" << endl; 161 | break; 162 | case VAR: 163 | cout << "VAR " << mc.z << " " << mc.x << endl; 164 | break; 165 | case FUNC: 166 | cout << "FUNC " << mc.z << " " << mc.x << "()" << endl; 167 | break; 168 | case PARAM: 169 | cout << "PARA " << mc.z << " " << mc.x << endl; 170 | break; 171 | case GETARRAY: 172 | cout << mc.z << " = " << mc.x << "[" << mc.y << "]\n"; 173 | break; 174 | case PUTARRAY: 175 | cout << mc.z << "[" << mc.x << "]" << " = " << mc.y << "\n"; 176 | break; 177 | case EXIT: 178 | cout << "EXIT\n"; 179 | break; 180 | default: 181 | break; 182 | } 183 | } 184 | cout << "\n"; 185 | } 186 | cout << "*********************\n"; 187 | } -------------------------------------------------------------------------------- /function.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | void showGlobal(); 7 | 8 | void showLocal(); 9 | 10 | void showAll(); 11 | 12 | void showString(); 13 | 14 | string int2string(int t); //修改 15 | 16 | int string2int(string s); //修改 17 | 18 | string genLabel(string app=""); 19 | 20 | string genTmp(); 21 | 22 | string genName(); 23 | 24 | void showFuncMidCode(); 25 | -------------------------------------------------------------------------------- /grammer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef GRAMMER_H 3 | #define GRAMMER_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include "midCode.h" 9 | using namespace std; 10 | 11 | //<字符串>   ::=  "{十进制编码为32,33,35-126的ASCII字符}" 12 | bool strings(); 13 | //<程序> ::= [<常量说明>][<变量说明>]{<有返回值函数定义>|<无返回值函数定义>}<主函数> 14 | bool procedure(); 15 | //<常量说明> ::=  const<常量定义>;{ const<常量定义>;} 16 | bool constDeclaration(bool isglobal); 17 | //<常量定义> ::= int<标识符>=<整数>{,<标识符>=<整数>} 18 | //   | char<标识符>=<字符>{,<标识符>=<字符>} 19 | bool constDefinition(bool isglobal); 20 | //<无符号整数>  ::= <非零数字>{<数字>}| 0 21 | bool unsignedInteger(int& value); 22 | //<整数> ::= [+|-]<无符号整数> 23 | bool integer(int& value); 24 | //<声明头部>   ::=  int<标识符> |char<标识符> 25 | bool declarationHead(string& tmp, int& type); 26 | //<变量说明>  ::= <变量定义>;{<变量定义>;} 27 | bool variableDeclaration(bool isglobal); 28 | //<变量定义>  ::= <类型标识符>(<标识符>|<标识符>'['<无符号整数>']') 29 | // {,(<标识符>|<标识符>'['<无符号整数>']' )} 30 | bool variableDefinition(bool isglobal); 31 | //<有返回值函数定义>  ::=  <声明头部>'('<参数表>')' '{'<复合语句>'}’ 32 | bool haveReturnValueFunction(); 33 | //<无返回值函数定义>  ::= void<标识符>'('<参数表>')''{'<复合语句>'}’ 34 | bool noReturnValueFunction(); 35 | //<参数表>    ::=  <类型标识符><标识符>{,<类型标识符><标识符>}| <空> 36 | bool parameterTable(string funcName, bool isRedefine); 37 | //<复合语句> ::=  [<常量说明>][<变量说明>]<语句列> 38 | bool compoundStatement(); 39 | //<主函数>    ::= void main‘(’‘)’ ‘{’<复合语句>‘}’ 40 | bool mainFunction(); 41 | //<表达式>    ::= [+|-]<项>{<加法运算符><项>}   42 | bool expression(int& type, string& ansTmp); 43 | //<项>     ::= <因子>{<乘法运算符><因子>} 44 | bool item(int& type, string& ansTmp); 45 | //<因子>    ::= <标识符>|<标识符>'['<表达式>']'|'('<表达式>')'|<整数>|<字符>|<有返回值函数调用语句> 46 | bool factor(int& type, string& ansTmp); 47 | //<语句>    ::= <条件语句>|<循环语句>| '{'<语句列>'}'| <有返回值函数调用语句>;  48 | // |<无返回值函数调用语句>;|<赋值语句>;|<读语句>;|<写语句>;|<空>;|<返回语句>; 49 | bool statement(); 50 | //<赋值语句>   ::=  <标识符>=<表达式>|<标识符>'['<表达式>']'=<表达式> 51 | bool assignStatement(); 52 | //<条件语句>  ::= if '('<条件>')'<语句>[else<语句>] 53 | bool conditionStatement(); 54 | //<条件>  ::=  <表达式><关系运算符><表达式>|<表达式> 55 | bool condition(); 56 | //<循环语句>   ::=  while '('<条件>')'<语句>| do<语句>while '('<条件>')' 57 | // |for'('<标识符>=<表达式>;<条件>;<标识符>=<标识符>(+|-)<步长>')'<语句> 58 | bool repeatStatement(); 59 | //<步长>::= <无符号整数> 60 | bool step(int& value); 61 | //<有返回值函数调用语句> ::= <标识符>'('<值参数表>')’ 62 | bool callHaveReturnValueFunction(); 63 | //<无返回值函数调用语句> ::= <标识符>'('<值参数表>')’ 64 | bool callNoReturnValueFunction(); 65 | //<值参数表>  ::= <表达式>{,<表达式>}|<空> 66 | bool valueParameterTable(string funcName); 67 | //<语句列>   ::= {<语句>} 68 | bool statementList(); 69 | //<读语句>    ::=  scanf '('<标识符>{,<标识符>}')’ 70 | bool readStatement(); 71 | //<写语句>    ::= printf '(' <字符串>,<表达式> ')'| printf '('<字符串> ')'| printf '('<表达式>')’ 72 | bool writeStatement(); 73 | //<返回语句>   ::=  return['('<表达式>')']   74 | bool returnStatement(); 75 | 76 | void checkBeforeFunc(); 77 | 78 | void fullNameMap(map& nameMap, vector ve, string funcName); 79 | 80 | void dealInlineFunc(string name, int& begin, int& end); 81 | #endif // !GRAMMER_H 82 | -------------------------------------------------------------------------------- /lexical.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "lexical.h" 6 | using namespace std; 7 | 8 | char ch; 9 | char token[100000]; 10 | int tokenI = 0; 11 | int num; //记录整形常量 12 | char con_ch; //记录字符型常量 13 | char s[100000]; //记录字符串常量 14 | enum typeId symbol; 15 | 16 | int len_reservedWord = 13; 17 | char reservedWord[20][10] = { 18 | "const", "int", "char", "void", "main", "if", "else", "do", "while", "for", "scanf", "printf", "return", 19 | }; 20 | 21 | int indexs = 0; //文件的索引 22 | int oldIndex; //用于做恢复 23 | int line = 1; //记录行号 24 | string filecontent; //文件的内容 25 | 26 | extern ofstream outputfile; 27 | extern ofstream errorfile; 28 | extern bool error; 29 | 30 | bool isSpace() 31 | { 32 | return (ch == ' '); 33 | } 34 | 35 | bool isNewline() 36 | { 37 | return (ch == '\n'); 38 | } 39 | 40 | bool isBlank() 41 | { 42 | return (ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r' || ch == '\f' || ch == '\v'); 43 | } 44 | 45 | bool isLetter() 46 | { 47 | return ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch == '_')); 48 | } 49 | 50 | bool isDigit() 51 | { 52 | return (ch >= '0' && ch <= '9'); 53 | } 54 | 55 | bool isPlus() 56 | { 57 | return (ch == '+'); 58 | } 59 | 60 | bool isMinu() 61 | { 62 | return (ch == '-'); 63 | } 64 | 65 | bool isMult() 66 | { 67 | return (ch == '*'); 68 | } 69 | 70 | bool isDiv() 71 | { 72 | return (ch == '/'); 73 | } 74 | 75 | bool isChar() { 76 | return (isLetter() || isDigit() || isPlus() || isMinu() || isMult() || isDiv()); 77 | } 78 | 79 | bool isLss() 80 | { 81 | return (ch == '<'); 82 | } 83 | 84 | bool isGre() 85 | { 86 | return (ch == '>'); 87 | } 88 | 89 | bool isExcla() 90 | { 91 | return (ch == '!'); 92 | } 93 | 94 | bool isAssign() 95 | { 96 | return (ch == '='); 97 | } 98 | 99 | bool isSemicn() 100 | { 101 | return (ch == ';'); 102 | } 103 | 104 | bool isComma() 105 | { 106 | return (ch == ','); 107 | } 108 | 109 | bool isLparent() 110 | { 111 | return (ch == '('); 112 | } 113 | 114 | bool isRparent() 115 | { 116 | return (ch == ')'); 117 | } 118 | 119 | bool isLbrack() 120 | { 121 | return (ch == '['); 122 | } 123 | 124 | bool isRbrack() 125 | { 126 | return (ch == ']'); 127 | } 128 | 129 | bool isLbrace() 130 | { 131 | return (ch == '{'); 132 | } 133 | 134 | bool isRbrace() 135 | { 136 | return (ch == '}'); 137 | } 138 | 139 | bool isSquo() 140 | { 141 | return (ch == '\''); 142 | } 143 | 144 | bool isDquo() 145 | { 146 | return (ch == '\"'); 147 | } 148 | 149 | bool isEOF() 150 | { 151 | //return ch == EOF; 152 | return indexs >= filecontent.size(); 153 | } 154 | 155 | bool isStringChar() 156 | { 157 | return ((ch >= 35 && ch <= 126) || ch == 32 || ch == 33); 158 | } 159 | 160 | bool isFactorFellow() 161 | { 162 | return (isLss() || isGre() || isAssign() || isExcla() 163 | || isPlus() || isMinu() || isMult() || isDiv() 164 | || isRparent() || isRbrack() || isComma() || isSemicn()); 165 | } 166 | 167 | void clearToken() 168 | { //清空token 169 | tokenI = 0; 170 | } 171 | 172 | void catToken() 173 | { //把当前字符ch拼到token上 174 | token[tokenI++] = ch; 175 | } 176 | 177 | void get_ch() 178 | { //读一个字符 179 | ch = filecontent[indexs++]; 180 | if (ch == '\n') { 181 | line++; 182 | } 183 | } 184 | 185 | void retract() 186 | { //把字符写回到输入流 187 | if (ch == '\n') { 188 | line--; 189 | } 190 | indexs--; 191 | } 192 | 193 | void retractString(int old) { 194 | for (int i = old; i < indexs; i++) { 195 | if (filecontent[i] == '\n') { 196 | line--; 197 | } 198 | } 199 | indexs = old; 200 | } 201 | 202 | int reserver() 203 | { //查找保留字 返回-1代表是标识符 不是保留字 否则返回保留字编码 204 | for (int i = 0; i < len_reservedWord; i++) { 205 | if (strcmp(reservedWord[i], token) == 0) { 206 | return i; 207 | } 208 | } 209 | return -1; 210 | } 211 | 212 | int transNum() 213 | { 214 | int res = 0; 215 | for (int i = 0; i < tokenI; i++) { 216 | res = res * 10 + (token[i] - '0'); 217 | } 218 | return res; 219 | } 220 | 221 | int getsym(int out) 222 | { 223 | oldIndex = indexs; //记录没有读取之前的indexs为oldIndex 224 | clearToken(); 225 | get_ch(); 226 | while (isBlank()) { 227 | get_ch(); 228 | }// 一直读 直到ch不是空白符 229 | if (isEOF()) { 230 | return -1; 231 | } 232 | if (isLetter()) { // 'a'-'z' 'A'-'Z' '_' 233 | while (isLetter() || isDigit()) { //拼标识符 <标识符>::=<字母>{<字母>|<数字>} 234 | catToken(); //把ch连接到token中 235 | get_ch(); //读ch 236 | } 237 | //此时ch不再是字母或者数字了 说明标识符结束了 238 | retract(); //回退ch 239 | token[tokenI] = '\0'; 240 | int resultValue = reserver(); //查找保留字表 241 | if (resultValue == -1) { //标识符 242 | symbol = IDENFR; 243 | } 244 | else { //保留字 245 | symbol = (typeId)resultValue; 246 | } 247 | return 1; 248 | } 249 | else if (isDigit()) { // '0'-'9' 数字 250 | while (isDigit()) { 251 | catToken(); 252 | get_ch(); 253 | } 254 | //此时ch不再是数字了 255 | retract(); 256 | token[tokenI] = '\0'; 257 | num = transNum(); 258 | symbol = INTCON; //整型常量 259 | if (token[0] == '0' && tokenI > 1) { 260 | if (out) { 261 | errorfile << line << " a\n"; //不符合词法 有前导的0 262 | error = true; 263 | } 264 | } 265 | return 1; 266 | } 267 | else if (isSquo()) { // ' 268 | get_ch(); 269 | if (isChar()) { 270 | char tmp = ch; //暂时记录 271 | get_ch(); 272 | if (isSquo()) { 273 | con_ch = tmp; 274 | symbol = CHARCON; //字符常量 275 | } 276 | else { 277 | //wrong! retract(); 缺少右单引号 不符合词法 278 | if (out) { 279 | errorfile << line << " a\n"; //不符合词法 280 | error = true; 281 | } 282 | int old = indexs; 283 | while (1) { 284 | if (isSquo()) { 285 | break; 286 | } 287 | if (isNewline()) { 288 | retractString(old-1); //回到这个读错了的 本该是'的位置 289 | break; 290 | } 291 | if (isFactorFellow()) { //, ; + - * / > < ! = 292 | indexs--; //需要把,;退回去 293 | break; 294 | } 295 | get_ch(); 296 | } 297 | con_ch = tmp; 298 | symbol = CHARCON; //字符常量 299 | } 300 | } 301 | else { 302 | //wrong! retract(); 字符不是a-z A-Z _+-*/ 303 | if (out) { 304 | errorfile << line << " a\n"; //不符合词法 305 | error = true; 306 | } 307 | char tmp = ch; 308 | get_ch(); 309 | if (isSquo()) { 310 | con_ch = tmp; 311 | symbol = CHARCON; //字符常量 312 | } 313 | else { 314 | //wrong! retract(); 缺少右单引号 不符合词法 315 | int old = indexs; 316 | while (1) { 317 | if (isSquo()) { 318 | break; 319 | } 320 | if (isNewline()) { 321 | retractString(old - 1); //回到这个读错了的 本该是'的位置 322 | break; 323 | } 324 | if (isFactorFellow()) { //, ; + - * / > < ! = 325 | indexs--; //需要把,;退回去 326 | break; 327 | } 328 | get_ch(); 329 | } 330 | con_ch = tmp; 331 | symbol = CHARCON; //字符常量 332 | } 333 | } 334 | return 1; 335 | } 336 | else if (isDquo()) { // " 337 | get_ch(); 338 | while (isStringChar()) { 339 | catToken(); 340 | get_ch(); 341 | } 342 | //此时ch不再是可以组成字符串的字符 343 | if (isDquo()) { //遇到了结束的双引号 344 | symbol = STRCON; //字符串常量 345 | token[tokenI] = '\0'; 346 | strcpy(s, token); //存到s中 347 | } 348 | else { //wrong! retract(); 缺少右双引号 不符合词法 349 | if (isNewline()) { 350 | line--; 351 | if (out) { 352 | errorfile << line << " a\n"; //不符合词法 353 | error = true; 354 | } 355 | while (1) { 356 | indexs--; 357 | if (filecontent[indexs] == ')') { 358 | break; 359 | } 360 | tokenI--; 361 | } 362 | symbol = STRCON; //字符串常量 363 | token[tokenI] = '\0'; 364 | strcpy(s, token); //存到s中 365 | } 366 | } 367 | return 1; 368 | } 369 | else if (isPlus()) { // + 370 | symbol = PLUS; 371 | return 1; 372 | } 373 | else if (isMinu()) { // - 374 | symbol = MINU; 375 | return 1; 376 | } 377 | else if (isMult()) { // * 378 | symbol = MULT; 379 | return 1; 380 | } 381 | else if (isDiv()) { // / 382 | symbol = DIV; 383 | return 1; 384 | } 385 | else if (isLss()) { // < 386 | get_ch(); 387 | if (isAssign()) { //<= 388 | symbol = LEQ; 389 | } 390 | else { 391 | symbol = LSS; //< 392 | retract(); //回退 393 | } 394 | return 1; 395 | } 396 | else if (isGre()) { // > 397 | get_ch(); 398 | if (isAssign()) { //>= 399 | symbol = GEQ; 400 | } 401 | else { 402 | symbol = GRE; //> 403 | retract(); //回退 404 | } 405 | return 1; 406 | } 407 | else if (isExcla()) { // ! 408 | get_ch(); 409 | if (isAssign()) { // != 410 | symbol = NEQ; 411 | } 412 | else { 413 | //wrong! retract(); !后边缺少= 414 | retract(); 415 | if (out) { 416 | errorfile << line << " a\n"; //不符合词法 417 | error = true; 418 | } 419 | symbol = NEQ; 420 | } 421 | return 1; 422 | } 423 | else if (isAssign()) { // = 424 | get_ch(); 425 | if (isAssign()) { //== 426 | symbol = EQL; 427 | } 428 | else { 429 | symbol = ASSIGN; 430 | retract(); //回退 431 | } 432 | return 1; 433 | } 434 | else if (isSemicn()) { // ; 435 | symbol = SEMICN; 436 | return 1; 437 | } 438 | else if (isComma()) { // , 439 | symbol = COMMA; 440 | return 1; 441 | } 442 | else if (isLparent()) { // ( 443 | symbol = LPARENT; 444 | return 1; 445 | } 446 | else if (isRparent()) { // ) 447 | symbol = RPARENT; 448 | return 1; 449 | } 450 | else if (isLbrack()) { // [ 451 | symbol = LBRACK; 452 | return 1; 453 | } 454 | else if (isRbrack()) { // ] 455 | symbol = RBRACK; 456 | return 1; 457 | } 458 | else if (isLbrace()) { // { 459 | symbol = LBRACE; 460 | return 1; 461 | } 462 | else if (isRbrace()) { // } 463 | symbol = RBRACE; 464 | return 1; 465 | } 466 | else { 467 | //wrong! retract(); 468 | if (out) { 469 | errorfile << line << " a\n"; //不符合词法 470 | error = true; 471 | } 472 | return getsym(out); 473 | } 474 | } 475 | 476 | void doOutput() 477 | { 478 | switch (symbol) { 479 | case IDENFR: 480 | outputfile << "IDENFR " << token << endl; 481 | break; 482 | case INTCON: 483 | outputfile << "INTCON " << token << endl; 484 | break; 485 | case CHARCON: 486 | outputfile << "CHARCON " << con_ch << endl; 487 | break; 488 | case STRCON: 489 | outputfile << "STRCON " << s << endl; 490 | break; 491 | case CONSTTK: 492 | outputfile << "CONSTTK const" << endl; 493 | break; 494 | case INTTK: 495 | outputfile << "INTTK int" << endl; 496 | break; 497 | case CHARTK: 498 | outputfile << "CHARTK char" << endl; 499 | break; 500 | case VOIDTK: 501 | outputfile << "VOIDTK void" << endl; 502 | break; 503 | case MAINTK: 504 | outputfile << "MAINTK main" << endl; 505 | break; 506 | case IFTK: 507 | outputfile << "IFTK if" << endl; 508 | break; 509 | case ELSETK: 510 | outputfile << "ELSETK else" << endl; 511 | break; 512 | case DOTK: 513 | outputfile << "DOTK do" << endl; 514 | break; 515 | case WHILETK: 516 | outputfile << "WHILETK while" << endl; 517 | break; 518 | case FORTK: 519 | outputfile << "FORTK for" << endl; 520 | break; 521 | case SCANFTK: 522 | outputfile << "SCANFTK scanf" << endl; 523 | break; 524 | case PRINTFTK: 525 | outputfile << "PRINTFTK printf" << endl; 526 | break; 527 | case RETURNTK: 528 | outputfile << "RETURNTK return" << endl; 529 | break; 530 | case PLUS: 531 | outputfile << "PLUS +" << endl; 532 | break; 533 | case MINU: 534 | outputfile << "MINU -" << endl; 535 | break; 536 | case MULT: 537 | outputfile << "MULT *" << endl; 538 | break; 539 | case DIV: 540 | outputfile << "DIV /" << endl; 541 | break; 542 | case LSS: 543 | outputfile << "LSS <" << endl; 544 | break; 545 | case LEQ: 546 | outputfile << "LEQ <=" << endl; 547 | break; 548 | case GRE: 549 | outputfile << "GRE >" << endl; 550 | break; 551 | case GEQ: 552 | outputfile << "GEQ >=" << endl; 553 | break; 554 | case EQL: 555 | outputfile << "EQL ==" << endl; 556 | break; 557 | case NEQ: 558 | outputfile << "NEQ !=" << endl; 559 | break; 560 | case ASSIGN: 561 | outputfile << "ASSIGN =" << endl; 562 | break; 563 | case SEMICN: 564 | outputfile << "SEMICN ;" << endl; 565 | break; 566 | case COMMA: 567 | outputfile << "COMMA ," << endl; 568 | break; 569 | case LPARENT: 570 | outputfile << "LPARENT (" << endl; 571 | break; 572 | case RPARENT: 573 | outputfile << "RPARENT )" << endl; 574 | break; 575 | case LBRACK: 576 | outputfile << "LBRACK [" << endl; 577 | break; 578 | case RBRACK: 579 | outputfile << "RBRACK ]" << endl; 580 | break; 581 | case LBRACE: 582 | outputfile << "LBRACE {" << endl; 583 | break; 584 | case RBRACE: 585 | outputfile << "RBRACE }" << endl; 586 | break; 587 | default: 588 | break; 589 | } 590 | } -------------------------------------------------------------------------------- /lexical.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef LEXICAL_H 3 | #define LEXICAL_H 4 | 5 | enum typeId { 6 | CONSTTK, INTTK, CHARTK, VOIDTK, MAINTK, IFTK, ELSETK, DOTK, WHILETK, FORTK, SCANFTK, PRINTFTK, RETURNTK, //保留字 7 | IDENFR, INTCON, CHARCON, STRCON, 8 | PLUS, MINU, MULT, DIV, LSS, LEQ, GRE, GEQ, EQL, NEQ, ASSIGN, SEMICN, COMMA, LPARENT, RPARENT, LBRACK, RBRACK, LBRACE, RBRACE 9 | }; 10 | 11 | bool isSpace(); 12 | 13 | bool isNewline(); 14 | 15 | bool isBlank(); 16 | 17 | bool isLetter(); 18 | 19 | bool isDigit(); 20 | 21 | bool isPlus(); 22 | 23 | bool isMinu(); 24 | 25 | bool isMult(); 26 | 27 | bool isDiv(); 28 | 29 | bool isChar(); 30 | 31 | bool isLss(); 32 | 33 | bool isGre(); 34 | 35 | bool isExcla(); 36 | 37 | bool isAssign(); 38 | 39 | bool isSemicn(); 40 | 41 | bool isComma(); 42 | 43 | bool isLparent(); 44 | 45 | bool isRparent(); 46 | 47 | bool isLbrack(); 48 | 49 | bool isRbrack(); 50 | 51 | bool isLbrace(); 52 | 53 | bool isRbrace(); 54 | 55 | bool isSquo(); 56 | 57 | bool isDquo(); 58 | 59 | bool isEOF(); 60 | 61 | bool isStringChar(); 62 | 63 | bool isValid(); 64 | 65 | bool isFactorFellow(); 66 | 67 | void clearToken(); 68 | 69 | void catToken(); 70 | 71 | void get_ch(); 72 | 73 | void retract(); 74 | 75 | void retractString(int oldIndex); 76 | 77 | int reserver(); 78 | 79 | int transNum(); 80 | 81 | int getsym(int out=1); 82 | 83 | void doOutput(); 84 | 85 | #endif // !LEXICAL_H 86 | 87 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "lexical.h" 4 | #include "grammer.h" 5 | #include "midCode.h" 6 | #include "mipsCode.h" 7 | #include "function.h" 8 | #include "optimize.h" 9 | using namespace std; 10 | 11 | extern string filecontent; //文件的内容 12 | ifstream inputfile; 13 | ofstream outputfile; 14 | ofstream errorfile; 15 | ofstream midCodefile; 16 | ofstream mipsCodefile; 17 | int debug = 1; 18 | bool error = false; 19 | 20 | int main() { 21 | inputfile.open("testfile.txt", ios::in); 22 | outputfile.open("output.txt", ios::out); 23 | errorfile.open("error.txt", ios::out); 24 | midCodefile.open("midCode.txt", ios::out); 25 | mipsCodefile.open("mips.txt", ios::out); 26 | string tmpIn; 27 | while (getline(inputfile, tmpIn)) { //读取文件内容 28 | filecontent.append(tmpIn); 29 | filecontent.append("\n"); 30 | } 31 | int re = getsym(); 32 | if (re < 0) { 33 | //error() 34 | } 35 | else { 36 | if (procedure()) { 37 | //success 38 | //cout << "True!" << endl; 39 | } 40 | else { 41 | //error() 42 | } 43 | } 44 | if (!error) { 45 | //midCodefile.open("midCode.txt", ios::out); 46 | //mipsCodefile.open("mips.txt", ios::out); 47 | if (debug) { 48 | showFuncMidCode(); 49 | } 50 | splitBlock(); 51 | if (debug) { 52 | showFuncBlock(); 53 | } 54 | outputMidCode(); 55 | genMips(); 56 | outputMipsCode(); 57 | //midCodefile.close(); 58 | //mipsCodefile.close(); 59 | if (debug) { 60 | showGlobal(); 61 | showAll(); 62 | showString(); 63 | } 64 | } 65 | inputfile.close(); 66 | outputfile.close(); 67 | errorfile.close(); 68 | midCodefile.close(); 69 | mipsCodefile.close(); 70 | return 0; 71 | } -------------------------------------------------------------------------------- /midCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "midCode.h" 5 | using namespace std; 6 | 7 | extern vector midCodeTable; 8 | extern ofstream midCodefile; 9 | 10 | void outputMidCode() { 11 | for (int i = 0; i < midCodeTable.size(); i++) { 12 | midCode mc = midCodeTable[i]; 13 | switch (mc.op) { 14 | case PLUSOP: 15 | midCodefile << mc.z << " = " << mc.x << " + " << mc.y << "\n"; 16 | break; 17 | case MINUOP: 18 | midCodefile << mc.z << " = " << mc.x << " - " << mc.y << "\n"; 19 | break; 20 | case MULTOP: 21 | midCodefile << mc.z << " = " << mc.x << " * " << mc.y << "\n"; 22 | break; 23 | case DIVOP: 24 | midCodefile << mc.z << " = " << mc.x << " / " << mc.y << "\n"; 25 | break; 26 | case LSSOP: 27 | midCodefile << mc.z << " = (" << mc.x << " < " << mc.y << ")\n"; 28 | break; 29 | case LEQOP: 30 | midCodefile << mc.z << " = (" << mc.x << " <= " << mc.y << ")\n"; 31 | break; 32 | case GREOP: 33 | midCodefile << mc.z << " = (" << mc.x << " > " << mc.y << ")\n"; 34 | break; 35 | case GEQOP: 36 | midCodefile << mc.z << " = (" << mc.x << " >= " << mc.y << ")\n"; 37 | break; 38 | case EQLOP: 39 | midCodefile << mc.z << " = (" << mc.x << " == " << mc.y << ")\n"; 40 | break; 41 | case NEQOP: 42 | midCodefile << mc.z << " = (" << mc.x << " != " << mc.y << ")\n"; 43 | break; 44 | case ASSIGNOP: 45 | midCodefile << mc.z << " = " << mc.x << "\n"; 46 | break; 47 | case GOTO: 48 | midCodefile << "GOTO " << mc.z << "\n"; 49 | break; 50 | case BZ: 51 | midCodefile << "BZ " << mc.z << "(" << mc.x << "=0)" << "\n"; 52 | break; 53 | case BNZ: 54 | midCodefile << "BNZ " << mc.z << "(" << mc.x << "=1)" << "\n"; 55 | break; 56 | case PUSH: 57 | midCodefile << "PUSH " << mc.z << "(" << mc.y << ")" << "\n"; 58 | break; 59 | case CALL: 60 | midCodefile << "CALL " << mc.z << "\n"; 61 | break; 62 | case RET: 63 | midCodefile << "RET " << mc.z << "\n"; 64 | break; 65 | case INLINERET: 66 | midCodefile << "INLINERET " << mc.z << "\n"; 67 | break; 68 | case RETVALUE: 69 | midCodefile << "RETVALUE " << mc.z << " = " << mc.x << "\n"; 70 | break; 71 | case SCAN: 72 | midCodefile << "SCAN " << mc.z << "\n"; 73 | break; 74 | case PRINT: 75 | midCodefile << "PRINT " << mc.z << " " << mc.x << "\n"; 76 | break; 77 | case LABEL: 78 | midCodefile << mc.z << ": \n"; 79 | break; 80 | case CONST: 81 | midCodefile << "CONST " << mc.z << " " << mc.x << " = " << mc.y << endl; 82 | break; 83 | case ARRAY: 84 | midCodefile << "ARRAY " << mc.z << " " << mc.x << "[" << mc.y << "]" << endl; 85 | break; 86 | case VAR: 87 | midCodefile << "VAR " << mc.z << " " << mc.x << endl; 88 | break; 89 | case FUNC: 90 | midCodefile << "FUNC " << mc.z << " " << mc.x << "()" << endl; 91 | break; 92 | case PARAM: 93 | midCodefile << "PARA " << mc.z << " " << mc.x << endl; 94 | break; 95 | case GETARRAY: 96 | midCodefile << mc.z << " = " << mc.x << "[" << mc.y << "]\n"; 97 | break; 98 | case PUTARRAY: 99 | midCodefile << mc.z << "[" << mc.x << "]" << " = " << mc.y << "\n"; 100 | break; 101 | case EXIT: 102 | midCodefile << "EXIT\n"; 103 | break; 104 | case INLINEEND: 105 | midCodefile << "INLINEEND " << mc.z << " " << mc.x << "\n"; 106 | break; 107 | default: 108 | break; 109 | } 110 | } 111 | } -------------------------------------------------------------------------------- /midCode.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | enum operation { 7 | PLUSOP, //+ 8 | MINUOP, //- 9 | MULTOP, //* 10 | DIVOP, // / 11 | LSSOP, //< 12 | LEQOP, //<= 13 | GREOP, //> 14 | GEQOP, //>= 15 | EQLOP, //== 16 | NEQOP, //!= 17 | ASSIGNOP, //= 18 | GOTO, //无条件跳转 19 | BZ, //不满足条件跳转 20 | BNZ, //满足条件跳转 21 | PUSH, //函数调用时参数传递 22 | CALL, //函数调用 23 | RET, //函数返回语句 24 | INLINERET, //函数内联之后的返回语句 25 | RETVALUE, //有返回值函数返回的结果 26 | SCAN, //读 27 | PRINT, //写 28 | LABEL, //标号 29 | CONST, //常量 30 | ARRAY, //数组 31 | VAR, //变量 32 | FUNC, //函数定义 33 | PARAM, //函数参数 34 | GETARRAY, //取数组的值 t = a[] 35 | PUTARRAY, //给数组赋值 a[] = t 36 | EXIT, //退出 main最后 37 | INLINEEND, //内联的结尾 38 | }; 39 | 40 | class midCode { //z = x op y 41 | public: 42 | operation op; // 操作 43 | string z; // 结果 44 | string x; // 左操作数 45 | string y; // 右操作数 46 | midCode(operation o, string zz, string xx, string yy) : op(o), z(zz), x(xx), y(yy) {} 47 | }; 48 | 49 | void outputMidCode(); 50 | -------------------------------------------------------------------------------- /mipsCode.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | enum mipsOperation { 7 | add, 8 | addi, 9 | sub, 10 | mult, 11 | mul, 12 | divop, 13 | mflo, 14 | mfhi, 15 | sll, 16 | beq, 17 | bne, 18 | bgt, //扩展指令 相当于一条ALU类指令+一条branch指令 19 | bge, //扩展指令 相当于一条ALU类指令+一条branch指令 20 | blt, //扩展指令 相当于一条ALU类指令+一条branch指令 21 | ble, //扩展指令 相当于一条ALU类指令+一条branch指令 22 | blez, //一条branch 23 | bgtz, //一条branch 24 | bgez, //一条branch 25 | bltz, //一条branch 26 | j, 27 | jal, 28 | jr, 29 | lw, 30 | sw, 31 | syscall, 32 | li, 33 | la, 34 | moveop, 35 | dataSeg, //.data 36 | textSeg, //.text 37 | asciizSeg, //.asciiz 38 | globlSeg, //.globl 39 | label, //产生标号 40 | }; 41 | 42 | class mipsCode { 43 | public: 44 | mipsOperation op; // 操作 45 | string z; // 结果 46 | string x; // 左操作数 47 | string y; // 右操作数 48 | int imme; // 立即数 49 | mipsCode(mipsOperation o, string zz, string xx, string yy, int i = 0) : op(o), z(zz), x(xx), y(yy), imme(i) {} 50 | }; 51 | 52 | void genMips(); 53 | 54 | void outputMipsCode(); 55 | 56 | void loadValue(string& name, string& regName, bool gene, int& va, bool& get, bool assign); 57 | 58 | void storeValue(string &name, string ®Name); 59 | -------------------------------------------------------------------------------- /optimize.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aptx1231/BUAA_Compiler/f95305b33dc0895ed672d73ddc97fad669f8794b/optimize.cpp -------------------------------------------------------------------------------- /optimize.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include "midCode.h" 6 | using namespace std; 7 | 8 | class Block { 9 | public: 10 | int start; 11 | int end; 12 | int nextBlock1; 13 | int nextBlock2; 14 | vector midCodeVector; 15 | vector use; 16 | vector def; 17 | vector in; 18 | vector out; 19 | 20 | Block(int s, int e, int n1, int n2) : start(s), end(e), nextBlock1(n1), nextBlock2(n2) { 21 | use = vector(); 22 | def = vector(); 23 | in = vector(); 24 | out = vector(); 25 | } 26 | 27 | void setnextBlock1(int n1) { 28 | nextBlock1 = n1; 29 | } 30 | 31 | void setnextBlock2(int n2) { 32 | nextBlock2 = n2; 33 | } 34 | 35 | void insert(midCode mc) { 36 | midCodeVector.push_back(mc); 37 | } 38 | 39 | void useInsert(string name) { 40 | use.push_back(name); 41 | } 42 | 43 | void defInsert(string name) { 44 | def.push_back(name); 45 | } 46 | 47 | void inInsert(string name) { 48 | in.push_back(name); 49 | } 50 | 51 | void outInsert(string name) { 52 | out.push_back(name); 53 | } 54 | 55 | void output() { 56 | cout << start << " " << end << " " << nextBlock1 << " " << nextBlock2 << "\n"; 57 | cout << "use: \n"; 58 | for (int i = 0; i < use.size(); i++) { 59 | cout << use[i] << " "; 60 | } 61 | cout << "\n"; 62 | cout << "def: \n"; 63 | for (int i = 0; i < def.size(); i++) { 64 | cout << def[i] << " "; 65 | } 66 | cout << "\n"; 67 | cout << "in: \n"; 68 | for (int i = 0; i < in.size(); i++) { 69 | cout << in[i] << " "; 70 | } 71 | cout << "\n"; 72 | cout << "out: \n"; 73 | for (int i = 0; i < out.size(); i++) { 74 | cout << out[i] << " "; 75 | } 76 | cout << "\n"; 77 | for (int i = 0; i < midCodeVector.size(); i++) { 78 | cout << "(" << start + i << ") "; 79 | midCode mc = midCodeVector[i]; 80 | switch (mc.op) { 81 | case PLUSOP: 82 | cout << mc.z << " = " << mc.x << " + " << mc.y << "\n"; 83 | break; 84 | case MINUOP: 85 | cout << mc.z << " = " << mc.x << " - " << mc.y << "\n"; 86 | break; 87 | case MULTOP: 88 | cout << mc.z << " = " << mc.x << " * " << mc.y << "\n"; 89 | break; 90 | case DIVOP: 91 | cout << mc.z << " = " << mc.x << " / " << mc.y << "\n"; 92 | break; 93 | case LSSOP: 94 | cout << mc.z << " = (" << mc.x << " < " << mc.y << ")\n"; 95 | break; 96 | case LEQOP: 97 | cout << mc.z << " = (" << mc.x << " <= " << mc.y << ")\n"; 98 | break; 99 | case GREOP: 100 | cout << mc.z << " = (" << mc.x << " > " << mc.y << ")\n"; 101 | break; 102 | case GEQOP: 103 | cout << mc.z << " = (" << mc.x << " >= " << mc.y << ")\n"; 104 | break; 105 | case EQLOP: 106 | cout << mc.z << " = (" << mc.x << " == " << mc.y << ")\n"; 107 | break; 108 | case NEQOP: 109 | cout << mc.z << " = (" << mc.x << " != " << mc.y << ")\n"; 110 | break; 111 | case ASSIGNOP: 112 | cout << mc.z << " = " << mc.x << "\n"; 113 | break; 114 | case GOTO: 115 | cout << "GOTO " << mc.z << "\n"; 116 | break; 117 | case BZ: 118 | cout << "BZ " << mc.z << "(" << mc.x << "=0)" << "\n"; 119 | break; 120 | case BNZ: 121 | cout << "BNZ " << mc.z << "(" << mc.x << "=1)" << "\n"; 122 | break; 123 | case PUSH: 124 | cout << "PUSH " << mc.z << "\n"; 125 | break; 126 | case CALL: 127 | cout << "CALL " << mc.z << "\n"; 128 | break; 129 | case RET: 130 | cout << "RET " << mc.z << "\n"; 131 | break; 132 | case INLINERET: 133 | cout << "INLINERET " << mc.z << "\n"; 134 | break; 135 | case RETVALUE: 136 | cout << "RETVALUE " << mc.z << " = " << mc.x << "\n"; 137 | break; 138 | case SCAN: 139 | cout << "SCAN " << mc.z << "\n"; 140 | break; 141 | case PRINT: 142 | cout << "PRINT " << mc.z << " " << mc.x << "\n"; 143 | break; 144 | case LABEL: 145 | cout << mc.z << ": \n"; 146 | break; 147 | case CONST: 148 | cout << "CONST " << mc.z << " " << mc.x << " = " << mc.y << endl; 149 | break; 150 | case ARRAY: 151 | cout << "ARRAY " << mc.z << " " << mc.x << "[" << mc.y << "]" << endl; 152 | break; 153 | case VAR: 154 | cout << "VAR " << mc.z << " " << mc.x << endl; 155 | break; 156 | case FUNC: 157 | cout << "FUNC " << mc.z << " " << mc.x << "()" << endl; 158 | break; 159 | case PARAM: 160 | cout << "PARA " << mc.z << " " << mc.x << endl; 161 | break; 162 | case GETARRAY: 163 | cout << mc.z << " = " << mc.x << "[" << mc.y << "]\n"; 164 | break; 165 | case PUTARRAY: 166 | cout << mc.z << "[" << mc.x << "]" << " = " << mc.y << "\n"; 167 | break; 168 | case EXIT: 169 | cout << "EXIT\n"; 170 | break; 171 | default: 172 | break; 173 | } 174 | } 175 | } 176 | }; 177 | 178 | void splitBlock(); 179 | 180 | void showFuncBlock(); 181 | 182 | void calUseDef(Block& bl, string funcName); 183 | 184 | void calInOut(); 185 | -------------------------------------------------------------------------------- /symbolItem.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef SYMBOLITEM_H 3 | #define SYMBOLITEM_H 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | class symbolItem { 12 | public: 13 | string name; 14 | int kind; //var const function array 15 | int type; //int char void 16 | int constInt; 17 | char constChar; 18 | int length; //数组长度 对于函数用于记录这个函数有多少变量(参数+局部变量+临时) 19 | vector parameterTable; //参数类型 20 | int addr; //地址 21 | symbolItem(string s, int add = 0, int k = 0, int t = 0, int ci = 0, char cc = ' ', int l = 0) : 22 | name(s), kind(k), type(t), constInt(ci), constChar(cc), length(l), addr(add) { 23 | parameterTable = vector(); 24 | } 25 | symbolItem() {} 26 | void output() { 27 | cout << name << " "; 28 | switch (type) { 29 | case 1: 30 | cout << "int "; 31 | break; 32 | case 2: 33 | cout << "char "; 34 | break; 35 | case 3: 36 | cout << "void "; 37 | break; 38 | } 39 | switch (kind) { 40 | case 1: 41 | cout << "var "; 42 | break; 43 | case 2: 44 | cout << "const "; 45 | if (type == 1) { 46 | cout << constInt << " "; 47 | } 48 | else if (type == 2) { 49 | cout << constChar << " "; 50 | } 51 | break; 52 | case 3: 53 | cout << "func "; 54 | cout << "parameters: ("; 55 | for (int i = 0; i < parameterTable.size(); i++) { 56 | if (parameterTable[i] == 1) { 57 | cout << "int "; 58 | } 59 | else { 60 | cout << "char "; 61 | } 62 | } 63 | cout << ")" << " 参数个数: " << length << " "; 64 | break; 65 | case 4: 66 | cout << "array "; 67 | cout << length << " "; 68 | break; 69 | } 70 | cout << "addr: " << addr; 71 | cout << "\n"; 72 | } 73 | void insert(int t) { 74 | parameterTable.push_back(t); 75 | } 76 | }; 77 | 78 | #endif // !SYMBOLITEM_H 79 | -------------------------------------------------------------------------------- /test/error-CALL.txt: -------------------------------------------------------------------------------- 1 | case CALL: { 2 | string sx; 3 | paramSize = globalSymbolTable[mc.z].parameterTable.size(); 4 | while (paramSize) { 5 | sx = "$t0"; 6 | paramSize--; 7 | if (pushOpStack.empty()) { 8 | cout << "ERROR!!!!!!!!\n"; 9 | } 10 | else { 11 | midCode tmpMc = pushOpStack.top(); 12 | pushOpStack.pop(); 13 | loadValue(tmpMc.z, sx, true, va, get1); 14 | mipsCodeTable.push_back(mipsCode(sw, sx, "$sp", "", -4 * paramSize)); 15 | } 16 | } 17 | vector varList; 18 | for (int i = 3; i <= 9; i++) { 19 | if (tRegBusy[i]) { 20 | varList.push_back("$t" + int2string(i)); 21 | } 22 | } 23 | for (int i = 0; i <= 7; i++) { 24 | if (sRegBusy[i]) { 25 | varList.push_back("$s" + int2string(i)); 26 | } 27 | } 28 | int len = 4 * globalSymbolTable[mc.z].length + 4 * varList.size() + 8; 29 | mipsCodeTable.push_back(mipsCode(addi, "$sp", "$sp", "", -len)); 30 | mipsCodeTable.push_back(mipsCode(sw, "$ra", "$sp", "", 4)); 31 | mipsCodeTable.push_back(mipsCode(sw, "$fp", "$sp", "", 8)); 32 | for (int i = 0; i < varList.size(); i++) { 33 | mipsCodeTable.push_back(mipsCode(sw, varList[i], "$sp", "", 8 + 4 * i + 4)); 34 | } 35 | mipsCodeTable.push_back(mipsCode(addi, "$fp", "$sp", "", len)); 36 | mipsCodeTable.push_back(mipsCode(jal, mc.z, "", "")); 37 | for (int i = 0; i < varList.size(); i++) { 38 | mipsCodeTable.push_back(mipsCode(lw, varList[i], "$sp", "", 8 + 4 * i + 4)); 39 | } 40 | mipsCodeTable.push_back(mipsCode(lw, "$fp", "$sp", "", 8)); 41 | mipsCodeTable.push_back(mipsCode(lw, "$ra", "$sp", "", 4)); 42 | mipsCodeTable.push_back(mipsCode(addi, "$sp", "$sp", "", len)); 43 | break; 44 | } -------------------------------------------------------------------------------- /test/error-PUTARRAY.txt: -------------------------------------------------------------------------------- 1 | case PUTARRAY: { 2 | string sx = "$t0", sy = "$t1"; 3 | //midCodefile << mc.z << "[" << mc.x << "]" << " = " << mc.y << "\n"; 4 | //mc.x可能是标识符也可能是数值 数组下标 $t0 5 | //mc.z是数组名 6 | //mc.y可能是标识符也可能是数值 $t1 7 | loadValue(mc.y, sy, true, va, get1); 8 | get1 = false; 9 | loadValue(mc.x, sx, false, va, get1); 10 | if (allLocalSymbolTable[curFuncName].find(mc.z) != allLocalSymbolTable[curFuncName].end() 11 | && allLocalSymbolTable[curFuncName][mc.z].kind == 4) { //array 12 | addr = allLocalSymbolTable[curFuncName][mc.z].addr; 13 | if (!get1) { //数组下标保存在sx寄存器 14 | mipsCodeTable.push_back(mipsCode(addi, "$t2", "$fp", "", -4 * addr)); 15 | mipsCodeTable.push_back(mipsCode(sll, "$t0", sx, "", 2)); 16 | mipsCodeTable.push_back(mipsCode(sub, "$t2", "$t2", "$t0")); 17 | mipsCodeTable.push_back(mipsCode(sw, sy, "$t2", "", 0)); 18 | } 19 | else { //拿到了数组下标 存在了va中 20 | mipsCodeTable.push_back(mipsCode(sw, sy, "$fp", "", -4 * (addr + va))); 21 | } 22 | } 23 | else if (globalSymbolTable.find(mc.z) != globalSymbolTable.end() 24 | && globalSymbolTable[mc.z].kind == 4) { //array 25 | addr = globalSymbolTable[mc.z].addr; 26 | if (!get1) { //数组下标保存在sx寄存器 27 | mipsCodeTable.push_back(mipsCode(addi, "$t2", "$gp", "", addr * 4)); 28 | mipsCodeTable.push_back(mipsCode(sll, "$t0", sx, "", 2)); 29 | mipsCodeTable.push_back(mipsCode(add, "$t2", "$t2", "$t0")); 30 | mipsCodeTable.push_back(mipsCode(sw, sy, "$t2", "", 0)); 31 | } 32 | else { 33 | mipsCodeTable.push_back(mipsCode(sw, sy, "$gp", "", (addr + va) * 4)); 34 | } 35 | } 36 | break; 37 | } -------------------------------------------------------------------------------- /test/error-比较运算符.txt: -------------------------------------------------------------------------------- 1 | case LSSOP: { //< 2 | string sx = "$t0", sy = "$t1"; 3 | get1 = false; 4 | loadValue(mc.x, sx, false, va, get1); 5 | get2 = false; 6 | loadValue(mc.y, sy, false, va2, get2); 7 | mcNext = mcVe[i + 1]; 8 | if (mcNext.op == BZ) { //0跳 x>=y跳 9 | if (get1 && get2) { 10 | if (va >= va2) { 11 | mipsCodeTable.push_back(mipsCode(j, mcNext.z, "", "")); 12 | } 13 | } 14 | else if (get1 && !get2) { //va >= $t1 15 | mipsCodeTable.push_back(mipsCode(ble, sy, int2string(va), mcNext.z)); //ble <=跳转 代价4 16 | } 17 | else if (!get1 && get2) { //$t0 >= va2 18 | mipsCodeTable.push_back(mipsCode(bge, sx, int2string(va2), mcNext.z)); //bge >=跳转 代价3 19 | } 20 | else { //$t0 >= $t1 21 | mipsCodeTable.push_back(mipsCode(bge, sx, sy, mcNext.z)); //bge >=跳转 代价3 22 | } 23 | } 24 | else if (mcNext.op == BNZ) { //1跳 x跳转 代价4 32 | } 33 | else if (!get1 && get2) { //$t0 < va2 34 | mipsCodeTable.push_back(mipsCode(blt, sx, int2string(va2), mcNext.z)); //blt <跳转 代价3 35 | } 36 | else { //$t0 < $t1 37 | mipsCodeTable.push_back(mipsCode(blt, sx, sy, mcNext.z)); //blt <跳转 代价3 38 | } 39 | } 40 | i++; 41 | break; 42 | } 43 | case LEQOP: { //<= 44 | string sx = "$t0", sy = "$t1"; 45 | get1 = false; 46 | loadValue(mc.x, sx, false, va, get1); 47 | get2 = false; 48 | loadValue(mc.y, sy, false, va2, get2); 49 | mcNext = mcVe[i + 1]; 50 | if (mcNext.op == BZ) { //0跳 x>y跳 51 | if (get1 && get2) { 52 | if (va > va2) { 53 | mipsCodeTable.push_back(mipsCode(j, mcNext.z, "", "")); 54 | } 55 | } 56 | else if (get1 && !get2) { //va > $t1 57 | mipsCodeTable.push_back(mipsCode(blt, sy, int2string(va), mcNext.z)); //blt <跳转 代价3 58 | } 59 | else if (!get1 && get2) { //$t0 > va2 60 | mipsCodeTable.push_back(mipsCode(bgt, sx, int2string(va2), mcNext.z)); //bgt >跳转 代价4 61 | } 62 | else { //$t0 > $t1 63 | mipsCodeTable.push_back(mipsCode(bgt, sx, sy, mcNext.z)); //bgt >跳转 代价3 64 | } 65 | } 66 | else if (mcNext.op == BNZ) { //1跳 x<=y跳 67 | if (get1 && get2) { 68 | if (va <= va2) { 69 | mipsCodeTable.push_back(mipsCode(j, mcNext.z, "", "")); 70 | } 71 | } 72 | else if (get1 && !get2) { //va <= $t1 73 | mipsCodeTable.push_back(mipsCode(bge, sy, int2string(va), mcNext.z)); //bge >=跳转 代价3 74 | } 75 | else if (!get1 && get2) { //$t0 <= va2 76 | mipsCodeTable.push_back(mipsCode(ble, sx, int2string(va2), mcNext.z)); //ble <=跳转 代价4 77 | } 78 | else { //$t0 <= $t1 79 | mipsCodeTable.push_back(mipsCode(ble, sx, sy, mcNext.z)); //ble <=跳转 代价3 80 | } 81 | } 82 | i++; 83 | break; 84 | } 85 | case GREOP: { //> 86 | string sx = "$t0", sy = "$t1"; 87 | get1 = false; 88 | loadValue(mc.x, sx, false, va, get1); 89 | get2 = false; 90 | loadValue(mc.y, sy, false, va2, get2); 91 | mcNext = mcVe[i + 1]; 92 | if (mcNext.op == BZ) { //0跳 x<=y跳 93 | if (get1 && get2) { 94 | if (va <= va2) { 95 | mipsCodeTable.push_back(mipsCode(j, mcNext.z, "", "")); 96 | } 97 | } 98 | else if (get1 && !get2) { //va <= $t1 99 | mipsCodeTable.push_back(mipsCode(bge, sy, int2string(va), mcNext.z)); //bge >=跳转 代价3 100 | } 101 | else if (!get1 && get2) { //$t0 <= va2 102 | mipsCodeTable.push_back(mipsCode(ble, sx, int2string(va2), mcNext.z)); //ble <=跳转 代价4 103 | } 104 | else { //$t0 <= $t1 105 | mipsCodeTable.push_back(mipsCode(ble, sx, sy, mcNext.z)); //ble <=跳转 代价3 106 | } 107 | } 108 | else if (mcNext.op == BNZ) { //1跳 x>y跳 109 | if (get1 && get2) { 110 | if (va > va2) { 111 | mipsCodeTable.push_back(mipsCode(j, mcNext.z, "", "")); 112 | } 113 | } 114 | else if (get1 && !get2) { //va > $t1 115 | mipsCodeTable.push_back(mipsCode(blt, sy, int2string(va), mcNext.z)); //blt <跳转 代价3 116 | } 117 | else if (!get1 && get2) { //$t0 > va2 118 | mipsCodeTable.push_back(mipsCode(bgt, sx, int2string(va2), mcNext.z)); //bgt >跳转 代价4 119 | } 120 | else { //$t0 > $t1 121 | mipsCodeTable.push_back(mipsCode(bgt, sx, sy, mcNext.z)); //bgt >跳转 代价3 122 | } 123 | } 124 | i++; 125 | break; 126 | } 127 | case GEQOP: { //>= 128 | string sx = "$t0", sy = "$t1"; 129 | get1 = false; 130 | loadValue(mc.x, sx, false, va, get1); 131 | get2 = false; 132 | loadValue(mc.y, sy, false, va2, get2); 133 | mcNext = mcVe[i + 1]; 134 | if (mcNext.op == BZ) { //0跳 x跳转 代价4 142 | } 143 | else if (!get1 && get2) { //$t0 < va2 144 | mipsCodeTable.push_back(mipsCode(blt, sx, int2string(va2), mcNext.z)); //blt <跳转 代价3 145 | } 146 | else { //$t0 < $t1 147 | mipsCodeTable.push_back(mipsCode(blt, sx, sy, mcNext.z)); //blt <跳转 代价3 148 | } 149 | } 150 | else if (mcNext.op == BNZ) { //1跳 x>=y跳 151 | if (get1 && get2) { 152 | if (va >= va2) { 153 | mipsCodeTable.push_back(mipsCode(j, mcNext.z, "", "")); 154 | } 155 | } 156 | else if (get1 && !get2) { //va >= $t1 157 | mipsCodeTable.push_back(mipsCode(ble, sy, int2string(va), mcNext.z)); //ble <=跳转 代价4 158 | } 159 | else if (!get1 && get2) { //$t0 >= va2 160 | mipsCodeTable.push_back(mipsCode(bge, sx, int2string(va2), mcNext.z)); //bge >=跳转 代价3 161 | } 162 | else { //$t0 >= $t1 163 | mipsCodeTable.push_back(mipsCode(bge, sx, sy, mcNext.z)); //bge >=跳转 代价3 164 | } 165 | } 166 | i++; 167 | break; 168 | } 169 | case EQLOP: { 170 | string sx = "$t0", sy = "$t1"; 171 | get1 = false; 172 | loadValue(mc.x, sx, false, va, get1); 173 | get2 = false; 174 | loadValue(mc.y, sy, false, va2, get2); 175 | mcNext = mcVe[i + 1]; 176 | if (mcNext.op == BZ) { //0跳 177 | if (get1 && get2) { 178 | if (va != va2) { 179 | mipsCodeTable.push_back(mipsCode(j, mcNext.z, "", "")); 180 | } 181 | } 182 | else if (get1 && !get2) { 183 | mipsCodeTable.push_back(mipsCode(li, sx, "", "", va)); 184 | mipsCodeTable.push_back(mipsCode(bne, sx, sy, mcNext.z)); 185 | } 186 | else if (!get1 && get2) { 187 | mipsCodeTable.push_back(mipsCode(li, sy, "", "", va2)); 188 | mipsCodeTable.push_back(mipsCode(bne, sx, sy, mcNext.z)); 189 | } 190 | else { 191 | mipsCodeTable.push_back(mipsCode(bne, sx, sy, mcNext.z)); 192 | } 193 | } 194 | else if (mcNext.op == BNZ) { //1跳 195 | if (get1 && get2) { 196 | if (va == va2) { 197 | mipsCodeTable.push_back(mipsCode(j, mcNext.z, "", "")); 198 | } 199 | } 200 | else if (get1 && !get2) { 201 | mipsCodeTable.push_back(mipsCode(li, sx, "", "", va)); 202 | mipsCodeTable.push_back(mipsCode(beq, sx, sy, mcNext.z)); 203 | } 204 | else if (!get1 && get2) { 205 | mipsCodeTable.push_back(mipsCode(li, sy, "", "", va2)); 206 | mipsCodeTable.push_back(mipsCode(beq, sx, sy, mcNext.z)); 207 | } 208 | else { 209 | mipsCodeTable.push_back(mipsCode(beq, sx, sy, mcNext.z)); 210 | } 211 | } 212 | i++; 213 | break; 214 | } 215 | case NEQOP: { 216 | string sx = "$t0", sy = "$t1"; 217 | get1 = false; 218 | loadValue(mc.x, sx, false, va, get1); 219 | get2 = false; 220 | loadValue(mc.y, sy, false, va2, get2); 221 | mcNext = mcVe[i + 1]; 222 | if (mcNext.op == BZ) { //0跳 223 | if (get1 && get2) { 224 | if (va == va2) { 225 | mipsCodeTable.push_back(mipsCode(j, mcNext.z, "", "")); 226 | } 227 | } 228 | else if (get1 && !get2) { 229 | mipsCodeTable.push_back(mipsCode(li, sx, "", "", va)); 230 | mipsCodeTable.push_back(mipsCode(beq, sx, sy, mcNext.z)); 231 | } 232 | else if (!get1 && get2) { 233 | mipsCodeTable.push_back(mipsCode(li, sy, "", "", va2)); 234 | mipsCodeTable.push_back(mipsCode(beq, sx, sy, mcNext.z)); 235 | } 236 | else { 237 | mipsCodeTable.push_back(mipsCode(beq, sx, sy, mcNext.z)); 238 | } 239 | } 240 | else if (mcNext.op == BNZ) { //1跳 241 | if (get1 && get2) { 242 | if (va != va2) { 243 | mipsCodeTable.push_back(mipsCode(j, mcNext.z, "", "")); 244 | } 245 | } 246 | else if (get1 && !get2) { 247 | mipsCodeTable.push_back(mipsCode(li, sx, "", "", va)); 248 | mipsCodeTable.push_back(mipsCode(bne, sx, sy, mcNext.z)); 249 | } 250 | else if (!get1 && get2) { 251 | mipsCodeTable.push_back(mipsCode(li, sy, "", "", va2)); 252 | mipsCodeTable.push_back(mipsCode(bne, sx, sy, mcNext.z)); 253 | } 254 | else { 255 | mipsCodeTable.push_back(mipsCode(bne, sx, sy, mcNext.z)); 256 | } 257 | } 258 | i++; 259 | break; 260 | } -------------------------------------------------------------------------------- /test/sRegTest.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | void ccc() { 4 | int a,b,c,d,e,r,t,y,u,i,o,p; 5 | 6 | a=-3;b=1; 7 | if (a>0) { 8 | b=1;c=1;d=1;e=1;r=1;t=1;y=1;u=1;i=1;o=1;p=1; 9 | } 10 | else { 11 | a=1; 12 | } 13 | printf(b); 14 | } 15 | 16 | void eee() { 17 | int a,b,c,d,e,r,t,y,u,i,o,p; 18 | b=2; 19 | a=-3; 20 | if (a>0) { 21 | a=1;c=1;d=1;e=1;r=1;t=1;y=1;u=1;i=1;o=1;p=1; 22 | } 23 | else { 24 | b=1; 25 | } 26 | printf(a); 27 | } 28 | 29 | void ddd() { 30 | int a,b,c,d,e,r,t,y,u,i,o,p; 31 | b=1; 32 | a=3; 33 | if (a>0) { 34 | b=1;c=1;d=1;e=1;r=1;t=1;y=1;u=1;i=1;o=1;p=1; 35 | } 36 | else { 37 | a=1; 38 | } 39 | printf(a); 40 | } 41 | 42 | void qqq() { 43 | int a,b,c,d,e,r,t,y,u,i,o,p; 44 | b=2; 45 | a=-3; 46 | if (a>0) { 47 | a=1;b=1;c=1;d=1;e=1;r=1;t=1;y=1;u=1;i=1;o=1;p=1; 48 | } 49 | printf(b); 50 | } 51 | 52 | void f() { 53 | int a,b,c,d,e,r,t,y,u,i,o,p; 54 | a=-10;b=2; 55 | while(a>0) { 56 | b=1;c=1;d=1;e=1;r=1;t=1;y=1;u=1;i=1;o=1;p=1; 57 | a=-1; 58 | } 59 | a=1; 60 | printf(b); 61 | } 62 | 63 | void ff() { 64 | int a,b,c,d,e,r,t,y,u,i,o,p; 65 | a=10;b=2; 66 | do { 67 | a=1; 68 | a=a-1;b=1;c=1;d=1;e=1;r=1;t=1;y=1;u=1;i=1;o=1;p=1; 69 | printf(a); 70 | } while (a>0) 71 | a=1; 72 | printf(b); 73 | } 74 | 75 | void fff() { 76 | int i,a1,a9,a2; 77 | for (i=0;i<10;i=i+1) { 78 | a9=1; 79 | a1=a9; 80 | if (a1>0) { 81 | a2=2; 82 | } 83 | else { 84 | a2=1; 85 | } 86 | a9=10; 87 | printf(a2); 88 | } 89 | } 90 | 91 | void cc(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9) { 92 | int i; 93 | for (i=0;i<10;i=i+1) { 94 | a1=a9; 95 | if (a1>0) { 96 | a2=2; 97 | } 98 | else { 99 | a2=1; 100 | } 101 | a9=10; 102 | printf(a2); 103 | } 104 | } 105 | 106 | 107 | void c(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9) { 108 | int i; 109 | for (i=0;i<10;i=i+1) { 110 | a1=a9; 111 | a9=10; 112 | printf(a2); 113 | printf(i); 114 | } 115 | } 116 | 117 | void main() { 118 | c(1,2,3,4,5,6,7,8,-1); 119 | cc(1,2,3,4,5,6,7,8,-1); 120 | ccc(); 121 | f(); 122 | ff(); 123 | fff(); 124 | qqq(); 125 | ddd(); 126 | eee(); 127 | } -------------------------------------------------------------------------------- /test/sRegTestRes.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 0 3 | 2 4 | 1 5 | 2 6 | 2 7 | 2 8 | 3 9 | 2 10 | 4 11 | 2 12 | 5 13 | 2 14 | 6 15 | 2 16 | 7 17 | 2 18 | 8 19 | 2 20 | 9 21 | 1 22 | 2 23 | 2 24 | 2 25 | 2 26 | 2 27 | 2 28 | 2 29 | 2 30 | 2 31 | 1 32 | 2 33 | 0 34 | 1 35 | 2 36 | 2 37 | 2 38 | 2 39 | 2 40 | 2 41 | 2 42 | 2 43 | 2 44 | 2 45 | 2 46 | 3 47 | -3 48 | 49 | -- program is finished running -- 50 | 51 | -------------------------------------------------------------------------------- /test/test/16061160_test1.txt: -------------------------------------------------------------------------------- 1 | const int songzhuoyang = 2; 2 | const char cookiez = '_'; 3 | int a; 4 | int b,c; 5 | int d[10]; 6 | int e[20],f,g; 7 | char ch1; 8 | char ch2,ch3; 9 | char ch4[10]; 10 | char ch5,ch6[19]; 11 | 12 | void main(){ 13 | const int SongZhuoyang = 100; 14 | const char cookiezT = 'T'; 15 | int a1; 16 | int b1,c1; 17 | int d1[10]; 18 | int e1[20],f1,g1; 19 | char ch7; 20 | char ch8,ch9; 21 | char ch10[10]; 22 | char ch11,ch12[19]; 23 | a = 1; 24 | b = 2; 25 | c = 3; 26 | d = 4; 27 | e = 5; 28 | f = 6; 29 | g = 7; 30 | a1 = 8; 31 | b1 = 9; 32 | c1 = 10; 33 | f1 = 11; 34 | g1 = 12; 35 | ch1 = 'A'; 36 | ch2 = 'B'; 37 | ch3 = 'C'; 38 | ch5 = 'D'; 39 | ch7 = 'E'; 40 | ch8 = 'F'; 41 | ch9 = 'G'; 42 | ch11 = 'H'; 43 | printf(songzhuoyang); 44 | printf(cookiez); 45 | printf(SongZhuoyang); 46 | printf(cookiezT); 47 | printf(" a = " , a); 48 | printf(" b = " , b); 49 | printf(" c = " , c); 50 | printf(" f = " , f); 51 | printf(" g = " , g); 52 | printf(" a1 = " , a1); 53 | printf(" b1 = " , b1); 54 | printf(" c1 = " , c1); 55 | printf(" f1 = " , f1); 56 | printf(" g1 = " , g1); 57 | printf(" ch1 = " , ch1); 58 | printf(" ch2 = " , ch2); 59 | printf(" ch3 = " , ch3); 60 | printf(" ch5 = " , ch5); 61 | printf(" ch7 = " , ch7); 62 | printf(" ch8 = " , ch8); 63 | printf(" ch9 = " , ch9); 64 | printf(" ch11 = " , ch11); 65 | return; 66 | } -------------------------------------------------------------------------------- /test/test/16061160_test10.txt: -------------------------------------------------------------------------------- 1 | const int const_1=1,const_2=2; 2 | const int const_3=3; 3 | const char const_a='a',const_b='b'; 4 | const char const_c='c'; 5 | const char const_='_'; 6 | int count,num[10]; 7 | char letter[5]; 8 | char s; 9 | int _,__,___,__a__,_a_; 10 | int judge; 11 | 12 | 13 | int get_sum(int a) 14 | { 15 | int b; 16 | judge = judge + 1; 17 | count = count + 1; 18 | b=0; 19 | if(a!=1){ 20 | b = get_sum(a-1); 21 | judge = judge + 1; 22 | } 23 | return(b + a); 24 | 25 | } 26 | 27 | int count_to_10() 28 | { 29 | judge = judge + 1; 30 | return(get_sum(10)); 31 | } 32 | 33 | char higher(char a) 34 | { 35 | return('A'); 36 | } 37 | 38 | 39 | char prt_message() 40 | { 41 | judge = judge + 1; 42 | printf(" !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); 43 | return('a'); 44 | } 45 | 46 | 47 | void test_global_variable() 48 | { 49 | int a,flag; 50 | judge = judge + 1; 51 | a=0; 52 | flag=0; 53 | count=2; 54 | letter[0]='a'; 55 | letter[1]='1'; 56 | letter[2]='_'; 57 | letter[3]='+'; 58 | letter[4]='A'; 59 | printf(count); 60 | printf(letter[0]); 61 | 62 | if(count==2){ 63 | judge = judge + 1; 64 | } 65 | 66 | if(count==1){ 67 | judge = judge + 1; 68 | flag=1; 69 | } 70 | if(flag==0){ 71 | judge = judge + 1; 72 | } 73 | 74 | _=1; 75 | __=2; 76 | ___=3; 77 | __a__=4; 78 | _a_=5; 79 | 80 | flag=0; 81 | if(_==1) 82 | judge = judge + 1; 83 | if(_==0){ 84 | flag=1; 85 | } 86 | if(flag==0) 87 | judge = judge + 1; 88 | 89 | flag=0; 90 | if(__==2) 91 | judge = judge + 1; 92 | if(__==0){ 93 | judge = judge + 1; 94 | flag=1; 95 | } 96 | if(flag==0) 97 | judge = judge + 1; 98 | 99 | flag=0; 100 | if(___==3) 101 | judge = judge + 1; 102 | if(___==0){ 103 | judge = judge + 1; 104 | flag=1; 105 | } 106 | if(flag==0) 107 | judge = judge + 1; 108 | 109 | flag=0; 110 | if(__a__==4) 111 | judge = judge + 1; 112 | if(__a__==0){ 113 | judge = judge + 1; 114 | flag=1; 115 | } 116 | if(flag==0) 117 | judge = judge + 1; 118 | 119 | flag=0; 120 | if(_a_==5) 121 | judge = judge + 1; 122 | if(_a_==0){ 123 | judge = judge + 1; 124 | flag=1; 125 | } 126 | if(flag==0) 127 | judge = judge + 1; 128 | 129 | 130 | return ; 131 | } 132 | 133 | void test_global_const(){ 134 | int x; 135 | char y; 136 | int num[2]; 137 | int _; 138 | _=100; 139 | if(_==100) 140 | judge = judge + 1; 141 | num[0]=100; 142 | num[1]=101; 143 | if(num[0]==100) 144 | judge = judge + 1; 145 | if(num[1]==101) 146 | judge = judge + 1; 147 | judge = judge + 1; 148 | if(const_1==1) 149 | judge = judge + 1; 150 | if(const_a=='a') 151 | judge = judge + 1; 152 | if(const_=='_') 153 | judge = judge + 1; 154 | 155 | y = const_a; 156 | x = y + const_a; 157 | if(x == 194) judge = judge +1; 158 | 159 | switch(const_1){ 160 | case 1:{ 161 | judge = judge + 1; 162 | } 163 | default:; 164 | } 165 | 166 | switch(const_1){ 167 | case 0:{ 168 | judge = judge + 1; 169 | } 170 | default:{ 171 | ; 172 | } 173 | } 174 | 175 | x=0; 176 | while(xx){ 210 | judge = judge + 1; 211 | } 212 | if(x==y){ 213 | judge = judge + 1; 214 | flag=1; 215 | } 216 | if(flag==0){ 217 | judge = judge + 1; 218 | } 219 | if(x!=y){ 220 | judge = judge + 1; 221 | } 222 | x=0; 223 | while(x<=1){ 224 | if(x<=y){ 225 | judge = judge + 1; 226 | } 227 | x = x + 1; 228 | } 229 | x = 0; 230 | while(x<=1){ 231 | if(y>=x){ 232 | judge = judge + 1; 233 | } 234 | x = x + 1; 235 | } 236 | 237 | } 238 | 239 | void test_expression() 240 | { 241 | int a,b; 242 | char c,d,e,f,g; 243 | a=0; 244 | b=0; 245 | c='a'; 246 | d='b'; 247 | e='c'; judge = judge + 1; 248 | a = +a; 249 | if(a==0) judge = judge + 1; 250 | a = -a; 251 | if(a==0) judge = judge + 1; 252 | a = +c; 253 | if(a==97) judge = judge + 1; 254 | a = -c; 255 | if(a==-97) judge = judge + 1; 256 | a = const_3; 257 | if(a==3) judge = judge + 1; 258 | a = +-1; 259 | if(a==-1) judge = judge + 1; 260 | a = --1; 261 | if(a==1) judge = judge + 1; 262 | a = ++1; 263 | if(a==1) judge = judge + 1; 264 | a = -+1; 265 | if(a==-1) judge = judge + 1; 266 | a = (((1))); 267 | if(a==1) judge = judge + 1; 268 | a = 3*-3; 269 | if(a==-9) judge = judge + 1; 270 | 271 | b=5; 272 | a = -(num[b]+num[b+const_1]+3)*count_to_10()/const_3+'a'+const_a-3*3/4+letter[0]; 273 | if(a==33) judge = judge +1; 274 | a = 1; 275 | b = 1; 276 | f = 'a'; 277 | g = 'q'; 278 | a = a*(b+c*(d+(e+f*g))); 279 | judge = judge + a/1024; 280 | return ; 281 | } 282 | 283 | void test_while() 284 | { 285 | int a,b; 286 | 287 | b=9; 288 | while(b>=0){ 289 | if(b>=5) 290 | judge = judge + 1; 291 | b = b - 1; 292 | } 293 | 294 | b=0; 295 | while(b<=2){ 296 | switch(b){ 297 | case 0:{ 298 | judge = judge + 1; 299 | } 300 | case 1:{ 301 | judge = judge + 1; 302 | } 303 | default:{ 304 | judge = judge + 1; 305 | } 306 | } 307 | b = b+1; 308 | } 309 | 310 | b = 0; 311 | a = 0; 312 | while(a<=2){ 313 | b = 0; 314 | while(b<=2){ 315 | judge = judge + 1; 316 | b = b + 1; 317 | } 318 | a = a + 1; 319 | } 320 | 321 | 322 | b=0; 323 | a = 11; 324 | while(b<10){ 325 | num[b] = b*a; 326 | judge = judge + num[b]; 327 | b = b + 1; 328 | a = a + 2; 329 | } 330 | 331 | return ; 332 | } 333 | 334 | void test_if() 335 | { 336 | int a,flag; 337 | char c; 338 | a = 0; 339 | flag=0; 340 | judge = judge + 1; 341 | if(const_1==1){ 342 | 343 | } 344 | 345 | if(const_1==1) 346 | ; 347 | 348 | if (const_1==1){ 349 | ; 350 | } 351 | 352 | a=0; 353 | while(a<=1){ 354 | if(const_1==1){ 355 | if(a==0){ 356 | judge = judge + 1; 357 | flag=1; 358 | } 359 | if(flag==0){ 360 | judge = judge + 1; 361 | } 362 | } 363 | flag=0; 364 | a=a+1; 365 | } 366 | 367 | a=0; 368 | if (const_1==1){ 369 | while(a<2){ 370 | judge = judge + 1; 371 | a = a + 1; 372 | } 373 | 374 | } 375 | 376 | a=0; 377 | while(a<=1){ 378 | if(const_1==1){ 379 | switch(a){ 380 | case 0:{ 381 | judge = judge + 1; 382 | } 383 | default: 384 | judge = judge + 1; 385 | } 386 | } 387 | a = a + 1; 388 | } 389 | 390 | 391 | flag=0; 392 | c='a'; 393 | if(c=='a'){ 394 | judge = judge + 1; 395 | } 396 | 397 | if(c=='b'){ 398 | judge = judge + 1; 399 | flag=1; 400 | } 401 | if(flag==0){ 402 | judge = judge + 1; 403 | } 404 | 405 | 406 | return ; 407 | } 408 | 409 | void test_switch() 410 | { 411 | int a,b,flag,test; 412 | char c; 413 | flag=0; 414 | test=0; 415 | a=0; 416 | b=0; 417 | while(a<=6){ 418 | switch(a){ 419 | case 0:{ 420 | if(const_1==1){ 421 | judge = judge + 1; 422 | num[0]=const_3; 423 | num[const_3]=get_sum(const_3); 424 | } 425 | if(const_1==0){ 426 | judge = judge + 1; 427 | flag=1; 428 | } 429 | if(flag==0){ 430 | judge = judge + 1; 431 | } 432 | test=1; 433 | } 434 | case 1:{ 435 | judge = judge + 1; 436 | b=0; 437 | while(b<=2){ 438 | judge = judge + 1; 439 | b = b +1; 440 | } 441 | test = 1; 442 | } 443 | case 2:{ 444 | ; 445 | } 446 | default:{ 447 | judge = judge + 1; 448 | switch(a/const_3){ 449 | case 1:{ 450 | judge = judge + 1; 451 | test = 1; 452 | } 453 | default:{ ; } 454 | } 455 | if(test==0){ 456 | judge = judge + 1; 457 | } 458 | test = 1; 459 | } 460 | } 461 | 462 | if(test==0){ 463 | judge = judge + 1; 464 | } 465 | test=0; 466 | a = a + 1; 467 | } 468 | 469 | a=0; 470 | while(a<3){ 471 | if(a==0) 472 | c = 'a'; 473 | if(a==1) 474 | c = 'b'; 475 | if(a==2) 476 | c = 'c'; 477 | switch(c){ 478 | case 'a':{ 479 | judge = judge + 1; 480 | } 481 | case 'b':{ 482 | judge = judge + 1; 483 | } 484 | default:{ 485 | judge = judge + 1; 486 | } 487 | } 488 | a = a + 1; 489 | } 490 | 491 | a=1; 492 | switch(a){ 493 | case 1: ; 494 | } 495 | 496 | 497 | return ; 498 | } 499 | 500 | void test_function() 501 | { 502 | int a; 503 | char c; 504 | 505 | a = 11; 506 | c = 'a'; 507 | judge = judge + 1; 508 | printf(higher(c)+count_to_10()); 509 | 510 | prt_message(); 511 | 512 | 513 | return ; 514 | 515 | } 516 | 517 | void test_printf(){ 518 | int x; 519 | x=1; 520 | judge = judge + 1; 521 | printf(x); 522 | 523 | x=5; 524 | printf(-(num[x]+num[x+const_1]+3)*count_to_10()/const_3+'a'+const_a-3*3/4); 525 | 526 | 527 | return ; 528 | } 529 | 530 | void test_scanf(){ 531 | int x,y,z; 532 | char m,n,q; 533 | judge = judge + 1; 534 | scanf(x,m); 535 | judge = judge + x + m; 536 | scanf(m); 537 | letter[3] = m; 538 | judge = judge + m; 539 | scanf(x); 540 | judge = judge + x; 541 | 542 | return ; 543 | } 544 | 545 | void test_varib2() 546 | { 547 | judge = judge + 1; 548 | if(_==1){ 549 | judge = judge + 1; 550 | } 551 | return ; 552 | } 553 | 554 | void test_varib() 555 | { 556 | int _; 557 | judge = judge + 1; 558 | _ = 100; 559 | if(_==100) 560 | judge = judge + 1; 561 | test_varib2(); 562 | return ; 563 | } 564 | 565 | void test_para4(int a,int b,int c,int d,int e,int f,int g) 566 | { 567 | judge = judge + a +b+c+d+e+f+g; 568 | } 569 | 570 | int test_para1(int a,int b){ 571 | judge = judge + 1; 572 | return (1); 573 | } 574 | 575 | char test_para2(char a,char b){ 576 | judge = judge + 1; 577 | return ('a'); 578 | } 579 | 580 | void test_para3(char a,int b){ 581 | judge = judge + 1; 582 | return ; 583 | } 584 | 585 | void test_para(){ 586 | judge = judge + 1; 587 | test_para1(1,2); 588 | test_para2('a','b'); 589 | test_para3('a',1); 590 | test_para4(1,2,3,4,5,6,7); 591 | return ; 592 | } 593 | 594 | void main() 595 | { 596 | 597 | int x; 598 | judge = 0; 599 | judge = judge + 1; 600 | x=0; 601 | test_global_variable(); 602 | 603 | test_global_const(); 604 | 605 | test_brackets(); 606 | 607 | test_compare_operator(); 608 | 609 | test_expression(); 610 | 611 | test_while(); 612 | 613 | test_if(); 614 | 615 | test_switch(); 616 | 617 | test_function(); 618 | 619 | test_printf(); 620 | 621 | test_scanf(); 622 | 623 | test_varib(); 624 | 625 | test_para(); 626 | 627 | printf("all test:",judge); 628 | 629 | 630 | return ; 631 | } -------------------------------------------------------------------------------- /test/test/16061160_test11.txt: -------------------------------------------------------------------------------- 1 | const int const_int_1 = 12345679, const_int_2 = 0; 2 | const int const_int_3 = -0, const_int_4 = -12345679; 3 | const char const_char_1 = '9', const_char_2 = '_', const_char_3 = '+', const_char_4 = '*'; 4 | 5 | int global_int_array_1[5], global_int_1; 6 | char global_char_array_1[5], global_char_1; 7 | int global_int_2, global_int_array_2[5]; 8 | char global_char_2, global_char_array_2[5], global_char_array_3[10]; 9 | 10 | void hanoi(int n, char from, char tmp, char to) 11 | { 12 | if (n > 0) { 13 | hanoi(n - 1, from, to, tmp); 14 | printf("take"); 15 | printf(n); 16 | printf("from"); 17 | printf(from); 18 | printf("to"); 19 | printf(to); 20 | hanoi(n - 1, tmp, from, to); 21 | } 22 | return; 23 | } 24 | 25 | int Fibonacci(int n) { 26 | if (n == 0) 27 | return (0); 28 | if (n == 1) 29 | return (1); 30 | return (Fibonacci(n - 1) + Fibonacci(n - 2)); 31 | } 32 | 33 | int fac(int n) { 34 | if (n == 1) 35 | return (1); 36 | return (n * fac(n - 1)); 37 | } 38 | 39 | void initGlobalArray() { 40 | global_int_array_1[0] = 0; 41 | global_int_array_1[1] = 1; 42 | global_int_array_1[2] = global_int_array_1[0] + global_int_array_1[1]; 43 | global_int_array_1[3] = global_int_array_1[2] + global_int_array_1[1]; 44 | global_int_array_1[4] = global_int_array_1[3] + global_int_array_1[2]; 45 | global_int_array_2[0] = global_int_array_1[1]; 46 | global_int_array_2[1] = global_int_array_2[0] * global_int_array_1[2]; 47 | global_int_array_2[2] = global_int_array_2[1] * global_int_array_1[3]; 48 | global_int_array_2[3] = global_int_array_2[2] * global_int_array_1[4]; 49 | global_int_array_2[4] = global_int_array_2[3] / global_int_array_1[4]; 50 | global_char_array_1[0] = 'a'; 51 | global_char_array_1[1] = 'A'; 52 | global_char_array_1[2] = 'z'; 53 | global_char_array_1[3] = 'Z'; 54 | global_char_array_1[4] = '_'; 55 | global_char_array_2[0] = '+'; 56 | global_char_array_2[1] = '-'; 57 | global_char_array_2[2] = '*'; 58 | global_char_array_2[3] = '/'; 59 | global_char_array_2[4] = '6'; 60 | return; 61 | } 62 | 63 | void assignGlobal(int a, char b, int c, char d) { 64 | global_int_1 = a; 65 | global_char_1 = b; 66 | global_int_2 = c; 67 | global_char_2 = d; 68 | return; 69 | } 70 | 71 | void printGlobalConst() { 72 | printf(const_int_1); 73 | printf(const_int_2); 74 | printf(const_int_3); 75 | printf(const_int_4); 76 | printf(const_char_1); 77 | printf(const_char_2); 78 | printf(const_char_3); 79 | printf(const_char_4); 80 | return; 81 | } 82 | 83 | void printGlobalVar() { 84 | printf(global_int_1); 85 | printf(global_int_2); 86 | printf(global_char_1); 87 | printf(global_char_2); 88 | return; 89 | } 90 | void printGlobalArray() { 91 | printf(global_int_array_1[0]); 92 | printf(global_int_array_1[1]); 93 | printf(global_int_array_1[2]); 94 | printf(global_int_array_1[3]); 95 | printf(global_int_array_1[4]); 96 | printf(global_int_array_2[0]); 97 | printf(global_int_array_2[1]); 98 | printf(global_int_array_2[2]); 99 | printf(global_int_array_2[3]); 100 | printf(global_int_array_2[4]); 101 | printf(global_char_array_1[0]); 102 | printf(global_char_array_1[1]); 103 | printf(global_char_array_1[2]); 104 | printf(global_char_array_1[3]); 105 | printf(global_char_array_1[4]); 106 | printf(global_char_array_2[0]); 107 | printf(global_char_array_2[1]); 108 | printf(global_char_array_2[2]); 109 | printf(global_char_array_2[3]); 110 | printf(global_char_array_2[4]); 111 | return; 112 | } 113 | 114 | void testGlobal() { 115 | initGlobalArray(); 116 | assignGlobal(const_int_1, const_char_1, const_int_4, const_char_4); 117 | printGlobalConst(); 118 | printGlobalVar(); 119 | printGlobalArray(); 120 | return; 121 | } 122 | 123 | int testReturnInt(int a) { 124 | return (a + 1); 125 | } 126 | 127 | char testReturnChar(int a) { 128 | switch (a) { 129 | case 1: 130 | return ('a'); 131 | case 2: 132 | return ('b'); 133 | case 3: 134 | return ('c'); 135 | default: { 136 | 137 | } 138 | } 139 | return ('_'); 140 | } 141 | 142 | void testReturn() { 143 | printf(testReturnInt(Fibonacci(fac(3) + fac(2)))); 144 | printf(testReturnChar(1)); 145 | printf(testReturnChar(2)); 146 | printf(testReturnChar(3)); 147 | printf(testReturnChar(4)); 148 | return; 149 | } 150 | 151 | void testAssignAndExp() { 152 | const int const_int_1 = 1, const_int_2 = -1, const_int_3 = 0; 153 | const char const_char_1 = 'a', const_char_2 = 'A', const_char_3 = 'b'; 154 | 155 | int int_temp_1, int_temp_2, int_array[2], int_temp_3; 156 | char char_temp_1, char_array[2], char_temp_2; 157 | 158 | int_temp_1 = const_int_1 + (Fibonacci(Fibonacci(Fibonacci(5))) + const_int_1 / (const_int_2 * const_int_4)) / fac(3); 159 | int_temp_2 = fac(fac(3) + fac(2)); 160 | int_array[const_int_3] = testReturnInt(0); 161 | int_array[const_int_1 - (const_int_2 * const_int_4 + testReturnInt(0) * const_int_4)] = const_int_1 + testReturnInt(const_int_1); 162 | int_temp_3 = int_array[const_int_3] - (int_array[const_int_1 - (const_int_2 * const_int_4 + testReturnInt(0) * const_int_4)] * ('b' - 'a')); 163 | char_array[0] = testReturnChar(int_array[0]); 164 | char_array[const_int_1 - (const_int_2 * const_int_4 + testReturnInt(0) * const_int_4)] = testReturnChar(int_array[1] + const_int_1); 165 | char_temp_1 = char_array[0]; 166 | char_temp_2 = char_array[const_int_1 - (const_int_2 * const_int_4 + testReturnInt(0) * const_int_4)]; 167 | printf(int_temp_1); 168 | printf(int_temp_2); 169 | printf(int_temp_3); 170 | printf(int_array[const_int_3]); 171 | printf(int_array[const_int_1 - (const_int_2 * const_int_4 + testReturnInt(0) * const_int_4)]); 172 | printf(char_temp_1); 173 | printf(char_temp_2); 174 | printf(char_array[0]); 175 | printf(char_array[const_int_1]); 176 | printf(char_temp_1 * (char_array[0] + char_array[0] * char_temp_2)); 177 | return; 178 | } 179 | 180 | void testIO() { 181 | scanf(global_int_1); 182 | scanf(global_char_1); 183 | printf("!@\n#$^&*()Qqaa123[];',./"); 184 | printf(global_int_1); 185 | printf(global_char_1); 186 | scanf(global_int_1, global_char_1); 187 | printf("!@\n#$^&*()Qqaa123[];',./", global_int_1); 188 | printf(global_char_1); 189 | scanf(global_char_1, global_int_1); 190 | printf("!@\n#$^&*()Qqaa123[];',./", global_char_1); 191 | printf(global_int_1); 192 | return; 193 | } 194 | 195 | void testIfWhile() 196 | { 197 | int i, j, k; 198 | char a, b, c, d; 199 | k = 10; 200 | a = '+'; 201 | b = '-'; 202 | c = '*'; 203 | d = '/'; 204 | if (k > 0) 205 | { 206 | if (k <= 10) 207 | { 208 | i = 0; 209 | while (i < k / 2) 210 | { 211 | switch (i - (i / 2 * 2)) { 212 | case 0: { 213 | global_char_array_3[i] = a; 214 | } 215 | case 1: { 216 | global_char_array_3[i] = b; 217 | } 218 | } 219 | printf(global_char_array_3[i]); 220 | i = i + 1; 221 | } 222 | while (i < k) 223 | { 224 | switch (i - (i / 2 * 2)) { 225 | case 0: { 226 | global_char_array_3[i] = c; 227 | } 228 | default: { 229 | global_char_array_3[i] = d; 230 | } 231 | } 232 | printf(global_char_array_3[i]); 233 | i = i + 1; 234 | } 235 | } 236 | } 237 | return; 238 | } 239 | 240 | 241 | int func_ret_int_1(int i_1, int i_2, char c_1, char c_2) { 242 | int i_temp; 243 | char c_temp; 244 | printf("INPUT of func_ret_int_1:"); 245 | printf(i_1); 246 | printf(i_2); 247 | printf(c_1); 248 | printf(c_2); 249 | i_temp = i_1; 250 | i_1 = i_2; 251 | i_2 = i_temp; 252 | c_temp = c_1; 253 | c_1 = c_2; 254 | c_2 = c_temp; 255 | printf("OPERATE of func_ret_int_1:"); 256 | printf(i_1); 257 | printf(i_2); 258 | printf(c_1); 259 | printf(c_2); 260 | return (i_1); 261 | 262 | } 263 | 264 | void testPara() { 265 | int i, j; 266 | char c, d; 267 | i = const_int_1; 268 | j = const_int_4; 269 | c = 'A'; 270 | d = 'Z'; 271 | printf(func_ret_int_1(i, j, c, d)); 272 | return; 273 | } 274 | 275 | void testRecursion() { 276 | hanoi((Fibonacci(fac(2) * fac(3)) - fac(Fibonacci(3) + Fibonacci(4))) / fac(Fibonacci(2) + Fibonacci(3)), 'a', 'b', 'c'); 277 | return; 278 | } 279 | 280 | void main() 281 | { 282 | printf("************************************************"); 283 | printf("Start testing recursion:"); 284 | testRecursion(); 285 | printf("************************************************"); 286 | printf("Start testing global:"); 287 | testGlobal(); 288 | printf("************************************************"); 289 | printf("Start testing return:"); 290 | testReturn(); 291 | printf("************************************************"); 292 | printf("Start testing I/O:"); 293 | testIO(); 294 | printf("************************************************"); 295 | printf("Start testing assign & exp:"); 296 | testAssignAndExp(); 297 | printf("************************************************"); 298 | printf("Start testing if & while:"); 299 | testIfWhile(); 300 | printf("************************************************"); 301 | printf("Start testing switch:"); 302 | testSwitch(); 303 | printf("************************************************"); 304 | printf("Start testing parameter:"); 305 | testPara(); 306 | printf("************************************************"); 307 | return; 308 | } -------------------------------------------------------------------------------- /test/test/16061160_test12.txt: -------------------------------------------------------------------------------- 1 | const char cha='a',chb='b',chc='c',chd='d'; 2 | const char chA='A',chB='B',chC='C',chD='D'; 3 | 4 | int c,d; 5 | int abc[3],i; 6 | 7 | int fun1(int a,int b){ 8 | c=0; 9 | c=(a+b)*b+a*b; 10 | printf(" c=(a+b)*b+a*b "); 11 | printf(" c=",c); 12 | printf(" "); 13 | return(c); 14 | } 15 | 16 | char fun2(int a,int b){ 17 | c=0; 18 | d=0; 19 | printf("input c and d:"); 20 | scanf(c,d); 21 | if(c>d) 22 | printf(a); 23 | if(c=b){ 33 | printf(" cha-chad "); 34 | printf(cha); 35 | printf(chb); 36 | printf(chc); 37 | printf(chd); 38 | } 39 | if(a2); 63 | } 64 | 65 | int fun5(int a,int b){ 66 | return(fun1(a,b)); 67 | } 68 | 69 | char fun6(int a,int b){ 70 | return (fun2(a,b)); 71 | } 72 | 73 | int fun7(){ 74 | abc[0]=11; 75 | return(abc[0]); 76 | } 77 | 78 | void main(){ 79 | int flag; 80 | int num; 81 | char ch; 82 | num=0; 83 | ch='a'; 84 | scanf(flag); 85 | switch(flag){ 86 | case 1:{ 87 | num=fun1(2,3); 88 | ch=fun2(2,3); 89 | fun3(2,3); 90 | fun5(2,3); 91 | fun6(2,3); 92 | } 93 | case 2:{ 94 | num=fun1(3,2); 95 | ch=fun2(3,2); 96 | fun3(3,2); 97 | fun5(3,2); 98 | fun6(3,2); 99 | } 100 | case 3:{ 101 | num=fun1(2,2); 102 | ch=fun2(2,2); 103 | fun3(2,2); 104 | fun5(2,2); 105 | fun6(2,2); 106 | } 107 | case 4: 108 | fun4(); 109 | case 5:{ 110 | num=fun7(); 111 | } 112 | default:printf("error!"); 113 | } 114 | printf(num); 115 | printf(ch); 116 | } -------------------------------------------------------------------------------- /test/test/16061160_test13.txt: -------------------------------------------------------------------------------- 1 | const char cha='a',chb='b'; 2 | const char CHA='A',CHB='B'; 3 | const int I=3; 4 | int c,d; 5 | int abc[3],i; 6 | 7 | int expre(int a,int b,int x,int y,int z){ 8 | {{{{{;};};};};} 9 | i=i+1; 10 | c=0; 11 | if(i 0){ 48 | printf(" # 5 "); 49 | } 50 | if(a > f1 / 7){ 51 | printf(" # 6 "); 52 | } 53 | if(b >= d[4] - 2){ 54 | printf(" # 7 "); 55 | } 56 | if(b >= --7){ 57 | printf(" # 8 "); 58 | } 59 | if(+e1[9] - g1 < e1[9] + g1){ 60 | printf(" # 9 "); 61 | } 62 | if(g1 * e1[9] < f1){ 63 | printf(" # 10 "); 64 | } 65 | if(3 <= 5){ 66 | printf(" # 11 "); 67 | } 68 | if(5 <= 1){ 69 | printf(" # 12 "); 70 | } 71 | if(5 <= 5){ 72 | printf(" # 13 "); 73 | } 74 | if(b - b){ 75 | printf(" # 14 "); 76 | } 77 | if(b){ 78 | printf(" # 15 "); 79 | } 80 | a = 1; 81 | while(a < 10){ 82 | printf(a); 83 | a = a + 1; 84 | } 85 | while(a){ 86 | printf(a); 87 | a = a - 2; 88 | } 89 | a1 = 3; 90 | return; 91 | } -------------------------------------------------------------------------------- /test/test/16061160_test5.txt: -------------------------------------------------------------------------------- 1 | const int global_single_int = 2; 2 | const int global_int_1 = 3 , global_int_2 = 4; 3 | const char global_single_char = '_'; 4 | const char global_char_1 = '+' , global_char_2 = '9'; 5 | int a; 6 | int b,c; 7 | int d[10]; 8 | int e[20],f,g; 9 | char ch1; 10 | char ch2,ch3; 11 | char ch4[10]; 12 | char ch5,ch6[19]; 13 | 14 | int fac(int n){ 15 | if(n == 1) return (1); 16 | return (n * fac(n - 1)); 17 | } 18 | 19 | char getch3(char ch , char ch1 , char ch2){ 20 | const int func7_const1 = 3,func7_const2 = 2; 21 | const char func7_const3 = '3',func7_const4 = 'o'; 22 | int a1; 23 | int e1[10]; 24 | char c; 25 | c = ch2; 26 | return(c); 27 | } 28 | 29 | char getch2(char ch){ 30 | const int func6_const1 = 3,func6_const2 = 2; 31 | const char func6_const3 = '3',func6_const4 = 'o'; 32 | int a1; 33 | int e1[10]; 34 | char c; 35 | return(ch); 36 | } 37 | 38 | char getch1(){ 39 | const int func5_const1 = 3,func5_const2 = 2; 40 | const char func5_const3 = 't',func5_const4 = '_'; 41 | int a1; 42 | int e1[10]; 43 | char c; 44 | return(global_char_1); 45 | } 46 | 47 | int add_mult3(int x , int y){ 48 | const int func3_const1 = 3,func3_const2 = 2; 49 | int a1; 50 | int e1[10]; 51 | e1[2] = (x + y) * func3_const1; 52 | return (e1[2]); 53 | } 54 | 55 | int add_mult2(int x , int y){ 56 | const int func2_const1 = 1,func2_const2 = 2; 57 | int a1; 58 | int e1[10]; 59 | a1 = (x + y) * func2_const2; 60 | return (a1); 61 | } 62 | 63 | int add_mult1(int x , int y , int z){ 64 | const int func4_const1 = 1,func4_const2 = 2; 65 | int a1; 66 | int e1[10]; 67 | a1 = add_mult2(x , y) + z; 68 | return (a1); 69 | } 70 | 71 | void print_num(int x){ 72 | const int func1_const1 = 1,func1_const2 = 2; 73 | int a1; 74 | int e1[10]; 75 | a = a + 1; 76 | printf(" | print_num called: " , a); 77 | printf(" x = " , x); 78 | } 79 | 80 | void main(){ 81 | const int local_single_int = 100; 82 | const int local_int_1 = 101 , local_int_2 = 102; 83 | const char local_single_char = '-'; 84 | const char local_char_1 = '*' , local_char_2 = '/'; 85 | int a1; 86 | int b1,c1; 87 | int d1[12]; 88 | int e1[20],f1,g1; 89 | char ch7; 90 | char ch8,ch9; 91 | char ch10[10]; 92 | char ch11,ch12[19]; 93 | a = 0; 94 | b = 5; 95 | f1 = 2; 96 | f = 3; 97 | d[f1] = 6; 98 | b1 = 7; 99 | d1[f] = 8; 100 | d1[10] = 9; 101 | d[(b - f) * f1] = 10; 102 | print_num(4); 103 | print_num(b); 104 | print_num(d[f1]); 105 | print_num(b1); 106 | print_num(d1[f]); 107 | print_num(d1[b1 + f]); 108 | print_num(d[(b - f) * f1]); 109 | a1 = 1; 110 | while(a1 < 5){ 111 | print_num(a1); 112 | a1 = a1 + 1; 113 | } 114 | print_num(add_mult2(a , f1)); 115 | g1 = add_mult2(1 , 3); 116 | print_num(g1); 117 | print_num(add_mult3(b , f1)); 118 | print_num(add_mult1(1 , 2 , 3)); 119 | printf(getch1()); 120 | printf(getch2('q')); 121 | printf(getch3('s','z','y')); 122 | printf(fac(1)); 123 | printf(fac(5)); 124 | return; 125 | } -------------------------------------------------------------------------------- /test/test/16061160_test6.txt: -------------------------------------------------------------------------------- 1 | int n; 2 | int a[10],flag[10]; 3 | 4 | void perm(int k){ 5 | int i; 6 | if(k==n){ 7 | i = 0; 8 | while(i < n){ 9 | printf(a[i]); 10 | i = i + 1; 11 | } 12 | return; 13 | } 14 | i = 1; 15 | while(i <= n){ 16 | if(flag[i] == 0){ 17 | flag[i] = 1; 18 | a[k]=i; 19 | perm(k+1); 20 | flag[i] = 0; 21 | } 22 | i = i + 1; 23 | } 24 | } 25 | void main(){ 26 | scanf(n); 27 | perm(0); 28 | } -------------------------------------------------------------------------------- /test/test/16061160_test7.txt: -------------------------------------------------------------------------------- 1 | const int _const_int_0 = +0, _const_int_1 = +1, _const_int_2 = -3; 2 | const char _const_char_0 = '*', _const_char_1 = '0'; 3 | 4 | int _var_int_0, _var_int_arr_0[1]; 5 | int _var_int_1, _var_int_arr_1[17]; 6 | char _var_char_0, _var_char_arr_0[1]; 7 | char _var_char_1, _var_char_arr_1[17]; 8 | 9 | int count_i, count_j; 10 | int test, _______; 11 | char _char, i; 12 | 13 | char getNext(){ 14 | _char = _char; 15 | return (_char); 16 | } 17 | 18 | char getFormer(){ 19 | _char = _char; 20 | return (_char); 21 | } 22 | 23 | void testLoop0(){ 24 | _var_char_arr_1[count_i] = getNext(); 25 | count_i = count_i + 1; 26 | } 27 | 28 | void testLoop1(){ 29 | _var_int_arr_1[count_j] = getFormer() + 1 - 1; 30 | count_j = count_j - 1; 31 | } 32 | 33 | void checkResult(int i){ 34 | if(_var_int_arr_1[i] != (_var_char_arr_1[16 - i])) 35 | test = 0; 36 | 37 | } 38 | 39 | void testArithmetic(){ 40 | const int standard = 100, ten = 10; 41 | int temp; 42 | temp = --10 -+ 10 + ten * (_const_char_1 - _const_char_0) / _const_int_2 + standard / (ten + _const_int_2 - _const_int_1); 43 | if(temp) 44 | printf("Arithmetic Check Succeed!"); 45 | if(temp==0) 46 | printf("Arithmetic Check Failed!"); 47 | temp = (standard + _const_int_1 * 15) / ten; 48 | if(temp == 11) 49 | printf("Division Check Passed!"); 50 | if(temp!=11) 51 | printf("Error in Division Unit!"); 52 | } 53 | 54 | void testValue(){ 55 | _var_int_0 = -0; 56 | if(_var_int_0 == _const_int_0) 57 | if(_var_int_0 < _const_int_1) 58 | if(_var_int_0 > _const_int_2) 59 | printf("Value Assignment Succeed!"); 60 | if(_var_int_0 <= _const_int_2) 61 | printf("Negative Assignment Failed!"); 62 | if(_var_int_0 >= _const_int_1) 63 | printf("Positive Assignment Failed!"); 64 | if(_var_int_0 != _const_int_0) 65 | printf("Basic Assignment Failed!"); 66 | 67 | } 68 | 69 | 70 | void testDo(){ 71 | int i; 72 | _char = '0'; 73 | count_i = 0; 74 | count_j = 16; 75 | testLoop0(); 76 | while(count_i <= 16) 77 | testLoop0(); 78 | 79 | _char = _char; 80 | 81 | testLoop1(); 82 | while(count_j >= 0) 83 | testLoop1(); 84 | i = 0; 85 | test = 1; 86 | { 87 | checkResult(i); 88 | i = i + 1; 89 | } 90 | while(i <= 16) 91 | { 92 | checkResult(i); 93 | i = i + 1; 94 | } 95 | if(test) 96 | printf("Do Loop Passed!"); 97 | if(test==0) 98 | printf("Error in Do Loop!"); 99 | } 100 | 101 | void testIO(char c, int i){ 102 | int temp; 103 | printf(c + i); 104 | printf("Beginning IO Test..."); 105 | scanf(i); 106 | scanf(c); 107 | printf("Number One is:", i); 108 | temp = c + 1 - 1; 109 | printf("Number Two is:", temp); 110 | printf("The Result is:", c + i); 111 | } 112 | 113 | void testCaseInsensitive(){ 114 | int temp; 115 | temp = -0; 116 | temp = _const_int_1; 117 | switch(temp){ 118 | case 0: printf("Case Sensitive Detected!"); 119 | case 1: printf("Case Insensitive Passed!"); 120 | default: printf("Error in testing Case Insensitive!"); 121 | } 122 | } 123 | 124 | void testSwitch(int i){ 125 | int Temp; 126 | Temp = i; 127 | _var_int_1 = +1; 128 | switch(Temp + _const_int_2 + _var_int_1 * _const_int_1){ 129 | case -5: printf("Error in Argument Passing!"); 130 | case 0: printf("Unknown Error Happened!"); 131 | case 1: { 132 | printf("Switch Logic Check Succeed!"); 133 | printf("Switch Parse Check Succeed!"); 134 | } 135 | default: printf("Switch Logic Error!"); 136 | } 137 | } 138 | 139 | void testIf(){ 140 | if(_const_char_1 - _const_char_0 != 2 * 3) 141 | printf("If Statement Error!"); 142 | if(_const_char_1 - _const_char_0 == 2 * 3) 143 | printf("Passed If Statement Testing!"); 144 | } 145 | 146 | int testRecursive(int i){ 147 | if(i == 1) 148 | return (1); 149 | if(i!=1) 150 | return (i + testRecursive(i - 1)); 151 | } 152 | 153 | void testArray(){ 154 | _var_int_arr_0[0] = '*' + 1 - 1; 155 | _var_char_arr_0[0] = _const_char_0; 156 | if(_var_int_arr_0[0] + 1 == _var_char_arr_0[0] + 1) 157 | printf("Initial Array Test Passed!"); 158 | if(_var_int_arr_0[0] + 1 != _var_char_arr_0[0] + 1) 159 | printf("Error in Array Testing!"); 160 | } 161 | 162 | void testEmptyFunction(){} 163 | 164 | void testFunction(){;;;;;;} 165 | 166 | void main(){ 167 | int temp; 168 | int in1, in2; 169 | char in3; 170 | _char = '0'; 171 | _______ = 1024; 172 | scanf(in1, in2, in3); 173 | printf(in1 + in2 * in3 / in1 - in2); 174 | testValue(); 175 | testArithmetic(); 176 | testArray(); 177 | testFunction(); 178 | testEmptyFunction(); 179 | testIO('0', 0); 180 | testIf(); 181 | testDo(); 182 | testSwitch(-1 * _const_int_2); 183 | 184 | scanf(temp); 185 | printf("The Sequential Result for Input is:", testRecursive(temp)); 186 | if(testRecursive(10) == 55) 187 | printf("Recursive Check Succeed!"); 188 | if(testRecursive(10) != 55) 189 | printf("Error in Recursive Check!"); 190 | testCaseInsensitive(); 191 | return; 192 | } -------------------------------------------------------------------------------- /test/test/16061160_test8.txt: -------------------------------------------------------------------------------- 1 | const int con1 = 1, con2 = 2; 2 | const char ch1 = '_'; 3 | int i, j, array[100], scope_var; 4 | char ch; 5 | 6 | int Fibonacci(int n) { 7 | if (n == 1) { 8 | return(1); 9 | } 10 | if (n == 2) { 11 | return(1); 12 | } 13 | return(Fibonacci(n - 1) + Fibonacci(n - 2)); 14 | } 15 | 16 | int test_recursion() { 17 | if (Fibonacci(8) == 21) { 18 | printf("success!"); 19 | return(0); 20 | } 21 | printf("fail!"); 22 | return(0); 23 | } 24 | int test_if(int plus) { 25 | const char flag = 'g'; 26 | char ch; 27 | ch = 'a'; 28 | if (ch + plus > flag + 1 - 1) { 29 | printf("success1!"); 30 | } 31 | if (1) { 32 | printf("success2!"); 33 | } 34 | if (0) { 35 | printf("fail1!"); 36 | } 37 | return(0); 38 | } 39 | 40 | void test_while() { 41 | const int j = 100; 42 | int sum; 43 | sum = 0; 44 | i = 0; 45 | while (i < j) { 46 | array[i] = i; 47 | i = i + 1; 48 | } 49 | ;;;;;; 50 | {{{}}{}} 51 | i = 0; 52 | while (i < j) { 53 | sum = sum + i; 54 | i = i + 1; 55 | } 56 | if (sum == 4950) { 57 | printf("success!"); 58 | return; 59 | } 60 | printf("fail!"); 61 | return; 62 | } 63 | 64 | void test_switch() { 65 | char flag; 66 | flag = 'a'; 67 | } 68 | 69 | int to_upper(char a, int b) { 70 | return(a - b); 71 | } 72 | void test_scanf_printf() { 73 | char a, b, c; 74 | scanf(a, b, c); 75 | printf("Input is:"); 76 | printf(a); 77 | printf(b); 78 | printf(c); 79 | printf(to_upper(a, 32)); 80 | printf(to_upper(b, 32)); 81 | printf(to_upper(c, 32)); 82 | } 83 | void test_relation_operator(int x, int y) { 84 | if (x == y) printf(" x==y:true"); 85 | if (x > y) printf(" x>y:true"); 86 | if (x < y) printf(" x= y) printf(" x>=y:true"); 88 | if (x <= y) printf(" x<=y:true"); 89 | if (x != y) printf(" x!=y:true"); 90 | } 91 | void test_expression() { 92 | int x, y, z, A[1]; 93 | x = 10; 94 | y = 1; 95 | A[0 * 2] = 2; 96 | z = +x + (-1 * x*A[0] * (-y)*(+y) / 2 + '*' + to_upper('a', --32) - 'A'); 97 | if (z == 62) { 98 | printf("success!"); 99 | return; 100 | } 101 | printf("fail!"); 102 | return; 103 | } 104 | void test_symbol() { 105 | char _a1, _a2, _a3, _a4, _a5; 106 | int aa, Aa, array[30], i, sum; 107 | _a1 = '*'; 108 | _a2 = '/'; 109 | _a3 = '+'; 110 | _a4 = '-'; 111 | _a5 = '_'; 112 | aa = 1; 113 | Aa = 0; 114 | if (aa == 0) printf("fail1!"); 115 | i = 0; 116 | sum = _a1 + _a2 + _a3 + _a4 + _a5; 117 | while (i < 10) { 118 | array[i] = i; 119 | i = i + 1; 120 | } 121 | while (i < 20) { 122 | array[i] = 'a' + i - 10; 123 | i = i + 1; 124 | } 125 | while (i < 30) { 126 | array[i] = 'A' + i - 20; 127 | i = i + 1; 128 | } 129 | i = 0; 130 | while (i < 30) { 131 | sum = sum + array[i]; 132 | i = i + 1; 133 | } 134 | if (sum == 2027) { 135 | printf("success!"); 136 | return; 137 | } 138 | printf("fail2!"); 139 | return; 140 | } 141 | 142 | 143 | void local_scope() { 144 | int scope_var; 145 | scope_var = 10; 146 | printf("the local scope_var = ", scope_var); 147 | } 148 | void test_scope() { 149 | int tem; 150 | scope_var = 20; 151 | tem = scope_var; 152 | printf("the global scope_var = ", scope_var); 153 | local_scope(); 154 | printf("the global scope_var = ", scope_var); 155 | if (tem == scope_var) { 156 | printf("success!"); 157 | return; 158 | } 159 | printf("fail!"); 160 | return; 161 | } 162 | 163 | void test() { 164 | int i, j, k; 165 | j = 0; 166 | k = 9; 167 | printf("test_recursion begin:"); 168 | test_recursion(); 169 | printf("test_if begin:"); 170 | test_if(20); 171 | printf("test_while begin:"); 172 | test_while(); 173 | printf("test_switch begin:"); 174 | test_switch(); 175 | printf("test_scanf_printf begin:"); 176 | test_scanf_printf(); 177 | printf("test_expression begin:"); 178 | test_expression(); 179 | printf("test_symbol begin:"); 180 | test_symbol(); 181 | printf("test_relation_operator begin:"); 182 | test_relation_operator(con1 + 2, con2 + 4); 183 | test_relation_operator(con1 + 6, con2 + 3); 184 | test_relation_operator(con1 + 1, con2); 185 | printf("test_scope begin:"); 186 | test_scope(); 187 | printf("*********************"); 188 | j = j + 1; 189 | ch = ch1; 190 | printf("ch=", ch); 191 | printf("@#$$&*^^^^test end"); 192 | } 193 | void main() { 194 | test(); 195 | } -------------------------------------------------------------------------------- /test/test/16061160_test9.txt: -------------------------------------------------------------------------------- 1 | const char const_char_a = 'a', const_char_b = 'b', const_char_c = 'c', const_char_A = 'A'; 2 | const int const_int_postive = 1, const_int_zero = +0, const_int_negtive = -1; 3 | const char const_char_num = '9', const_char_underline = '_',const_char_add = '+', const_char_mul 4 | 5 | = '*'; 6 | 7 | int int_global_var; 8 | char char_global_var; 9 | int sort[10]; 10 | 11 | void testIfWhile() 12 | { 13 | const int test_if_1 = 1, test_if_2 = 2; 14 | const char const_char_a = 'b', const_char_b = 'a'; 15 | int test_if_result, loop_num; 16 | 17 | test_if_result = 0; 18 | loop_num = 0; 19 | if(const_char_a + 1 > const_char_b + 1) 20 | { 21 | while(test_if_result < 100) 22 | { 23 | test_if_result = test_if_result + loop_num; 24 | loop_num = loop_num + 1; 25 | } 26 | switch((test_if_result - 100) * 3) 27 | { 28 | case 15: 29 | { 30 | test_if_result = -test_if_result * 10 + 2 * loop_num + 117- const_char_a; 31 | loop_num = +loop_num - test_if_result + 10 * const_char_b - 100 + 42 + 34 + 2 * 32 | 33 | test_if_result; 34 | } 35 | default: 36 | { 37 | test_if_result = +test_if_result * 4 + 5 * loop_num + 24- const_char_b + 'c'; 38 | loop_num = -loop_num + test_if_result + 4 * const_char_b - 1653 + 453 + 555 - 4 * 39 | 40 | test_if_result; 41 | } 42 | } 43 | } 44 | printf(test_if_result); 45 | printf(loop_num); 46 | 47 | test_if_result = 0; 48 | loop_num = 0; 49 | if(const_char_a + 1 >= const_char_b + 1) 50 | { 51 | while(test_if_result < 10) 52 | { 53 | if(loop_num > 2) 54 | test_if_result = test_if_result + loop_num; 55 | loop_num = loop_num + 1; 56 | if(test_if_result / 2 * 2 == test_if_result) 57 | loop_num = loop_num + 1; 58 | } 59 | } 60 | printf(test_if_result); 61 | printf(loop_num); 62 | 63 | test_if_result = 0; 64 | loop_num = 0; 65 | if(test_if_1 < test_if_2) 66 | { 67 | while(test_if_result < 20) 68 | { 69 | switch(test_if_result - test_if_result / 2 * 2) 70 | { 71 | case 0: test_if_result = test_if_result + 1; 72 | case 1: test_if_result = test_if_result + loop_num; 73 | } 74 | loop_num = loop_num + 1; 75 | } 76 | if(test_if_result == test_if_result / 2 * 2) 77 | { 78 | test_if_result = +test_if_result * 7 + 2 * loop_num + 99 - const_char_b + 'g'; 79 | loop_num = -loop_num + test_if_result + 2 * const_char_b - 523 + 53 + 355 - 2 * 80 | 81 | test_if_result; 82 | } 83 | } 84 | printf(test_if_result); 85 | printf(loop_num); 86 | 87 | if(test_if_2 <= test_if_1) 88 | printf("2 <= 1"); 89 | 90 | if(test_if_1 == test_if_2) 91 | printf("1 == 2"); 92 | 93 | test_if_result = 0; 94 | loop_num = 0; 95 | if(test_if_1 != test_if_2) 96 | { 97 | switch(test_if_result - loop_num * 2) 98 | { 99 | case 0: 100 | { 101 | while(test_if_result < 30) 102 | { 103 | switch(test_if_result / 2 * 2 - test_if_result) 104 | { 105 | case -1: test_if_result = test_if_result + 1; 106 | case 0: test_if_result = test_if_result + loop_num; 107 | } 108 | loop_num = loop_num + 1; 109 | } 110 | } 111 | default: 112 | { 113 | test_if_result = -1; 114 | loop_num = -1; 115 | } 116 | } 117 | } 118 | printf(test_if_result); 119 | printf(loop_num); 120 | 121 | if(test_if_1 - test_if_1) 122 | printf("1 - 1 = true"); 123 | 124 | int_global_var = -1; 125 | char_global_var = 'a'; 126 | } 127 | 128 | void testSwitchCase(int test_switch_int, char test_switch_char) 129 | { 130 | switch(test_switch_int) 131 | { 132 | case -1: 133 | { 134 | while(test_switch_int * 20 + 30 / 5 < 10) 135 | if(test_switch_int <= 0) 136 | test_switch_int = test_switch_int + 1; 137 | } 138 | case 0: 139 | { 140 | if(test_switch_int - const_int_postive * const_int_negtive == 0) 141 | while(test_switch_int <= 0) 142 | test_switch_int = (test_switch_int + 1) * -2; 143 | } 144 | case 1: test_switch_int = -233; 145 | default: test_switch_int = 233; 146 | } 147 | switch(test_switch_char) 148 | { 149 | case 'a': test_switch_char = const_char_a; 150 | case 'A': test_switch_char = const_char_A; 151 | case '9': test_switch_char = const_char_num; 152 | case '_': test_switch_char = const_char_underline; 153 | case '+': test_switch_char = const_char_add; 154 | case '*': test_switch_char = const_char_mul; 155 | default: test_switch_char = 'w'; 156 | } 157 | switch(0) 158 | { 159 | case 0:; 160 | } 161 | printf(test_switch_int); 162 | printf(test_switch_char); 163 | int_global_var = -2; 164 | char_global_var = 'b'; 165 | } 166 | 167 | int returnInt() 168 | { 169 | return (10); 170 | } 171 | 172 | char returnChar() 173 | { 174 | return ('r'); 175 | } 176 | 177 | int returnIntParameter(int select, int int_parameter_1, int int_parameter_2, int int_parameter_3, 178 | 179 | int int_parameter_4) 180 | { 181 | int _tmp; 182 | switch(select) 183 | { 184 | case 1: _tmp = int_parameter_1; 185 | case 2: _tmp = int_parameter_2; 186 | case 3: _tmp = int_parameter_3; 187 | case 4: _tmp = int_parameter_4; 188 | default: _tmp = 0; 189 | } 190 | return (_tmp); 191 | } 192 | 193 | char returnCharParameter(int select, char char_parameter_1, char char_parameter_2, char 194 | 195 | char_parameter_3, char char_parameter_4) 196 | { 197 | switch(select) 198 | { 199 | case 1: return (char_parameter_1); 200 | case 2: return (char_parameter_2); 201 | case 3: return (char_parameter_3); 202 | case 4: return (char_parameter_4); 203 | default: return ('0'); 204 | } 205 | } 206 | 207 | void testAssign() 208 | { 209 | const int test_assign_10 = +10; 210 | int int_test_assign, int_array_test_assign[3]; 211 | char char_test_assign, char_array_test_assign[3]; 212 | 213 | int_test_assign = ++10 + returnIntParameter(2, --1, -+2, +test_assign_10, (returnChar() - 214 | 215 | const_char_a)) * ('c' - const_char_a) / 2; 216 | int_array_test_assign[int_test_assign / 4 + const_char_a - 'a' - 2] = --2; 217 | int_array_test_assign[+-2 + int_test_assign / int_array_test_assign[0] + returnInt() - 11] = -returnInt() + (test_assign_10 * (returnChar() - const_char_a) + returnIntParameter(4, -'b' + 'c', returnInt(), test_assign_10, (97))); 218 | int_array_test_assign[-+255 + int_array_test_assign[1] + 'b' * 2 - 196] = -'b' + 'c'; 219 | 220 | char_test_assign = returnChar(); 221 | char_array_test_assign[0] = returnCharParameter(4, 'a', returnChar(), returnChar(), const_char_a); 222 | char_array_test_assign[1] = char_array_test_assign[0]; 223 | char_array_test_assign[2] = 'x'; 224 | printf(int_test_assign); 225 | printf(int_array_test_assign[0]); 226 | printf(int_array_test_assign[1]); 227 | printf(int_array_test_assign[2]); 228 | printf(char_test_assign); 229 | printf(char_array_test_assign[0]); 230 | printf(char_array_test_assign[1]); 231 | printf(char_array_test_assign[2]); 232 | 233 | int_global_var = -3; 234 | char_global_var = 'c'; 235 | } 236 | 237 | void testInputOutput() 238 | { 239 | int int_test_input; 240 | char char_test_input; 241 | int int_global_var; 242 | char char_global_var; 243 | scanf(int_test_input, char_test_input); 244 | printf(" !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); 245 | printf("int_test_input = ", int_test_input); 246 | printf("char_test_input = ",char_test_input); 247 | int_global_var = -4; 248 | char_global_var = 'd'; 249 | } 250 | 251 | void testTrick() 252 | { 253 | {;{};} 254 | {{{}}} 255 | ;;;;;; 256 | } 257 | 258 | int judge1(int a, int b, int c, int d) 259 | { 260 | int ret; 261 | ret = 0; 262 | if(a < b) 263 | if(c >= d) 264 | ret = 1; 265 | return (ret); 266 | } 267 | 268 | int judge2(int a, int b, int c, int d) 269 | { 270 | int ret; 271 | ret = 0; 272 | if(a < b) 273 | if(c <= d) 274 | ret = 1; 275 | return (ret); 276 | } 277 | 278 | void Qsort(int low, int high) 279 | { 280 | int first, last, key; 281 | first = low; 282 | last = high; 283 | key = sort[first]; 284 | if(low >= high) 285 | return; 286 | 287 | while(first < last) 288 | { 289 | while(judge1(first, last, sort[last], key) == 1) 290 | last = last - 1; 291 | 292 | sort[first] = sort[last]; 293 | 294 | while(judge2(first, last, sort[first], key) == 1) 295 | first = first + 1; 296 | 297 | sort[last] = sort[first]; 298 | } 299 | sort[first] = key; 300 | Qsort(low, first-1); 301 | Qsort(first+1, high); 302 | return; 303 | } 304 | 305 | void testRecursion() 306 | { 307 | int i; 308 | sort[0] = 5; 309 | sort[1] = 8; 310 | sort[2] = 3; 311 | sort[3] = 6; 312 | sort[4] = 4; 313 | sort[5] = 1; 314 | sort[6] = 2; 315 | sort[7] = 7; 316 | Qsort(0,7); 317 | i = 0; 318 | while(i < 8) 319 | { 320 | printf(sort[i]); 321 | i = i + 1; 322 | } 323 | int_global_var = -5; 324 | char_global_var = 'e'; 325 | } 326 | 327 | 328 | void testCallFunction() 329 | { 330 | int test_select; 331 | scanf(test_select); 332 | switch(test_select) 333 | { 334 | case 0: 335 | testIfWhile(); 336 | case 1: 337 | { 338 | testSwitchCase(const_int_postive,const_char_a); 339 | testSwitchCase(const_int_negtive,'+'); 340 | testSwitchCase(0,'_'); 341 | testSwitchCase(2,'9'); 342 | } 343 | case 2: 344 | testAssign(); 345 | case 3: 346 | testInputOutput(); 347 | case 4: 348 | testRecursion(); 349 | } 350 | printf(int_global_var); 351 | printf(char_global_var); 352 | } 353 | 354 | void main() 355 | { 356 | int_global_var = 0; 357 | char_global_var = '_'; 358 | testCallFunction(); 359 | testTrick(); 360 | } -------------------------------------------------------------------------------- /test/test/16231246.txt: -------------------------------------------------------------------------------- 1 | const int zero = 0; 2 | const int one = 1, minus_two = -2; 3 | const char renn = '0'; 4 | const char first_alpha = 'a', first_captical = 'A'; 5 | int yyy[7],xxxx, x, zzzzzzz; 6 | char testchar1, testchar2; 7 | 8 | int fact(int t) 9 | { 10 | if(t < 3) 11 | return(t); 12 | else 13 | return(fact(t-1)*t); 14 | } 15 | 16 | int rt15() 17 | { 18 | const int zero=1; 19 | return(3*5/one+one-zero); 20 | } 21 | 22 | char fun2(int a, int b, int c, int d, int e, int f) 23 | { 24 | printf(a+b*c*d/e-f); 25 | return('A'); 26 | } 27 | 28 | void main() 29 | { 30 | const int b =+1, c=2, d=3, e=4; 31 | char ar[2]; 32 | int r, m1[8]; 33 | int aa, a; 34 | 35 | x = +-1; 36 | printf(x); 37 | r = -4; 38 | printf(r); 39 | aa = 6; 40 | printf("!@#$%%abcd114514ABCD963287~;:", fact(aa)); 41 | ar[d*e+6*(minus_two+b-1)] = fun2(x-r, aa, b, e, c, minus_two) ; 42 | printf((ar[zero/fact(4)])); 43 | ar[1] = '0'; 44 | aa = -b * (ar[1] + rt15()) + (c - e * 9) + 2 * 'm'; 45 | printf(aa); 46 | a = aa; 47 | scanf(a, x); 48 | if (a > 0) 49 | { 50 | printf("1 hit "); 51 | if (x <= 0) { 52 | r = a / x * a; 53 | printf(r); 54 | printf("2 hit"); 55 | } 56 | r = r + aa; 57 | printf(r); 58 | } 59 | if (x != 0) 60 | { 61 | printf("3 hit "); 62 | if (a == -4) 63 | { 64 | r = r + 1; 65 | printf(r); 66 | printf("4 hit "); 67 | } 68 | } 69 | if (a < 0) 70 | { 71 | printf("5 hit "); 72 | if (x >= 0) 73 | { 74 | r = r + a; 75 | printf(r); 76 | printf("6 hit "); 77 | } 78 | } 79 | else 80 | { 81 | r = r * -1; 82 | printf(r); 83 | } 84 | for(a = -2; a下边的部分 93 | 1 94 | 3 95 | -> 96 | 1 hit 97 | 117 98 | 3 hit 99 | -117 100 | 23 101 | 102 | -3 103 | 1 104 | -> 105 | 3 hit 106 | 5 hit 107 | -7 108 | 6 hit 109 | 21 110 | 111 | 6 112 | -1 113 | -> 114 | 1 hit 115 | -36 116 | 2 hit 117 | 85 118 | 3 hit 119 | -85 120 | 19 121 | 122 | -4 123 | 3 124 | -> 125 | 3 hit 126 | -3 127 | 4 hit 128 | 5 hit 129 | -7 130 | 6 hit 131 | 23 -------------------------------------------------------------------------------- /test/test/czy.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aptx1231/BUAA_Compiler/f95305b33dc0895ed672d73ddc97fad669f8794b/test/test/czy.txt -------------------------------------------------------------------------------- /test/test/fc.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aptx1231/BUAA_Compiler/f95305b33dc0895ed672d73ddc97fad669f8794b/test/test/fc.txt -------------------------------------------------------------------------------- /test/test/mee/testfile1.txt: -------------------------------------------------------------------------------- 1 | const char b_ = '-', c1 = '*', d1 = '/', e2 = 'a', d5 = 'A', aqe = 'Z', a1_1 = 'A', a2__ = 'Q', _='q', __='w', ___='1'; 2 | 3 | int f1() { 4 | char d;int g[10], h[10];int e, f, c; 5 | g[1] = 10; 6 | e = 1; 7 | f = g[1]; 8 | if (e <= f) { 9 | c = 10; 10 | } 11 | return (10); 12 | } 13 | int f_(int A) { 14 | const int a = +10; int b, c; char d;int g[10], h[10];int e, f, i; 15 | for (i = a - b; i<=(a+b); i=i-2) { 16 | printf("aaa"); 17 | } 18 | return (A); 19 | } 20 | int ff(char c) { 21 | int a; 22 | f_(a); 23 | do { 24 | printf(a/10); 25 | } while (a) 26 | return (c/'1'); 27 | } 28 | int F11(int A, char b) { 29 | const int a = +10; 30 | return (A+b); 31 | } 32 | int _ff(char a, int b) { 33 | scanf(a); 34 | F11(b, a); 35 | return (a/b); 36 | } 37 | 38 | char _11() { 39 | const int _a = +10; 40 | return ('+'); 41 | } 42 | char f__1(int a) { 43 | const char aA_ = '+'; 44 | return ('a'); 45 | } 46 | char f1__(char a) { 47 | const int _B = -102, c_ = +110; 48 | return (a); 49 | } 50 | char F2211(int a, char b) { 51 | const int AD = +10; int BB, c_c; char dc1; int ee; 52 | return ('1'); 53 | } 54 | char f12(char a, int b) { 55 | const char b_ = '-', c1 = '*', d1 = '/', e2 = 'a', d5 = 'A', aqe = 'Z', a1_1 = 'A', a2__ = 'Q', _='q', __='w'; 56 | return (a); 57 | } 58 | 59 | void g11() { 60 | return; 61 | } 62 | void G11(int a) { 63 | return; 64 | } 65 | void f123(char a) { 66 | G11(10); 67 | } 68 | void f_1_2(int a, char b) { 69 | 70 | } 71 | void g_2_1(char a, int b) { 72 | return; 73 | } 74 | 75 | void g_2_2(int a, int b) { 76 | return; 77 | } 78 | 79 | void main() { 80 | const int a = +10; 81 | g11(); 82 | f123('q'); 83 | g_2_2(a, a); 84 | } 85 | -------------------------------------------------------------------------------- /test/test/mee/testfile2.txt: -------------------------------------------------------------------------------- 1 | int e, f; 2 | 3 | void f1() { 4 | int a; 5 | int A[10]; 6 | return; 7 | } 8 | void f2(int a) { 9 | int A[10]; 10 | return; 11 | } 12 | void f3(char a) { 13 | int b, c[10], d[10]; 14 | } 15 | void f4(int a, char b) { 16 | int g[10], h[10];int e, f; 17 | } 18 | void f5(char a, int b) { 19 | int e, f; 20 | return; 21 | } 22 | 23 | void _1() { 24 | char a; 25 | char A[10]; 26 | return; 27 | } 28 | void _2(int a) { 29 | char A[10]; 30 | return; 31 | } 32 | void _3(char a) { 33 | char b, c[10], d[10]; 34 | } 35 | void _4(int a, char b) { 36 | char g[10], h[10];int e, f; 37 | } 38 | void _5(char a, int b) { 39 | char e, f; 40 | return; 41 | } 42 | 43 | void main() { 44 | const int a = +10; const int b = -10, c = 10; char d; int g[10], h[10];int e, f[100], gg, i; 45 | f[8] = (a+b); 46 | f[a-c] = a-c; 47 | e = 1000; 48 | e = -1000; 49 | e = 10+10; 50 | e = 10-10; 51 | if (a == b) { 52 | if (c == e) { 53 | printf(gg); 54 | } 55 | } 56 | if (a != b) { 57 | gg = -100; 58 | } 59 | if (a >= b) { 60 | gg = 10; 61 | } 62 | if (a <= b) { 63 | gg = 10; 64 | } 65 | if (a > b) { 66 | g[1] = c*b; 67 | } 68 | if (a < b) { 69 | g[1] = f[1]/10; 70 | } 71 | if (a) { 72 | g[a] = 1; 73 | } 74 | if (a-b) { 75 | f[a] = b; 76 | } 77 | while (a == b) { 78 | scanf(gg); 79 | scanf(gg, e); 80 | if (a-b) { 81 | f[a] = b; 82 | } 83 | printf(a); 84 | } 85 | while (a == b) 86 | ; 87 | 88 | do { 89 | printf(a/10); 90 | } while (a) 91 | do 92 | printf(a/10); 93 | while (a) 94 | do 95 | ; 96 | while (a) 97 | for (i = a - b; i<=(a+b); i=i+2) { 98 | printf("abc", i); 99 | } 100 | for (i = a - b; i<=(a+b); i=i-2) { 101 | printf("aaa"); 102 | } 103 | for (i = a - b; i<=(a+b); i=i-2) 104 | printf("aaa"); 105 | } 106 | -------------------------------------------------------------------------------- /test/test/mee/testfile3.txt: -------------------------------------------------------------------------------- 1 | const int a1_1 = +10; 2 | const int b2 = -10, cc = 10; 3 | const char aA = '+'; 4 | const char AAb_ = '-', cQ1 = '*', d1Q = '/', e2 = 'a', d5 = 'A', aqe = 'Z', a2__ = 'Q', _='q', __='w'; 5 | const char a = 'a', b = 'b', c= 'c', d='d', e='e',f='f',g='g',h='h',i='i',j='j',k='k',l='l',m='m',n='n',o='o',p='p',q='q',r='r',s='s',t='t',u='u',v='v',w='w',x='x',y='y',z='z'; 6 | const char A = 'A', B = 'B', C= 'C', D='D', E='E',F='F',G='G',H='H',I='I',J='J',K='K',L='L',M='M',N='N',O='O',P='P',Q='Q',R='R',S='S',T='T',U='U',V='V',W='W',X='X',Y='Y',Z='Z'; 7 | const int a____A = +0, _b = -0, c_ = 0; 8 | 9 | char g__[10], h__[10];char e__, f__; 10 | 11 | int f_d() { 12 | return (10); 13 | } 14 | int f1(int a) { 15 | return (a); 16 | } 17 | int f12(char a) { 18 | return (a/10); 19 | } 20 | int f1_(int a, char b) { 21 | return (a+b); 22 | } 23 | int _f1(char a, int b) { 24 | return (b); 25 | } 26 | int fff(int a,int b) { 27 | return (a); 28 | } 29 | 30 | void main() { 31 | int a, d, f, e; 32 | int c[100],g[1000]; 33 | int b; 34 | if (a == b) { 35 | if (f == e) { 36 | fff(a,b); 37 | } 38 | } 39 | else 40 | f = e; 41 | if (a != b) { 42 | a = -100; 43 | } 44 | else { 45 | 46 | } 47 | if (a != b) 48 | a = -100; 49 | if (a >= b) { 50 | b = 10; 51 | } 52 | else { 53 | b = 1; 54 | a = 2; 55 | e = 3; 56 | } 57 | if (a <= b) { 58 | c[1] = 10; 59 | } 60 | else { 61 | ; 62 | } 63 | if (a > b) { 64 | g[1] = c[1]-b; 65 | } 66 | else ; 67 | 68 | if (a > b) ; 69 | else ; 70 | if (a >= b) ; 71 | if (a < b) { 72 | g[1] = f; 73 | } 74 | else { 75 | if (a > b) 76 | a = b; 77 | else 78 | a = -b; 79 | } 80 | if (a) { 81 | g[a] = 1; 82 | ; 83 | } 84 | else { 85 | a = 10; 86 | } 87 | if (a-b) { 88 | f = b; 89 | } 90 | else { 91 | ; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /test/test/mee/testfile4.txt: -------------------------------------------------------------------------------- 1 | void main() { 2 | 3 | } -------------------------------------------------------------------------------- /test/test/mee/testfile5.txt: -------------------------------------------------------------------------------- 1 | int f() { 2 | return (1); 3 | } 4 | 5 | void main() { 6 | int a,b,c,d,e,g; 7 | int A[100]; 8 | a = +b; 9 | a = -A[1]; 10 | a = (+b); 11 | a = +10; 12 | a = f(); 13 | 14 | a = +a * a; 15 | a = -a * A[a]; 16 | a = a * (+a * a); 17 | a = +a / 10; 18 | a = a / f(); 19 | 20 | a = -A[e] * a; 21 | a = +A[a+e] * A[a-e]; 22 | a = A[b] * (b*b); 23 | a = -A[1] / 10; 24 | a = A[a] / f(); 25 | 26 | a = -f() / a; 27 | a = -f() / A[e+a]; 28 | a = +f() / (a/e); 29 | a = f() * 10; 30 | a = f() * f(); 31 | } 32 | -------------------------------------------------------------------------------- /test/test/mee/testfile6.txt: -------------------------------------------------------------------------------- 1 | const char b_ = '-', c1 = '*', d1 = '/', e2 = 'a', d5 = 'A', aqe = 'Z', a1_1 = 'A', a2__ = 'Q', _='q', __='w', ___='1'; 2 | 3 | void main() { 4 | const int a = +10; 5 | } 6 | -------------------------------------------------------------------------------- /test/test/mzy.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aptx1231/BUAA_Compiler/f95305b33dc0895ed672d73ddc97fad669f8794b/test/test/mzy.txt -------------------------------------------------------------------------------- /test/test/old16231246.txt: -------------------------------------------------------------------------------- 1 | const int zero = 0; 2 | const int one = 1, minus_two = -0002; 3 | const char renn = '0'; 4 | const char first_alpha = 'a', first_captical = 'A'; 5 | int x, yy, zz, yyy[7]; 6 | 7 | int fact(int t) 8 | { 9 | if(t < 3) 10 | return(t); 11 | else 12 | return(fact(t-1)*t); 13 | } 14 | 15 | int rt15 16 | { 17 | const int zero=1; 18 | return(3*5/ONE+one-ZERO); 19 | } 20 | 21 | char fun2(int a, int b, int c, int d, int e, int f) 22 | { 23 | printf(a+b*c*d/e-f); 24 | return('A'); 25 | } 26 | 27 | void main() 28 | { 29 | const int b =+01, c=2, d=3, e=4; 30 | char ar[2]; 31 | int r, m1[8]; 32 | int aa, a; 33 | 34 | x = +-1; 35 | r = -4; 36 | aa = 6; 37 | printf("!@#$%%abcd114514ABCD963287~;: ", fact(AA)); 38 | AR[d*e+6*(minus_two+b-1)] = fun2(x-r, aa, b, e, c, minus_two) ; 39 | printf((ar[ZeRo/fact(4)])); 40 | ar[1] = '0'; 41 | aa = -b * (ar[1] + rt15) + (c - e * 9) + 2 * 'm'; 42 | printf(aa); 43 | a = aa; 44 | scanf(a, x); 45 | if (a > 0) 46 | { 47 | printf("1 hit "); 48 | if (x <= 0) { 49 | r = a / x * a; 50 | printf("2 hit "); 51 | } 52 | r = r + aa; 53 | } 54 | if (x != 0) 55 | { 56 | printf("3 hit "); 57 | if (a == -4) 58 | { 59 | r = r + 1; 60 | printf("4 hit "); 61 | } 62 | } 63 | if (a < 0) 64 | { 65 | printf("5 hit "); 66 | if (x >= 0) 67 | { 68 | r = r + a; 69 | printf("6 hit "); 70 | } 71 | } 72 | else 73 | { 74 | r = r * -1; 75 | printf(r); 76 | } 77 | for(a = -2; a 0) 82 | { 83 | x = x - c; 84 | } 85 | printf(x); 86 | } 87 | 88 | 先输出 89 | !@#$%abcd114514ABCD963287~;: 90 | 720 91 | 17 92 | 65 93 | 121 94 | 95 | 之后根据输入,可得相应输出为->下边的部分 96 | 1 97 | 3 98 | -> 99 | 1 hit 100 | 3 hit 101 | -117 102 | -1 103 | 104 | -3 105 | 1 106 | -> 107 | 3 hit 108 | 5 hit 109 | 6 hit 110 | -1 111 | 112 | 6 113 | -1 114 | -> 115 | 1 hit 116 | 2 hit 117 | 3 hit 118 | -85 119 | -1 120 | 121 | -4 122 | 3 123 | -> 124 | 3 hit 125 | 4 hit 126 | 5 hit 127 | 6 hit 128 | -1 -------------------------------------------------------------------------------- /test/test/qys.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aptx1231/BUAA_Compiler/f95305b33dc0895ed672d73ddc97fad669f8794b/test/test/qys.txt -------------------------------------------------------------------------------- /test/test/testFibnaci.txt: -------------------------------------------------------------------------------- 1 | int Fibnaci(int n){ 2 | if(n==1) 3 | return (1); 4 | else{ if(n==2){ 5 | return (1); 6 | }else { 7 | return (Fibnaci(n-1)+Fibnaci(n-2)); 8 | } 9 | } 10 | } 11 | void main(){ 12 | int a; a = 6; printf(a*a*a*a); for(a=1;a<9;a=a+1){ printf(Fibnaci(a)); } } 13 | } -------------------------------------------------------------------------------- /test/test/testarray.txt: -------------------------------------------------------------------------------- 1 | const int max = 10; 2 | int array1[100]; 3 | char array2[100]; 4 | void printglobal() { 5 | int i; 6 | printf("global array"); 7 | for (i = 0;i < max;i = i + 1) { 8 | printf(array1[i]); 9 | printf(array2[i]); 10 | } 11 | } 12 | void initglobal() { 13 | int i; 14 | for (i = 0;i < max;i = i + 1) { 15 | array1[i] = 200 - i; 16 | array2[i] = 'c'; 17 | } 18 | } 19 | void printlocallocal() { 20 | int i; 21 | int array1[100]; 22 | char array2[100]; 23 | for (i = 0;i < max;i = i + 1) { 24 | array1[i] = array1[i] + i * i; 25 | array2[i] = 'b'; 26 | } 27 | printf("locallocal array"); 28 | for (i = 0;i < max;i = i + 1) { 29 | printf(array1[i]); 30 | printf(array2[i]); 31 | } 32 | } 33 | void main() { 34 | int i; 35 | int array1[100]; 36 | char array2[100]; 37 | for (i = 0;i < max;i = i + 1) { 38 | array1[i] = i; 39 | array2[i] = 'a'; 40 | } 41 | initglobal() 42 | printglobal(); 43 | printf("local array"); 44 | printlocallocal(); 45 | for (i = 0;i < max;i = i + 1) { 46 | printf(array1[i]); 47 | printf(array2[i]); 48 | } 49 | printlocallocal(); 50 | } -------------------------------------------------------------------------------- /test/test/testbigparatable.txt: -------------------------------------------------------------------------------- 1 | int big(int a,int b,int c,int d, int e,int f, int g,int h,int i,int j,int k,int l) { 2 | printf(a); 3 | printf(b); 4 | printf(c); 5 | printf(d); 6 | printf(e); 7 | printf(f); 8 | printf(g); 9 | printf(h); 10 | printf(i); 11 | printf(j); 12 | printf(k); 13 | printf(l); 14 | return (11); 15 | } 16 | void main() { 17 | big(1,2,3,4,5,6,7,8,9,10,11,12); 18 | } -------------------------------------------------------------------------------- /test/test/testclang.txt: -------------------------------------------------------------------------------- 1 | int g_val; 2 | 3 | int add_g_val(int i){ 4 | g_val = g_val+i ; 5 | return (g_val) ; 6 | } 7 | 8 | void swap(int x, int y){ 9 | int temp; 10 | 11 | printf("x = ", x) ; 12 | printf("y = ", y) ; 13 | temp = x; 14 | x=y; 15 | y=temp; 16 | printf("SWAP x = ", x) ; 17 | printf("SWAP y = ", y) ; 18 | } 19 | 20 | void main() 21 | { 22 | g_val = 5 ; 23 | swap(g_val, add_g_val(g_val) ) ; 24 | } -------------------------------------------------------------------------------- /test/test/testcompute.txt: -------------------------------------------------------------------------------- 1 | const int a1 = 1, a2 = 2; 2 | const char c1 = 'c', c2 = 'd'; 3 | int a3; 4 | char c3; 5 | int ca1[100]; 6 | void main() { 7 | int a; 8 | int b; 9 | int c; 10 | int d; 11 | a = a1; 12 | printf("1 = ",a); 13 | a = a2; 14 | printf("2 = ",a); 15 | b = -a2; 16 | printf("-2 = ",b); 17 | } -------------------------------------------------------------------------------- /test/test/testdeeppara.txt: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return (a + b); 3 | } 4 | int sub(int a, int b) { 5 | return (a - b); 6 | } 7 | int sum(int a, int b, int c) { 8 | return (a + b + c); 9 | } 10 | int get10() { 11 | return (10); 12 | } 13 | void main(){ 14 | int a; 15 | int b; 16 | a = 4; 17 | b = 3; 18 | printf("6=",add(1+add(1,2),2)); 19 | printf("7=",add(a,sub(a,sub(a,b)))); 20 | printf("6=",add(sub(a,sub(a,b)),sub(a,sub(a,b)))); 21 | printf("15=",sum(add(a,a),sub(a,b),sum(1,2,3))); 22 | printf("20=",add(get10(),get10())); 23 | } -------------------------------------------------------------------------------- /test/test/testfile1.txt: -------------------------------------------------------------------------------- 1 | const int z = -1234567890; 2 | int d1; 3 | void f1() 4 | { 5 | const int c1 = 1; 6 | int i1,r1; 7 | r1 = 0; 8 | for (i1 = 0;i1 < 10;i1 = i1 + 1) { 9 | r1 = r1 + c1; 10 | } 11 | printf(r1); 12 | } 13 | int f2(int n2) 14 | { 15 | int i2,r2; 16 | i2 = 0; 17 | r2 = 0; 18 | while (i2 != n2) { 19 | r2 = r2 + i2; 20 | i2 = i2 + 1; 21 | } 22 | while (i2 <= 2 * n2) i2 = i2 + 1; 23 | return (r2); 24 | } 25 | int f3(int n3,int n4) { 26 | int i3; 27 | char c3; 28 | int r3; 29 | char c4; 30 | r3 = 0; 31 | c3 = 'f'; 32 | i3 = n3; 33 | c4 = '3'; 34 | do { 35 | r3 = r3 + i3; 36 | i3 = i3 + 1; 37 | } while (i3 <= n4) 38 | do i3 = i3 - 1; 39 | while (i3 > 0) 40 | return (r3 + 1); 41 | } 42 | void f4() { 43 | int i4,r; 44 | i4 = 10; 45 | r = 0; 46 | for (i4 = 10;i4 > 0;i4 = i4 - 1) { 47 | r = r - i4; 48 | } 49 | printf(r); 50 | } 51 | void main () { 52 | int b2,b3; 53 | f1(); 54 | b2 = f2(5); 55 | printf(b2); 56 | b3 = b2 + f3(1,4); 57 | printf(b3); 58 | } 59 | -------------------------------------------------------------------------------- /test/test/testfile2.txt: -------------------------------------------------------------------------------- 1 | const int n1 = 0,n2 = +0,n3 = -0,n4 = 12340,n5 = +5678,n6 = -90; 2 | const char _1 = '+',c1 = '-',c2 = '*',c3 = '/',c4 = 'a',c5 = 'A',c6 = '0',c7 = '9',c9 = '_'; 3 | int r1,r2; 4 | char r3,r4,r5; 5 | void f1(char c8) 6 | { 7 | printf(n1); 8 | printf(n2); 9 | printf(n3); 10 | printf(n4); 11 | printf(n5); 12 | printf(n6); 13 | printf(c1); 14 | printf(c2); 15 | printf(c3); 16 | printf(c4); 17 | printf(c5); 18 | printf(c6); 19 | printf(c7); 20 | printf(c8); 21 | printf(c9); 22 | printf(" !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); 23 | printf("string",_1); 24 | } 25 | void main() 26 | { 27 | f1('a'); 28 | scanf(r1,r2,r3,r4); 29 | printf(r1); 30 | scanf(r5); 31 | printf(r2); 32 | printf(r3); 33 | printf(r4); 34 | printf(r5); 35 | } 36 | -------------------------------------------------------------------------------- /test/test/testfile3.txt: -------------------------------------------------------------------------------- 1 | const char t0 = 'T'; 2 | const char f0 = 'F'; 3 | void main() 4 | { 5 | const int t = 1,f = 0; 6 | int a1,a2; 7 | a1 = 5; 8 | a2 = 10; 9 | if (t) printf("True"); 10 | else printf("False"); 11 | if (f) printf(t0); 12 | else printf(f0); 13 | if (a1 >= 4) printf("True"); 14 | if (a2 != 11) printf("True"); 15 | if (a1 == a2) { 16 | printf("True"); 17 | printf("block"); 18 | } else { 19 | printf("False"); 20 | printf("block"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /test/test/testfile4.txt: -------------------------------------------------------------------------------- 1 | int f1(int n2) 2 | { 3 | int i2,r2; 4 | i2 = 0; 5 | r2 = 0; 6 | while (i2 != n2) { 7 | r2 = r2 + i2; 8 | i2 = i2 + 1; 9 | } 10 | return (r2); 11 | } 12 | int f2 (int n3) 13 | { 14 | return ((2 * (-n3))); 15 | } 16 | char f3() 17 | { 18 | return ('A'); 19 | } 20 | void main() 21 | { 22 | int a1,a2,a3,a4; 23 | int b1,b2,b3,b4; 24 | int c1,c2,c3,c4; 25 | int a[10]; 26 | char d1; 27 | a1 = +1; 28 | a2 = -1; 29 | a3 = +a1 * a2; 30 | a4 = -a3 * a2; 31 | d1 = '+'; 32 | a[0] = (+a1 * a2) + a4; 33 | b1 = -a1 + a2; 34 | b2 = -a2 / a2 - a4; 35 | b3 = -a1 + f1(a1 + a1); 36 | b4 = -a1 * (-a2); 37 | c1 = (-a1) + (-f1(a1)); 38 | a[ a1 + a1] = d1 + a1; 39 | a[3] = f2(4); 40 | a[4] = 1 * a[3] + 1; 41 | printf(f3()); 42 | printf(f1((+a1)) + f1(2)); 43 | } 44 | -------------------------------------------------------------------------------- /test/test/testfile6.txt: -------------------------------------------------------------------------------- 1 | const int a = 123; 2 | const int fuck = 2147483647, your = -65536, mother = +19260817; 3 | const char c = 'h', d = '+'; 4 | const char cc = '*'; 5 | 6 | int kkk, jjj; 7 | char moha, hath; 8 | int arr[100], aaa, hhh[5]; 9 | 10 | int calc(int x, int y, int z) { 11 | return ((x + y) * (y - z)); 12 | } 13 | 14 | char next_char(char c) { 15 | return (c); 16 | } 17 | 18 | void hello_world() { 19 | printf("Hello World!"); 20 | } 21 | 22 | int fact(int n) { 23 | if (n <= 1) return (1); 24 | else return (n * fact(n - 1)); 25 | } 26 | 27 | char get_hath() { 28 | scanf(moha); 29 | return (moha); 30 | } 31 | 32 | void arrange(int n, int i) 33 | { 34 | const int ONE = 1; 35 | int j, tmp; 36 | if (i >= n - 1) 37 | { 38 | for (j = 0; j < n; j = j + 1) printf(" ", arr[j]); 39 | printf("\n"); 40 | return; 41 | } 42 | else { 43 | arrange(n, i + 1); 44 | for (j = i + ONE; j < n; j = j + 1) if (arr[i] != arr[j]) 45 | { 46 | tmp = arr[i]; 47 | arr[i] = arr[j]; 48 | arr[j] = tmp; 49 | arrange(n, i + 1); 50 | } 51 | for (j = i + 1; n > j; j = j + 1) { 52 | tmp = arr[i]; 53 | arr[i] = arr[j]; 54 | arr[j] = tmp; 55 | } 56 | } 57 | } 58 | 59 | void main() { 60 | int i; 61 | hello_world(); 62 | hath = get_hath(); 63 | printf(hath * a / (hath + next_char(cc))); 64 | scanf(kkk, jjj, aaa); 65 | printf( calc(kkk, jjj - get_hath(), 3) ); 66 | scanf(aaa); 67 | for (i = 0; i < 100; i = i + 1) { 68 | arr[i] = i + 1; 69 | } 70 | while (aaa >= 0) { 71 | arrange(aaa, 0); 72 | if (aaa == 12) printf(fact(aaa)); 73 | else printf("mmp ", d); 74 | aaa = aaa - 1; 75 | } 76 | scanf(aaa); 77 | do { 78 | printf("fuck: ", fuck); 79 | printf("your: ", your); 80 | printf("mother: ", mother); 81 | if (aaa != 20000) printf("hehe", aaa); 82 | aaa = aaa + '0'; 83 | } while (aaa < 10000); 84 | } 85 | -------------------------------------------------------------------------------- /test/test/testfile7.txt: -------------------------------------------------------------------------------- 1 | const int a = 123; 2 | const int fuck = 2147483647, your = -65536, mother = +19260817; 3 | const char c = 'h', d = '+'; 4 | const char cc = '*'; 5 | 6 | int calc(int x, int y, int z) { 7 | return ((x + y) * (y - z)); 8 | } 9 | 10 | char next_char(char c) { 11 | return (c); 12 | } 13 | 14 | int fact(int n) { 15 | if (n <= 1) return (1); 16 | else return (n * fact(n - 1)); 17 | } 18 | 19 | char get_hath() { 20 | char moha; 21 | scanf(moha); 22 | return (moha); 23 | } 24 | 25 | void main() { 26 | char hath; 27 | int aaa, kkk, jjj; 28 | hath = get_hath(); 29 | printf(hath * a / (hath + next_char(cc))); 30 | scanf(kkk, jjj, aaa); 31 | printf( calc(kkk, jjj - get_hath(), 3) ); 32 | scanf(aaa); 33 | while (aaa >= 0) { 34 | if (aaa == 12) printf(fact(aaa)); 35 | else printf("mmp ", aaa); 36 | aaa = aaa - 1; 37 | } 38 | scanf(aaa); 39 | do { 40 | printf("fuck: ", fuck); 41 | printf("your: ", your); 42 | printf("mother: ", mother); 43 | if (aaa != 20000) printf("hehe", aaa); 44 | aaa = aaa + '0'; 45 | } while (aaa < 10000); 46 | } 47 | -------------------------------------------------------------------------------- /test/test/testfile8.txt: -------------------------------------------------------------------------------- 1 | int kkk, jjj; 2 | char moha, hath; 3 | int arr[100], aaa; 4 | 5 | void hello_world() { 6 | const int bbb = 2147483647; 7 | printf("Hello World! ", bbb / 2); 8 | } 9 | 10 | void arrange(int n, int i) 11 | { 12 | const int ONE = 1; 13 | int j, tmp; 14 | if (i >= n - 1) 15 | { 16 | for (j = 0; j < n; j = j + 1) printf(" ", arr[j]); 17 | printf("\n"); 18 | return; 19 | } 20 | else { 21 | arrange(n, i + 1); 22 | for (j = i + ONE; j < n; j = j + 1) if (arr[i] != arr[j]) 23 | { 24 | tmp = arr[i]; 25 | arr[i] = arr[j]; 26 | arr[j] = tmp; 27 | arrange(n, i + 1); 28 | } 29 | for (j = i + 1; n > j; j = j + 1) { 30 | tmp = arr[i]; 31 | arr[i] = arr[j]; 32 | arr[j] = tmp; 33 | } 34 | } 35 | } 36 | 37 | void main() { 38 | const int a = 123, cc = -11; 39 | int i; 40 | hello_world(); 41 | scanf(hath); 42 | moha = hath; 43 | printf(hath * a / (moha + cc)); 44 | scanf(kkk, jjj, aaa); 45 | printf(hath); 46 | scanf(aaa); 47 | for (i = 0; i < 100; i = i + 1) { 48 | arr[i] = i + 1; 49 | } 50 | while (aaa >= 0) { 51 | arrange(aaa, 0); 52 | if (aaa == 12) printf(aaa); 53 | else printf("mmp ", aaa); 54 | aaa = aaa - 1; 55 | } 56 | scanf(aaa); 57 | do { 58 | printf("fuck: "); 59 | printf("your: "); 60 | printf("mother: "); 61 | if (aaa != 20000) printf("hehe", aaa); 62 | aaa = aaa + '0'; 63 | } while (aaa < 10000); 64 | } 65 | -------------------------------------------------------------------------------- /test/test/testfile9.txt: -------------------------------------------------------------------------------- 1 | void main() { 2 | const int kkk = 19260817; 3 | const int jjj = -19260817; 4 | char str[20]; 5 | char c; 6 | int a, b; 7 | scanf(a, b); 8 | printf("hello world!", -a + b); 9 | printf(a - b); 10 | printf(a * b); 11 | for (a = kkk; a > 0; a = a - 10086) { 12 | if (b != 0) { 13 | printf("good! ", a / b); 14 | } else printf("fuck! ", + b / a); 15 | } 16 | for (a = 19; a + 1; a = a - 1) { 17 | str[a] = 'G'; 18 | printf(str[a]); 19 | printf(" and ", jjj); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /test/test/testfunction.txt: -------------------------------------------------------------------------------- 1 | int minus1(int n) { 2 | n = n - 1; 3 | printf("minus", n); 4 | if (n) { 5 | return (divide2(n)); 6 | } 7 | return (n); 8 | } 9 | int divide2(int n) { 10 | n = n / 2; 11 | printf("divide",n); 12 | if (n) { 13 | return (minus1(n)); 14 | } 15 | return(n); 16 | } 17 | void main() { 18 | minus1(1000); 19 | } -------------------------------------------------------------------------------- /test/test/testglobalandlocal.txt: -------------------------------------------------------------------------------- 1 | const int a = 0; 2 | void printer() { 3 | printf(a); 4 | } 5 | void main() { 6 | const int a = 1; 7 | printf(a); 8 | printer(); 9 | } -------------------------------------------------------------------------------- /test/test/testio.txt: -------------------------------------------------------------------------------- 1 | const int a1 = 1, a2 = 2; 2 | const char c1 = 'c', c2 = 'd'; 3 | int a3; 4 | char c3; 5 | int ca1[100]; 6 | int getint() { 7 | return (3); 8 | } 9 | char getchar() { 10 | return ('e'); 11 | } 12 | void main() { 13 | printf("ha\\l\0lo\n"); 14 | printf(1); 15 | printf("hallo"); 16 | printf(1); 17 | printf("hello world\n"); 18 | printf(1706); 19 | printf("1 = ",a1); 20 | printf("'c' = ",c1); 21 | printf("3 = ",getint()); 22 | printf("'e' = ", getchar()); 23 | printf(ca1[0]); 24 | printf(ca1[getint()]); 25 | printf("input int a3, a3:"); 26 | scanf(a3,a3); 27 | printf("a3 = ",a3); 28 | printf("input char c3, c3:"); 29 | scanf(c3,c3); 30 | printf("c3 = ",c3); 31 | printf("ca[1] = 3"); 32 | ca1[1] = getint(); 33 | printf(ca1[1]); 34 | } -------------------------------------------------------------------------------- /test/test/testloop.txt: -------------------------------------------------------------------------------- 1 | void main() { 2 | int i; 3 | i = 100; 4 | while(i) { 5 | printf("while",i); 6 | i = i - 1; 7 | } 8 | for (i = 0;i * i < 500;i = i + 1) { 9 | printf("for",i * i); 10 | } 11 | do { 12 | printf("do",i); 13 | i = i / 2; 14 | } 15 | while(i>0); 16 | } -------------------------------------------------------------------------------- /test/test/testmergesort.txt: -------------------------------------------------------------------------------- 1 | int arr[100], tmp[100]; 2 | 3 | void merge(int s1, int l1, int s2, int l2) { 4 | int i, j, k; 5 | i = 0; 6 | j = 0; 7 | k = s1; 8 | while (i < l1) { 9 | if (j < l2) { 10 | if (arr[s1 + i] > arr[s2 + j]) { 11 | tmp[k] = arr[s2 + j]; 12 | j = j + 1; 13 | } else { 14 | tmp[k] = arr[s1 + i]; 15 | i = i + 1; 16 | } 17 | k = k + 1; 18 | } else { 19 | while (i < l1) { 20 | tmp[k] = arr[s1 + i]; 21 | k = k + 1; 22 | i = i + 1; 23 | } 24 | } 25 | } 26 | while (j < l2) { 27 | tmp[k] = arr[s2 + j]; 28 | k = k + 1; 29 | j = j + 1; 30 | } 31 | for (i = s1; i < k; i = i + 1) arr[i] = tmp[i]; 32 | } 33 | 34 | void inner_merge_sort(int start, int length) { 35 | int tmp, mid; 36 | if (length <= 1) return; 37 | if (length == 2) { 38 | if (arr[start] > arr[start + 1]) { 39 | tmp = arr[start]; 40 | arr[start] = arr[start + 1]; 41 | arr[start + 1] = tmp; 42 | } 43 | return; 44 | } 45 | mid = length / 2; 46 | inner_merge_sort(start, mid); 47 | inner_merge_sort(start + mid, length - mid); 48 | merge(start, mid, start + mid, length - mid); 49 | } 50 | 51 | void main() { 52 | int n, i, a; 53 | printf("17373248"); 54 | scanf(n); 55 | for (i = 0; i < n; i = i + 1) { 56 | scanf(a); 57 | arr[i] = a; 58 | } 59 | inner_merge_sort(0, n); 60 | for (i = 0; i < n; i = i + 1) printf(arr[i]); 61 | } -------------------------------------------------------------------------------- /test/test/testprime.txt: -------------------------------------------------------------------------------- 1 | int primes[10000]; 2 | int hasfactor(int n, int maxfactor) { 3 | int i; 4 | for (i = 2;i <= maxfactor; i = i + 1) { 5 | if (n == n / i * i) { 6 | return (0); 7 | } 8 | } 9 | return (1); 10 | } 11 | void main() { 12 | int i; 13 | int j; 14 | i = 0; 15 | j = 0; 16 | for (j = 0;j < 200;i = i + 1) { 17 | if (hasfactor(i, i - 1)) { 18 | primes[j] = i; 19 | printf("j",j); 20 | printf("i",i); 21 | j = j + 1; 22 | } 23 | } 24 | for (j = 100; j >= 0;j = j - 1) { 25 | printf(primes[j]); 26 | } 27 | } -------------------------------------------------------------------------------- /test/test/testpublic3.txt: -------------------------------------------------------------------------------- 1 | const char multi='+'; 2 | const int one=1; 3 | const int two=2; 4 | const int three=3; 5 | const int four=4; 6 | const int five=5; 7 | const int One=10; 8 | const int Two=20; 9 | const int Three=30; 10 | const int Four=40; 11 | const int Five=50; 12 | int small_array[10]; 13 | int large_array[100]; 14 | char mul; 15 | void pro_start() 16 | { 17 | int i; 18 | i=0; 19 | do 20 | { 21 | small_array[i]=Four; 22 | i=i+1; 23 | } 24 | while(i!=One) 25 | small_array[10]='a'; 26 | for(i=0;i<11;i=i+1) 27 | { 28 | printf(small_array[i]); 29 | printf("\n"); 30 | } 31 | printf("prepare finished.\n"); 32 | } 33 | int init_array(int number) 34 | { 35 | if(number!=5) 36 | { 37 | number=number+1; 38 | return(init_array(number)); 39 | } 40 | else 41 | { 42 | return(number); 43 | } 44 | } 45 | void save_in_array(char ch) 46 | { 47 | int i,j,location; 48 | for(i=0; 49 | i<5; 50 | i=i+1) 51 | {; 52 | for(j=0;j!=5;j=j+1) 53 | { 54 | large_array[i+j]=ch; 55 | } 56 | } 57 | for(i=10;i<51;i=i+1) 58 | { 59 | large_array[i]=ch; 60 | ch=ch+1; 61 | } 62 | printf(large_array[10]); 63 | printf("\n"); 64 | printf(large_array[50]); 65 | printf("\n"); 66 | } 67 | void main() 68 | { 69 | const char minus='-'; 70 | char ch; 71 | int number; 72 | int result; 73 | mul=multi; 74 | ch='a'; 75 | if(mul=='+'){} 76 | else 77 | { 78 | return 0; 79 | } 80 | pro_start(); 81 | printf("PROGRAM START!!!\n"); 82 | printf("your lucky number is:",one==One); 83 | printf("\n"); 84 | printf("choose your branch(from number 1 to 2):\n"); 85 | scanf(number); 86 | if(number!=0) 87 | { 88 | result=init_array(number); 89 | } 90 | result=+1-1+1-1*(19999+29999-10000*(result/result*(two-1*(Two-19))))+result; 91 | ch=ch+one; 92 | save_in_array(ch); 93 | printf(result); 94 | return 0; 95 | } -------------------------------------------------------------------------------- /test/test/testsmallfunction.txt: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return (a + b); 3 | } 4 | int sub(int a, int b) { 5 | return (a - b); 6 | } 7 | void main() { 8 | int a; 9 | int b; 10 | a = 1; 11 | b = 1; 12 | b = sub(a, b); 13 | printf("b", b); 14 | b = sub(3,add(a, a)); 15 | printf("b", b); 16 | a = add(a, b); 17 | printf("a", a); 18 | a = add(a, b); 19 | printf("a", a); 20 | a = add(a, b); 21 | printf("a", a); 22 | a = add(a, b); 23 | 24 | 25 | printf("b", b); 26 | b = add(a, b); 27 | 28 | } -------------------------------------------------------------------------------- /test/test/teststoreload.txt: -------------------------------------------------------------------------------- 1 | const int a1 = 1, a2 = 2; 2 | const char c1 = 'c', c2 = 'd'; 3 | int a3[100]; 4 | char c3; 5 | int aa1[100]; 6 | char ca1[100]; 7 | void main() { 8 | int i; 9 | int aa2[100]; 10 | for (i = 0;i < 20;i = i + 1) { 11 | a3[i] = i; 12 | aa2[i] = i; 13 | } 14 | for (i = 19;i >= 0; i = i - 2) { 15 | printf("a3:", a3[i]); 16 | printf("aa2:", aa2[i]); 17 | a3[i] = a3[i] - aa2[i]; 18 | printf(a3[i]); 19 | } 20 | for (i = 0;i < 20;i = i + 1) { 21 | a3[i] = i * i; 22 | aa2[i] = i * i * i; 23 | a3[i] = a3[i] - aa2[i]; 24 | printf(a3[i]); 25 | } 26 | } -------------------------------------------------------------------------------- /test/use-def错误.txt: -------------------------------------------------------------------------------- 1 | grammer.cpp 2654行 2 | case MINUOP: 3 | case MULTOP: 4 | case DIVOP: 5 | case GETARRAY: //mc.z << " = " << mc.x << "[" << mc.y << "] 增加 6 | case PUTARRAY: //mc.z << "[" << mc.x << "]" << " = " << mc.y 增加 7 | if (allLocalSymbolTable[funcName].find(mc.z) != allLocalSymbolTable[funcName].end() && //局部变量 8 | nameMap.find(mc.z) == nameMap.end()) { 9 | if (mc.z[0] == '#') { 10 | @@ -2708,7 +2710,6 @@ void fullNameMap(map& nameMap, vector ve, string funcNa 11 | } 12 | break; 13 | case ASSIGNOP: 14 | case GETARRAY: //mc.z << " = " << mc.x << "[" << mc.y << "] 删除 15 | if (allLocalSymbolTable[funcName].find(mc.z) != allLocalSymbolTable[funcName].end() && //局部变量 16 | nameMap.find(mc.z) == nameMap.end()) { 17 | if (mc.z[0] == '#') { 18 | @@ -2781,26 +2782,6 @@ void fullNameMap(map& nameMap, vector ve, string funcNa 19 | } 20 | } 21 | break; 22 | case PUTARRAY: //mc.z << "[" << mc.x << "]" << " = " << mc.y 删除 23 | if (allLocalSymbolTable[funcName].find(mc.z) != allLocalSymbolTable[funcName].end() && //局部变量 24 | nameMap.find(mc.z) == nameMap.end()) { 25 | if (mc.z[0] == '#') { 26 | nameMap[mc.z] = genTmp(); 27 | } 28 | else { 29 | nameMap[mc.z] = genName(); 30 | } 31 | } 32 | if (allLocalSymbolTable[funcName].find(mc.y) != allLocalSymbolTable[funcName].end() && //局部变量 33 | nameMap.find(mc.y) == nameMap.end()) { 34 | if (mc.y[0] == '#') { 35 | nameMap[mc.y] = genTmp(); 36 | } 37 | else { 38 | nameMap[mc.y] = genName(); 39 | } 40 | } 41 | break; 42 | case FUNC: 43 | case EXIT: 44 | break; 45 | 46 | 47 | 48 | optimize.cpp 49 | } 50 | break; 51 | case ASSIGNOP: 52 | case GETARRAY: 删除 53 | if (allLocalSymbolTable[funcName].find(mc.x) != allLocalSymbolTable[funcName].end() 54 | && allLocalSymbolTable[funcName][mc.x].kind == 1 && mc.x[0] != '#') { //局部变量 55 | if (use.find(mc.x) == use.end() && def.find(mc.x) == def.end()) { 56 | @@ -76,6 +75,20 @@ void calUseDef(Block& bl, string funcName) { 57 | } 58 | } 59 | break; 60 | case GETARRAY: //mc.z << " = " << mc.x << "[" << mc.y << "] 增加 61 | if (allLocalSymbolTable[funcName].find(mc.y) != allLocalSymbolTable[funcName].end() 62 | && allLocalSymbolTable[funcName][mc.y].kind == 1 && mc.y[0] != '#') { //局部变量 63 | if (use.find(mc.y) == use.end() && def.find(mc.y) == def.end()) { 64 | use.insert(mc.y); 65 | } 66 | } 67 | if (allLocalSymbolTable[funcName].find(mc.z) != allLocalSymbolTable[funcName].end() 68 | && allLocalSymbolTable[funcName][mc.z].kind == 1 && mc.z[0] != '#') { //局部变量 69 | if (use.find(mc.z) == use.end() && def.find(mc.z) == def.end()) { 70 | def.insert(mc.z); 71 | } 72 | } 73 | break; 74 | case PUSH: 75 | case RET: 76 | case INLINERET: 77 | @@ -105,17 +118,17 @@ void calUseDef(Block& bl, string funcName) { 78 | } 79 | } 80 | break; 81 | case PUTARRAY: 82 | case PUTARRAY: //mc.z << "[" << mc.x << "]" << " = " << mc.y 83 | if (allLocalSymbolTable[funcName].find(mc.y) != allLocalSymbolTable[funcName].end() 84 | && allLocalSymbolTable[funcName][mc.y].kind == 1 && mc.y[0] != '#') { //局部变量 85 | if (use.find(mc.y) == use.end() && def.find(mc.y) == def.end()) { 86 | use.insert(mc.y); 87 | } 88 | } 89 | if (allLocalSymbolTable[funcName].find(mc.z) != allLocalSymbolTable[funcName].end() 删除 90 | && allLocalSymbolTable[funcName][mc.z].kind == 1 && mc.z[0] != '#') { //局部变量 91 | if (use.find(mc.z) == use.end() && def.find(mc.z) == def.end()) { 92 | def.insert(mc.z); 93 | if (allLocalSymbolTable[funcName].find(mc.x) != allLocalSymbolTable[funcName].end() 增加 94 | && allLocalSymbolTable[funcName][mc.x].kind == 1 && mc.x[0] != '#') { //局部变量 95 | if (use.find(mc.x) == use.end() && def.find(mc.x) == def.end()) { 96 | use.insert(mc.x); 97 | } 98 | } 99 | break; -------------------------------------------------------------------------------- /test/代码生成样例.txt: -------------------------------------------------------------------------------- 1 | //把全局常量保存到栈中 2 | //需要修改 改为用一个vector顺序记录所有的全局常量 这样就可以按顺序每次sp-4了!!!!!! 3 | int maxsub = 0; 4 | for (map::iterator it = globalSymbolTable.begin(); it != globalSymbolTable.end(); it++) { 5 | symbolItem item = it->second; 6 | if (item.kind == 2) { //常量 7 | int imme; 8 | if (item.type == 1) { 9 | imme = item.constInt; 10 | } 11 | else { 12 | imme = item.constChar; 13 | } 14 | mipsCodeTable.push_back(mipsCode(li, "$t0", "", "", imme)); 15 | mipsCodeTable.push_back(mipsCode(sw, "$t0", "$sp", "", -4*item.addr)); 16 | if (4 * item.addr > maxsub) { 17 | maxsub = 4 * item.addr; 18 | } 19 | } 20 | } 21 | if (maxsub) { 22 | mipsCodeTable.push_back(mipsCode(addi, "$sp", "$sp", "", -maxsub)); 23 | } 24 | 25 | 26 | //处理函数内部的局部常量 27 | for (map::iterator it = allLocalSymbolTable[mc.x].begin(); it != allLocalSymbolTable[mc.x].end(); it++) { 28 | symbolItem item = it->second; 29 | if (item.kind == 2) { //常量 30 | int imme; 31 | if (item.type == 1) { 32 | imme = item.constInt; 33 | } 34 | else { 35 | imme = item.constChar; 36 | } 37 | mipsCodeTable.push_back(mipsCode(li, "$t0", "", "", imme)); 38 | mipsCodeTable.push_back(mipsCode(sw, "$t0", "$sp", "", -4 * item.addr)); 39 | if (4 * item.addr > maxsub) { 40 | maxsub = 4 * item.addr; 41 | } 42 | } 43 | } 44 | 45 | 46 | f[8] = (a+b); 47 | f[a-c] = a-c; 48 | e = 1000; 49 | e = -1000; 50 | e = 10+10; 51 | e = 10-10; 52 | if (a == b) { 53 | if (c == e) { 54 | printf(gg); 55 | } 56 | } 57 | if (a != b) { 58 | gg = -100; 59 | } 60 | if (a >= b) { 61 | gg = 10; 62 | } 63 | if (a <= b) { 64 | gg = 10; 65 | } 66 | if (a > b) { 67 | g[1] = c*b; 68 | } 69 | if (a < b) { 70 | g[1] = f[1]/10; 71 | } 72 | if (a) { 73 | g[a] = 1; 74 | } 75 | if (a-b) { 76 | f[a] = b; 77 | } 78 | while (a == b) { 79 | scanf(gg); 80 | scanf(gg, e); 81 | if (a-b) { 82 | f[a] = b; 83 | } 84 | printf(a); 85 | } 86 | while (a == b) 87 | ; 88 | 89 | do { 90 | printf(a/10); 91 | } while (a) 92 | do 93 | printf(a/10); 94 | while (a) 95 | do 96 | ; 97 | while (a) 98 | for (i = a - b; i<=(a+b); i=i+2) { 99 | printf("abc", i); 100 | } 101 | for (i = a - b; i<=(a+b); i=i-2) { 102 | printf("aaa"); 103 | } 104 | for (i = a - b; i<=(a+b); i=i-2) 105 | printf("aaa"); 106 | 107 | 108 | const int cons1=1, const3=3; 109 | const char cc = 'a', dd = 'A'; 110 | int e[10], f; 111 | 112 | void f1() { 113 | const int c1=10, d=100; 114 | int a; 115 | int A[10],dd; 116 | A[1] = a; 117 | return; 118 | } 119 | 120 | int f2(int a) { 121 | char A[10]; 122 | f1(); 123 | return (('a')); 124 | } 125 | 126 | int f22(int a) { 127 | char A[10]; 128 | return (a); 129 | } 130 | 131 | char f222(int a) { 132 | char A[10]; 133 | return ('a'); 134 | } 135 | 136 | void main() { 137 | const int a = +10; const int b = -10, c = 10; char d; int g[10], h[10];int e, f[100], gg, i; 138 | scanf(e); 139 | scanf(e, d); 140 | f2(a); 141 | f2(e); 142 | f2(-1); 143 | f2(('a')); 144 | printf(-10); 145 | printf("a", 'd'); 146 | printf("abc", i); 147 | printf("aaa"); 148 | printf("aaa"); 149 | printf(10); 150 | printf('a'); 151 | printf(a); 152 | printf(c); 153 | printf(d); 154 | printf(d+a); 155 | printf(e); 156 | } 157 | 158 | 159 | const int cons1=1, const3=3; 160 | const char cc = 'a', dd = 'A'; 161 | int e[10], f; 162 | 163 | void f1() { 164 | const int c1=10, d=100; 165 | int a; 166 | int A[10],dd; 167 | A[1] = a; 168 | return; 169 | } 170 | 171 | int f2(int a) { 172 | char A[10]; 173 | f1(); 174 | return (('a')); 175 | } 176 | 177 | int f22(int a) { 178 | char A[10]; 179 | a = 3; 180 | return (a); 181 | } 182 | 183 | char f222(int a) { 184 | char A[10]; 185 | return ('a'); 186 | } 187 | 188 | void main() { 189 | const int a = +10; const int b = -10, c = 10; 190 | char d; int g[10];int e, f[100], gg, i; 191 | d = '+'; 192 | g[0] = 0; 193 | g[1] = 1; 194 | g[2] = 2; 195 | g[3] = 3; 196 | g[4] = 4; 197 | g[5] = 5; 198 | g[6] = 6; 199 | g[7] = 7; 200 | g[8] = 8; 201 | g[9] = 9; 202 | e = 101; 203 | printf(d); 204 | printf(g[0]); 205 | printf(g[1]); 206 | printf(g[2]); 207 | printf(g[3]); 208 | printf(g[4]); 209 | printf(g[5]); 210 | printf(g[6]); 211 | printf(g[7]); 212 | printf(g[8]); 213 | printf(g[9]); 214 | printf(e); 215 | } 216 | 217 | int test; 218 | void main(){ 219 | int a,b,c,d; 220 | a=-1; 221 | printf(a); 222 | a=1; 223 | b=2; 224 | c=3; 225 | d=4; 226 | a = a + 1; 227 | b = 1 + 1; 228 | c = 1 + a; 229 | d = a + c; 230 | a = a + -1; 231 | b = 1 + -1; 232 | c = -1 + a; 233 | d = a + c; 234 | printf(a); 235 | printf(b); 236 | printf(c); 237 | printf(d); 238 | printf(a+b+c+d); 239 | d = a - c; 240 | c = a - -1; 241 | b = -1 - a; 242 | a = -1 - -2; 243 | printf(a); 244 | printf(b); 245 | printf(c); 246 | printf(d); 247 | printf(a+b+c+d); 248 | } 249 | 250 | 251 | const int q=1; 252 | void f() { 253 | return; 254 | } 255 | void main(){ 256 | int a,b,c,d; 257 | a = 1; 258 | b = 2; 259 | if (1==q) { 260 | printf("="); 261 | } 262 | else { 263 | printf("!="); 264 | } 265 | if (1==3) { 266 | printf("="); 267 | } 268 | else { 269 | printf("!="); 270 | } 271 | if (a==b) { 272 | printf("="); 273 | } 274 | else { 275 | printf("!="); 276 | } 277 | if (a==1) { 278 | printf("="); 279 | } 280 | else { 281 | printf("!="); 282 | } 283 | if (3==b) { 284 | printf("="); 285 | } 286 | else { 287 | printf("!="); 288 | } 289 | } 290 | 291 | const int q=1; 292 | void f() { 293 | return; 294 | } 295 | void main(){ 296 | int a,b,c,d; 297 | a = 1; 298 | b = 2; 299 | do { 300 | printf("1???"); 301 | } while (q==3); 302 | do { 303 | printf("2???"); 304 | } while (1==2); 305 | do { 306 | printf("3???"); 307 | } while (1==b); 308 | do { 309 | printf("4???"); 310 | } while (a==2); 311 | do { 312 | printf("5???"); 313 | } while (a==b); 314 | } 315 | 316 | const int q=1; 317 | void f() { 318 | return; 319 | } 320 | void main(){ 321 | int a,b,c,d; 322 | a = 1; 323 | b = 2; 324 | do { 325 | printf("1???"); 326 | } while (q!=1); 327 | do { 328 | printf("2???"); 329 | } while (1!=1); 330 | do { 331 | printf("3???"); 332 | } while (2!=b); 333 | do { 334 | printf("4???"); 335 | } while (a!=1); 336 | do { 337 | printf("5???"); 338 | } while (a!=a); 339 | } 340 | 341 | const int q=1; 342 | void f() { 343 | return; 344 | } 345 | void main(){ 346 | int a,b,c,d; 347 | a = 1; 348 | b = 2; 349 | if (1>=2) { 350 | printf("a"); 351 | } 352 | else { 353 | printf("b"); 354 | } 355 | if (a>=1) { 356 | printf("a"); 357 | } 358 | else { 359 | printf("b"); 360 | } 361 | if (1>=b) { 362 | printf("a"); 363 | } 364 | else { 365 | printf("b"); 366 | } 367 | if (1>=q) { 368 | printf("a"); 369 | } 370 | else { 371 | printf("b"); 372 | } 373 | if (1>=2) { 374 | printf("a"); 375 | } 376 | else { 377 | printf("b"); 378 | } 379 | if (a>=b) { 380 | printf("a"); 381 | } 382 | else { 383 | printf("b"); 384 | } 385 | } 386 | 387 | 388 | int computeFac(int n) { 389 | if (n == 1) { 390 | return (1); 391 | } 392 | return (computeFac(n - 1) * n); 393 | } 394 | 395 | int fiboRecursion(int n) { 396 | const int outRange = -1; 397 | 398 | if (n < 0) { 399 | printf("n should not less than 0"); 400 | } 401 | if (n < 2) { 402 | return (n); 403 | } else { 404 | return (fiboRecursion(n - 1) + fiboRecursion(n - 2)); 405 | } 406 | } 407 | 408 | int f(int x) { 409 | if (x < 10) { 410 | printf("less than 10"); 411 | return (10); 412 | } 413 | else { 414 | printf("more than 10"); 415 | return (0); 416 | } 417 | return (20); 418 | } 419 | 420 | void g() { 421 | } 422 | 423 | void main() { 424 | g(); 425 | printf(f(-1)); 426 | printf(computeFac(1)); 427 | printf(computeFac(2)); 428 | printf(computeFac(3)); 429 | } 430 | 431 | 432 | void compare(char c, int n) { 433 | int i; 434 | char tmp; 435 | i = 0; 436 | 437 | do { 438 | scanf(tmp); 439 | if ((tmp) < (c)) { 440 | printf("<"); 441 | } else { 442 | printf(">"); 443 | } 444 | i = i + 1; 445 | } while (i < n); 446 | } 447 | 448 | void printAllChar() { 449 | printf(" !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}"); 450 | } 451 | 452 | int computeFac(int n) { 453 | if (n == 1) { 454 | return (1); 455 | } 456 | return (computeFac(n - 1) * n); 457 | } 458 | 459 | int returnInt() { 460 | return (computeFac(3)); 461 | } 462 | 463 | char returnChar() { 464 | return ('a'); 465 | } 466 | 467 | void main() { 468 | char A; 469 | int i; 470 | A = 'z'; 471 | printAllChar(); 472 | compare(returnChar(), returnInt()); 473 | compare(A, returnInt()); 474 | 475 | } 476 | 477 | const int a=10,b=1,c=-1,d=10; 478 | 479 | void main() { 480 | if (a+b != (c+d)*(-a-(-8))) { 481 | printf(a+b); 482 | printf((c+d)*(-a-(-8))); 483 | } 484 | else { 485 | printf(a); 486 | printf(-a); 487 | } 488 | 489 | } 490 | 491 | -------------------------------------------------------------------------------- /test/文法.txt: -------------------------------------------------------------------------------- 1 | <加法运算符> ::= +|- 2 | <乘法运算符>  ::= *|/ 3 | <关系运算符>  ::=  <|<=|>|>=|!=|== 4 | <字母>   ::= _|a|...|z|A|...|Z 5 | <数字>   ::= 0|<非零数字> 6 | <非零数字>  ::= 1|...|9 7 | <字符>    ::=  '<加法运算符>'|'<乘法运算符>'|'<字母>'|'<数字>' 8 | <字符串>   ::=  "{十进制编码为32,33,35-126的ASCII字符}" 9 | <程序>    ::= [<常量说明>][<变量说明>]{<有返回值函数定义>|<无返回值函数定义>}<主函数> 10 | <常量说明> ::=  const<常量定义>;{ const<常量定义>;} 11 | <常量定义>   ::=   int<标识符>=<整数>{,<标识符>=<整数>} 12 |   | char<标识符>=<字符>{,<标识符>=<字符>} 13 | <无符号整数>  ::= <非零数字>{<数字>}| 0 14 | <整数>        ::= [+|-]<无符号整数> 15 | <标识符>    ::=  <字母>{<字母>|<数字>} 16 | <声明头部>   ::=  int<标识符> |char<标识符> 17 | <变量说明>  ::= <变量定义>;{<变量定义>;} 18 | <变量定义>  ::= <类型标识符>(<标识符>|<标识符>'['<无符号整数>']'){,(<标识符>|<标识符>'['<无符号整数>']' )} 19 | //<无符号整数>表示数组元素的个数,其值需大于0 20 | <类型标识符>      ::=  int | char 21 | <有返回值函数定义>  ::=  <声明头部>'('<参数表>')' '{'<复合语句>'}' 22 | <无返回值函数定义>  ::= void<标识符>'('<参数表>')''{'<复合语句>'}' 23 | <复合语句>   ::=  [<常量说明>][<变量说明>]<语句列> 24 | <参数表>    ::=  <类型标识符><标识符>{,<类型标识符><标识符>}| <空> 25 | <主函数>    ::= void main'('')' '{'<复合语句>'}' 26 | <表达式>    ::= [+|-]<项>{<加法运算符><项>}   //[+|-]只作用于第一个<项> 27 | <项>     ::= <因子>{<乘法运算符><因子>} 28 | <因子>    ::= <标识符>|<标识符>'['<表达式>']'|'('<表达式>')'|<整数>|<字符>|<有返回值函数调用语句> 29 | <语句>    ::= <条件语句>|<循环语句>| '{'<语句列>'}'| <有返回值函数调用语句>;|<无返回值函数调用语句>;|<赋值语句>;|<读语句>;|<写语句>;|<空>;|<返回语句>; 30 | <赋值语句>   ::=  <标识符>=<表达式>|<标识符>'['<表达式>']'=<表达式> 31 | <条件语句>  ::= if '('<条件>')'<语句>[else<语句>] 32 | <条件>    ::=  <表达式><关系运算符><表达式> //整型表达式之间才能进行关系运算 33 | |<表达式> //表达式为整型,其值为0时条件为假,值不为0条件为真 34 | <循环语句>   ::=  while '('<条件>')'<语句>| do<语句>while '('<条件>')' |for'('<标识符>=<表达式>;<条件>;<标识符>=<标识符>(+|-)<步长>')'<语句> 35 | <步长>::= <无符号整数> 36 | <有返回值函数调用语句> ::= <标识符>'('<值参数表>')' 37 | <无返回值函数调用语句> ::= <标识符>'('<值参数表>')' 38 | <值参数表>   ::= <表达式>{,<表达式>}|<空> 39 | <语句列>   ::= {<语句>} 40 | <读语句>    ::=  scanf '('<标识符>{,<标识符>}')' 41 | <写语句>    ::= printf '(' <字符串>,<表达式> ')'| printf '('<字符串> ')'| printf '('<表达式>')' 42 | <返回语句>   ::=  return['('<表达式>')']    43 | -------------------------------------------------------------------------------- /test/新建文本文档.txt: -------------------------------------------------------------------------------- 1 | int x[100]; 2 | int y[100]; 3 | int num[100]; 4 | int Middle_Number_1(int N){ 5 | int middle_n,i,j,biggest,biggest_tag,intermediate; 6 | int num_copy[100]; 7 | middle_n = N / 2; 8 | i = 0; 9 | j = 0; 10 | biggest = -1; 11 | biggest_tag = -1; 12 | intermediate = 0; 13 | 14 | 15 | i = 0; 16 | do{ 17 | intermediate = num[i]; 18 | num_copy[i]= intermediate; 19 | i = i + 1; 20 | }while(i < N); 21 | 22 | 23 | i = 0; 24 | do{ 25 | j = 0; 26 | biggest = -1; 27 | biggest_tag = -1; 28 | for(j = 0;j < N - i;j = j + 1){ 29 | if(num_copy[j] > biggest){ 30 | biggest = num_copy[j]; 31 | biggest_tag = j; 32 | } 33 | } 34 | intermediate = num_copy[N-1-i]; 35 | num_copy[N-1-i] = biggest; 36 | num_copy[biggest_tag] = intermediate; 37 | 38 | i = i + 1; 39 | }while(i < N); 40 | 41 | return (num_copy[middle_n]); 42 | } 43 | 44 | int Middle_Number_2(int N){ 45 | int middle_n,i,j,biggest,biggest_tag,intermediate; 46 | int num_copy[100]; 47 | middle_n = N / 2; 48 | i = 0; 49 | j = 0; 50 | biggest = -1; 51 | biggest_tag = -1; 52 | intermediate = 0; 53 | 54 | i = 0; 55 | for(i = 0;i < N;i = i + 1){ 56 | intermediate = num[i]; 57 | num_copy[i]= intermediate; 58 | } 59 | 60 | i = 0; 61 | for(i = 0; i< N; i = i + 1){ 62 | j = 0; 63 | biggest = -1; 64 | biggest_tag = -1; 65 | while(j < N - i){ 66 | if(num_copy[j] > biggest){ 67 | biggest = num_copy[j]; 68 | biggest_tag = j; 69 | } 70 | j = j + 1; 71 | } 72 | 73 | intermediate = num_copy[N-1-i]; 74 | num_copy[N-1-i] = biggest; 75 | num_copy[biggest_tag] = intermediate; 76 | } 77 | 78 | return (num_copy[middle_n]); 79 | 80 | } 81 | int Middle_Number_3(int N){ 82 | int middle_n,i,j,biggest,biggest_tag,intermediate; 83 | int num_copy[100]; 84 | middle_n = N / 2; 85 | i = 0; 86 | j = 0; 87 | biggest = -1; 88 | biggest_tag = -1; 89 | intermediate = 0; 90 | i = 0; 91 | while(i < N){ 92 | intermediate = num[i]; 93 | num_copy[i]= intermediate; 94 | i = i + 1; 95 | } 96 | 97 | i = 0; 98 | while(i < N){ 99 | j = 0; 100 | biggest = -1; 101 | biggest_tag = -1; 102 | for(j = 0;j < N - i;j = j + 1){ 103 | if(num_copy[j] > biggest){ 104 | biggest = num_copy[j]; 105 | biggest_tag = j; 106 | } 107 | } 108 | 109 | intermediate = num_copy[N-1-i]; 110 | num_copy[N-1-i] = biggest; 111 | num_copy[biggest_tag] = intermediate; 112 | i = i + 1; 113 | } 114 | 115 | return (num_copy[middle_n]); 116 | } 117 | 118 | int Nearest_Point_1(int n,int N){ 119 | int i, j, distance, tag; 120 | distance = 2147483647; 121 | tag = -1; 122 | i = 0; 123 | do{ 124 | if((x[n] - x[i])*(x[n] - x[i]) + (y[n] - y[i])*(y[n] - y[i]) < distance){ 125 | if(i != n){ 126 | distance = (x[n] - x[i])*(x[n] - x[i]) + (y[n] - y[i])*(y[n] - y[i]); 127 | tag = i; 128 | } 129 | } 130 | i = i + 1; 131 | }while(i < N); 132 | if(tag == -1){ 133 | printf("Can not find Nearest_Point"); 134 | } 135 | return (tag); 136 | } 137 | 138 | int Nearest_Point_2(int n,int N){ 139 | int i, j, distance, tag; 140 | distance = 2147483647; 141 | tag = -1; 142 | i = 0; 143 | for(i = 0;i < N; i = i + 1){ 144 | if((x[n] - x[i])*(x[n] - x[i]) + (y[n] - y[i])*(y[n] - y[i]) < distance){ 145 | if(i != n){ 146 | distance = (x[n] - x[i])*(x[n] - x[i]) + (y[n] - y[i])*(y[n] - y[i]); 147 | tag = i; 148 | } 149 | } 150 | 151 | } 152 | 153 | if(tag == -1){ 154 | printf("Can not find Nearest_Point"); 155 | } 156 | return (tag); 157 | } 158 | 159 | int Nearest_Point_3(int n,int N){ 160 | int i, j, distance, tag; 161 | distance = 2147483647; 162 | tag = -1; 163 | i = 0; 164 | while(i < N){ 165 | if((x[n] - x[i])*(x[n] - x[i]) + (y[n] - y[i])*(y[n] - y[i]) < distance){ 166 | if(i != n){ 167 | distance = (x[n] - x[i])*(x[n] - x[i]) + (y[n] - y[i])*(y[n] - y[i]); 168 | tag = i; 169 | } 170 | } 171 | i = i + 1; 172 | } 173 | 174 | if(tag == -1){ 175 | printf("Can not find Nearest_Point"); 176 | } 177 | return (tag); 178 | } 179 | void main(){ 180 | const int const1 = -30, const2 = +30; 181 | int N, point_n, tag, i, k; 182 | int correct1,correct2; 183 | 184 | 185 | 186 | N = 100; 187 | 188 | 189 | for(k = 0; k < N; k = k+1){ 190 | 191 | num[k] = k-k*k; 192 | } 193 | 194 | for(k = 0; k < N; k = k+1){ 195 | 196 | x[k] = k-k*k; 197 | } 198 | 199 | for(k = 0; k < N; k = k+1){ 200 | y[k] = k-k*k; 201 | } 202 | 203 | point_n = 10; 204 | 205 | printf("Caculate of const is ",const1 * const2 + const1 - const2); 206 | 207 | correct1 = 0; 208 | printf("In function 1,Middle_Number is ",Middle_Number_1(N)); 209 | printf("In function 2,Middle_Number is ",Middle_Number_2(N)); 210 | printf("In function 3,Middle_Number is ",Middle_Number_3(N)); 211 | if(Middle_Number_1(N) == Middle_Number_2(N)){ 212 | if(Middle_Number_2(N) == Middle_Number_3(N)){ 213 | printf("Middle_Number Answer Correct!"); 214 | correct1 = 1; 215 | }else{ 216 | printf("Middle_Number Error1!"); 217 | } 218 | }else{ 219 | printf("Middle_Number Error2!"); 220 | } 221 | correct2 = 0; 222 | tag = Nearest_Point_1(point_n,N); 223 | printf("In function 1,Nearest point is ",tag); 224 | tag = Nearest_Point_2(point_n,N); 225 | printf("In function 2,Nearest point is ",tag); 226 | tag = Nearest_Point_3(point_n,N); 227 | printf("In function 3,Nearest point is ",tag); 228 | if(Nearest_Point_1(point_n,N) == Nearest_Point_2(point_n,N)){ 229 | 230 | 231 | if(Nearest_Point_2(point_n,N) == Nearest_Point_3(point_n,N)){ 232 | printf("Nearest_Point Answer Correct!"); 233 | correct2 = 1; 234 | }else{ 235 | printf("Nearest_Point Error1!"); 236 | } 237 | }else{ 238 | printf("Nearest_Point Error2!"); 239 | } 240 | if(correct1 == 1){ 241 | if(correct2 == 1){ 242 | printf("All Cyclic Procedure Right!"); 243 | }else{ 244 | printf("Cyclic Procedure Error!"); 245 | } 246 | }else{ 247 | printf("Cyclic Procedure Error!"); 248 | } 249 | } -------------------------------------------------------------------------------- /test/竞速.txt: -------------------------------------------------------------------------------- 1 | const int MAX_NUM = 128 ; 2 | 3 | int factorial(int n){ 4 | if(n<=1) return (1); 5 | 6 | return(n*factorial(n-1)) ; 7 | } 8 | 9 | int mod(int x, int y){ 10 | x=x-x/y*y; 11 | 12 | return (x) ; 13 | } 14 | 15 | void swap(int x, int y){ 16 | int temp; 17 | 18 | printf("x = ", x) ; 19 | printf("y = ", y) ; 20 | temp = x; 21 | x=y; 22 | y=temp; 23 | printf("SWAP x = ", x) ; 24 | printf("SWAP y = ", y) ; 25 | } 26 | 27 | int full_num(int n, int j, int a){ 28 | return (n*100+j*10+a) ; 29 | } 30 | 31 | int flower_num(int n, int j, int a){ 32 | return (n*n*n+j*j*j+a*a*a) ; 33 | } 34 | 35 | void complete_flower_num() 36 | { 37 | int k[128]; 38 | int i,j,n,s,x1,y; 39 | int m,k2,h,leap,x2; 40 | int a,b,c ; 41 | 42 | 43 | for(j=2;j< MAX_NUM ;j=j+1) 44 | { 45 | n = -1; 46 | s = j; 47 | for(i=1; i= 128) 55 | printf("OVERFLOW!") ; 56 | else 57 | k[n] = i; 58 | } 59 | } 60 | 61 | if(s==0) 62 | { 63 | printf("complete number: ",j); 64 | for(i=0;i<=n;i=i+1) 65 | printf(" ",k[i]); 66 | printf(" ") ; 67 | } 68 | } 69 | 70 | printf("---------------------------------------------------------------"); 71 | printf("'water flower'number is:"); 72 | y = 0 ; 73 | for(i=100;i<100+MAX_NUM;i=i+1){ 74 | n=i/100; 75 | j=mod(i/10,10); 76 | a=mod(i,10); 77 | if(full_num(n,j,a)==flower_num(n, j, a)){ 78 | k[y] = i ; 79 | y = y + 1 ; 80 | } 81 | } 82 | for(i = 0 ; i 1) { 22 | if (out) { 23 | errorfile << line << " a\n"; //不符合词法 有前导的0 24 | error = true; 25 | } 26 | } 27 | 28 | 双引号错误的 29 | if (isNewline()) { -------------------------------------------------------------------------------- /test/错误处理样例.txt: -------------------------------------------------------------------------------- 1 | const char a='c, b='c'; 2 | void %main(){ 3 | int a,b[10]; 4 | a='?; 5 | a='???'; 6 | a = b[('?)]; 7 | b[('?a)] = 10; 8 | a='csq'; 9 | a = b[('c)]; 10 | b[('c)] = 10; 11 | } 12 | 13 | in%t cha%nge1; 14 | 词法 0开头的数字 15 | const int %const1 = 1, const2 = -100; 16 | const char const3 = '?'; 17 | in%t cha%nge1; 18 | char change3; 19 | int gets1%(int var1,int var2){ 20 | change1 %= var1 + var2 ; 21 | printf("ddd, 1); 22 | printf("aa"); 23 | printf("bbb); 24 | printf("ccc",1); 25 | printf("ddd, 1); 26 | %return (change1); 27 | } 28 | void %main(){ 29 | change1 = 10*gets1%(1,2); 30 | printf("Hello World"); 31 | printf(gets1(10, 20)); 32 | } 33 | 34 | 35 | 36 | int f(int a, int b) { 37 | return (a); 38 | } 39 | 40 | int t() {} 41 | void main() 42 | { 43 | int f; 44 | f(1,2); 45 | for (t=0;10;t=t+1) {;} 46 | return (0); 47 | } 48 | 49 | 50 | int q; 51 | int f(int a, int b) { 52 | return (a); 53 | } 54 | 55 | int t() {} 56 | void main() 57 | { 58 | int f; 59 | char w; 60 | f(1,2); 61 | for (t=0;10;t=t+1) {;} 62 | q=10; 63 | scanf(a,q); 64 | scanf(f); 65 | w = 1; 66 | return (0); 67 | } 68 | 69 | 70 | int q; 71 | int f(int a, int b) { 72 | return (a); 73 | } 74 | 75 | int t() {} 76 | 77 | int v() {} 78 | void main() 79 | { 80 | int f; 81 | char w; 82 | f(1,2); 83 | for (t=0;10;t=t+1) {;} 84 | q=10; 85 | scanf(a,q); 86 | scanf(f); 87 | w = a[10]; 88 | w = f(10,10); 89 | w = 10*t(); 90 | w = 10*qqq(); 91 | w = qqq(); 92 | w = v()*t()*qqq()*t; 93 | w = e; 94 | return (0); 95 | } 96 | 97 | 98 | 99 | void f(int a, int b) { 100 | return (a); 101 | } 102 | 103 | void main() 104 | { 105 | f(); 106 | return (0); 107 | } 108 | 109 | 110 | int f(int a, int b) { 111 | return (a); 112 | } 113 | 114 | int ff(int a) 115 | {} 116 | 117 | void fff() {} 118 | 119 | int ffff(int a, char b, int c) {} 120 | 121 | void main() 122 | { 123 | return (0); 124 | if ('a'>'b') {} 125 | if(('c')) { 126 | } 127 | else { 128 | } 129 | 130 | while ('d') { 131 | 132 | } 133 | 134 | do { 135 | } 136 | while(10) 137 | 138 | for (t=0;'0';t=t+1) {;} 139 | 140 | f(1); 141 | f(1,2); 142 | f(); 143 | f('1',2); 144 | ff(10); 145 | ff('1'); 146 | fff(); 147 | fff(1); 148 | ffff(); 149 | ffff(10*f(1,2), 'a', 10); 150 | ffff(10,10,10); 151 | ffff(10,'q'); 152 | ffff('q','q','q'); 153 | return (0); 154 | } 155 | 156 | 157 | int f(int a, int b) { 158 | return (a); 159 | } 160 | 161 | char g(int a,int b) { 162 | } 163 | 164 | void main() 165 | { 166 | const char ccc='0'; 167 | int a[10]; 168 | char c; 169 | char cc[10]; 170 | 171 | a['a']=1; 172 | a[('a')]=1; 173 | a['a'-'b']=1; 174 | a['a'*'a']=1; 175 | a[10]=1; 176 | a[f(1,2)]=1; 177 | a[g(1,2)]=1; 178 | a[c]=1; 179 | a[c[10]]=1; 180 | a[ccc]=1; 181 | a[10]=a[10]*10; 182 | a[10]=(a[10]); 183 | a[10]=cc[10]*10; 184 | a[10]=c[10]*10; 185 | a[10]=(cc[10]); 186 | a[10]=c[10]; 187 | c[10]=10; 188 | a[10]=cc['a']; 189 | return (0); 190 | } 191 | 192 | 193 | const int q = 10; 194 | const int w = 10; 195 | void main() 196 | { 197 | const char ccc='0'; 198 | const int qwq=1; 199 | int a[10]; 200 | char c; 201 | char cc[10]; 202 | int q; 203 | q=10; 204 | qwq=1; 205 | ccc='1'; 206 | scanf(c,qwq); 207 | scanf(qwq); 208 | scanf(q); 209 | for (c=0;c;c=c+1) {} 210 | for (q=0;q;q=q+1) {} 211 | for (w=0;w;w=w+1) {} 212 | for (ccc=0;ccc;ccc=ccc+1) {} 213 | return (0); 214 | } 215 | 216 | 217 | 218 | const int a1_1 = +10 219 | const int b2 = -10, cc = 10 220 | const char aA = '+' const char AAb_ = '-', cQ1 = '*', d1Q = '/' 221 | const int a____A = +0, _b = -0, c_ = 0 222 | 223 | int f(){} 224 | void g() {} 225 | void main() 226 | { 227 | int a, d, f, e 228 | int c[100],g[1000],h,aaa[10] 229 | int b int aqw 230 | a=10 231 | f(); 232 | scanf(a) 233 | printf("1") 234 | a=20; 235 | g(); 236 | for (a=0 a a=a+1) { 237 | } 238 | return (1) 239 | } 240 | 241 | 242 | int f( {} 243 | 244 | char ff(int a {} 245 | 246 | char fff (int a, char b{} 247 | 248 | void g( {} 249 | 250 | void gg(int a {} 251 | 252 | void ggg (int a, char b{} 253 | 254 | void main({ 255 | int a,b,c; 256 | g(1); 257 | gg(); 258 | ggg(1,'1'); 259 | ggg('1',1); 260 | a = ('a'; 261 | a = ('1') 262 | a = (1+2*3; 263 | a = f()*(1; 264 | if (10 {} 265 | else {} 266 | if (f() {} 267 | if (10*f() {} 268 | while (10 {} 269 | do { 270 | } while (f() 271 | do{ 272 | } while (('a') 273 | do { 274 | } while (('a')<('b') 275 | for (a=0;a;a=a+10 {} 276 | for (a=0;a;a=a-1{} 277 | f(; 278 | ff(1; 279 | ff(f(); 280 | fff(1,'1'; 281 | fff(1,ff(f(); 282 | scanf(a,b; 283 | scanf(a; 284 | printf("aaaa"; 285 | printf(f(); 286 | printf(10+10); 287 | printf("aaa",f(); 288 | printf("aa", 10; 289 | printf(10; 290 | k(); 291 | k(); 292 | k(1); 293 | return (1; 294 | return ; 295 | return (0); 296 | } 297 | 298 | 299 | int f( {} 300 | 301 | char ff(int a {} 302 | 303 | char fff (int a, char b{} 304 | 305 | void g( {} 306 | 307 | void gg(int a {} 308 | 309 | void ggg (int a, char b{} 310 | 311 | void main({ 312 | int a[10]; 313 | int b; 314 | f(; 315 | b = a[f(]; 316 | a[f(] = 10; 317 | a[f(] = ff(1; 318 | b = f(*10; 319 | b = f(+10; 320 | b = (f(*10)+10; 321 | b = f(/10; 322 | b = f(/10; 323 | b = 10*a[f(]; 324 | b = (f(); 325 | b = 10*(f(); 326 | if (f( < f()) {} 327 | if (f(<= f()) {} 328 | for (b=f(;10;b=b+10){} 329 | printf("a",f(); 330 | printf(10*f(); 331 | return (f(); 332 | fff(f(,'a'); 333 | ff(f(); 334 | return (0); 335 | } 336 | 337 | 338 | 339 | int f( {} 340 | 341 | char ff(int a {} 342 | 343 | char fff (int a, char b{} 344 | 345 | void g( {} 346 | 347 | void gg(int a {} 348 | 349 | void ggg (int a, char b{} 350 | 351 | void main({ 352 | int b,a[10,c; 353 | int d[1; 354 | char q[10,w[10]; 355 | char e[10,r; 356 | e[1 = 10; 357 | a[10 = 10; 358 | a[10] = a[10 * 10; 359 | a[10] = a[10 * f(); 360 | a[10] = 10*a[10; 361 | a[10] = 10*(a[10); 362 | ff(a[10); 363 | return (0); 364 | } 365 | 366 | 367 | int f( {} 368 | 369 | char ff(int a {} 370 | 371 | char fff (int a, char b{} 372 | 373 | void g( {} 374 | 375 | void gg(int a {} 376 | 377 | void ggg (int a, char b{} 378 | 379 | void main({ 380 | do { 381 | }while (1); 382 | do { 383 | } (1); 384 | do { 385 | } 386 | (1); 387 | do { 388 | } 389 | 390 | (1); 391 | return (0); 392 | } 393 | 394 | 395 | const char t = 'a, q='a'; 396 | int f() { 397 | return; 398 | } 399 | 400 | int ff() { 401 | return (1); 402 | } 403 | 404 | int fff() { 405 | return ('1'); 406 | } 407 | 408 | int ffff() { 409 | return (('1')); 410 | } 411 | 412 | char fffff() { 413 | return (1); 414 | } 415 | 416 | char ffffff() { 417 | return ('1'); 418 | } 419 | 420 | int fffffff() { 421 | 422 | } 423 | 424 | void g() { 425 | return; 426 | } 427 | 428 | void gg() { 429 | return (1); 430 | } 431 | 432 | void ggg() { 433 | return ('1'); 434 | } 435 | 436 | void gggg() { 437 | return (('1')); 438 | } 439 | 440 | void ggggg() { 441 | return (1); 442 | } 443 | 444 | void gggggg() { 445 | return ('1'); 446 | } 447 | 448 | void ggggggg() { 449 | 450 | } 451 | 452 | void main(){ 453 | const char a=10; 454 | return (1); 455 | } 456 | -------------------------------------------------------------------------------- /编译申优文档.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aptx1231/BUAA_Compiler/f95305b33dc0895ed672d73ddc97fad669f8794b/编译申优文档.pdf --------------------------------------------------------------------------------