├── .gitignore ├── VM ├── ArrayOutBound.java ├── Handler.java ├── InputEmpty.java ├── LabelNotExists.java ├── Labels.java ├── Main.java ├── Mars-Compile-2021.jar ├── MyException.java ├── ParamsNumberUnmatched.java ├── Pointer.java ├── Quaternion.java ├── RunningStack.java ├── VM.iml ├── VariableNotExists.java └── autoTest.cpp ├── autoTest.sh ├── control-flow-graphs ├── BasicBlock.h ├── ControlFlowGraphBuilder.h ├── Function.h ├── Quaternion │ ├── Assign.h │ ├── Binary.h │ ├── CreatePointer.h │ ├── Divide.h │ ├── Equal.h │ ├── FuncCall.h │ ├── GetReturn.h │ ├── Getint.h │ ├── GreaterOrEqual.h │ ├── GreaterThan.h │ ├── Jump.h │ ├── JumpWhenFalse.h │ ├── JumpWhenTrue.h │ ├── LessOrEqual.h │ ├── LessThan.h │ ├── LoadParam.h │ ├── LoadPointer.h │ ├── LoadWord.h │ ├── Minus.h │ ├── Mod.h │ ├── Multiply.h │ ├── Negate.h │ ├── NotEqual.h │ ├── NotLogic.h │ ├── PhiFunction.h │ ├── Plus.h │ ├── PrintInteger.h │ ├── PrintString.h │ ├── PushParam.h │ ├── Quaternion.h │ ├── ReadInt.h │ ├── Return.h │ └── StoreWord.h ├── RegisterAllocate.h ├── StaticSingleAssignment.h └── lib.h ├── lexer ├── Cursor.h ├── Lexer.h ├── LexerError.h ├── Source.h ├── SyntaxKind.h ├── Token.h ├── TokenSource.h └── lib.h ├── lib.h ├── main.cpp ├── nodes ├── AssignStmtNode.h ├── BinExprNode.h ├── BlockNode.h ├── BreakStmt.h ├── CompileUnitNode.h ├── ContinueStmtNode.h ├── ErrorNode.h ├── ExprNode.h ├── ExprStmtNode.h ├── FuncArgsNode.h ├── FuncDefNode.h ├── FuncExprNode.h ├── FuncParamNode.h ├── FuncParamsNode.h ├── FuncTypeNode.h ├── IfStmtNode.h ├── InitValNode.h ├── LiteralExprNode.h ├── Node.h ├── PrefixExprNode.h ├── PrefixOPNode.h ├── PrintfStmtNode.h ├── ReturnStmtNode.h ├── StatementNode.h ├── TokenNode.h ├── VarDeclNode.h ├── VarDefNode.h ├── VarExprNode.h ├── WhileStmtNode.h └── lib.h ├── parser ├── Context.h ├── ErrorKind.h ├── Parser.h ├── Symbol.h ├── TreeBuilder.h └── lib.h ├── test ├── input1.txt ├── input10.txt ├── input11.txt ├── input12.txt ├── input13.txt ├── input14.txt ├── input15.txt ├── input16.txt ├── input17.txt ├── input18.txt ├── input19.txt ├── input2.txt ├── input20.txt ├── input21.txt ├── input22.txt ├── input23.txt ├── input24.txt ├── input25.txt ├── input26.txt ├── input27.txt ├── input28.txt ├── input29.txt ├── input3.txt ├── input30.txt ├── input31.txt ├── input32.txt ├── input33.txt ├── input34.txt ├── input35.txt ├── input36.txt ├── input37.txt ├── input38.txt ├── input39.txt ├── input4.txt ├── input40.txt ├── input41.txt ├── input42.txt ├── input43.txt ├── input44.txt ├── input45.txt ├── input46.txt ├── input47.txt ├── input48.txt ├── input49.txt ├── input5.txt ├── input50.txt ├── input51.txt ├── input52.txt ├── input53.txt ├── input54.txt ├── input55.txt ├── input56.txt ├── input57.txt ├── input58.txt ├── input59.txt ├── input6.txt ├── input60.txt ├── input61.txt ├── input62.txt ├── input63.txt ├── input64.txt ├── input65.txt ├── input66.txt ├── input67.txt ├── input68.txt ├── input69.txt ├── input7.txt ├── input70.txt ├── input71.txt ├── input72.txt ├── input73.txt ├── input74.txt ├── input75.txt ├── input76.txt ├── input77.txt ├── input78.txt ├── input79.txt ├── input8.txt ├── input80.txt ├── input81.txt ├── input82.txt ├── input9.txt ├── output1.txt ├── output10.txt ├── output11.txt ├── output12.txt ├── output13.txt ├── output14.txt ├── output15.txt ├── output16.txt ├── output17.txt ├── output18.txt ├── output19.txt ├── output2.txt ├── output20.txt ├── output21.txt ├── output22.txt ├── output23.txt ├── output24.txt ├── output25.txt ├── output26.txt ├── output27.txt ├── output28.txt ├── output29.txt ├── output3.txt ├── output30.txt ├── output31.txt ├── output32.txt ├── output33.txt ├── output34.txt ├── output35.txt ├── output36.txt ├── output37.txt ├── output38.txt ├── output39.txt ├── output4.txt ├── output40.txt ├── output41.txt ├── output42.txt ├── output43.txt ├── output44.txt ├── output45.txt ├── output46.txt ├── output47.txt ├── output48.txt ├── output49.txt ├── output5.txt ├── output50.txt ├── output51.txt ├── output52.txt ├── output53.txt ├── output54.txt ├── output55.txt ├── output56.txt ├── output57.txt ├── output58.txt ├── output59.txt ├── output6.txt ├── output60.txt ├── output61.txt ├── output62.txt ├── output63.txt ├── output64.txt ├── output65.txt ├── output66.txt ├── output67.txt ├── output68.txt ├── output69.txt ├── output7.txt ├── output70.txt ├── output71.txt ├── output72.txt ├── output73.txt ├── output74.txt ├── output75.txt ├── output76.txt ├── output77.txt ├── output78.txt ├── output79.txt ├── output8.txt ├── output80.txt ├── output81.txt ├── output82.txt ├── output9.txt ├── testfile1.txt ├── testfile10.txt ├── testfile11.txt ├── testfile12.txt ├── testfile13.txt ├── testfile14.txt ├── testfile15.txt ├── testfile16.txt ├── testfile17.txt ├── testfile18.txt ├── testfile19.txt ├── testfile2.txt ├── testfile20.txt ├── testfile21.txt ├── testfile22.txt ├── testfile23.txt ├── testfile24.txt ├── testfile25.txt ├── testfile26.txt ├── testfile27.txt ├── testfile28.txt ├── testfile29.txt ├── testfile3.txt ├── testfile30.txt ├── testfile31.txt ├── testfile32.txt ├── testfile33.txt ├── testfile34.txt ├── testfile35.txt ├── testfile36.txt ├── testfile37.txt ├── testfile38.txt ├── testfile39.txt ├── testfile4.txt ├── testfile40.txt ├── testfile41.txt ├── testfile42.txt ├── testfile43.txt ├── testfile44.txt ├── testfile45.txt ├── testfile46.txt ├── testfile47.txt ├── testfile48.txt ├── testfile49.txt ├── testfile5.txt ├── testfile50.txt ├── testfile51.txt ├── testfile52.txt ├── testfile53.txt ├── testfile54.txt ├── testfile55.txt ├── testfile56.txt ├── testfile57.txt ├── testfile58.txt ├── testfile59.txt ├── testfile6.txt ├── testfile60.txt ├── testfile61.txt ├── testfile62.txt ├── testfile63.txt ├── testfile64.txt ├── testfile65.txt ├── testfile66.txt ├── testfile67.txt ├── testfile68.txt ├── testfile69.txt ├── testfile7.txt ├── testfile70.txt ├── testfile71.txt ├── testfile72.txt ├── testfile73.txt ├── testfile74.txt ├── testfile75.txt ├── testfile76.txt ├── testfile77.txt ├── testfile78.txt ├── testfile79.txt ├── testfile8.txt ├── testfile80.txt ├── testfile81.txt ├── testfile82.txt └── testfile9.txt ├── 申优文档.md ├── 设计文档.md └── 语法解析重构.md /.gitignore: -------------------------------------------------------------------------------- 1 | **/.vscode 2 | /VM/out 3 | /VM/result.txt 4 | /VM/InstructionStatistics.txt -------------------------------------------------------------------------------- /VM/ArrayOutBound.java: -------------------------------------------------------------------------------- 1 | public class ArrayOutBound extends MyException { 2 | 3 | @Override 4 | public void print(int line) { 5 | System.out.println("Array Out of Bound on Line " + (line + 1)); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /VM/InputEmpty.java: -------------------------------------------------------------------------------- 1 | public class InputEmpty extends MyException { 2 | @Override 3 | public void print(int line) { 4 | System.out.println("Input has empty " + (line + 1)); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /VM/LabelNotExists.java: -------------------------------------------------------------------------------- 1 | public class LabelNotExists extends MyException { 2 | private String label; 3 | 4 | public LabelNotExists(String label) { 5 | this.label = label; 6 | } 7 | 8 | public void print(int line) { 9 | System.out.println("Read nonexistent label '" + label + "' on line " + (line + 1)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /VM/Labels.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.HashMap; 3 | 4 | public class Labels { 5 | private static final HashMap map = new HashMap<>(); 6 | private static final HashMap> params = new HashMap<>(); 7 | 8 | public static boolean addLabel(String label, ArrayList arrayList, Integer line) { 9 | if (map.containsKey(label)) { 10 | return false; 11 | } 12 | map.put(label, line); 13 | params.put(label, arrayList); 14 | return true; 15 | } 16 | 17 | public static Integer getLabel(String label) throws LabelNotExists { 18 | Integer res = map.get(label); 19 | if (res == null) { 20 | throw new LabelNotExists(label); 21 | } 22 | return res; 23 | } 24 | 25 | public static ArrayList getParams(String label) throws LabelNotExists { 26 | ArrayList ret = params.get(label); 27 | if (ret == null) { 28 | throw new LabelNotExists(label); 29 | } 30 | return ret; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /VM/Mars-Compile-2021.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/VM/Mars-Compile-2021.jar -------------------------------------------------------------------------------- /VM/MyException.java: -------------------------------------------------------------------------------- 1 | public abstract class MyException extends Exception { 2 | abstract public void print(int line); 3 | } 4 | -------------------------------------------------------------------------------- /VM/ParamsNumberUnmatched.java: -------------------------------------------------------------------------------- 1 | public class ParamsNumberUnmatched extends MyException { 2 | @Override 3 | public void print(int line) { 4 | System.out.println("ParamsNumberUnmatched " + line); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /VM/Pointer.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Pointer { 4 | private ArrayList arrayList; 5 | private int n; 6 | 7 | public Pointer() {} 8 | 9 | public Pointer(int size) { 10 | arrayList = new ArrayList<>(); 11 | this.n = 0; 12 | for (int i = 0; i < size; i++) { 13 | arrayList.add(0); 14 | } 15 | } 16 | 17 | public Pointer(ArrayList global) { 18 | this.n = 0; 19 | arrayList = new ArrayList<>(); 20 | int size = Integer.parseInt(global.get(1)); 21 | if (global.size() == 2) { 22 | for (int i = 0; i < size; i++) { 23 | arrayList.add(0); 24 | } 25 | } else { 26 | assert global.size() == size + 2; 27 | for (int i = 2; i < global.size(); i++) { 28 | arrayList.add(Integer.parseInt(global.get(i))); 29 | } 30 | } 31 | } 32 | 33 | public int getVal(int offset) throws ArrayOutBound { 34 | if (offset + n >= arrayList.size() || offset + n < 0) { 35 | throw new ArrayOutBound(); 36 | } 37 | return arrayList.get(offset + n); 38 | } 39 | 40 | public void setVal(int offset, int res) throws ArrayOutBound { 41 | if (offset + n >= arrayList.size() || offset + n < 0) { 42 | throw new ArrayOutBound(); 43 | } 44 | arrayList.set(offset + n, res); 45 | } 46 | 47 | public void addN(int offset) throws ArrayOutBound { 48 | if (offset + n >= arrayList.size() || offset + n < 0) { 49 | throw new ArrayOutBound(); 50 | } 51 | this.n += offset; 52 | } 53 | 54 | @Override 55 | public Object clone() { 56 | Pointer ret = new Pointer(); 57 | ret.n = this.n; 58 | ret.arrayList = this.arrayList; 59 | return ret; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /VM/VM.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /VM/VariableNotExists.java: -------------------------------------------------------------------------------- 1 | public class VariableNotExists extends MyException { 2 | String variable; 3 | 4 | public VariableNotExists(String variable) { 5 | this.variable = variable; 6 | } 7 | 8 | public void print(int line) { 9 | System.out.println("Read nonexistent variable '" + variable + "' on line " + (line + 1)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /VM/autoTest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char cmd[1000]; 5 | vector vct; 6 | int main(int argc, char **argv) { 7 | bool ir = false; 8 | bool debug = false; 9 | if (argc > 1 && strcmp(argv[1], "ir") == 0) { 10 | ir = true; 11 | if (argc > 2) { 12 | debug = true; 13 | for (int i = 2; i < argc; i++) { 14 | vct.push_back(atoi(argv[i])); 15 | } 16 | } 17 | } else if (argc > 1) { 18 | debug = true; 19 | for (int i = 1; i < argc; i++) { 20 | vct.push_back(atoi(argv[i])); 21 | } 22 | } 23 | if (!debug) { 24 | for (int i = 1; i <= 82; i++) { 25 | vct.push_back(i); 26 | } 27 | } 28 | sprintf(cmd, "rm result.txt"); 29 | system(cmd); 30 | sprintf(cmd, "g++ ../main.cpp -o compiler -O2"); 31 | system(cmd); 32 | if (ir) { 33 | sprintf(cmd, "javac Main.java"); 34 | system(cmd); 35 | } 36 | for (auto i : vct) { 37 | cout << "testing " << i << endl; 38 | sprintf(cmd, "cp ../test/testfile%d.txt testfile.txt", i); 39 | system(cmd); 40 | sprintf(cmd, "./compiler"); 41 | system(cmd); 42 | sprintf(cmd, "cp ../test/input%d.txt input.txt", i); 43 | system(cmd); 44 | if (ir) { 45 | sprintf(cmd, "java Main > output.txt"); 46 | system(cmd); 47 | } else { 48 | sprintf(cmd, "java -jar Mars-Compile-2021.jar mips.txt < input.txt " 49 | "> output.txt && sed -i '1,2d' output.txt"); 50 | system(cmd); 51 | } 52 | sprintf(cmd, "echo 'Testcase%d\n' >> result.txt", i); 53 | system(cmd); 54 | sprintf(cmd, "diff -b ../test/output%d.txt output.txt >> result.txt", i); 55 | system(cmd); 56 | } 57 | if (!debug) { 58 | if (ir) { 59 | sprintf(cmd, "rm ir.txt testfile.txt data.txt input.txt error.txt output.txt compiler"); 60 | system(cmd); 61 | } else { 62 | sprintf(cmd, "rm mips.txt testfile.txt input.txt error.txt output.txt compiler"); 63 | system(cmd); 64 | } 65 | } 66 | if (ir) { 67 | sprintf(cmd, "rm *.class"); 68 | system(cmd); 69 | } 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /autoTest.sh: -------------------------------------------------------------------------------- 1 | cd VM 2 | g++ autoTest.cpp -o a.out 3 | ./a.out $@ 4 | rm a.out 5 | -------------------------------------------------------------------------------- /control-flow-graphs/ControlFlowGraphBuilder.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/26. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_CONTROLFLOWGRAPHBUILDER_H 6 | #define SYSYCOMPILER_CONTROLFLOWGRAPHBUILDER_H 7 | #include "Function.h" 8 | using namespace std; 9 | 10 | class ControlFlowGraphBuilder { 11 | set funcs; 12 | Function* curFunc; 13 | BasicBlock *curBasicBlock; 14 | int basicBlockCnt; 15 | int tmpCnt; 16 | public: 17 | ControlFlowGraphBuilder() : basicBlockCnt(0), tmpCnt(0), curBasicBlock(nullptr), curFunc(nullptr) {} 18 | 19 | void insert(Quaternion *quaternion) { 20 | curBasicBlock->insert(quaternion); 21 | } 22 | 23 | string tmpVar() { 24 | tmpCnt++; 25 | return "tmp_" + to_string(tmpCnt); 26 | } 27 | 28 | BasicBlock* newBasicBlock() { 29 | return new BasicBlock(++basicBlockCnt); 30 | } 31 | 32 | void changeFunction(Function *function) { 33 | curFunc = function; 34 | funcs.insert(function); 35 | } 36 | 37 | void changeCur(BasicBlock *newBasicBlock) { 38 | if (newBasicBlock == this->curBasicBlock) { 39 | return; 40 | } 41 | curBasicBlock = newBasicBlock; 42 | curFunc->addBasicBlock(newBasicBlock); 43 | } 44 | 45 | void simplify() { 46 | for (auto & i : funcs) { 47 | i->simplify(); 48 | } 49 | } 50 | 51 | void staticSingleAssignment() { 52 | for (auto & i : funcs) { 53 | i->staticSingleAssignment(); 54 | } 55 | } 56 | 57 | void optimize() { 58 | for (auto & i : funcs) { 59 | i->optimize(); 60 | } 61 | } 62 | 63 | void print(ofstream *out) { 64 | for (auto & i : funcs) { 65 | i->print(out); 66 | } 67 | } 68 | 69 | void registerAllocate() { 70 | for (auto & i : funcs) { 71 | i->registerAllocate(); 72 | } 73 | } 74 | 75 | void assembly(ofstream *out) { 76 | for (auto & i : funcs) { 77 | i->assembly(out); 78 | } 79 | } 80 | 81 | BasicBlock* getCur() { 82 | return this->curBasicBlock; 83 | } 84 | } controlFlowGraphBuilder; 85 | #endif //SYSYCOMPILER_CONTROLFLOWGRAPHBUILDER_H 86 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/Assign.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/26. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_ASSIGN_H 6 | #define SYSYCOMPILER_ASSIGN_H 7 | class Assign : public Quaternion{ 8 | string assigned; 9 | string integer; 10 | public: 11 | Assign(const string *assigned, const string *integer) : Quaternion(IRKind::ASSIGN), 12 | assigned(*assigned), integer(*integer) {} 13 | 14 | void print(ofstream *out) override { 15 | *out << "assign " << assigned << " " << integer << endl; 16 | } 17 | 18 | string* getDefine() override { 19 | return &assigned; 20 | } 21 | 22 | set* getUse() override { 23 | auto ret = new set; 24 | if (!exprResIsNumber(&integer)) { 25 | ret->insert(&integer); 26 | } 27 | return ret; 28 | } 29 | 30 | void globalValueNumbering(map *constValue, map *copyValue, 31 | map*>* previous, 32 | map*>* cur) override { 33 | if (constValue->count(integer)) { 34 | integer = to_string((*constValue)[integer]); 35 | } else if (copyValue->count(integer)) { 36 | integer = (*copyValue)[integer]; 37 | } 38 | if (exprResIsNumber(&integer)) { 39 | (*constValue)[assigned] = stoi(integer); 40 | } else { 41 | (*copyValue)[assigned] = integer; 42 | } 43 | } 44 | 45 | void assembly(ofstream *out, Function*) override { 46 | if (integer[0] == 's') { 47 | *out << "lw $27, " + integer.substr(2) + "($sp)" << endl; 48 | integer = "$27"; 49 | } 50 | if (assigned[0] == '$') { 51 | if (exprResIsNumber(&integer)) { 52 | *out << "li " << assigned << ", " << integer << endl; 53 | } else { 54 | *out << "move " + assigned << ", " << integer << endl; 55 | } 56 | } else { 57 | if (exprResIsNumber((&integer))) { 58 | *out << "li $27, " << integer << endl; 59 | integer = "$27"; 60 | } 61 | *out << "sw " + integer << ", " << assigned.substr(2) << "($sp)" << endl; 62 | } 63 | } 64 | }; 65 | #endif //SYSYCOMPILER_ASSIGN_H 66 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/Binary.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/11/20. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_BINARY_H 6 | #define SYSYCOMPILER_BINARY_H 7 | 8 | class Binary : public Quaternion { 9 | protected: 10 | string integer1; 11 | string integer2; 12 | string assigned; 13 | bool dealUsing(map *constValue, map *copyValue) { 14 | if (constValue->count(integer1)) { 15 | integer1 = to_string((*constValue)[integer1]); 16 | } else if (copyValue->count(integer1)) { 17 | integer1 = (*copyValue)[integer1]; 18 | } 19 | if (constValue->count(integer2)) { 20 | integer2 = to_string((*constValue)[integer2]); 21 | } else if (copyValue->count(integer2)) { 22 | integer2 = (*copyValue)[integer2]; 23 | } 24 | return false; 25 | } 26 | public: 27 | Binary(IRKind kind, string *assigned, string *integer1, string *integer2) : Quaternion(kind), 28 | integer1(*integer1), integer2(*integer2), assigned(*assigned) {} 29 | 30 | string* getDefine() override { 31 | return &assigned; 32 | } 33 | 34 | set* getUse() override { 35 | auto ret = new set; 36 | if (!exprResIsNumber(&integer1)) { 37 | ret->insert(&integer1); 38 | } 39 | if (!exprResIsNumber(&integer2)) { 40 | ret->insert(&integer2); 41 | } 42 | return ret; 43 | } 44 | }; 45 | #endif //SYSYCOMPILER_BINARY_H 46 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/CreatePointer.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/26. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_CREATEPOINTER_H 6 | #define SYSYCOMPILER_CREATEPOINTER_H 7 | 8 | class CreatePointer : public Quaternion { 9 | string name; 10 | int size; 11 | public: 12 | CreatePointer(string *name, int size) : 13 | Quaternion(IRKind::CREATE_POINTER), name(*name), size(size) {} 14 | 15 | string* getDefine() override { 16 | return &name; 17 | } 18 | 19 | int getSize() const { 20 | return size; 21 | } 22 | 23 | void print(ofstream *out) override { 24 | *out << "createPointer " << name << " " << size << endl; 25 | } 26 | 27 | void globalValueNumbering(map *constValue, map *copyValue, 28 | map*>* previous, 29 | map*>* cur) override {} 30 | 31 | void assembly(ofstream*, Function*) override {} 32 | }; 33 | #endif //SYSYCOMPILER_CREATEPOINTER_H 34 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/FuncCall.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/29. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_FUNCCALL_H 6 | #define SYSYCOMPILER_FUNCCALL_H 7 | #include "Quaternion.h" 8 | 9 | class FuncCall : public Quaternion { 10 | string name; 11 | public: 12 | explicit FuncCall(string *name) : Quaternion(IRKind::FUNC_CALL), 13 | name(*name) {} 14 | 15 | void print(ofstream *out) override { 16 | *out << "funcCall " << this->name << endl; 17 | } 18 | 19 | void globalValueNumbering(map *constValue, map *copyValue, 20 | map*>* previous, 21 | map*>* cur) override {} 22 | 23 | void assembly(ofstream *out, Function*) override { 24 | *out << "jal " << name << endl; 25 | } 26 | }; 27 | 28 | #endif //SYSYCOMPILER_FUNCCALL_H 29 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/GetReturn.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/11/16. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_GETRETURN_H 6 | #define SYSYCOMPILER_GETRETURN_H 7 | 8 | class GetReturn : public Quaternion { 9 | string assigned; 10 | public: 11 | explicit GetReturn(string *assigned) : Quaternion(IRKind::GET_RETURN), assigned(*assigned) {} 12 | 13 | string* getDefine() override { 14 | return &assigned; 15 | } 16 | 17 | void print(ofstream *out) override { 18 | *out << "getReturn " << assigned << endl; 19 | } 20 | 21 | void globalValueNumbering(map *constValue, map *copyValue, 22 | map*>* previous, 23 | map*>* cur) override {} 24 | 25 | void assembly(ofstream *out, Function*) override { 26 | if (assigned[0] == '$') { 27 | *out << "move " << assigned << ", $v0" << endl; 28 | } else { 29 | *out << "sw $v0, " << assigned.substr(2) << "($sp)" << endl; 30 | } 31 | } 32 | }; 33 | #endif //SYSYCOMPILER_GETRETURN_H 34 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/Getint.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/11/6. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_GETINT_H 6 | #define SYSYCOMPILER_GETINT_H 7 | class Getint : public Quaternion { 8 | string assigned; 9 | public: 10 | explicit Getint(string *assigned) : Quaternion(IRKind::GETINT), 11 | assigned(*assigned) {} 12 | 13 | void print(ofstream *out) override { 14 | *out << "getInt " << assigned << endl; 15 | } 16 | 17 | string* getDefine() override { 18 | return &assigned; 19 | } 20 | 21 | void globalValueNumbering(map *constValue, map *copyValue, 22 | map*>* previous, 23 | map*>* cur) override {} 24 | 25 | void assembly(ofstream *out, Function*) override { 26 | if (assigned[0] == '$') { 27 | *out << "move " << assigned << ", $v0" << endl; 28 | } else { 29 | *out << "sw $v0," << assigned.substr(2) << "($sp)" << endl; 30 | } 31 | } 32 | }; 33 | #endif //SYSYCOMPILER_GETINT_H 34 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/GreaterOrEqual.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/29. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_GREATEROREQUAL_H 6 | #define SYSYCOMPILER_GREATEROREQUAL_H 7 | 8 | class GreaterOrEqual : public Binary { 9 | public: 10 | GreaterOrEqual(string *assigned, string *integer1, string *integer2) : 11 | Binary(IRKind::GE_BIN, assigned, integer1, integer2) {} 12 | 13 | void print(ofstream *out) override { 14 | *out << "greatEqual " << assigned << " " << integer1 << " " << integer2 << endl; 15 | } 16 | 17 | void globalValueNumbering(map *constValue, map *copyValue, 18 | map*>* previous, 19 | map*>* cur) override { 20 | dealUsing(constValue, copyValue); 21 | if (exprResIsNumber(&integer1) && exprResIsNumber(&integer2)) { 22 | (*constValue)[assigned] = stoi(integer1) >= stoi(integer2) ? 1 : 0; 23 | return; 24 | } else if (integer1 == integer2) { 25 | (*constValue)[assigned] = 1; 26 | return; 27 | } 28 | HashOfQuaternion hash(integer1, integer2); 29 | if ((*previous)[IRKind::GE_BIN]->count(hash)) { 30 | (*copyValue)[assigned] = (*(*previous)[IRKind::GE_BIN])[hash]; 31 | } else { 32 | (*(*previous)[IRKind::GE_BIN])[hash] = assigned; 33 | (*cur)[IRKind::GE_BIN]->insert(hash); 34 | } 35 | } 36 | 37 | void assembly(ofstream *out, Function*) override { 38 | if (exprResIsNumber(&integer1)) { 39 | *out << "li $27, " + integer1 << endl; 40 | integer1 = "$27"; 41 | } else if (integer1[0] != '$') { 42 | *out << "lw $27, " + integer1.substr(2) + "($sp)" << endl; 43 | integer1 = "$27"; 44 | } 45 | if (!exprResIsNumber(&integer2) && integer2[0] != '$') { 46 | *out << "lw $28, " + integer2.substr(2) << "($sp)" << endl; 47 | integer2 = "$28"; 48 | } 49 | if (assigned[0] == '$') { 50 | *out << "sge " << assigned << ", " << integer1 << ", " << integer2 << endl; 51 | } else { 52 | *out << "sge $27, " << integer1 << ", " << integer2 << endl; 53 | *out << "sw $27," << assigned.substr(2) << "($sp)" << endl; 54 | } 55 | } 56 | }; 57 | #endif //SYSYCOMPILER_GREATEROREQUAL_H 58 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/GreaterThan.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/29. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_GREATERTHAN_H 6 | #define SYSYCOMPILER_GREATERTHAN_H 7 | class GreaterThan : public Binary { 8 | public: 9 | GreaterThan(string *assigned, string *integer1, string *integer2) : 10 | Binary(IRKind::GT_BIN, assigned, integer1, integer2) {} 11 | 12 | void print(ofstream *out) override { 13 | *out << "greatThan " << assigned << " " << integer1 << " " << integer2 << endl; 14 | } 15 | 16 | void globalValueNumbering(map *constValue, map *copyValue, 17 | map*>* previous, 18 | map*>* cur) override { 19 | if (dealUsing(constValue, copyValue)) { 20 | return; 21 | } 22 | if (exprResIsNumber(&integer1) && exprResIsNumber(&integer2)) { 23 | (*constValue)[assigned] = stoi(integer1) > stoi(integer2) ? 1 : 0; 24 | return; 25 | } else if (integer1 == integer2) { 26 | (*constValue)[assigned] = 0; 27 | return; 28 | } 29 | HashOfQuaternion hash(integer1, integer2); 30 | if ((*previous)[IRKind::GT_BIN]->count(hash)) { 31 | (*copyValue)[assigned] = (*(*previous)[IRKind::GT_BIN])[hash]; 32 | } else { 33 | (*(*previous)[IRKind::GT_BIN])[hash] = assigned; 34 | (*cur)[IRKind::GT_BIN]->insert(hash); 35 | } 36 | } 37 | 38 | void assembly(ofstream *out, Function*) override { 39 | if (exprResIsNumber(&integer1)) { 40 | *out << "li $27, " + integer1 << endl; 41 | integer1 = "$27"; 42 | } else if (integer1[0] != '$') { 43 | *out << "lw $27, " + integer1.substr(2) + "($sp)" << endl; 44 | integer1 = "$27"; 45 | } 46 | if (!exprResIsNumber(&integer2) && integer2[0] != '$') { 47 | *out << "lw $28, " + integer2.substr(2) << "($sp)" << endl; 48 | integer2 = "$28"; 49 | } 50 | if (assigned[0] == '$') { 51 | *out << "sgt " << assigned << ", " << integer1 << ", " << integer2 << endl; 52 | } else { 53 | *out << "sgt $27, " << integer1 << ", " << integer2 << endl; 54 | *out << "sw $27," << assigned.substr(2) << "($sp)" << endl; 55 | } 56 | } 57 | }; 58 | #endif //SYSYCOMPILER_GREATERTHAN_H 59 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/Jump.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/30. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_JUMP_H 6 | #define SYSYCOMPILER_JUMP_H 7 | 8 | class Jump : public Quaternion { 9 | BasicBlock *basicBlock; 10 | public: 11 | explicit Jump(BasicBlock *basicBlock) : Quaternion(IRKind::JUMP), basicBlock(basicBlock) {} 12 | 13 | BasicBlock* getJumpBlock() override { 14 | return basicBlock; 15 | } 16 | 17 | void setJumpBlock(BasicBlock *block) override { 18 | this->basicBlock = block; 19 | } 20 | 21 | void print(ofstream *out) override { 22 | *out << "jump " << *basicBlock->getTag() << endl; 23 | } 24 | 25 | void globalValueNumbering(map *constValue, map *copyValue, 26 | map*>* previous, 27 | map*>* cur) override {} 28 | 29 | void assembly(ofstream *out, Function*) override { 30 | *out << "j " << *basicBlock->getTag() << endl; 31 | } 32 | }; 33 | #endif //SYSYCOMPILER_JUMP_H 34 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/JumpWhenFalse.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/29. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_JUMPWHENFALSE_H 6 | #define SYSYCOMPILER_JUMPWHENFALSE_H 7 | class JumpWhenFalse : public Quaternion { 8 | BasicBlock *basicBlock; 9 | string integer; 10 | public: 11 | JumpWhenFalse(BasicBlock *basicBlock, string *integer) : Quaternion(IRKind::JUMP_FALSE), 12 | basicBlock(basicBlock), integer(*integer) {} 13 | 14 | BasicBlock* getJumpBlock() override { 15 | return basicBlock; 16 | } 17 | 18 | void setJumpBlock(BasicBlock *block) override { 19 | this->basicBlock = block; 20 | } 21 | 22 | void print(ofstream *out) override { 23 | *out << "jumpWhenFalse " << *basicBlock->getTag() << " " << integer << endl; 24 | } 25 | 26 | set* getUse() override { 27 | auto ret = new set; 28 | ret->insert(&integer); 29 | return ret; 30 | } 31 | 32 | void globalValueNumbering(map *constValue, map *copyValue, 33 | map*>* previous, 34 | map*>* cur) override { 35 | if (constValue->count(integer)) { 36 | integer = to_string((*constValue)[integer]); 37 | } else if (copyValue->count(integer)) { 38 | integer = (*copyValue)[integer]; 39 | } 40 | } 41 | 42 | void assembly(ofstream *out, Function*) override { 43 | if (exprResIsNumber(&integer)) { 44 | if (stoi(integer) == 0) { 45 | *out << "j " << *basicBlock->getTag() << endl; 46 | } 47 | return; 48 | } else if (integer[0] != '$') { 49 | *out << "lw $27, " << integer.substr(2) << "($sp)" << endl; 50 | integer = "$27"; 51 | } 52 | *out << "beqz " << integer << ", " << *basicBlock->getTag() << endl; 53 | } 54 | }; 55 | #endif //SYSYCOMPILER_JUMPWHENFALSE_H 56 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/JumpWhenTrue.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/29. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_JUMPWHENTRUE_H 6 | #define SYSYCOMPILER_JUMPWHENTRUE_H 7 | class JumpWhenTrue : public Quaternion { 8 | BasicBlock *basicBlock; 9 | string integer; 10 | public: 11 | JumpWhenTrue(BasicBlock *basicBlock, string *integer) : Quaternion(IRKind::JUMP_TRUE), 12 | basicBlock(basicBlock), integer(*integer) {} 13 | 14 | void print(ofstream *out) override { 15 | *out << "jumpWhenTrue " << *basicBlock->getTag() << " " << integer << endl; 16 | } 17 | 18 | BasicBlock* getJumpBlock() override { 19 | return basicBlock; 20 | } 21 | 22 | void setJumpBlock(BasicBlock *block) override { 23 | this->basicBlock = block; 24 | } 25 | 26 | set* getUse() override { 27 | auto ret = new set; 28 | ret->insert(&integer); 29 | return ret; 30 | } 31 | 32 | void globalValueNumbering(map *constValue, map *copyValue, 33 | map*>* previous, 34 | map*>* cur) override { 35 | if (constValue->count(integer)) { 36 | integer = to_string((*constValue)[integer]); 37 | } else if (copyValue->count(integer)) { 38 | integer = (*copyValue)[integer]; 39 | } 40 | } 41 | 42 | void assembly(ofstream *out, Function*) override { 43 | if (exprResIsNumber(&integer)) { 44 | if (stoi(integer) != 0) { 45 | *out << "j " << *basicBlock->getTag() << endl; 46 | } 47 | return; 48 | } else if (integer[0] != '$') { 49 | *out << "lw $27, " << integer.substr(2) << "($sp)" << endl; 50 | integer = "$27"; 51 | } 52 | *out << "bnez " << integer << ", " << *basicBlock->getTag() << endl; 53 | } 54 | }; 55 | #endif //SYSYCOMPILER_JUMPWHENTRUE_H 56 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/LessOrEqual.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/29. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_LESSOREQUAL_H 6 | #define SYSYCOMPILER_LESSOREQUAL_H 7 | 8 | class LessOrEqual : public Binary { 9 | public: 10 | LessOrEqual(string *assigned, string *integer1, string *integer2) : 11 | Binary(IRKind::LE_BIN, assigned, integer1, integer2) {} 12 | 13 | void print(ofstream *out) override { 14 | *out << "lessEqual " << assigned << " " << integer1 << " " << integer2 << endl; 15 | } 16 | 17 | void globalValueNumbering(map *constValue, map *copyValue, 18 | map*>* previous, 19 | map*>* cur) override { 20 | if (dealUsing(constValue, copyValue)) { 21 | return; 22 | } 23 | if (exprResIsNumber(&integer1) && exprResIsNumber(&integer2)) { 24 | (*constValue)[assigned] = stoi(integer1) <= stoi(integer2) ? 1 : 0; 25 | return; 26 | } else if (integer1 == integer2) { 27 | (*constValue)[assigned] = 1; 28 | return; 29 | } 30 | HashOfQuaternion hash(integer1, integer2); 31 | if ((*previous)[IRKind::LE_BIN]->count(hash)) { 32 | (*copyValue)[assigned] = (*(*previous)[IRKind::LE_BIN])[hash]; 33 | } else { 34 | (*(*previous)[IRKind::LE_BIN])[hash] = assigned; 35 | (*cur)[IRKind::LE_BIN]->insert(hash); 36 | } 37 | } 38 | 39 | void assembly(ofstream *out, Function*) override { 40 | if (exprResIsNumber(&integer1)) { 41 | *out << "li $27, " + integer1 << endl; 42 | integer1 = "$27"; 43 | } else if (integer1[0] != '$') { 44 | *out << "lw $27, " + integer1.substr(2) + "($sp)" << endl; 45 | integer1 = "$27"; 46 | } 47 | if (!exprResIsNumber(&integer2) && integer2[0] != '$') { 48 | *out << "lw $28, " + integer2.substr(2) << "($sp)" << endl; 49 | integer2 = "$28"; 50 | } 51 | if (assigned[0] == '$') { 52 | *out << "sle " << assigned << ", " << integer1 << ", " << integer2 << endl; 53 | } else { 54 | *out << "sle $27, " << integer1 << ", " << integer2 << endl; 55 | *out << "sw $27," << assigned.substr(2) << "($sp)" << endl; 56 | } 57 | } 58 | }; 59 | #endif //SYSYCOMPILER_LESSOREQUAL_H 60 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/LoadParam.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/11/15. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_LOADPARAM_H 6 | #define SYSYCOMPILER_LOADPARAM_H 7 | class LoadParam : public Quaternion { 8 | string integer; 9 | int num; 10 | public: 11 | LoadParam(string *param, int num) : Quaternion(IRKind::LOAD_PARAM), 12 | integer(*param), num(num) {} 13 | 14 | string* getDefine() override { 15 | return &integer; 16 | } 17 | 18 | void print(ofstream *out) override { 19 | *out << "pop " << integer << " " << num << endl; 20 | } 21 | 22 | void globalValueNumbering(map *constValue, map *copyValue, 23 | map*>* previous, 24 | map*>* cur) override {} 25 | 26 | void assembly(ofstream *out, Function *function) override; 27 | }; 28 | #endif //SYSYCOMPILER_LOADPARAM_H 29 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/Negate.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/29. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_NEGATE_H 6 | #define SYSYCOMPILER_NEGATE_H 7 | class Negate : public Quaternion { 8 | string assigned; 9 | string integer; 10 | public: 11 | Negate(string *assigned, string *integer) : Quaternion(IRKind::NEGATE_UNI), 12 | assigned(*assigned), integer(*integer) {} 13 | 14 | void print(ofstream *out) override { 15 | *out << "negate " << assigned << " " << integer << endl; 16 | } 17 | 18 | string* getDefine() override { 19 | return &assigned; 20 | } 21 | 22 | set* getUse() override { 23 | auto ret = new set; 24 | ret->insert(&integer); 25 | return ret; 26 | } 27 | 28 | void globalValueNumbering(map *constValue, map *copyValue, 29 | map*>* previous, 30 | map*>* cur) override { 31 | if (constValue->count(integer)) { 32 | integer = to_string((*constValue)[integer]); 33 | } else if (copyValue->count(integer)) { 34 | integer = (*copyValue)[integer]; 35 | } 36 | bool integerIsConst = exprResIsNumber(&integer); 37 | if (integerIsConst) { 38 | (*constValue)[assigned] = -stoi(integer); 39 | return; 40 | } 41 | HashOfQuaternion hash(integer, ""); 42 | if ((*previous)[IRKind::NEGATE_UNI]->count(hash)) { 43 | (*copyValue)[assigned] = (*(*previous)[IRKind::NEGATE_UNI])[hash]; 44 | } else { 45 | (*(*previous)[IRKind::NEGATE_UNI])[hash] = assigned; 46 | (*cur)[IRKind::NEGATE_UNI]->insert(hash); 47 | } 48 | } 49 | 50 | void assembly(ofstream *out, Function*) override { 51 | if (integer[0] != '$') { 52 | *out << "lw $27, " << integer.substr(2) << "($sp)" << endl; 53 | integer = "$27"; 54 | } 55 | if (assigned[0] == '$') { 56 | *out << "subu " << assigned << ", $0, " << integer << endl; 57 | } else { 58 | *out << "subu $27, $0, "<< integer << endl; 59 | *out << "sw $27, " << assigned.substr(2) << "($sp)" << endl; 60 | } 61 | } 62 | }; 63 | 64 | #endif //SYSYCOMPILER_NEGATE_H 65 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/NotLogic.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/29. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_NOTLOGIC_H 6 | #define SYSYCOMPILER_NOTLOGIC_H 7 | class NotLogic : public Quaternion { 8 | string assigned; 9 | string integer; 10 | public: 11 | NotLogic(string *assigned, string *integer) : Quaternion(IRKind::NOT_UNI), 12 | assigned(*assigned), integer(*integer) {} 13 | 14 | void print(ofstream *out) override { 15 | *out << "not " << assigned << " " << integer << endl; 16 | } 17 | 18 | string* getDefine() override { 19 | return &assigned; 20 | } 21 | 22 | set* getUse() override { 23 | auto ret = new set; 24 | ret->insert(&integer); 25 | return ret; 26 | } 27 | 28 | void globalValueNumbering(map *constValue, map *copyValue, 29 | map*>* previous, 30 | map*>* cur) override { 31 | if (constValue->count(integer)) { 32 | integer = to_string((*constValue)[integer]); 33 | } else if (copyValue->count(integer)) { 34 | integer = (*copyValue)[integer]; 35 | } 36 | bool integerIsConst = exprResIsNumber(&integer); 37 | if (integerIsConst) { 38 | (*constValue)[assigned] = stoi(integer) == 0 ? 1 : 0; 39 | return; 40 | } 41 | HashOfQuaternion hash(integer, ""); 42 | if ((*previous)[IRKind::NOT_UNI]->count(hash)) { 43 | (*copyValue)[assigned] = (*(*previous)[IRKind::NOT_UNI])[hash]; 44 | } else { 45 | (*(*previous)[IRKind::NOT_UNI])[hash] = assigned; 46 | (*cur)[IRKind::NOT_UNI]->insert(hash); 47 | } 48 | } 49 | 50 | void assembly(ofstream *out, Function*) override { 51 | if (integer[0] != '$') { 52 | *out << "lw $27, " << integer.substr(2) << "($sp)" << endl; 53 | integer = "$27"; 54 | } 55 | if (assigned[0] == '$') { 56 | *out << "seq " << assigned << ", $0, " << integer << endl; 57 | } else { 58 | *out << "seq $27, $0, "<< integer << endl; 59 | *out << "sw $27, " << assigned.substr(2) << "($sp)" << endl; 60 | } 61 | } 62 | }; 63 | 64 | #endif //SYSYCOMPILER_NOTLOGIC_H 65 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/PhiFunction.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/11/14. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_PHIFUNCTION_H 6 | #define SYSYCOMPILER_PHIFUNCTION_H 7 | 8 | class PhiFunction : public Quaternion { 9 | typedef pair PhiItem; 10 | vector items; 11 | string assigned; 12 | string original; 13 | public: 14 | explicit PhiFunction(const string *assigned) : Quaternion(IRKind::PHI), 15 | assigned(*assigned), original(*assigned) {} 16 | 17 | string* getOriginal() { 18 | return &original; 19 | } 20 | 21 | string* getDefine() override { 22 | return &assigned; 23 | } 24 | 25 | set* getUse() override { 26 | auto ret = new set; 27 | for (auto & i : items) { 28 | ret->insert(&i.first); 29 | } 30 | return ret; 31 | } 32 | 33 | void print(ofstream *out) override; 34 | 35 | void addItem(string *name, BasicBlock *basicBlock) { 36 | items.emplace_back(*name, basicBlock); 37 | } 38 | 39 | string* getItem(BasicBlock *basicBlock) { 40 | for (auto & i : items) { 41 | if (basicBlock == i.second) { 42 | return &i.first; 43 | } 44 | } 45 | return nullptr; 46 | } 47 | 48 | void globalValueNumbering(map *constValue, map *copyValue, 49 | map*>* previous, 50 | map*>* cur) override { 51 | for (auto & i : items) { 52 | if (constValue->count(i.first)) { 53 | i.first = to_string((*constValue)[i.first]); 54 | } else if (copyValue->count(i.first)) { 55 | i.first = (*copyValue)[i.first]; 56 | } 57 | } 58 | } 59 | 60 | void assembly(ofstream *out, Function*) override { 61 | *out << "ERROR, use phi to assembly" << endl; 62 | } 63 | }; 64 | #endif //SYSYCOMPILER_PHIFUNCTION_H 65 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/PrintInteger.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/27. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_PRINTINTEGER_H 6 | #define SYSYCOMPILER_PRINTINTEGER_H 7 | class PrintInteger : public Quaternion { 8 | string integer; 9 | public: 10 | explicit PrintInteger(string *integer) : Quaternion(IRKind::PRINT_INT), 11 | integer(*integer) {} 12 | 13 | void print(ofstream *out) override { 14 | *out << "printInteger " << integer << endl; 15 | } 16 | 17 | set* getUse() override { 18 | auto ret = new set; 19 | ret->insert(&integer); 20 | return ret; 21 | } 22 | 23 | void globalValueNumbering(map *constValue, map *copyValue, 24 | map*>* previous, 25 | map*>* cur) override { 26 | if (constValue->count(integer)) { 27 | integer = to_string((*constValue)[integer]); 28 | } else if (copyValue->count(integer)) { 29 | integer = (*copyValue)[integer]; 30 | } 31 | } 32 | 33 | void assembly(ofstream *out, Function*) override { 34 | if (integer[0] == '$') { 35 | *out << "move $a0," << integer << endl; 36 | } else if (integer[0] == 's') { 37 | *out << "lw $a0, " << integer.substr(2) << "($sp)" << endl; 38 | } else { 39 | *out << "li $a0, " << integer << endl; 40 | } 41 | *out << "li $v0, 1" << endl; 42 | *out << "syscall" << endl; 43 | } 44 | }; 45 | #endif //SYSYCOMPILER_PRINTINTEGER_H 46 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/PrintString.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/27. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_PRINTSTRING_H 6 | #define SYSYCOMPILER_PRINTSTRING_H 7 | 8 | class PrintString : public Quaternion { 9 | string str; 10 | public: 11 | explicit PrintString(string *str) : Quaternion(IRKind::PRINT_STR), 12 | str(move(*str)) {} 13 | 14 | void print(ofstream *out) override { 15 | *out << "printString " << str << endl; 16 | } 17 | 18 | void globalValueNumbering(map *constValue, map *copyValue, 19 | map*>* previous, 20 | map*>* cur) override {} 21 | 22 | void assembly(ofstream *out, Function*) override { 23 | *out << "la $a0, " << str << endl; 24 | *out << "li $v0, 4" << endl; 25 | *out << "syscall" << endl; 26 | } 27 | }; 28 | #endif //SYSYCOMPILER_PRINTSTRING_H 29 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/PushParam.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/11/15. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_PUSHPARAM_H 6 | #define SYSYCOMPILER_PUSHPARAM_H 7 | class PushParam : public Quaternion { 8 | string integer; 9 | int num; 10 | public: 11 | PushParam(string *param, int num) : Quaternion(IRKind::PUSH_PARAM), 12 | integer(*param), num(num) {} 13 | 14 | set* getUse() override { 15 | auto ret = new set; 16 | ret->insert(&integer); 17 | return ret; 18 | } 19 | 20 | void print(ofstream *out) override { 21 | *out << "push " << integer << " " << num << endl; 22 | } 23 | 24 | void globalValueNumbering(map *constValue, map *copyValue, 25 | map*>* previous, 26 | map*>* cur) override { 27 | if (constValue->count(integer)) { 28 | integer = to_string((*constValue)[integer]); 29 | } else if (copyValue->count(integer)) { 30 | integer = (*copyValue)[integer]; 31 | } 32 | } 33 | 34 | void assembly(ofstream *out, Function*) override { 35 | if (num < 4) { 36 | if (integer[0] == '$') { 37 | *out << "move " << "$a" << num << ", " << integer << endl; 38 | } else if (integer[0] == 's') { 39 | *out << "lw $a" << num << ", " << integer.substr(2) << "($sp)" << endl; 40 | } else if (integer[0] == 'a') { 41 | *out << "addi $a" << num << ", $sp, " << integer.substr(5) << endl; 42 | } else { 43 | *out << "li $a" << num << ", " << integer << endl; 44 | } 45 | } else { 46 | if (integer[0] == '$') { 47 | *out << "sw " << integer << ", " << -(num - 3) * 4 << "($sp)" << endl; 48 | } else { 49 | if (integer[0] == 's') { 50 | *out << "lw $27, " << integer.substr(2) << "($sp)" << endl; 51 | } else if (integer[0] == 'a') { 52 | *out << "addi $27" << num << ", $sp, " << integer.substr(5) << endl; 53 | } else { 54 | *out << "li $27, " << integer << endl; 55 | } 56 | *out << "sw $27, " << -(num - 3) * 4 << "($sp)" << endl; 57 | } 58 | } 59 | } 60 | }; 61 | #endif //SYSYCOMPILER_PUSHPARAM_H 62 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/ReadInt.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/11/16. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_READINT_H 6 | #define SYSYCOMPILER_READINT_H 7 | 8 | class ReadInt : public Quaternion { 9 | public: 10 | ReadInt() : Quaternion(IRKind::READ_INT) {} 11 | 12 | void print(ofstream *out) override { 13 | *out << "readInt" << endl; 14 | } 15 | 16 | void globalValueNumbering(map *constValue, map *copyValue, 17 | map*>* previous, 18 | map*>* cur) override {} 19 | 20 | void assembly(ofstream *out, Function*) override { 21 | *out << "li $v0, 5" << endl; 22 | *out << "syscall" << endl; 23 | } 24 | }; 25 | #endif //SYSYCOMPILER_READINT_H 26 | -------------------------------------------------------------------------------- /control-flow-graphs/Quaternion/Return.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/31. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_RETURN_H 6 | #define SYSYCOMPILER_RETURN_H 7 | class Return : public Quaternion { 8 | string integer; 9 | public: 10 | explicit Return(string *integer) : Quaternion(IRKind::RETURN), 11 | integer(*integer) {} 12 | 13 | void print(ofstream *out) override { 14 | *out << "return " << integer << endl; 15 | } 16 | 17 | void globalValueNumbering(map *constValue, map *copyValue, 18 | map*>* previous, 19 | map*>* cur) override { 20 | if (constValue->count(integer)) { 21 | integer = to_string((*constValue)[integer]); 22 | } else if (copyValue->count(integer)) { 23 | integer = (*copyValue)[integer]; 24 | } 25 | } 26 | 27 | set* getUse() override { 28 | auto ret = new set(); 29 | ret->insert(&integer); 30 | return ret; 31 | } 32 | 33 | void assembly(ofstream *out, Function *function) override; 34 | }; 35 | #endif //SYSYCOMPILER_RETURN_H 36 | -------------------------------------------------------------------------------- /control-flow-graphs/RegisterAllocate.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/11/18. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_REGISTERALLOCATE_H 6 | #define SYSYCOMPILER_REGISTERALLOCATE_H 7 | class RegisterAllocate { 8 | const static int registerNumber = 18; 9 | public: 10 | static int allocateRegister(map *weight, map *cur, string *var) { 11 | bool vis[registerNumber]; 12 | memset(vis, 0, sizeof vis); 13 | for (auto & i : *cur) { 14 | vis[i.first - 8] = true; 15 | } 16 | for (int i = 0; i < registerNumber; i++) { 17 | if (!vis[i]) { 18 | return i + 8; 19 | } 20 | } 21 | int mn = (*weight)[*var]; 22 | for (auto & i : *cur) { 23 | if ((*weight)[i.second] < mn) { 24 | mn = (*weight)[i.second]; 25 | } 26 | } 27 | for (auto & i : *cur) { 28 | if ((*weight)[*var] == mn) { 29 | return i.first; 30 | } 31 | } 32 | return -1; 33 | } 34 | }; 35 | #endif //SYSYCOMPILER_REGISTERALLOCATE_H 36 | -------------------------------------------------------------------------------- /control-flow-graphs/lib.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/25. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_CFG_LIB_H 6 | #define SYSYCOMPILER_CFG_LIB_H 7 | #include "Quaternion/Quaternion.h" 8 | #include "Quaternion/PhiFunction.h" 9 | #include "Quaternion/Binary.h" 10 | #include "RegisterAllocate.h" 11 | #include "BasicBlock.h" 12 | #include "Quaternion/NotEqual.h" 13 | #include "Quaternion/Equal.h" 14 | #include "Quaternion/LessThan.h" 15 | #include "Quaternion/GreaterThan.h" 16 | #include "Quaternion/LessOrEqual.h" 17 | #include "Quaternion/GreaterOrEqual.h" 18 | #include "Quaternion/Plus.h" 19 | #include "Quaternion/Minus.h" 20 | #include "Quaternion/Multiply.h" 21 | #include "Quaternion/Divide.h" 22 | #include "Quaternion/Mod.h" 23 | #include "Quaternion/FuncCall.h" 24 | #include "Quaternion/Jump.h" 25 | #include "Quaternion/JumpWhenTrue.h" 26 | #include "Quaternion/JumpWhenFalse.h" 27 | #include "Quaternion/NotLogic.h" 28 | #include "Quaternion/Negate.h" 29 | #include "Quaternion/StoreWord.h" 30 | #include "Quaternion/LoadWord.h" 31 | #include "Quaternion/LoadPointer.h" 32 | #include "Quaternion/CreatePointer.h" 33 | #include "Quaternion/Assign.h" 34 | #include "Quaternion/PrintString.h" 35 | #include "Quaternion/PrintInteger.h" 36 | #include "Quaternion/Return.h" 37 | #include "Quaternion/Getint.h" 38 | #include "Quaternion/ReadInt.h" 39 | #include "Quaternion/GetReturn.h" 40 | #include "Quaternion/PushParam.h" 41 | #include "Quaternion/LoadParam.h" 42 | #include "ControlFlowGraphBuilder.h" 43 | #endif //SYSYCOMPILER_CFG_LIB_H 44 | -------------------------------------------------------------------------------- /lexer/Cursor.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/9/23. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_CURSOR_H 6 | #define SYSYCOMPILER_CURSOR_H 7 | #include "Source.h" 8 | using namespace std; 9 | 10 | class Cursor { 11 | string::iterator now; 12 | const string::iterator begin; 13 | const string::iterator end; 14 | 15 | public: 16 | explicit Cursor(Source *content) : begin(content->getBegin()), end(content->getEnd()) { 17 | now = begin; 18 | } 19 | 20 | char previous() { 21 | if (now == begin) { 22 | return EOF; 23 | } else { 24 | return *(now - 1); 25 | } 26 | } 27 | 28 | char n_thChar(int n) { 29 | if (now + n >= end) { 30 | return EOF; 31 | } else { 32 | return *(now + n); 33 | } 34 | } 35 | 36 | char first() { 37 | return n_thChar(0); 38 | } 39 | 40 | bool isEOF() { 41 | return now >= end; 42 | } 43 | 44 | void bump() { 45 | if (now < end) { 46 | now++; 47 | } 48 | } 49 | 50 | void eatWhile(bool f(char)) { 51 | while (!isEOF() && f(first())) { 52 | bump(); 53 | } 54 | } 55 | 56 | int lengthConsumed() { 57 | return int(now - begin); 58 | } 59 | }; 60 | 61 | #endif //SYSYCOMPILER_CURSOR_H 62 | -------------------------------------------------------------------------------- /lexer/LexerError.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/9/25. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_LEXERERROR_H 6 | #define SYSYCOMPILER_LEXERERROR_H 7 | #include "Token.h" 8 | 9 | class LexerError { 10 | int lineNum; 11 | public: 12 | explicit LexerError(Token* token) { 13 | lineNum = token->line; 14 | } 15 | void printError() const { 16 | printf("Lexer Error on line %d\n", lineNum); 17 | } 18 | }; 19 | #endif //SYSYCOMPILER_LEXERERROR_H 20 | -------------------------------------------------------------------------------- /lexer/Source.h: -------------------------------------------------------------------------------- 1 | #ifndef SYSYCOMPILER_SOURCE_H 2 | #define SYSYCOMPILER_SOURCE_H 3 | #include 4 | using namespace std; 5 | 6 | class Source { 7 | string sources; 8 | vectorline; 9 | public: 10 | explicit Source(string *fileName) { 11 | sources = ""; 12 | line.clear(); 13 | ifstream myFile(*fileName); 14 | if (!myFile.is_open()) { 15 | cout << "Can't open" << endl; 16 | } else { 17 | string tmp; 18 | line.push_back(0); 19 | while (getline(myFile, tmp)) { 20 | sources += tmp; 21 | sources += '\n'; 22 | line.push_back(int(sources.length())); 23 | } 24 | } 25 | myFile.close(); 26 | } 27 | 28 | int getLine(int num) { 29 | int ret; 30 | for (int i = 0; i < line.size(); i++) { 31 | if (num < line[i]) { 32 | ret = i; 33 | break; 34 | } 35 | } 36 | return ret; 37 | } 38 | 39 | string::iterator getBegin() { 40 | return sources.begin(); 41 | } 42 | 43 | string::iterator getEnd() { 44 | return sources.end(); 45 | } 46 | }; 47 | 48 | 49 | #endif //SYSYCOMPILER_SOURCE_H 50 | -------------------------------------------------------------------------------- /lexer/Token.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/9/25. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_TOKEN_H 6 | #define SYSYCOMPILER_TOKEN_H 7 | #include "SyntaxKind.h" 8 | #include "Source.h" 9 | 10 | Source *source; 11 | class Token { 12 | public: 13 | SyntaxKind kind; 14 | string content; 15 | int line; 16 | Token(SyntaxKind kind, int start, int len) : kind(kind) { 17 | content = ""; 18 | for (int i = 0; i < len; i++) { 19 | content += *(source->getBegin() + start + i); 20 | } 21 | line = source->getLine(start); 22 | } 23 | 24 | bool isEOF() const { 25 | return kind == SyntaxKind::EOF_CHAR; 26 | } 27 | 28 | bool isError() const { 29 | return kind == SyntaxKind::ERROR; 30 | } 31 | }; 32 | #endif //SYSYCOMPILER_TOKEN_H 33 | -------------------------------------------------------------------------------- /lexer/TokenSource.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/9/28. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_TOKENSOURCE_H 6 | #define SYSYCOMPILER_TOKENSOURCE_H 7 | #include "Token.h" 8 | class TokenSource { 9 | vector tokens; 10 | vector::iterator cur; 11 | 12 | public: 13 | explicit TokenSource(vector *tokens) { 14 | this->tokens = *tokens; 15 | cur = this->tokens.begin(); 16 | } 17 | 18 | Token* first() { 19 | return nthToken(0); 20 | } 21 | 22 | Token* nthToken(int n) { 23 | if (cur + n >= tokens.end()) { 24 | return nullptr; 25 | } else { 26 | return *(cur + n); 27 | } 28 | } 29 | 30 | int lastLine() { 31 | if (cur == tokens.begin()) { 32 | return 0; 33 | } 34 | return nthToken(-1)->line; 35 | } 36 | 37 | void bump() { 38 | if (cur != tokens.end()) { 39 | cur++; 40 | } 41 | } 42 | }; 43 | #endif //SYSYCOMPILER_TOKENSOURCE_H 44 | -------------------------------------------------------------------------------- /lexer/lib.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/24. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_LEXER_LIB_H 6 | #define SYSYCOMPILER_LEXER_LIB_H 7 | #include "Cursor.h" 8 | #include "Lexer.h" 9 | #include "LexerError.h" 10 | #include "Source.h" 11 | #include "SyntaxKind.h" 12 | #include "Token.h" 13 | #include "TokenSource.h" 14 | #endif //SYSYCOMPILER_LEXER_LIB_H 15 | -------------------------------------------------------------------------------- /lib.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/9/23. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_LIB_H 6 | #define SYSYCOMPILER_LIB_H 7 | #include 8 | #define IR false 9 | #define OPT true 10 | using namespace std; 11 | 12 | bool exprResIsNumber(const string *res) { 13 | return regex_match(*res, regex("[+-]*[0-9]+")); 14 | } 15 | #include "lexer/lib.h" 16 | #include "parser/lib.h" 17 | 18 | #endif //SYSYCOMPILER_LIB_H 19 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | using namespace std; 3 | 4 | vector tokens; 5 | Node *root; 6 | int main() { 7 | ofstream outputFile("error.txt"); 8 | string inputFile = "testfile.txt"; 9 | source = new Source(&inputFile); 10 | Lexer lexer(source); 11 | tokens = lexer.tokenize(); 12 | if (lexer.hasError()) { 13 | lexer.printError(); 14 | return 0; 15 | } 16 | TokenSource tokenSource(&tokens); 17 | Parser parser(&tokenSource); 18 | root = parser.parse(); 19 | //root->print(&outputFile); 20 | root->checkError(new ErrorCheckCtx(), new ErrorCheckRet()); 21 | if (!errorList.empty()) { 22 | for (auto & i : errorList) { 23 | outputFile << i.second << " " << errorKind2Code[i.first] << endl; 24 | } 25 | outputFile.close(); 26 | return 0; 27 | } 28 | root->buildIR(new BuildIRCtx, new BuildIRRet); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /nodes/AssignStmtNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_ASSIGNSTMTNODE_H 6 | #define SYSYCOMPILER_ASSIGNSTMTNODE_H 7 | #include "ExprNode.h" 8 | using namespace std; 9 | 10 | class AssignStmtNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | if (children[0]->getKind() == SyntaxKind::EXPR) { 14 | ((ExprNode*)children[0])->print(out, ExprType::L_VAL); 15 | } 16 | for (int i = 1; i < children.size(); i++) { 17 | children[i]->print(out); 18 | } 19 | } 20 | 21 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 22 | if (children[0]->getKind() == SyntaxKind::EXPR) { 23 | ctx->isLVal = true; 24 | children[0]->checkError(ctx, ret); 25 | ctx->isLVal = false; 26 | } 27 | for (int i = 1; i < children.size(); i++) { 28 | children[i]->checkError(ctx, ret); 29 | } 30 | } 31 | 32 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 33 | ctx->isLVal = true; 34 | children[0]->buildIR(ctx, ret); 35 | bool isArray = ret->isArray; 36 | string *lVal = ret->res; 37 | string *offset = ret->param; 38 | ret->param = nullptr; 39 | string *integer; 40 | bool getInt = false; 41 | for (int i = 1; i < children.size(); i++) { 42 | children[i]->buildIR(ctx, ret); 43 | if (children[i]->getKind() == SyntaxKind::EXPR) { 44 | integer = ret->res; 45 | } else if (children[i]->getKind() == SyntaxKind::GETINT_KW) { 46 | integer = new string(controlFlowGraphBuilder.tmpVar()); 47 | controlFlowGraphBuilder.insert(new ReadInt()); 48 | controlFlowGraphBuilder.insert(new Getint(integer)); 49 | getInt = true; 50 | } 51 | } 52 | if (isArray) { 53 | controlFlowGraphBuilder.insert(new StoreWord(integer, lVal, offset)); 54 | delete offset; 55 | } else { 56 | controlFlowGraphBuilder.insert(new Assign(lVal, integer)); 57 | } 58 | if (getInt) { 59 | delete integer; 60 | } 61 | } 62 | }; 63 | #endif //SYSYCOMPILER_ASSIGNSTMTNODE_H 64 | -------------------------------------------------------------------------------- /nodes/BlockNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_BLOCKNODE_H 6 | #define SYSYCOMPILER_BLOCKNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class BlockNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | children[0]->print(out); 14 | for (int i = 1; i < children.size() - 1; i++) { 15 | children[i]->print(out); 16 | } 17 | children.back()->print(out); 18 | *out << "" << endl; 19 | } 20 | 21 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 22 | if (ctx->afterFuncDef) { 23 | ctx->afterFuncDef = false; 24 | } else { 25 | symbol.startBlock(); 26 | } 27 | bool flag = false; 28 | for (auto & i : children) { 29 | if (i->getKind() == SyntaxKind::STMT) { 30 | flag = true; 31 | } 32 | i->checkError(ctx, ret); 33 | } 34 | if (!flag) { 35 | ret->isReturn = false; 36 | } 37 | symbol.finishBlock(); 38 | } 39 | 40 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 41 | if (ctx->afterFuncDef) { 42 | ctx->afterFuncDef = false; 43 | } else { 44 | symbol.startBlock(); 45 | } 46 | for (auto & i : children) { 47 | i->buildIR(ctx, ret); 48 | } 49 | symbol.finishBlock(); 50 | } 51 | }; 52 | #endif //SYSYCOMPILER_BLOCKNODE_H 53 | -------------------------------------------------------------------------------- /nodes/BreakStmt.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_BREAKSTMT_H 6 | #define SYSYCOMPILER_BREAKSTMT_H 7 | #include "Node.h" 8 | #include "TokenNode.h" 9 | using namespace std; 10 | 11 | class BreakStmtNode : public Node { 12 | public: 13 | void print(ofstream *out) override { 14 | for (auto & i : children) { 15 | i->print(out); 16 | } 17 | } 18 | 19 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 20 | for (auto & i : children) { 21 | i->checkError(ctx, ret); 22 | } 23 | if (ctx->loopNum == 0) { 24 | errorList.emplace_back(ErrorKind::BREAK_CONTINUE_OUT_LOOP, children[0]->getStartLine()); 25 | } 26 | } 27 | 28 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 29 | controlFlowGraphBuilder.insert(new Jump(ctx->breakBlock)); 30 | } 31 | }; 32 | #endif //SYSYCOMPILER_BREAKSTMT_H 33 | -------------------------------------------------------------------------------- /nodes/CompileUnitNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_COMPILEUNITNODE_H 6 | #define SYSYCOMPILER_COMPILEUNITNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class CompileUnitNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | for (auto & i : children) { 14 | i->print(out); 15 | } 16 | *out << "" << endl; 17 | } 18 | 19 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet * ret) override { 20 | symbol.startBlock(); 21 | for (auto & i : children) { 22 | i->checkError(ctx, ret); 23 | ctx->isGlobal = false; 24 | } 25 | delete ctx; 26 | delete ret; 27 | symbol.finishBlock(); 28 | symbol.clear(); 29 | } 30 | 31 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 32 | symbol.startBlock(); 33 | ctx->isGlobal = true; 34 | for (auto & i : children) { 35 | if (i->getKind() == SyntaxKind::FUNC_DEF) { 36 | ctx->isGlobal = false; 37 | } 38 | i->buildIR(ctx, ret); 39 | } 40 | if (OPT) { 41 | controlFlowGraphBuilder.simplify(); 42 | controlFlowGraphBuilder.staticSingleAssignment(); 43 | controlFlowGraphBuilder.optimize(); 44 | } 45 | if (IR) { 46 | ofstream dataFile("data.txt"); 47 | symbol.printGlobalVar(&dataFile); 48 | ofstream irFile("ir.txt"); 49 | controlFlowGraphBuilder.print(&irFile); 50 | } else { 51 | controlFlowGraphBuilder.registerAllocate(); 52 | ofstream assemblyFile("mips.txt"); 53 | assemblyFile << ".data" << endl; 54 | symbol.printGlobalVar(&assemblyFile); 55 | assemblyFile << ".text" << endl; 56 | assemblyFile << "jal func_main" << endl; 57 | assemblyFile << "ori $v0, $0, 10" << endl; 58 | assemblyFile << "syscall" << endl; 59 | controlFlowGraphBuilder.assembly(&assemblyFile); 60 | } 61 | delete ctx; 62 | delete ret; 63 | symbol.finishBlock(); 64 | } 65 | }; 66 | #endif //SYSYCOMPILER_COMPILEUNITNODE_H 67 | -------------------------------------------------------------------------------- /nodes/ContinueStmtNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_CONTINUESTMTNODE_H 6 | #define SYSYCOMPILER_CONTINUESTMTNODE_H 7 | #include "Node.h" 8 | #include "TokenNode.h" 9 | using namespace std; 10 | 11 | class ContinueStmtNode : public Node { 12 | public: 13 | void print(ofstream *out) override { 14 | for (auto & i : children) { 15 | i->print(out); 16 | } 17 | } 18 | 19 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 20 | for (auto & i : children) { 21 | i->checkError(ctx, ret); 22 | } 23 | if (ctx->loopNum == 0) { 24 | errorList.emplace_back(ErrorKind::BREAK_CONTINUE_OUT_LOOP, children[0]->getStartLine()); 25 | } 26 | } 27 | 28 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 29 | controlFlowGraphBuilder.insert(new Jump(ctx->continueBlock)); 30 | } 31 | }; 32 | #endif //SYSYCOMPILER_CONTINUESTMTNODE_H 33 | -------------------------------------------------------------------------------- /nodes/ErrorNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_ERRORNODE_H 6 | #define SYSYCOMPILER_ERRORNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class ErrorNode : public Node { 11 | ErrorKind errorKind; 12 | int line; 13 | public: 14 | ErrorNode(ErrorKind errorKind, int line) : errorKind(errorKind), line(line) {} 15 | void print(ofstream *out) override { 16 | *out << "" << endl; 17 | } 18 | 19 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 20 | errorList.emplace_back(errorKind, line); 21 | } 22 | }; 23 | #endif //SYSYCOMPILER_ERRORNODE_H 24 | -------------------------------------------------------------------------------- /nodes/ExprStmtNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_EXPRSTMTNODE_H 6 | #define SYSYCOMPILER_EXPRSTMTNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class ExprStmtNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | for (auto & i : children) { 14 | i->print(out); 15 | } 16 | } 17 | 18 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 19 | children[0]->buildIR(ctx, ret); 20 | } 21 | }; 22 | #endif //SYSYCOMPILER_EXPRSTMTNODE_H 23 | -------------------------------------------------------------------------------- /nodes/FuncArgsNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_FUNCARGSNODE_H 6 | #define SYSYCOMPILER_FUNCARGSNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class FuncArgsNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | for (auto & i : children) { 14 | i->print(out); 15 | } 16 | *out << "" << endl; 17 | } 18 | 19 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 20 | for (auto & i : children) { 21 | auto *ret0 = new ErrorCheckRet(); 22 | i->checkError(ctx, ret0); 23 | if (i->getKind() == SyntaxKind::EXPR) { 24 | ret->args.push_back(ret0->dimension); 25 | } 26 | delete ret0; 27 | } 28 | } 29 | 30 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 31 | vectorargs; 32 | for (auto & i : children) { 33 | i->buildIR(ctx, ret); 34 | if (i->getKind() == SyntaxKind::EXPR) { 35 | args.push_back(ret->res); 36 | } 37 | } 38 | ret->args = args; 39 | } 40 | }; 41 | #endif //SYSYCOMPILER_FUNCARGSNODE_H 42 | -------------------------------------------------------------------------------- /nodes/FuncParamsNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_FUNCPARAMSNODE_H 6 | #define SYSYCOMPILER_FUNCPARAMSNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class FuncParamsNode : public Node { 11 | void print(ofstream *out) override { 12 | for (auto & i : children) { 13 | i->print(out); 14 | } 15 | *out << "" << endl; 16 | } 17 | 18 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 19 | for (auto & i : children) { 20 | auto ret0 = new ErrorCheckRet; 21 | i->checkError(ctx, ret0); 22 | if (i->getKind() == SyntaxKind::FUNC_PARAM) { 23 | ret->args.push_back(ret0->dimension); 24 | } 25 | delete ret0; 26 | } 27 | } 28 | 29 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 30 | ret->clear(); 31 | vector args; 32 | for (auto & i : children) { 33 | i->buildIR(ctx, ret); 34 | if (i->getKind() == SyntaxKind::FUNC_PARAM) { 35 | args.push_back(ret->param); 36 | } 37 | } 38 | ret->args = args; 39 | } 40 | }; 41 | #endif //SYSYCOMPILER_FUNCPARAMSNODE_H 42 | -------------------------------------------------------------------------------- /nodes/FuncTypeNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_FUNCTYPENODE_H 6 | #define SYSYCOMPILER_FUNCTYPENODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class FuncTypeNode : public Node { 11 | public: 12 | void print(ofstream *out, bool isMain) { 13 | for (auto & i : children) { 14 | i->print(out); 15 | } 16 | if (!isMain) 17 | *out << "" << endl; 18 | } 19 | 20 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 21 | if (children[0]->getKind() == SyntaxKind::VOID_KW) { 22 | ret->isVoid = true; 23 | } 24 | } 25 | }; 26 | #endif //SYSYCOMPILER_FUNCTYPENODE_H 27 | -------------------------------------------------------------------------------- /nodes/IfStmtNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_IFSTMTNODE_H 6 | #define SYSYCOMPILER_IFSTMTNODE_H 7 | #include "ExprNode.h" 8 | using namespace std; 9 | 10 | class IfStmtNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | for (int i = 0; i < children.size(); i++) { 14 | if (i == 2 && children[i]->getKind() == SyntaxKind::EXPR) { 15 | ((ExprNode*)children[i])->print(out, ExprType::CONDITION); 16 | } else { 17 | children[i]->print(out); 18 | } 19 | } 20 | } 21 | 22 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 23 | BasicBlock *cfg3 = ctx->elseFinalBlock; 24 | bool needJumpElse = ctx->needJumpElse; 25 | children[2]->buildIR(ctx, ret); 26 | bool hasElse = false; 27 | for (auto & i : children) { 28 | if (i->getKind() == SyntaxKind::ELSE_KW) { 29 | hasElse = true; 30 | break; 31 | } 32 | } 33 | if (hasElse) { 34 | BasicBlock *elseCFG = controlFlowGraphBuilder.newBasicBlock(); 35 | controlFlowGraphBuilder.insert(new JumpWhenFalse(elseCFG, ret->res)); 36 | controlFlowGraphBuilder.changeCur(controlFlowGraphBuilder.newBasicBlock()); 37 | ctx->needJumpElse = true; 38 | BasicBlock *finalCFG = controlFlowGraphBuilder.newBasicBlock(); 39 | ctx->elseFinalBlock = finalCFG; 40 | children[4]->buildIR(ctx, ret); 41 | controlFlowGraphBuilder.changeCur(elseCFG); 42 | ctx->needJumpElse = false; 43 | children[6]->buildIR(ctx, ret); 44 | controlFlowGraphBuilder.changeCur(finalCFG); 45 | } else { 46 | BasicBlock *finalCFG = controlFlowGraphBuilder.newBasicBlock(); 47 | controlFlowGraphBuilder.insert(new JumpWhenFalse(finalCFG, ret->res)); 48 | controlFlowGraphBuilder.changeCur(controlFlowGraphBuilder.newBasicBlock()); 49 | ctx->needJumpElse = false; 50 | children[4]->buildIR(ctx, ret); 51 | controlFlowGraphBuilder.changeCur(finalCFG); 52 | } 53 | ctx->elseFinalBlock = cfg3; 54 | ctx->needJumpElse = needJumpElse; 55 | } 56 | }; 57 | #endif //SYSYCOMPILER_IFSTMTNODE_H 58 | -------------------------------------------------------------------------------- /nodes/InitValNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_INITVALNODE_H 6 | #define SYSYCOMPILER_INITVALNODE_H 7 | #include "ExprNode.h" 8 | using namespace std; 9 | 10 | class InitValNode : public Node { 11 | public: 12 | void print(ofstream *out, bool isConst) { 13 | for (auto & i : children) { 14 | if (i->getKind() == SyntaxKind::EXPR) { 15 | ((ExprNode*)i)->print(out, isConst ? ExprType::CONST_EXPR : ExprType::EXPR); 16 | } else if (i->getKind() == SyntaxKind::INIT_VAL) { 17 | ((InitValNode*)i)->print(out, isConst); 18 | } else { 19 | i->print(out); 20 | } 21 | } 22 | if (isConst) { 23 | *out << "" << endl; 24 | } else { 25 | *out << "" << endl; 26 | } 27 | } 28 | 29 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 30 | vector init; 31 | for (auto & i : children) { 32 | i->buildIR(ctx, ret); 33 | if (i->getKind() == SyntaxKind::EXPR) { 34 | init.push_back(ret->res); 35 | } else if (i->getKind() == SyntaxKind::INIT_VAL) { 36 | for (auto & j : ret->init) { 37 | init.push_back(j); 38 | } 39 | } 40 | } 41 | ret->init = init; 42 | } 43 | }; 44 | #endif //SYSYCOMPILER_INITVALNODE_H 45 | -------------------------------------------------------------------------------- /nodes/LiteralExprNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_LITERALEXPRNODE_H 6 | #define SYSYCOMPILER_LITERALEXPRNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class LiteralExprNode : public Node { 11 | string res; 12 | public: 13 | void print(ofstream *out) override { 14 | for (auto & i : children) { 15 | i->print(out); 16 | } 17 | *out << "" << endl; 18 | } 19 | 20 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 21 | string *content; 22 | if (children[0]->getKind() == SyntaxKind::INTEGER) { 23 | content = ((TokenNode*)children[0])->getContent(); 24 | for (auto i : *content) { 25 | ret->val = ret->val * 10 + i - '0'; 26 | } 27 | ret->isConst = true; 28 | } 29 | } 30 | 31 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 32 | this->res = *((TokenNode*)children[0])->getContent(); 33 | ret->res = &this->res; 34 | } 35 | }; 36 | #endif //SYSYCOMPILER_LITERALEXPRNODE_H 37 | -------------------------------------------------------------------------------- /nodes/Node.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/9/28. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_NODE_H 6 | #define SYSYCOMPILER_NODE_H 7 | #include "../lexer/lib.h" 8 | #include "../parser/ErrorKind.h" 9 | #include "../parser/Symbol.h" 10 | #include "../parser/Context.h" 11 | #include "../control-flow-graphs/lib.h" 12 | using namespace std; 13 | 14 | class Node { 15 | protected: 16 | int startLine; 17 | int finishLine; 18 | SyntaxKind kind; 19 | vector children; 20 | public: 21 | void setNodeElement(SyntaxKind syntaxKind, int line) { 22 | this->kind = syntaxKind; 23 | this->startLine = this->finishLine = line; 24 | } 25 | 26 | void setNodeElement(SyntaxKind syntaxKind) { 27 | this->kind = syntaxKind; 28 | this->startLine = children[0]->startLine; 29 | this->finishLine = children[children.size() - 1]->finishLine; 30 | } 31 | 32 | int getStartLine() const { 33 | return startLine; 34 | } 35 | 36 | int getFinishLine() const { 37 | return finishLine; 38 | } 39 | 40 | void addChild(Node* child) { 41 | children.insert(children.begin(), child); 42 | } 43 | 44 | SyntaxKind getKind() { 45 | return this->kind; 46 | } 47 | 48 | virtual void print(ofstream* out) { 49 | *out << syntaxKind2Name[kind] << " Use Wrong print" << endl; 50 | } 51 | 52 | virtual void checkError(ErrorCheckCtx* ctx, ErrorCheckRet *ret) { 53 | for (auto & i : children) { 54 | i->checkError(ctx, ret); 55 | } 56 | } 57 | 58 | virtual void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) {} 59 | }; 60 | 61 | #endif //SYSYCOMPILER_NODE_H -------------------------------------------------------------------------------- /nodes/PrefixOPNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_PREFIXOPNODE_H 6 | #define SYSYCOMPILER_PREFIXOPNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class PrefixOPNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | for (auto & i : children) { 14 | i->print(out); 15 | *out << "" << endl; 16 | } 17 | } 18 | int getSize() { 19 | return children.size(); 20 | } 21 | 22 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 23 | if (ctx->isConst) { 24 | int val = stoi(*ctx->res); 25 | for (int i = children.size() - 1; i >= 0; i--) { 26 | switch (children[i]->getKind()) { 27 | case SyntaxKind::NOT_LOGIC: 28 | val = !val; 29 | break; 30 | case SyntaxKind::MINUS: 31 | val = -val; 32 | default: 33 | break; 34 | } 35 | } 36 | *(ctx->res) = to_string(val); 37 | return; 38 | } 39 | string tmp; 40 | for (int i = children.size() - 1; i >= 0; i--) { 41 | tmp = controlFlowGraphBuilder.tmpVar(); 42 | switch (children[i]->getKind()) { 43 | case SyntaxKind::NOT_LOGIC: 44 | controlFlowGraphBuilder.insert(new NotLogic(&tmp, ctx->res)); 45 | *ctx->res = tmp; 46 | break; 47 | case SyntaxKind::MINUS: 48 | controlFlowGraphBuilder.insert(new Negate(&tmp, ctx->res)); 49 | *ctx->res = tmp; 50 | break; 51 | default: 52 | break; 53 | } 54 | } 55 | } 56 | }; 57 | #endif //SYSYCOMPILER_PREFIXOPNODE_H 58 | -------------------------------------------------------------------------------- /nodes/ReturnStmtNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_RETURNSTMTNODE_H 6 | #define SYSYCOMPILER_RETURNSTMTNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class ReturnStmtNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | for (auto & i : children) { 14 | i->print(out); 15 | } 16 | } 17 | 18 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 19 | int line; 20 | for (auto & i : children) { 21 | i->checkError(ctx, ret); 22 | if (i->getKind() == SyntaxKind::RETURN_KW) { 23 | line = i->getStartLine(); 24 | } else if (i->getKind() == SyntaxKind::EXPR && ctx->inVoidFunc) { 25 | errorList.emplace_back(ErrorKind::VOID_FUNC_RETURN_INTEGER, line); 26 | } 27 | } 28 | } 29 | 30 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 31 | string *res = nullptr; 32 | for (auto & i : children) { 33 | i->buildIR(ctx, ret); 34 | if (i->getKind() == SyntaxKind::EXPR) { 35 | res = ret->res; 36 | } 37 | } 38 | if (res == nullptr) { 39 | string tmp = "-"; 40 | controlFlowGraphBuilder.insert(new Return(&tmp)); 41 | } else { 42 | controlFlowGraphBuilder.insert(new Return(res)); 43 | } 44 | } 45 | }; 46 | #endif //SYSYCOMPILER_RETURNSTMTNODE_H 47 | -------------------------------------------------------------------------------- /nodes/StatementNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_STATEMENTNODE_H 6 | #define SYSYCOMPILER_STATEMENTNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class StatementNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | for (auto & i : children) { 14 | i->print(out); 15 | } 16 | *out << "" << endl; 17 | } 18 | 19 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 20 | for (auto & i : children) { 21 | i->checkError(ctx, ret); 22 | ret->isReturn = i->getKind() == SyntaxKind::RETURN_STMT; 23 | } 24 | } 25 | 26 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 27 | BasicBlock *continueCFG = ctx->continueBlock; 28 | BasicBlock *curBB = controlFlowGraphBuilder.getCur(); 29 | bool stmtAfterLoop = ctx->stmtAfterLoop; 30 | if (stmtAfterLoop) { 31 | ctx->continueBlock = controlFlowGraphBuilder.newBasicBlock(); 32 | } 33 | bool needJumpElse = ctx->needJumpElse; 34 | ctx->stmtAfterLoop = false; 35 | ctx->needJumpElse = false; 36 | for (auto & i : children) { 37 | i->buildIR(ctx, ret); 38 | } 39 | if (stmtAfterLoop) { 40 | controlFlowGraphBuilder.changeCur(ctx->continueBlock); 41 | ctx->condition->buildIR(ctx, ret); 42 | controlFlowGraphBuilder.insert(new JumpWhenTrue(curBB, ret->res)); 43 | } else if (needJumpElse) { 44 | controlFlowGraphBuilder.insert(new Jump(ctx->elseFinalBlock)); 45 | } 46 | ctx->continueBlock = continueCFG; 47 | } 48 | }; 49 | #endif //SYSYCOMPILER_STATEMENTNODE_H 50 | -------------------------------------------------------------------------------- /nodes/TokenNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_TOKENNODE_H 6 | #define SYSYCOMPILER_TOKENNODE_H 7 | #include "Node.h" 8 | using namespace std; 9 | 10 | class TokenNode : public Node { 11 | string *content; 12 | public: 13 | TokenNode(SyntaxKind kind, string* content, int line) : content(content) { 14 | this->setNodeElement(kind, line); 15 | } 16 | 17 | void print(ofstream *out) override { 18 | *out << syntaxKind2Name[kind] << " " << *content << endl; 19 | } 20 | 21 | string *getContent() { 22 | return content; 23 | } 24 | }; 25 | #endif //SYSYCOMPILER_TOKENNODE_H 26 | -------------------------------------------------------------------------------- /nodes/VarDeclNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_VARDECLNODE_H 6 | #define SYSYCOMPILER_VARDECLNODE_H 7 | #include "VarDefNode.h" 8 | using namespace std; 9 | 10 | class VarDeclNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | bool isConst = false; 14 | if (children[0]->getKind() == SyntaxKind::CONST_KW) { 15 | isConst = true; 16 | } 17 | for (auto & i : children) { 18 | if (i->getKind() == SyntaxKind::VAR_DEF) { 19 | ((VarDefNode*)i)->print(out, isConst); 20 | } else { 21 | i->print(out); 22 | } 23 | } 24 | if (isConst) { 25 | *out << "" << endl; 26 | } else { 27 | *out << "" << endl; 28 | } 29 | } 30 | 31 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 32 | ctx->isConst = children[0]->getKind() == SyntaxKind::CONST_KW; 33 | for (auto & i : children) { 34 | i->checkError(ctx, ret); 35 | } 36 | ctx->isConst = false; 37 | } 38 | 39 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 40 | for (auto & i : children) { 41 | ctx->isConst = children[0]->getKind() == SyntaxKind::CONST_KW; 42 | i->buildIR(ctx, ret); 43 | } 44 | ctx->isConst = false; 45 | } 46 | }; 47 | #endif //SYSYCOMPILER_VARDECLNODE_H 48 | -------------------------------------------------------------------------------- /nodes/WhileStmtNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_WHILESTMTNODE_H 6 | #define SYSYCOMPILER_WHILESTMTNODE_H 7 | #include "ExprNode.h" 8 | using namespace std; 9 | 10 | class WhileStmtNode : public Node { 11 | public: 12 | void print(ofstream *out) override { 13 | for (int i = 0; i < children.size(); i++) { 14 | if (i == 2 && children[i]->getKind() == SyntaxKind::EXPR) { 15 | ((ExprNode*)children[i])->print(out, ExprType::CONDITION); 16 | } else { 17 | children[i]->print(out); 18 | } 19 | } 20 | } 21 | 22 | void checkError(ErrorCheckCtx *ctx, ErrorCheckRet *ret) override { 23 | ctx->loopNum++; 24 | for (auto & i : children) { 25 | i->checkError(ctx, ret); 26 | } 27 | ctx->loopNum--; 28 | } 29 | 30 | void buildIR(BuildIRCtx *ctx, BuildIRRet *ret) override { 31 | BasicBlock *cfg2 = ctx->breakBlock; 32 | bool inLoop = ctx->stmtAfterLoop; 33 | children[2]->buildIR(ctx, ret); 34 | BasicBlock *final = controlFlowGraphBuilder.newBasicBlock(); 35 | ExprNode* condition = ctx->condition; 36 | controlFlowGraphBuilder.insert(new JumpWhenFalse(final, ret->res)); 37 | BasicBlock *body = controlFlowGraphBuilder.newBasicBlock(); 38 | controlFlowGraphBuilder.changeCur(body); 39 | ctx->breakBlock = final; 40 | ctx->stmtAfterLoop = true; 41 | ctx->condition = (ExprNode*) children[2]; 42 | children[4]->buildIR(ctx, ret); 43 | ctx->breakBlock = cfg2; 44 | ctx->stmtAfterLoop = inLoop; 45 | ctx->condition = condition; 46 | controlFlowGraphBuilder.changeCur(final); 47 | } 48 | }; 49 | #endif //SYSYCOMPILER_WHILESTMTNODE_H -------------------------------------------------------------------------------- /nodes/lib.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/8. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_NODES_LIB_H 6 | #define SYSYCOMPILER_NODES_LIB_H 7 | #include "Node.h" 8 | #include "CompileUnitNode.h" 9 | #include "ErrorNode.h" 10 | #include "FuncTypeNode.h" 11 | #include "FuncDefNode.h" 12 | #include "FuncParamsNode.h" 13 | #include "PrefixOPNode.h" 14 | #include "PrefixExprNode.h" 15 | #include "BinExprNode.h" 16 | #include "ExprNode.h" 17 | #include "FuncParamNode.h" 18 | #include "BlockNode.h" 19 | #include "AssignStmtNode.h" 20 | #include "ExprStmtNode.h" 21 | #include "StatementNode.h" 22 | #include "PrintfStmtNode.h" 23 | #include "ReturnStmtNode.h" 24 | #include "ContinueStmtNode.h" 25 | #include "BreakStmt.h" 26 | #include "IfStmtNode.h" 27 | #include "WhileStmtNode.h" 28 | #include "LiteralExprNode.h" 29 | #include "VarExprNode.h" 30 | #include "FuncExprNode.h" 31 | #include "FuncArgsNode.h" 32 | #include "InitValNode.h" 33 | #include "VarExprNode.h" 34 | #include "VarDefNode.h" 35 | #include "VarDeclNode.h" 36 | #include "TokenNode.h" 37 | 38 | #endif //SYSYCOMPILER_NODES_LIB_H 39 | -------------------------------------------------------------------------------- /parser/Context.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/25. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_CONTEXT_H 6 | #define SYSYCOMPILER_CONTEXT_H 7 | #include "../control-flow-graphs/BasicBlock.h" 8 | 9 | struct ErrorCheckRet { 10 | int val; 11 | int dimension; // defined in params, calculated in expression 12 | bool isConst, isReturn, isVoid; 13 | vector args{}; // dimensions of each param and argument 14 | ErrorCheckRet() : val(0), dimension(0), isConst(false), isReturn(false), isVoid(false) {} 15 | }; 16 | 17 | struct ErrorCheckCtx { 18 | bool inVoidFunc, afterFuncDef, isConst, isLVal, isGlobal; 19 | int loopNum; 20 | ErrorCheckCtx() : isConst(false), isLVal(false), isGlobal(false), 21 | afterFuncDef(false), inVoidFunc(false), loopNum(0) {} 22 | }; 23 | 24 | struct BuildIRRet { 25 | string *res; // stored in exprNode 26 | string *param; 27 | vector args; 28 | vector init; 29 | bool isArray; 30 | BuildIRRet() : res(nullptr), param(nullptr), isArray(false) {} 31 | 32 | void clear() { 33 | param = nullptr; 34 | args.clear(); 35 | init.clear(); 36 | } 37 | 38 | ~BuildIRRet() { 39 | delete param; 40 | } 41 | }; 42 | 43 | struct ExprNode; 44 | struct BuildIRCtx { 45 | bool isConst, afterFuncDef, isLVal, isGlobal, needJumpElse, stmtAfterLoop; 46 | string *res; 47 | ExprNode *condition; 48 | BasicBlock *continueBlock, *breakBlock, *elseFinalBlock; 49 | BuildIRCtx() : continueBlock(nullptr), breakBlock(nullptr), elseFinalBlock(nullptr), res(nullptr), 50 | condition(nullptr), isConst(false), afterFuncDef(false), isLVal(false), isGlobal(false), 51 | needJumpElse(false), stmtAfterLoop(false) {} 52 | }; 53 | 54 | #endif //SYSYCOMPILER_CONTEXT_H -------------------------------------------------------------------------------- /parser/ErrorKind.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/14. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_ERRORKIND_H 6 | #define SYSYCOMPILER_ERRORKIND_H 7 | #include 8 | using namespace std; 9 | 10 | enum class ErrorKind { 11 | INVALID_CHARACTER, 12 | REDEFINE_IDENT, 13 | UNDEFINED_IDENT, 14 | PARAMS_NUM_UNMATCHED, 15 | PARAM_TYPE_UNMATCHED, 16 | VOID_FUNC_RETURN_INTEGER, 17 | INT_FUNC_RETURN_LACKED, 18 | CONSTANT_ASSIGNED, 19 | SEMICOLON_LACKED, 20 | R_PAREN_LACKED, 21 | R_BRACKET_LACKED, 22 | FORMAT_CHAR_UNMATCHED, 23 | BREAK_CONTINUE_OUT_LOOP, 24 | UNDEFINED_ERROR, 25 | CORRECT 26 | }; 27 | 28 | map errorKind2Code = { 29 | {ErrorKind::INVALID_CHARACTER, 'a'}, 30 | {ErrorKind::REDEFINE_IDENT, 'b'}, 31 | {ErrorKind::UNDEFINED_IDENT, 'c'}, 32 | {ErrorKind::PARAMS_NUM_UNMATCHED, 'd'}, 33 | {ErrorKind::PARAM_TYPE_UNMATCHED, 'e'}, 34 | {ErrorKind::VOID_FUNC_RETURN_INTEGER, 'f'}, 35 | {ErrorKind::INT_FUNC_RETURN_LACKED, 'g'}, 36 | {ErrorKind::CONSTANT_ASSIGNED, 'h'}, 37 | {ErrorKind::SEMICOLON_LACKED, 'i'}, 38 | {ErrorKind::R_PAREN_LACKED, 'j'}, 39 | {ErrorKind::R_BRACKET_LACKED, 'k'}, 40 | {ErrorKind::FORMAT_CHAR_UNMATCHED, 'l'}, 41 | {ErrorKind::BREAK_CONTINUE_OUT_LOOP, 'm'}, 42 | {ErrorKind::UNDEFINED_ERROR, 'n'} 43 | }; 44 | 45 | vector> errorList; 46 | #endif //SYSYCOMPILER_ERRORKIND_H 47 | -------------------------------------------------------------------------------- /parser/TreeBuilder.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/9/26. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_TREEBUILDER_H 6 | #define SYSYCOMPILER_TREEBUILDER_H 7 | #include "../lexer/lib.h" 8 | #include "../nodes/lib.h" 9 | #include "ErrorKind.h" 10 | 11 | class TreeBuilder { 12 | typedef pair Parent; 13 | stack parent; 14 | stack children; 15 | public: 16 | void startNode(SyntaxKind kind) { 17 | parent.push(Parent(kind, children.size())); 18 | } 19 | void finishNode(Node *node) { 20 | Parent tmp = parent.top(); 21 | parent.pop(); 22 | while (children.size() > tmp.second) { 23 | node->addChild(children.top()); 24 | children.pop(); 25 | } 26 | node->setNodeElement(tmp.first); 27 | children.push(node); 28 | } 29 | 30 | // make left brother left son 31 | void startNodeAt(int point, SyntaxKind kind) { 32 | parent.push(Parent(kind, point)); 33 | } 34 | 35 | void terminalSymbol(SyntaxKind kind, string *content, int line) { 36 | auto *node = new TokenNode(kind, content, line); 37 | children.push(node); 38 | } 39 | 40 | void error(ErrorKind errorKind, int line) { 41 | Node* node = new ErrorNode(errorKind, line); 42 | node->setNodeElement(SyntaxKind::ERROR, line); 43 | if (errorKind != ErrorKind::UNDEFINED_ERROR) { 44 | children.push(node); 45 | return; 46 | } 47 | Parent tmp = parent.top(); 48 | parent.pop(); 49 | while (children.size() > tmp.second) { 50 | node->addChild(children.top()); 51 | children.pop(); 52 | } 53 | children.push(node); 54 | } 55 | 56 | int checkPoint() { 57 | return children.size(); 58 | } 59 | 60 | Node *root() { 61 | if (parent.size() == 0) { 62 | return children.top(); 63 | } else { 64 | printf("Parent is not empty, get root FAILED\n"); 65 | return children.top(); 66 | } 67 | } 68 | }; 69 | #endif //SYSYCOMPILER_TREEBUILDER_H 70 | -------------------------------------------------------------------------------- /parser/lib.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by gyp on 2021/10/24. 3 | // 4 | 5 | #ifndef SYSYCOMPILER_PARSER_LIB_H 6 | #define SYSYCOMPILER_PARSER_LIB_H 7 | 8 | #include "Parser.h" 9 | #include "TreeBuilder.h" 10 | #include "ErrorKind.h" 11 | #include "Symbol.h" 12 | 13 | #endif //SYSYCOMPILER_PARSER_LIB_H -------------------------------------------------------------------------------- /test/input1.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input1.txt -------------------------------------------------------------------------------- /test/input10.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input10.txt -------------------------------------------------------------------------------- /test/input11.txt: -------------------------------------------------------------------------------- 1 | 5 -------------------------------------------------------------------------------- /test/input12.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input12.txt -------------------------------------------------------------------------------- /test/input13.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | -------------------------------------------------------------------------------- /test/input14.txt: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /test/input15.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 3 3 | 4 4 | 5 -------------------------------------------------------------------------------- /test/input16.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 15 3 | 6 4 | 6 -------------------------------------------------------------------------------- /test/input17.txt: -------------------------------------------------------------------------------- 1 | 12345 2 | -------------------------------------------------------------------------------- /test/input18.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | -1 5 | 1 6 | 2 7 | 2 8 | -1 9 | 2 10 | 2 11 | -------------------------------------------------------------------------------- /test/input19.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input19.txt -------------------------------------------------------------------------------- /test/input2.txt: -------------------------------------------------------------------------------- 1 | 54633487 2 | 632917 3 | 30 -------------------------------------------------------------------------------- /test/input20.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /test/input21.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 2 4 | 3 5 | 3 6 | 4 7 | 2 8 | 4 9 | 0 10 | 1 11 | 1 12 | 0 13 | 123456789 14 | 987654321 15 | -1 -------------------------------------------------------------------------------- /test/input22.txt: -------------------------------------------------------------------------------- 1 | 1 -------------------------------------------------------------------------------- /test/input23.txt: -------------------------------------------------------------------------------- 1 | 0 -------------------------------------------------------------------------------- /test/input24.txt: -------------------------------------------------------------------------------- 1 | 19260817 2 | 114514 3 | 1919810 4 | 4 5 | 16 6 | 13 -------------------------------------------------------------------------------- /test/input25.txt: -------------------------------------------------------------------------------- 1 | 11 2 | 13 3 | 12341 4 | -------------------------------------------------------------------------------- /test/input26.txt: -------------------------------------------------------------------------------- 1 | 33 -------------------------------------------------------------------------------- /test/input27.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | -------------------------------------------------------------------------------- /test/input28.txt: -------------------------------------------------------------------------------- 1 | 6 -------------------------------------------------------------------------------- /test/input29.txt: -------------------------------------------------------------------------------- 1 | 233 2 | 1 3 | 42 4 | -15 5 | 0 6 | -------------------------------------------------------------------------------- /test/input3.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 23 3 | 3 4 | 4 5 | 52 6 | 6 7 | 7 8 | 8 9 | 99 10 | -------------------------------------------------------------------------------- /test/input30.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input30.txt -------------------------------------------------------------------------------- /test/input31.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 2 3 | -------------------------------------------------------------------------------- /test/input32.txt: -------------------------------------------------------------------------------- 1 | 9 2 | 8 3 | 7 4 | 6 5 | -------------------------------------------------------------------------------- /test/input33.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input33.txt -------------------------------------------------------------------------------- /test/input34.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 20 3 | 30 -------------------------------------------------------------------------------- /test/input35.txt: -------------------------------------------------------------------------------- 1 | 12308760 2 | -------------------------------------------------------------------------------- /test/input36.txt: -------------------------------------------------------------------------------- 1 | 1 -------------------------------------------------------------------------------- /test/input37.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 8 3 | 7 4 | 6 5 | 5 6 | 4 7 | 3 8 | 2 9 | 1 10 | -------------------------------------------------------------------------------- /test/input38.txt: -------------------------------------------------------------------------------- 1 | 10 -------------------------------------------------------------------------------- /test/input39.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 3 3 | 4 4 | 5 -------------------------------------------------------------------------------- /test/input4.txt: -------------------------------------------------------------------------------- 1 | 56 -------------------------------------------------------------------------------- /test/input40.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 2 3 | 3 4 | 1 5 | 0 -------------------------------------------------------------------------------- /test/input41.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input41.txt -------------------------------------------------------------------------------- /test/input42.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 4 3 | -------------------------------------------------------------------------------- /test/input43.txt: -------------------------------------------------------------------------------- 1 | 1150 -------------------------------------------------------------------------------- /test/input44.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 3 3 | 4 4 | 5 -------------------------------------------------------------------------------- /test/input45.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input45.txt -------------------------------------------------------------------------------- /test/input46.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 19123412 3 | 53 4 | 12142344 5 | 89 6 | 12354123 7 | 35 8 | 32151245 9 | 65 10 | 21532521 11 | 93 12 | 21654212 13 | 13 14 | 12312654 15 | 89 16 | 56335232 17 | 87 18 | 15687643 19 | 78 20 | 56323432 21 | 78 22 | -------------------------------------------------------------------------------- /test/input47.txt: -------------------------------------------------------------------------------- 1 | 123 2 | 0 3 | -234 -------------------------------------------------------------------------------- /test/input48.txt: -------------------------------------------------------------------------------- 1 | 1 -------------------------------------------------------------------------------- /test/input49.txt: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test/input5.txt: -------------------------------------------------------------------------------- 1 | -4 2 | 0 3 | 1 4 | 2 5 | 3 6 | 4 7 | 5 8 | 6 9 | 7 10 | 8 11 | -------------------------------------------------------------------------------- /test/input50.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input50.txt -------------------------------------------------------------------------------- /test/input51.txt: -------------------------------------------------------------------------------- 1 | 11 -------------------------------------------------------------------------------- /test/input52.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 9 -------------------------------------------------------------------------------- /test/input53.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input53.txt -------------------------------------------------------------------------------- /test/input54.txt: -------------------------------------------------------------------------------- 1 | 23 2 | 26 3 | 353 4 | -------------------------------------------------------------------------------- /test/input55.txt: -------------------------------------------------------------------------------- 1 | 13 2 | 29 3 | 37 4 | -------------------------------------------------------------------------------- /test/input56.txt: -------------------------------------------------------------------------------- 1 | 1 -------------------------------------------------------------------------------- /test/input58.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 1998 3 | 3 4 | 23 5 | 2021 6 | 9 7 | 23 8 | 168 9 | 5 10 | 21 11 | 2020 12 | 1 13 | 1 14 | 2010 15 | 2 16 | 28 17 | 2010 18 | 1 19 | 1 20 | 1998 21 | 2 22 | 25 23 | 1997 24 | 5 25 | 24 26 | -------------------------------------------------------------------------------- /test/input59.txt: -------------------------------------------------------------------------------- 1 | 1 -------------------------------------------------------------------------------- /test/input6.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input6.txt -------------------------------------------------------------------------------- /test/input60.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input60.txt -------------------------------------------------------------------------------- /test/input61.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input61.txt -------------------------------------------------------------------------------- /test/input62.txt: -------------------------------------------------------------------------------- 1 | 4 -------------------------------------------------------------------------------- /test/input63.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 2 3 | 12 4 | 23 5 | 34 6 | 45 7 | 2 8 | 2 9 | 34 10 | 45 11 | 56 12 | 67 13 | 1 14 | 3 15 | 4 16 | 1 17 | 1 18 | 1 19 | 1 20 | 2 21 | 2 22 | 2 23 | 2 24 | 3 25 | 3 26 | 3 27 | 3 28 | 1 29 | 2 30 | 4 31 | 4 32 | 3 33 | 1 34 | 1 35 | 2 36 | 3 37 | 3 38 | 12 39 | 23 40 | 54 41 | 32 42 | 12 43 | 21 44 | 34 45 | 12 46 | 54 47 | 2 48 | 2 49 | 1 50 | 1 51 | 1 52 | 1 53 | 2 54 | 34 55 | 54 56 | 2 57 | 3 58 | 1 59 | 2 60 | 2 61 | 2 62 | 1 63 | 3 64 | 12 65 | 23 66 | 34 67 | 3 68 | 4 69 | 1 70 | 1 71 | 2 72 | 3 73 | 4 74 | 3 75 | 2 76 | 12 77 | 23 78 | 23 79 | 34 80 | 45 81 | 56 82 | 1 83 | 1 84 | 5 85 | 12 86 | 23 87 | 34 88 | 45 89 | 56 90 | 1 91 | 3 92 | 12 93 | 23 94 | 34 95 | 3 96 | 1 97 | 1 98 | 1 99 | 1 100 | 1 101 | 2 102 | 2 103 | 2 104 | 2 105 | 14 106 | 325 107 | 231434 108 | 1233 109 | 2 110 | 2 111 | 810975 112 | 114514 113 | 13 114 | 31 115 | 3 -------------------------------------------------------------------------------- /test/input64.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 6 7 | 7 8 | 8 -------------------------------------------------------------------------------- /test/input65.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 12 -------------------------------------------------------------------------------- /test/input66.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 2 -------------------------------------------------------------------------------- /test/input67.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 2 3 | 1 -------------------------------------------------------------------------------- /test/input68.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input68.txt -------------------------------------------------------------------------------- /test/input69.txt: -------------------------------------------------------------------------------- 1 | 123 2 | 456 3 | 12 4 | 324 -------------------------------------------------------------------------------- /test/input7.txt: -------------------------------------------------------------------------------- 1 | 123 -------------------------------------------------------------------------------- /test/input70.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 0 3 | 1 4 | 6 5 | 8 6 | 20 7 | 30 8 | -------------------------------------------------------------------------------- /test/input71.txt: -------------------------------------------------------------------------------- 1 | 200 2 | -------------------------------------------------------------------------------- /test/input72.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 60 7 | 70 8 | 80 9 | 90 10 | 100 11 | 1100 12 | 1200 13 | 1300 14 | 1400 15 | 1500 16 | 16000 17 | 17000 18 | 18000 19 | 19000 20 | 24000 21 | 20000 22 | 21000 23 | 22000 24 | 23000 25 | 25000 26 | 0 27 | 1 28 | 550 -------------------------------------------------------------------------------- /test/input73.txt: -------------------------------------------------------------------------------- 1 | 33 2 | 28 3 | 501 4 | 233 5 | 114 6 | 711 7 | 266 8 | 500 9 | 77 -------------------------------------------------------------------------------- /test/input74.txt: -------------------------------------------------------------------------------- 1 | 9 2 | 8 3 | 7 4 | 6 5 | 5 6 | 4 7 | 3 8 | 2 9 | 1 10 | -------------------------------------------------------------------------------- /test/input75.txt: -------------------------------------------------------------------------------- 1 | 1 -------------------------------------------------------------------------------- /test/input76.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 1 -------------------------------------------------------------------------------- /test/input77.txt: -------------------------------------------------------------------------------- 1 | 42 2 | -------------------------------------------------------------------------------- /test/input78.txt: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /test/input79.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 -------------------------------------------------------------------------------- /test/input8.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/input8.txt -------------------------------------------------------------------------------- /test/input80.txt: -------------------------------------------------------------------------------- 1 | 2 2 | 3 3 | 3 4 | -------------------------------------------------------------------------------- /test/input81.txt: -------------------------------------------------------------------------------- 1 | 24 2 | 27 3 | 342526 4 | -------------------------------------------------------------------------------- /test/input82.txt: -------------------------------------------------------------------------------- 1 | 3 -------------------------------------------------------------------------------- /test/input9.txt: -------------------------------------------------------------------------------- 1 | 9 2 | 8 3 | 7 4 | 6 5 | 5 6 | 4 7 | 3 8 | 2 9 | 1 10 | -------------------------------------------------------------------------------- /test/output1.txt: -------------------------------------------------------------------------------- 1 | 19231204 2 | + is error! 3 | + is error! 4 | + is error! 5 | + is error! 6 | Break is error! 7 | a1+b1 is 3 8 | a2+b2 is -2 9 | -------------------------------------------------------------------------------- /test/output10.txt: -------------------------------------------------------------------------------- 1 | 19373025 2 | 1 3 | funi 4 | try 5 | try 6 | try 7 | try 8 | try 9 | try 10 | try -------------------------------------------------------------------------------- /test/output11.txt: -------------------------------------------------------------------------------- 1 | 19182650 2 | this is d 3 | 4 4 | 5 5 | 4 6 | 5 7 | 2 -------------------------------------------------------------------------------- /test/output12.txt: -------------------------------------------------------------------------------- 1 | 19182623 2 | -------------------------------------------------------------------------------- /test/output13.txt: -------------------------------------------------------------------------------- 1 | 10, 30972, -5 -------------------------------------------------------------------------------- /test/output14.txt: -------------------------------------------------------------------------------- 1 | 19373372 2 | fact[10] = 3628800 3 | fact[1] = 1 4 | fact[2] = 2 5 | fact[3] = 6 6 | fact[4] = 24 7 | fact[5] = 120 8 | fact[6] = 720 9 | fact[7] = 5040 10 | fact[8] = 40320 11 | -------------------------------------------------------------------------------- /test/output15.txt: -------------------------------------------------------------------------------- 1 | 19373479 2 | print int : 0 3 | print int : 2 4 | print int : 2 5 | print int : 0 6 | print int : 41 7 | print int : 61 8 | print int : 4884 9 | print int : -45015 -------------------------------------------------------------------------------- /test/output16.txt: -------------------------------------------------------------------------------- 1 | 19373373 2 | j : 4, k : 10, l : 4 3 | a! = 720, num = 5 4 | 6 5 | scanf a, b to get gcd and lcm 6 | gcd is 5 7 | lcm is 30 8 | -6 9 | scanf a to get Fibonacci 10 | fib is 8 -------------------------------------------------------------------------------- /test/output17.txt: -------------------------------------------------------------------------------- 1 | 19373682 2 | hello? 12345 12345 12345 3 | func1 done! 4 | func2 done! 5 | func3 input = 0 6 | func3 done! 7 | 204 204 100 100 20400 100 0 100 100 8 | func4 done! 9 | -------------------------------------------------------------------------------- /test/output18.txt: -------------------------------------------------------------------------------- 1 | 19373719 2 | 0 -2 3 | 0 2 4 | 4 5 | 0 6 | -------------------------------------------------------------------------------- /test/output19.txt: -------------------------------------------------------------------------------- 1 | 19373135 2 | l_const_0, should be 0: 0 3 | l_const_1, should be 1: 1 4 | l_const_3, should be 2: 2 5 | l_const_5[2], l_const_6[0][1], should be 3, 1: 3, 1 6 | l_var_0, should be 0: 0 7 | l_var_1, should be 2: 2 8 | l_var_3, should be 4: 4 9 | l_var_5, should be 5: 5 10 | l_var_6[2], l_var_7[0][1], should be 3, 1: 3, 1 -------------------------------------------------------------------------------- /test/output2.txt: -------------------------------------------------------------------------------- 1 | 18375200 2 | -163900461 3 | -628 4 | -925500 5 | ppgod7mi 6 | 4396yoga7 7 | 2333 8 | 9 | 10 | 11 | 632917 is not a prime, with factor 41. 12 | The 30th Fibonacci num is 832040. -------------------------------------------------------------------------------- /test/output20.txt: -------------------------------------------------------------------------------- 1 | 19373061 2 | 0 3 | 1 4 | 0 5 | 0 6 | 1 7 | 0 8 | 1 9 | 0 10 | t:1 -------------------------------------------------------------------------------- /test/output21.txt: -------------------------------------------------------------------------------- 1 | gcd is 1 2 | gcd is 1 3 | gcd is 1 4 | gcd is 2 5 | invaild input! 6 | invaild input! 7 | gcd is 9 8 | -------------------------------------------------------------------------------- /test/output22.txt: -------------------------------------------------------------------------------- 1 | 19373034 2 | 1 3 | 1 4 | 0 5 | 1 6 | 0 7 | 10 8 | 8 9 | 9 10 | 10 -------------------------------------------------------------------------------- /test/output23.txt: -------------------------------------------------------------------------------- 1 | 19373044 2 | s+1=2 3 | m=0 4 | m=1 5 | m=0 6 | m=0 7 | m=1 8 | m=2 9 | m=3 10 | before break -------------------------------------------------------------------------------- /test/output24.txt: -------------------------------------------------------------------------------- 1 | 19373022 2 | b:-19260604 3 | Bool1 is false! 4 | 1919820 5 | 1932053504 6 | -------------------------------------------------------------------------------- /test/output25.txt: -------------------------------------------------------------------------------- 1 | 19373341 2 | the num is 851 3 | the num is 11 4 | the num is 13 5 | the num is 12341 6 | 11 is odd! 7 | 13 is odd! 8 | max of 11 and 13 is 13 9 | min of 11 and 13 is 11 10 | No, 12341 is not a prime! It has a factor 7 11 | -------------------------------------------------------------------------------- /test/output26.txt: -------------------------------------------------------------------------------- 1 | 19373276 2 | !()*+,-./0123456789:;<>=?@[]^_`~{}| 3 | qwertyuiopasd 4 | fghjklzxcvbnmQWERTYUIOPASD 5 | FGHJKLZXCVBNM0 6 | 72 23 7 | addSum: 105, 45 8 | -2169 9 | 1008-6 10 | end -------------------------------------------------------------------------------- /test/output27.txt: -------------------------------------------------------------------------------- 1 | 19373315 2 | this is a yu ju ky 3 | unbelievable! 4 | var_a from getint() is 1 5 | getint is 1 6 | tmp 7 | tmp 8 | tmp 9 | tmp 10 | tmp 11 | -------------------------------------------------------------------------------- /test/output28.txt: -------------------------------------------------------------------------------- 1 | 19373332 2 | Stmt-3(Block): c = 2, d = 46 3 | Stmt-3(Block): e = 4, f = 829 4 | Stmt-4(if) check finish! 5 | Stmt-5,6 check finish! 6 | Stmt-7 check finish! 7 | Stmt-8 check finish! 8 | 9 | FormatChar. 10 | 13 -------------------------------------------------------------------------------- /test/output29.txt: -------------------------------------------------------------------------------- 1 | 19373348 2 | Got a number: 233! 3 | 247 4 | Got a number: 1! 5 | -3 6 | Got a number: 42! 7 | 738 8 | Got a number: -15! 9 | 1711 10 | Got a number: 0! 11 | 3667 12 | -------------------------------------------------------------------------------- /test/output3.txt: -------------------------------------------------------------------------------- 1 | 19231177 2 | 1 3 | 23 4 | 3 5 | 4 6 | 52 7 | 6 8 | 7 9 | 8 10 | 99 11 | 10 12 | appear 13 | 20 14 | not 15 | 30 16 | not 17 | 40 18 | not 19 | 50 20 | not 21 | 60 22 | not 23 | 99 24 | not 25 | 99 26 | not 27 | 99 28 | not 29 | print123 30 | 123 31 | /****/123 32 | /**/123 33 | print return value : 34 | in : 5 ,out = 50 35 | in : 5 ,out = 50 -------------------------------------------------------------------------------- /test/output30.txt: -------------------------------------------------------------------------------- 1 | 19373749 2 | 4 3 | 2 4 | no 5 | 0 6 | 0 7 | 0 8 | 1 9 | 1 10 | -1 -------------------------------------------------------------------------------- /test/output31.txt: -------------------------------------------------------------------------------- 1 | 19231007 2 | input recurrent cycle: 3 | cycle is: 2 4 | input initial value: 5 | initial value is: 2 6 | your input is good 7 | A = 7 8 | B = 3 9 | C = 1 10 | final value is: 1010 11 | -------------------------------------------------------------------------------- /test/output32.txt: -------------------------------------------------------------------------------- 1 | 19231111 2 | 0 2 3 0 3 | 0 2 3 0 4 | 6 2 12 6 5 | 23 20 55 48 6 | 5 7 | true 8 | true 9 | 2 10 | 123 11 | 123 12 | -------------------------------------------------------------------------------- /test/output33.txt: -------------------------------------------------------------------------------- 1 | 19231047 2 | climb 0 : 1 3 | climb 3 : 3 4 | climb 6 : 13 5 | test return1 : 14 6 | test climb sum 7 : 21 7 | test product - sum : 2854 8 | test nest 1 : 13 9 | test nest 2 : 89 10 | test file -------------------------------------------------------------------------------- /test/output34.txt: -------------------------------------------------------------------------------- 1 | 19231177 2 | b=5 3 | 20 4 | 30 5 | testBlock : b=1524 6 | result = 79626240 7 | 1 2 3 4 sum is : 10 8 | line1 is:11111;line2 is : 22222; line3 is : 33333; 9 | 1+2=3 10 | 1+2=3 11 | 19231177 -------------------------------------------------------------------------------- /test/output35.txt: -------------------------------------------------------------------------------- 1 | 19231150 2 | what you enter in is:12308760 3 | _a=5 4 | array_c[0] is:1 5 | array_d[0][0] is:1 6 | array_d[1][0] is:2 7 | _a=10 8 | _a=103 9 | 2 2 2 3 3 3 3 5 29 131 10 | Over -------------------------------------------------------------------------------- /test/output36.txt: -------------------------------------------------------------------------------- 1 | 19231188111111111 -------------------------------------------------------------------------------- /test/output37.txt: -------------------------------------------------------------------------------- 1 | 19231076 2 | >/!~varg1 is:1;~ 3 | >/!~varg2 is:6;~ 4 | >/!~varg3 is:7;~ 5 | >/!~varg4 is:6;~ 6 | >/!~varg5 is:5;~ 7 | >/!~varg6 is:4;~ 8 | >/!~varg7 is:3;~ 9 | >/!~varg8 is:2;~ 10 | >/!~varg9 is:-1;~ 11 | -------------------------------------------------------------------------------- /test/output38.txt: -------------------------------------------------------------------------------- 1 | 193733852345678910 -------------------------------------------------------------------------------- /test/output39.txt: -------------------------------------------------------------------------------- 1 | 19373330 2 | n 4 3 | p 25 4 | i 1 5 | sum of two D 3 6 | const D + 10 is 10 -_- 7 | after sort 1 1 3 4 5 8 | Squ const2 + 101 115 9 | slee882641 10 | sadhiukashjfasodifd51023sad65saf1erwg0fdss5a6d1asd56sa0ds55saad511sa2d31asd556gdsfsadsa 11 | -------------------------------------------------------------------------------- /test/output4.txt: -------------------------------------------------------------------------------- 1 | 19231204 2 | The score is 56. 3 | Yes! 4 | Yes! 5 | Yes! 6 | No! -------------------------------------------------------------------------------- /test/output40.txt: -------------------------------------------------------------------------------- 1 | 19182616 2 | 5 is a prime number 3 | c and d are both positive 4 | 2 5 | 3 6 | 4 7 | 1 8 | 2 9 | 5 10 | 6 -------------------------------------------------------------------------------- /test/output41.txt: -------------------------------------------------------------------------------- 1 | 19182622 2 | 19182622 3 | 19182622 4 | 19182622 5 | 19182622 6 | 19182622 7 | 19182622 8 | 19182622 9 | 19182622 10 | 19182622 -------------------------------------------------------------------------------- /test/output43.txt: -------------------------------------------------------------------------------- 1 | 19373459 2 | f1: 8221 3 | f0: 0 4 | ori00:5577 5 | -------------------------------------------------------------------------------- /test/output44.txt: -------------------------------------------------------------------------------- 1 | 19373479 2 | print int : 2 3 | print int : 2 4 | print int : 0 5 | print int : 41 6 | print int : 61 7 | print int : 4884 8 | print int : -45015 9 | print int : 10432 -------------------------------------------------------------------------------- /test/output45.txt: -------------------------------------------------------------------------------- 1 | 19373466 2 | const declaration: const int a[4][2]={{1, 2}, {3, 4}, {5, 6}, {7, 8}}, b[2][1] = {{9}, {10}} 3 | global var declaration: int c[1][2]={{11, 12}} 4 | global var declaration(not initialized): des[4][1], des2[4][2] 5 | use ConstDef in FuncFParam (exactly, the len of array) 6 | calculate a * b, your result is des[4][1] = {{29}, {67}, {105}, {143}} 7 | calculate des * c 8 | then calculate the sum of each row(store in rowSum[4]) 9 | your result is (4 row): 667, 1541, 2415, 3289 10 | finish 11 | -------------------------------------------------------------------------------- /test/output46.txt: -------------------------------------------------------------------------------- 1 | 19373372 2 | input > 19123412 53 3 | input > 12142344 89 4 | input > 12354123 35 5 | input > 32151245 65 6 | input > 21532521 93 7 | input > 21654212 13 8 | input > 12312654 89 9 | input > 56335232 87 10 | input > 15687643 78 11 | input > 56323432 78 12 | 19123412 -> 53 13 | 12142344 -> 89 14 | 12354123 -> 35 15 | 32151245 -> 65 16 | 21532521 -> 93 17 | 21654212 -> 13 18 | 12312654 -> 89 19 | 56335232 -> 87 20 | 15687643 -> 78 21 | 56323432 -> 78 22 | swap <19123412 53> <-> <12142344 89> 23 | swap <12142344 89> <-> <21532521 93> 24 | swap <19123412 53> <-> <32151245 65> 25 | swap <32151245 65> <-> <12142344 89> 26 | swap <12354123 35> <-> <19123412 53> 27 | swap <19123412 53> <-> <32151245 65> 28 | swap <32151245 65> <-> <12312654 89> 29 | swap <12354123 35> <-> <19123412 53> 30 | swap <19123412 53> <-> <32151245 65> 31 | swap <32151245 65> <-> <56335232 87> 32 | swap <12354123 35> <-> <19123412 53> 33 | swap <19123412 53> <-> <32151245 65> 34 | swap <32151245 65> <-> <15687643 78> 35 | swap <21654212 13> <-> <12354123 35> 36 | swap <12354123 35> <-> <19123412 53> 37 | swap <19123412 53> <-> <32151245 65> 38 | swap <32151245 65> <-> <56323432 78> 39 | swap <21654212 13> <-> <12354123 35> 40 | swap <12354123 35> <-> <19123412 53> 41 | swap <19123412 53> <-> <32151245 65> 42 | swap <21654212 13> <-> <12354123 35> 43 | swap <12354123 35> <-> <19123412 53> 44 | swap <21654212 13> <-> <12354123 35> 45 | 9 46 | 21532521 -> 93 47 | 12142344 -> 89 48 | 12312654 -> 89 49 | 56335232 -> 87 50 | 15687643 -> 78 51 | 56323432 -> 78 52 | 32151245 -> 65 53 | 19123412 -> 53 54 | 12354123 -> 35 55 | 21654212 -> 13 56 | -------------------------------------------------------------------------------- /test/output47.txt: -------------------------------------------------------------------------------- 1 | 19373755 2 | n1 = 100 3 | n2-5 = -30 4 | n2 + n3 = -15 5 | n2 * n3 = -250 6 | ! ()*+,-./0123456789:; 7 | <=>?@ 8 | ABCDEFGHIJKLMNOPQRSTUVWXYZ 9 | []^_` 10 | abcdefghijklmnopqrstuvwxyz{|}~ -------------------------------------------------------------------------------- /test/output48.txt: -------------------------------------------------------------------------------- 1 | 19373135 2 | test func, should be 2: 2 3 | test func, should be 2: 2 4 | test Exp, should be 5: 5 5 | test Block, should be 2: 2 6 | test if, should be 2, 2: 2, 2 7 | test while, should be 6: 6 8 | test break continue, should be 0, 1: 0, 1 9 | test printf, should be 2: 2 10 | test getint, should be 1: 1 11 | -------------------------------------------------------------------------------- /test/output49.txt: -------------------------------------------------------------------------------- 1 | 19373408 all weak points 2 | L and Eq is ok (no params) 3 | Rel is ok (with params) 4 | Add is ok1 5 | Add is ok2 6 | -1 7 | -1 8 | 1 9 | 1 10 | -------------------------------------------------------------------------------- /test/output5.txt: -------------------------------------------------------------------------------- 1 | 16061069 2 | input an integer,judge if it is prime number,10 groups in total 3 | input > 0 is needed 4 | input > 0 is needed 5 | 1 is not concerned 6 | 2 is a prime number 7 | 3 is a prime number 8 | 4 is not a prime number 9 | 5 is a prime number 10 | 6 is not a prime number 11 | 7 is a prime number 12 | 8 is not a prime number 13 | while times is 22 in total 14 | -------------------------------------------------------------------------------- /test/output50.txt: -------------------------------------------------------------------------------- 1 | 19373384 2 | [1, 3, 5, 7, 9, 11, 2, 9, 7, 8] 3 | [2, 5, 4, 8, 9, 6, 3, 7, 1, 1] 4 | Inner product: expect 324, output: 324 5 | SBC step 1: expect:[12, 25, 26, 42, 49, 44, 17, 43, 21, 23] 6 | output:[12, 25, 26, 42, 49, 44, 17, 43, 21, 23] 7 | SBC step 2 and EWM: expect:[70, 350, 276, 904, 1170, 666, 144, 784, 50, 54] 8 | output:[70, 350, 276, 904, 1170, 666, 144, 784, 50, 54] 9 | 10 | 11 | ================================================================================ 12 | PASSED 13 | ================================================================================ 14 | -------------------------------------------------------------------------------- /test/output51.txt: -------------------------------------------------------------------------------- 1 | 19373339 2 | 2 3 | 3 4 | 4 5 | 13 6 | 2 7 | 123456 8 | 123456 9 | 123456 -------------------------------------------------------------------------------- /test/output52.txt: -------------------------------------------------------------------------------- 1 | 19373611 2 | 19373611 3 | 19373611 4 | Please input base number a: 5 | Please input base number b: 6 | The No.0 number is -4. 7 | The No.1 number is 0. 8 | The No.2 number is 5. 9 | The No.3 number is 14. 10 | The No.4 number is 45. -------------------------------------------------------------------------------- /test/output53.txt: -------------------------------------------------------------------------------- 1 | 19373063 2 | 1.localVirable1 = 11,localVirable2 = 10 3 | 2. 4 | [2][0]:10|10 , [2]:-7|30 5 | [1][0]:8|8 , [1]:-8|20 6 | [0][0]:6|6 , [0]:-9|10 7 | 9? 8 | 3.function of kinds of parameters testified 9 | 4.non exp testified -------------------------------------------------------------------------------- /test/output54.txt: -------------------------------------------------------------------------------- 1 | 19373341 2 | m = 6, n = 23, mm = 26, nn = 353 3 | max of mm and nn: 353; min of mm and nn: 26 4 | mainConst1 = 10 5 | Sum of normalConst: 35 6 | 23 is odd! 7 | Sum of array: 6 8 | sum of array: 24 9 | array contains 4 even number! 10 | All even! 11 | -------------------------------------------------------------------------------- /test/output55.txt: -------------------------------------------------------------------------------- 1 | 19373341 2 | m = 3, n = 13, mm = 29, nn = 37 3 | max of mm and nn: 37; min of mm and nn: 29 4 | mainConst1 = 10 5 | Sum of normalConst: 66 6 | 13 is odd! 7 | Sum of array: 3 8 | sum of array: 24 9 | array contains 3 even number! 10 | All even! 11 | -------------------------------------------------------------------------------- /test/output56.txt: -------------------------------------------------------------------------------- 1 | 1837518218183751821837518218375182 -------------------------------------------------------------------------------- /test/output58.txt: -------------------------------------------------------------------------------- 1 | 16061069 2 | input groups 3 | input your birth year,month,day,ep:1998 1 1 4 | input current year,month,day,ep:2020 2 2 5 | you have lived 8585 days 6 | input your birth year,month,day,ep:1998 1 1 7 | input current year,month,day,ep:2020 2 2 8 | you have lived 676288 days 9 | input your birth year,month,day,ep:1998 1 1 10 | input current year,month,day,ep:2020 2 2 11 | please input time legally 12 | input your birth year,month,day,ep:1998 1 1 13 | input current year,month,day,ep:2020 2 2 14 | please input time legally 15 | -------------------------------------------------------------------------------- /test/output59.txt: -------------------------------------------------------------------------------- 1 | 19373734 2 | 19373734 3 | 19373734 4 | 19373734 5 | 19373734 6 | 19373734 7 | 19373734 8 | 19373734 9 | 19373734 10 | 19373734 -------------------------------------------------------------------------------- /test/output6.txt: -------------------------------------------------------------------------------- 1 | 19231177 2 | _ = 1; a = 20; b = 30; c = 460; 3 | 20+30=50; 460-1=459; 50*459=22950; 459/50=9; 22950mod30=0 4 | 50+459+22950=23459; 459-22950-9=-22500; 22950*9*0=0; 0/50/459=0; 9mod0mod50=9 5 | 10+17-23*37/43mod71-17*23+43=-340=-340 6 | 10+(17-23)*37/43mod(71-37)*-340=1710 7 | all chars : !()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~ 8 | 5!+7+3=130 9 | max: 2147483647 ; min : -2147483647 ; sum : 0 10 | 19231177 -------------------------------------------------------------------------------- /test/output60.txt: -------------------------------------------------------------------------------- 1 | 19231258 OK1 2 | OK2 3 | OK3 4 | OK4 5 | OK5 6 | OK6 7 | OK7 8 | OK8 9 | OK9 -------------------------------------------------------------------------------- /test/output61.txt: -------------------------------------------------------------------------------- 1 | 19231011 2 | start checking for logic expressions 3 | check for AND calculation: successfully! 4 | check for OR calculation: successfully! 5 | check for short-circuit calculation: successfully! 6 | end checking 7 | -------------------------------------------------------------------------------- /test/output62.txt: -------------------------------------------------------------------------------- 1 | 19231248-250 2 | 19231248 3 | 19231248 4 | 19231248 5 | 19231248 6 | 19231248 7 | 19231248 8 | 19231248 9 | 19231248 10 | 19231248 11 | -------------------------------------------------------------------------------- /test/output63.txt: -------------------------------------------------------------------------------- 1 | 19182604 2 | 0 3 | 1 4 | 2 5 | 3 6 | 4 7 | 5 8 | 6 9 | 7 10 | 8 11 | -------------------------------------------------------------------------------- /test/output64.txt: -------------------------------------------------------------------------------- 1 | 19182619 2 | CscanfOK 3 | DscanfOK 4 | sb:2 5 | continueOK 6 | breakOK 7 | 0Func2OK 8 | Func3OK 9 | Func4OK 10 | end -------------------------------------------------------------------------------- /test/output65.txt: -------------------------------------------------------------------------------- 1 | 19373573 2 | 4 3 | 6 4 | judgeB 2,3 = -1 5 | Hello 12 6 | 10 7 | 0-1 8 | -10 9 | 01 10 | 10 11 | 31 -------------------------------------------------------------------------------- /test/output66.txt: -------------------------------------------------------------------------------- 1 | 19373528 2 | f2() flag : 3 3 | f2() flag : 3 4 | flag = 1 : c1 = 3, c2 = -1 5 | f2() flag : 2 6 | f2() flag : 4 7 | flag = 0 : c1 = 13, c2 = -1 8 | f2() flag : 2 9 | f2() flag : 4 10 | flag = 0 : c1 = 23, c2 = -1 -------------------------------------------------------------------------------- /test/output67.txt: -------------------------------------------------------------------------------- 1 | 19182620 2 | 0 3 | 1 4 | 2 5 | 2 6 | 1 7 | 2 8 | 19182620 9 | 19182620 10 | 19182620 11 | -------------------------------------------------------------------------------- /test/output68.txt: -------------------------------------------------------------------------------- 1 | 19182636 2 | ! is ok 3 | < is ok 4 | > is ok 5 | <= is ok 6 | >= is ok 7 | == != is ok 8 | or is ok 9 | the priority of and/or is ok 10 | 6 7 11 | -------------------------------------------------------------------------------- /test/output69.txt: -------------------------------------------------------------------------------- 1 | 19373330 2 | max of mnpq is 456 3 | i or m ? 456 4 | out1 456 out2 229044 5 | sum of array var33 is 5583 6 | squ sum of s3 3 7 | max of four number 314721 8 | Kirov reporrrting 1 9 | For the Union 7 10 | okmotherfuckeroveridontwanttobehornyanymoreijustwanttobehappy 11 | -------------------------------------------------------------------------------- /test/output7.txt: -------------------------------------------------------------------------------- 1 | 19373755 2 | n = -864 3 | n = -7938 4 | n = 10 5 | n = -15 6 | n = 861 7 | n = 0 8 | test_if done! 9 | test_while done! 10 | test_break_continue done! -------------------------------------------------------------------------------- /test/output70.txt: -------------------------------------------------------------------------------- 1 | 19373372 2 | a[n] = a[n - 1] + a[n - 2] (for different init values) 3 | /* input = 0 */ 4 | Error! 5 | /* input = 1 */ 6 | 1 1 7 | 1 0 8 | when a[0] = 0, a[1] = 1 ==> a[1] = 1 9 | when a[0] = 1, a[1] = 1 ==> a[1] = 1 10 | when a[0] = 1, a[1] = 2 ==> a[1] = 2 11 | when a[0] = 1, a[1] = 3 ==> a[1] = 3 12 | when a[0] = 3, a[1] = 4 ==> a[1] = 4 13 | when a[0] = 1, a[1] = -1 ==> a[1] = -1 14 | ////////////////////////////////////// 15 | /* input = 6 */ 16 | 13 8 17 | 8 5 18 | when a[0] = 0, a[1] = 1 ==> a[6] = 8 19 | when a[0] = 1, a[1] = 1 ==> a[6] = 13 20 | when a[0] = 1, a[1] = 2 ==> a[6] = 21 21 | when a[0] = 1, a[1] = 3 ==> a[6] = 29 22 | when a[0] = 3, a[1] = 4 ==> a[6] = 47 23 | when a[0] = 1, a[1] = -1 ==> a[6] = -3 24 | ////////////////////////////////////// 25 | /* input = 8 */ 26 | 34 21 27 | 21 13 28 | when a[0] = 0, a[1] = 1 ==> a[8] = 21 29 | when a[0] = 1, a[1] = 1 ==> a[8] = 34 30 | when a[0] = 1, a[1] = 2 ==> a[8] = 55 31 | when a[0] = 1, a[1] = 3 ==> a[8] = 76 32 | when a[0] = 3, a[1] = 4 ==> a[8] = 123 33 | when a[0] = 1, a[1] = -1 ==> a[8] = -8 34 | ////////////////////////////////////// 35 | /* input = 20 */ 36 | 10946 6765 37 | 6765 4181 38 | when a[0] = 0, a[1] = 1 ==> a[20] = 6765 39 | when a[0] = 1, a[1] = 1 ==> a[20] = 10946 40 | when a[0] = 1, a[1] = 2 ==> a[20] = 17711 41 | when a[0] = 1, a[1] = 3 ==> a[20] = 24476 42 | when a[0] = 3, a[1] = 4 ==> a[20] = 39603 43 | when a[0] = 1, a[1] = -1 ==> a[20] = -2584 44 | ////////////////////////////////////// 45 | /* input = 30 */ 46 | 1346269 832040 47 | 832040 514229 48 | when a[0] = 0, a[1] = 1 ==> a[30] = 832040 49 | when a[0] = 1, a[1] = 1 ==> a[30] = 1346269 50 | when a[0] = 1, a[1] = 2 ==> a[30] = 2178309 51 | when a[0] = 1, a[1] = 3 ==> a[30] = 3010349 52 | when a[0] = 3, a[1] = 4 ==> a[30] = 4870847 53 | when a[0] = 1, a[1] = -1 ==> a[30] = -317811 54 | ////////////////////////////////////// 55 | /****************** END ******************/ 56 | -------------------------------------------------------------------------------- /test/output71.txt: -------------------------------------------------------------------------------- 1 | 19373354 2 | 2 -402114823 3 | 3 423174272 4 | 5 375872692 5 | 7 915987033 6 | 11 -34179219 7 | 13 1498631475 8 | 17 -1060809599 9 | 19 423174272 10 | 23 915987033 11 | 29 1498631475 12 | 31 1640319187 13 | 37 375872692 14 | 41 -2143283456 15 | 43 -34179219 16 | 47 1640319187 17 | 53 375872692 18 | 59 -34179219 19 | 61 1498631475 20 | 67 423174272 21 | 71 915987033 22 | 73 -2143283456 23 | 79 1640319187 24 | 83 423174272 25 | 89 -2143283456 26 | 97 -1060809599 27 | 101 375872692 28 | 103 915987033 29 | 107 -34179219 30 | 109 1498631475 31 | 113 -1060809599 32 | 127 1640319187 33 | 131 423174272 34 | 137 -2143283456 35 | 139 -34179219 36 | 149 375872692 37 | 151 915987033 38 | 157 1498631475 39 | 163 423174272 40 | 167 915987033 41 | 173 1498631475 42 | 179 423174272 43 | 181 375872692 44 | 191 1640319187 45 | 193 -1060809599 46 | 197 375872692 47 | 199 915987033 48 | -------------------------------------------------------------------------------- /test/output72.txt: -------------------------------------------------------------------------------- 1 | 19373459 2 | val20: 0, val21:5369, val22:4821, val23:4682, val24:8093 3 | val30: 0, val32: 7303, val34: 6137 4 | val40: 0, val42:-1, val43:0, val44:-1 5 | -------------------------------------------------------------------------------- /test/output73.txt: -------------------------------------------------------------------------------- 1 | 19373487 2 | the result is:4 3 | the result is:4 4 | out of range!! 5 | the result is:6 6 | the result is:4 7 | out of range!! 8 | the result is:6 9 | the result is:5 10 | the result is:4 -------------------------------------------------------------------------------- /test/output74.txt: -------------------------------------------------------------------------------- 1 | 19373311 2 | 123 3 | 123 4 | 123 5 | 123 6 | 123 7 | 4 5 6 8 | 36 9 | 16 10 | 10 11 | 63 12 | 36 13 | 1 2 3 14 | 4 5 6 15 | 7 8 9 16 | -------------------------------------------------------------------------------- /test/output75.txt: -------------------------------------------------------------------------------- 1 | 19373135 2 | global g_var_6[1][0]:1, g_var_6[1][1]:1 3 | local l_var_4:1, l_var_5:1, l_var_6:1, l_var_7:10, l_var_8:100 4 | exp l_var_4:1 5 | exp l_var_5:0 6 | exp l_var_6:0 7 | if l_var_4:3 8 | while l_var_5:0 9 | while l_var_5:0 10 | getint l_var_6:1 -------------------------------------------------------------------------------- /test/output76.txt: -------------------------------------------------------------------------------- 1 | 19373163 2 | -+-114514 = 114514 3 | -+-114514 > 0 4 | -+- 6 + 7 * (6 -9) / 3 / 2 = 3 5 | var10[1] = 3 6 | var13 = 1 7 | func1() = 0 8 | func3(+-12) = -12 9 | func4(1, var4, var6) = 3 10 | func4(const1, var6[1], var6) = -2 11 | -------------------------------------------------------------------------------- /test/output77.txt: -------------------------------------------------------------------------------- 1 | 19373384 2 | const and var // decl and def test 3 | expected result: 579, output: 579 4 | function (def and )call test 5 | expected result: {{589, 21}, {599, 31}}, output: {{589, 21}, {599, 31}} 6 | LExp test, falling into infinite loop indicates an error -- pass 7 | simple echo test, get a integer and read back: 42 8 | 9 | 10 | ================================================================================ 11 | PASSED 12 | ================================================================================ 13 | -------------------------------------------------------------------------------- /test/output78.txt: -------------------------------------------------------------------------------- 1 | 19373408 all weak points 2 | L and Eq is ok (no params) 3 | Rel is ok (with params) 4 | Add is ok 5 | getint is ok 6 | -1 7 | -1 8 | 1 9 | 1 10 | -------------------------------------------------------------------------------- /test/output79.txt: -------------------------------------------------------------------------------- 1 | 19373421 2 | const_var1+const_var2=:3 3 | const_array1[1]=:2 4 | array2[0]:=3 5 | g(2):=6 6 | 123456789 7 | 1 8 | 5 9 | 6 10 | AK!!! -------------------------------------------------------------------------------- /test/output8.txt: -------------------------------------------------------------------------------- 1 | 19182650 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 21 9 | 1 10 | 6 -------------------------------------------------------------------------------- /test/output80.txt: -------------------------------------------------------------------------------- 1 | 19373235 2 | Testing Short-circuit evaluation : 0 1 1 0 1 0 1 1 0 1 1 1 3 | Testing func_pass_array(int a[][4) ...... 4 | 1 2 3 4 5 | 5 6 7 8 6 | 10 11 12 13 7 | Testing func_pass_PartArray(int a[]) ...... 5 6 7 8 8 | Testing func_pass_arrayElement(int a) ...... 3 9 | Testing complex calculate ...... result = -10. 10 | Testing D2array traversal and assignment. 11 | Array array_E: 12 | 0 1 2 13 | 1 2 3 14 | Testing func_if_D2array(a[][3], b) ...... 15 | 0 1 3 16 | 1 2 0 17 | Loop 7 times, once continue, once break. Symbol j = 5. func_while(x, y)is done! 18 | -------------------------------------------------------------------------------- /test/output81.txt: -------------------------------------------------------------------------------- 1 | 19373341 2 | m = 3, n = 24, mm = 27, nn = 342526 3 | max of mm and nn: 342526; min of mm and nn: 27 4 | mainConst1 = 10 5 | Sum of normalConst: 35 6 | 24 is even! 7 | Sum of array: 3 8 | sum of array: 24 9 | Contains odd! 10 | 27 and 342526 contains at least an odd! 11 | -------------------------------------------------------------------------------- /test/output82.txt: -------------------------------------------------------------------------------- 1 | 19373630 2 | 1 3 | 120 4 | 720 5 | 5040 6 | 1 7 | 2 8 | 4 9 | 3 10 | 30250 11 | -------------------------------------------------------------------------------- /test/output9.txt: -------------------------------------------------------------------------------- 1 | 19231076 2 | >/!~varg1 is:-1;~ 3 | >/!~varg2 is:-8;~ 4 | >/!~varg3 is:7;~ 5 | >/!~varg4 is:6;~ 6 | >/!~varg5 is:5;~ 7 | >/!~varg6 is:4;~ 8 | >/!~varg7 is:3;~ 9 | >/!~varg8 is:2;~ 10 | >/!~varg9 is:1;~ 11 | -------------------------------------------------------------------------------- /test/testfile1.txt: -------------------------------------------------------------------------------- 1 | const int a1 = 1, a2 = +3, a3 = 8; 2 | int b1 = 0 + 2, b2 = -5, b3 = +6; 3 | 4 | int main() { 5 | printf("19231204"); 6 | printf("\n"); 7 | int n = 10; 8 | while (n) { 9 | n = n - 1; 10 | if (n < b3) { 11 | continue; 12 | printf("Continue is error!\n"); 13 | } 14 | if (n < a1) { 15 | break; 16 | printf("Break is error!And < is error!\n"); 17 | } 18 | if (n == a2) { 19 | printf("+ is correct!\n"); 20 | } else { 21 | printf("+ is error!\n"); 22 | } 23 | if (n == b1) { 24 | break; 25 | } 26 | } 27 | if (n != b1) { 28 | if (n == 0) { 29 | printf("Break is error!\n"); 30 | } else { 31 | printf("Continue is error!\n"); 32 | } 33 | } 34 | printf("a1+b1 is %d\n", a1 + b1); 35 | printf("a2+b2 is %d\n", a2 + b2); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /test/testfile10.txt: -------------------------------------------------------------------------------- 1 | const int ci=1; 2 | const int ciexp=1+1*1/1%1-1; 3 | const int ca=1,cb=2; 4 | int i=-+1; 5 | int iexp=1+1*1/1%1-1; 6 | int a=1,b=2; 7 | void funv(int a,int b) {{ 8 | printf("funv\n"); 9 | }} 10 | int funi(int a){ 11 | printf("funi\n"); 12 | return 0; 13 | } 14 | int funvnop(){ 15 | return 0; 16 | } 17 | int main() { 18 | int ma1,ma2,ma3,ma4; 19 | printf("19373025\n"); 20 | ma1=1; 21 | ma2=1+1; 22 | ma3=1; 23 | ma4=1; 24 | if(ma1!=ma2) { 25 | int mif1=1; 26 | printf("%d\n",mif1); 27 | } else { 28 | return 0; 29 | } 30 | if(ma1==ma3){ 31 | funi(ma1); 32 | } 33 | if(ma1!=ma3){ 34 | funvnop(); 35 | } 36 | if(ma1>=ma3){ 37 | 38 | } 39 | if(ma2<=ma3){ 40 | funvnop(); 41 | } 42 | if(ma1>ma3){ 43 | funvnop(); 44 | } 45 | if(ma2 2 | int getint(){ 3 | int n; 4 | scanf("%d",&n); 5 | return n; 6 | } 7 | */ 8 | const int singleConstDecl = 23; 9 | //const int singleConstDecl_0 = singleConstDecl + 123 ,singleConstDecl_1 = singleConstDecl + 3 ; 10 | const int singleConstDecl_0 = 13 ,singleConstDecl_1 = 3 ; 11 | 12 | int singleVarDecl = 3 - 13; 13 | int singleVarDecl_0 = 23 ,singleVarDecl_1 = +-+(3-13),singleVarDecl_2; 14 | 15 | void funcDef_void(){ 16 | 17 | } 18 | 19 | int funcDef_0(int var){ 20 | int ans = var * 10; 21 | return ans; 22 | } 23 | 24 | int funcDef_1(int var1, int var2){ 25 | int a = var1 * var2; 26 | int b; 27 | int c; 28 | if (var2 != 0){ 29 | b = a + var1%var2; 30 | c = var1/var2; 31 | } 32 | else { 33 | b = a+var1; 34 | c = var1/2; 35 | } 36 | 37 | b = b - a; 38 | { 39 | ; 40 | a+b; 41 | } 42 | if (a < 0) a = -a; 43 | return (1+a)*(b+c); 44 | } 45 | void printInt(int var){ 46 | printf("print int : %d\n",var); 47 | return ; 48 | } 49 | int main(){ 50 | printf("19373479\n"); 51 | int a = 10,b,c,d,e; 52 | b = getint(); 53 | c = getint(); 54 | d = getint(); 55 | e = getint(); 56 | if(b>5){ 57 | b = 5; 58 | 59 | } 60 | while (a) { 61 | a = a - 1; 62 | if (c>=a){ 63 | c = c / (a+1) + a; 64 | } 65 | if (d <= a){ 66 | d = d*a; 67 | } 68 | else{ 69 | d = d % (a+3); 70 | } 71 | while(e < d){ 72 | e = e + a; 73 | if (e == b) break; 74 | if (e != c) { 75 | e = c + e; 76 | continue; 77 | } 78 | } 79 | } 80 | 81 | if(!a){ 82 | printInt(a); 83 | } 84 | printInt(b); 85 | printInt(c); 86 | printInt(d); 87 | printInt(e); 88 | int f = funcDef_1(e,d); 89 | int g = funcDef_1(f, funcDef_0(c)); 90 | funcDef_void(); 91 | printInt(f); 92 | printInt(g); 93 | singleVarDecl_2 = funcDef_1(funcDef_1(singleVarDecl,singleVarDecl_2), funcDef_1(singleConstDecl_0,singleConstDecl_1)); 94 | printInt(singleVarDecl_2); 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /test/testfile16.txt: -------------------------------------------------------------------------------- 1 | void fun1() 2 | { 3 | int j; 4 | int k = 4, l = 6; 5 | l = k; 6 | j = 3; 7 | j = (j % 20) - 1 + 9; 8 | int i=0; 9 | while (i <= 7) 10 | { 11 | i = i + 1; 12 | if (j == i) 13 | { 14 | k = k + j; 15 | continue; 16 | } 17 | j = j - 1; 18 | } 19 | printf("j : %d, k : %d, l : %d\n", j, k, l); 20 | } 21 | 22 | int fun2(int a) 23 | { 24 | int b = 1; 25 | int num = 1; 26 | while (a >= 1) 27 | { 28 | b = b * a; 29 | a = a - 1; 30 | if (a == 1) 31 | { 32 | break; 33 | } 34 | else if (a != 1) 35 | { 36 | num = num + 1; 37 | } 38 | } 39 | printf("a! = %d, num = %d\n", b, num); 40 | return 1; 41 | } 42 | 43 | int fun3(int a, int b) 44 | { 45 | if (a > b) 46 | { 47 | return a; 48 | } 49 | else if (a < b) 50 | { 51 | return b; 52 | } 53 | return a; 54 | } 55 | 56 | int gcd(int a, int b) 57 | { 58 | if (a % b == 0) 59 | { 60 | return b; 61 | } 62 | return gcd(b, a % b); 63 | } 64 | 65 | int lcm(int a, int b) 66 | { 67 | int gcds = gcd(a, b); 68 | return a * b / gcds; 69 | } 70 | 71 | void fun4(int a, int b, int c) 72 | { 73 | int d = (a + b - c) * a; 74 | printf("%d\n", d); 75 | } 76 | 77 | int fun5(int a) 78 | { 79 | if (a == 1) 80 | { 81 | return 1; 82 | } 83 | else if (a == 2) 84 | { 85 | return 1; 86 | } 87 | return fun5(a - 1) + fun5(a - 2); 88 | } 89 | 90 | int main() 91 | { 92 | printf("19373373\n"); 93 | fun1(); 94 | fun2(6); 95 | printf("%d\n", fun3(2, fun3(3, 6))); 96 | int a, b; 97 | printf("scanf a, b to get gcd and lcm\n"); 98 | a = getint(); 99 | b = getint(); 100 | printf("gcd is %d\n", gcd(a, b)); 101 | printf("lcm is %d\n", lcm(a, b)); 102 | int temp; 103 | temp = getint(); 104 | fun4(temp, 3, 10); 105 | printf("scanf a to get Fibonacci\n"); 106 | int fib; 107 | fib = getint(); 108 | printf("fib is %d\n", fun5(fib)); 109 | return 0; 110 | } -------------------------------------------------------------------------------- /test/testfile18.txt: -------------------------------------------------------------------------------- 1 | const int a = 1, b = 2; 2 | const int c = 3; 3 | int d, e = 1 + 2; 4 | int f; 5 | 6 | 7 | int double2(int x1) { 8 | if (!x1) { 9 | return 2*x1; 10 | } 11 | else { 12 | return (2*-x1); 13 | } 14 | return 0; 15 | } 16 | 17 | int add(int x1, int x2) { 18 | int sum = 0; 19 | sum = x1 + x2; 20 | return sum; 21 | } 22 | 23 | int sub(int x1, int x2) { 24 | int sum = 0; 25 | sum = x1 - x2; 26 | if (sum >= 0) { 27 | return sum; 28 | } 29 | if (sum <= 0) { 30 | return 0; 31 | } 32 | return 0; 33 | } 34 | 35 | void print_none() { 36 | return; 37 | } 38 | 39 | int main() { 40 | { 41 | printf("19373719\n"); 42 | int x = 0; 43 | int flag; 44 | int temp; 45 | int x1, x2; 46 | while(x < 4) { 47 | if (x != 4) { 48 | x = x + 1; 49 | } else { 50 | break; 51 | } 52 | flag = getint(); 53 | if (flag == 0) { 54 | x1 = getint(); 55 | temp = double2(x1) % 1; 56 | printf("%d ", temp); 57 | temp = double2(x1) / +1; 58 | printf("%d\n", temp); 59 | continue; 60 | } 61 | if (flag > 0) { 62 | x1 = getint(); 63 | x2 = getint(); 64 | printf("%d\n", add(x1, x2)); 65 | continue; 66 | } 67 | if (flag < 0) { 68 | x1 = getint(); 69 | x2 = getint(); 70 | printf("%d\n", sub(x1, x2)); 71 | continue; 72 | } 73 | 74 | } 75 | } 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /test/testfile19.txt: -------------------------------------------------------------------------------- 1 | /*cover basis, focus on Decl*/ 2 | int main(){ 3 | printf("19373135\n"); 4 | const int l_const_0 = 0; 5 | printf("l_const_0, should be 0: %d\n", l_const_0); 6 | const int l_const_1 = l_const_0 + 1; 7 | printf("l_const_1, should be 1: %d\n", l_const_1); 8 | const int l_const_2 = 2, l_const_3 = 2 * l_const_1, l_const_4 = 4; 9 | printf("l_const_3, should be 2: %d\n", l_const_3); 10 | const int l_const_5[3] = {1, 2, 3}, l_const_6[2][2] = {{1, l_const_1}, {}}; 11 | printf("l_const_5[2], l_const_6[0][1], should be 3, 1: %d, %d\n", l_const_5[2], l_const_6[0][1]); 12 | int l_var_0 = 0; 13 | printf("l_var_0, should be 0: %d\n", l_var_0); 14 | int l_var_1 = 2 * (l_var_0 + 1); 15 | printf("l_var_1, should be 2: %d\n", l_var_1); 16 | int l_var_2, l_var_3 = 2 * l_var_1, l_var_4; 17 | printf("l_var_3, should be 4: %d\n", l_var_3); 18 | int l_var_5; 19 | l_var_5 = 5; 20 | printf("l_var_5, should be 5: %d\n", l_var_5); 21 | int l_var_6[3] = {1, 2, 3}, l_var_7[2][2] = {{1, l_const_1}, {1, 1}}; 22 | printf("l_var_6[2], l_var_7[0][1], should be 3, 1: %d, %d", l_var_6[2], l_var_7[0][1]); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /test/testfile2.txt: -------------------------------------------------------------------------------- 1 | int main() { 2 | printf("18375200\n"); 3 | int _origin, _1, _2, _3, _4, _5, _6, _7, _8, _9; 4 | _origin = getint(); 5 | _1 = _origin * + - +3; 6 | printf("%d\n", _1); 7 | _2 = (_1 % 10086) % 2333; 8 | printf("%d\n", _2); 9 | _3 = _2 * (_1 * _1) / 326; 10 | printf("%d\n", _3); 11 | printf("ppgod7mi"); 12 | printf("\n%dyoga%d\n", 4396, 7); 13 | printf("2333\n\n\n\n"); 14 | _4 = getint(); 15 | _5 = 2; 16 | if (_4 >= 2) _6 = 1; 17 | else _6 = 0; 18 | while (_5 <= _4 / 2) { 19 | if (_4 % _5 == 0) { 20 | _6 = 0; 21 | break; 22 | } 23 | _5 = _5 + 1; 24 | } 25 | if (_6 == 0)printf("%d is not a prime, with factor %d.\n", _4, _5); 26 | else printf("%d is a prime.\n", _4); 27 | _7 = getint(); 28 | _8 = 1; 29 | _9 = 1; 30 | _2 = 2; 31 | while (_2 < _7) { 32 | _3 = (_8 + _9) % 1000007; 33 | _8 = _9; 34 | _9 = _3; 35 | _2 = _2 + 1; 36 | } 37 | printf("The %dth Fibonacci num is %d.", _7, _9); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /test/testfile20.txt: -------------------------------------------------------------------------------- 1 | 2 | int isPrime(int m) { 3 | int i; 4 | i = 2; 5 | while (i < m) { 6 | if (m % i == 0) 7 | return 0; 8 | i = i + 1; 9 | } 10 | return 1; 11 | } 12 | 13 | 14 | void check_group(int t[]) { 15 | int i; 16 | i = 0; 17 | while (i < 2) { 18 | int tmp; 19 | tmp = t[i]; 20 | tmp = isPrime(tmp); 21 | printf("%d\n", tmp); 22 | i = i + 1; 23 | } 24 | 25 | 26 | } 27 | 28 | int test(int t[][2]) { 29 | if (!t[0][1]) { 30 | ; 31 | } 32 | return 0; 33 | } 34 | 35 | int main() { 36 | 37 | printf("19373061\n"); 38 | int num; 39 | num = 30; 40 | printf("%d\n", isPrime(num)); 41 | num = 31; 42 | printf("%d\n", isPrime(num)); 43 | int tmp[3][2] = {{111, 2222},{3, 4},{5, 6}}; 44 | check_group(tmp[0]); 45 | check_group(tmp[1]); 46 | check_group(tmp[2]); 47 | 48 | int t; 49 | t = 1; 50 | while (t > 0) { 51 | if (t < 10) 52 | break; 53 | t = t + 1; 54 | } 55 | printf("t:%d\n", t); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /test/testfile21.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/testfile21.txt -------------------------------------------------------------------------------- /test/testfile23.txt: -------------------------------------------------------------------------------- 1 | 2 | int a,b=2+0; 3 | int c=3+0; 4 | int d=4; 5 | 6 | void shayemeiyou(){ 7 | int m; 8 | } 9 | int yi(){ 10 | return 1; 11 | } 12 | int pidoubushi(int n){ 13 | return 0; 14 | } 15 | int mult(int g,int k){ 16 | return g*k; 17 | } 18 | int main(){ 19 | int i=0; 20 | int m; 21 | int s=1; 22 | printf("19373044\n"); 23 | printf("s+1=%d\n",s+1); 24 | m=getint(); 25 | printf("m=%d\n",m); 26 | { 27 | } 28 | if(!m){ 29 | m=m+yi(); 30 | } 31 | printf("m=%d\n",m); 32 | if(m==1){ 33 | m=m-1; 34 | } 35 | else; 36 | printf("m=%d\n",m); 37 | while(m!=3){ 38 | printf("m=%d\n",m); 39 | m=-+-((mult(m,1)/1+1)%4); 40 | } 41 | i=mult(m,1); 42 | printf("m=%d\n",m); 43 | while(m>0){ 44 | if(m<=0){ 45 | continue; 46 | } 47 | printf("before break\n"); 48 | break; 49 | } 50 | if(m>=0){ 51 | } 52 | return 0; 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /test/testfile24.txt: -------------------------------------------------------------------------------- 1 | const int p = 88; 2 | int q = 66, r = 155; 3 | int s = 15; 4 | int max(int a, int b) { 5 | if (a > b) { 6 | return a; 7 | } 8 | else { 9 | return b; 10 | } 11 | return a; 12 | } 13 | int min(int a, int b) { 14 | if (a < b) { 15 | return a; 16 | } 17 | else { 18 | return b; 19 | } 20 | return a; 21 | } 22 | int scan() { 23 | int tmp = 0; 24 | tmp = getint(); 25 | return tmp + 0; 26 | } 27 | void print(int num) { 28 | printf("%d\n", num); 29 | return; 30 | } 31 | void noUse(int a) { 32 | int b = a; 33 | } 34 | int mid(int a, int b, int c) { 35 | int m = 0; 36 | if (max(a, b) == min(b, c)) { 37 | return b; 38 | } 39 | else { 40 | if (max(a, b) != min(a, c)) 41 | return c; 42 | else 43 | return a; 44 | } 45 | return b; 46 | } 47 | int factorial(int n) { 48 | int i = n; 49 | int ret = 1; 50 | if (n > 20) { 51 | printf("Your Num is too Big!!!\n"); 52 | return -1; 53 | } 54 | while (i) { 55 | ret = ret * i; 56 | i = i - 1; 57 | } 58 | return ret; 59 | } 60 | int main() { 61 | int a = max(min(p, q), max(s, scan())); 62 | int b = min(r, scan()), c = 58; 63 | const int d = 65535; 64 | int bool1 = 0, bool2 = 1, bool3 = -1; 65 | int tmp = -10; 66 | printf("19373022\n"); 67 | b = b + c - a; 68 | printf("b:%d\n", b); 69 | ; 70 | (bool2 + bool3); 71 | bool1; 72 | if (!bool1) { 73 | printf("Bool1 is false!\n"); 74 | } 75 | 2147483647; 76 | { 77 | int tmp = 0; 78 | tmp = scan(); 79 | print(tmp + 10); 80 | c = tmp; 81 | } 82 | a = scan(); 83 | b = scan(); 84 | c = scan(); 85 | if (mid(a, b, c) <= a) { 86 | printf("Good!,Num is %d\n", a + mid(a, b, c) / 6 * c % 2 - (bool1 * bool3)); 87 | } 88 | else { 89 | if (mid(a, b, c) < c) { 90 | printf("Oh!?\n"); 91 | } 92 | else { 93 | printf("%d\n", factorial(mid(a, b, c) % d)); 94 | } 95 | } 96 | noUse(a); 97 | return 0; 98 | } -------------------------------------------------------------------------------- /test/testfile26.txt: -------------------------------------------------------------------------------- 1 | const int cn0 = 12; 2 | const int cn1 = cn0 + 1, cn2 = cn1 + cn0, cn3 = 76 * 12; 3 | int vr0 = cn1; 4 | int vr1; 5 | int vr2, vr3 = 99, vr4; 6 | 7 | int rand() { 8 | return 180; 9 | } 10 | 11 | int addSum(int x) { 12 | int t = x, sum = 0; 13 | while (t>t == t y) return gcd(x, x % y); 24 | return gcd(y, y%x); 25 | } 26 | 27 | void fv0() { 28 | return; 29 | } 30 | 31 | void fv1(int x, int y, int z) { 32 | 33 | } 34 | 35 | int main() { 36 | const int cn4 = 23; 37 | int vr5 = 0; 38 | printf("%d%d%d%d%d%d%d%d\n",1,9,3,7,3,2,7,6); 39 | printf(" !()*+,-./0123456789:;<>=?@[]^_`~{}|\n"); 40 | printf("qwertyuiopasd\nfghjklzxcvbnm"); 41 | printf("QWERTYUIOPASD\nFGHJKLZXCVBNM"); 42 | vr5 = getint(); 43 | vr5 = cn4 * vr5; 44 | ; 45 | 1+1; 46 | { 47 | if (cn4 >= cn4 != cn4 <= cn4) ; 48 | fv0(); 49 | } 50 | while (vr5 > 0) { 51 | vr5 = vr5 - 1; 52 | if (vr5 % 2 == 1) continue; 53 | vr1 = vr1 + 1; 54 | vr5 = vr5 / 2; 55 | } 56 | while((1)){break;} 57 | if (1) vr3 = 9; 58 | printf("%d\n", vr5); 59 | printf("%d %d\n", gcd(72, 18), gcd(vr1, cn4)); 60 | printf("addSum: %d, %d\n", addSum(14), addSum(vr1)); 61 | printf("%d\n", +-+1*-+-rand() *(12) -vr1 /2 /2 + -+-100%97%2 - (1+1)*(1+1) *(1+1)); 62 | printf("1008%d\n", -6); 63 | printf("end"); 64 | return 0; 65 | } -------------------------------------------------------------------------------- /test/testfile27.txt: -------------------------------------------------------------------------------- 1 | // 编译单元 2 | /* add exp 3 | 4 | 5 | 6 | */ 7 | //声明 8 | const int const_a = 1; 9 | const int const_a0 = 1 + 3, _const_a1 = 1 + 3 * 4 / 5 % 2; 10 | int var_a; 11 | int var_a0, var_a1; 12 | int var_init = -1; 13 | int var_init0 = -2 +-+5, var_init1 = 4 / 2; 14 | 15 | //函数定义 16 | void func_void0() { 17 | return; 18 | } 19 | 20 | void func_void1(int a1) { 21 | return; 22 | } 23 | 24 | void func_void2(int a1, int a2, int a3) { 25 | return; 26 | } 27 | 28 | int func_int0() { 29 | return 0; 30 | } 31 | 32 | int func_int1(int a1) { 33 | return 1; 34 | } 35 | 36 | int func_int2(int a1, int a2) { 37 | return 2; 38 | } 39 | 40 | 41 | // 主函数 42 | int main() { 43 | printf("19373315\n"); 44 | var_a = -const_a; 45 | var_a0 = (const_a + 1) * -5 * -1 % 7 + 1 -+-5; 46 | var_a1 = 1; 47 | int tmp_a = 0; 48 | int tmp_1 = 1; 49 | ; 50 | func_void0(); 51 | 1 + 1; 52 | { 53 | 54 | } 55 | { 56 | printf("this is a yu ju ky\n"); 57 | } 58 | var_a = 1; 59 | while (var_a <= 10) { 60 | var_a = var_a + 1; 61 | if (var_a >= 8) { 62 | break; 63 | } else { 64 | continue; 65 | } 66 | } 67 | if (1) { 68 | if (1 == 1) { 69 | if (func_int0() == 0) { 70 | if (func_int1(1) == 1) { 71 | if (func_int2(1, 2) - 5 +-1 != 1) { 72 | if (1 < 2) { 73 | if (2 > 1) { 74 | printf("unbelievable!\n"); 75 | } 76 | } 77 | } 78 | } 79 | } 80 | } 81 | } 82 | if (!0) ; 83 | if (!func_int0()) ; 84 | if (2 - !func_int1(1) == 2) ; 85 | 86 | var_a = getint(); 87 | printf("var_a from getint() is %d\n", var_a); 88 | printf("getint is %d\n", 1); 89 | printf("tmp\n"); 90 | printf("tmp\n"); 91 | printf("tmp\n"); 92 | printf("tmp\n"); 93 | printf("tmp\n"); 94 | return 0; 95 | } -------------------------------------------------------------------------------- /test/testfile28.txt: -------------------------------------------------------------------------------- 1 | /*test5:Stmt的[2]~[9] */ 2 | void test() { 3 | return; 4 | } 5 | 6 | int main() 7 | { 8 | //Decl 9 | int a = 2; 10 | int b = 5; 11 | 12 | //First Stmt 13 | printf("19373332\n"); //[1] 14 | 15 | //Stmt 16 | /*[2]: [Exp] ';' //有无Exp两种情况*/ 17 | a + b; 18 | ; 19 | 20 | /*[3]: Block */ 21 | { 22 | /*重复0次*/ 23 | } 24 | { 25 | /*重复多次*/ 26 | int c = 1; 27 | int d = 0; 28 | c = a; 29 | d = b * 9 + 1; 30 | printf("Stmt-3(Block): c = %d, d = %d\n", c, d); //[2] 31 | int e = 1; 32 | int f = 0; 33 | e = 2 * c; 34 | f = e * d / c * 9 + 1; 35 | printf("Stmt-3(Block): e = %d, f = %d\n", e, f); //[3] 36 | } 37 | 38 | /*[4]: 'if' '( Cond ')' Stmt [ 'else' Stmt ] // 1.有else 2.无else*/ 39 | if(b != 0) a = a + 1; 40 | if(b != 0) { 41 | a = 0; 42 | } else { 43 | a = 1; 44 | } 45 | printf("Stmt-4(if) check finish!\n"); //[4] 46 | 47 | /*[5]: 'while' '(' Cond ')' Stmt*/ 48 | /*[6]: 'break' ';' | 'continue' ';'*/ 49 | int i = 0; 50 | while(a < 10) { 51 | i = i * 2; 52 | a = a + 1; 53 | if(a >= 6) { 54 | continue; 55 | } 56 | if(a == 5) { 57 | break; 58 | } 59 | } 60 | printf("Stmt-5,6 check finish!\n"); //[5] 61 | 62 | /*[7]: 'return' [Exp] ';' // 1.有Exp 2.无Exp*/ 63 | /*最后的return 0就是了*/ 64 | printf("Stmt-7 check finish!\n"); //[6] 65 | 66 | /*[8]: LVal = 'getint''('')'';'*/ 67 | a = getint(); 68 | //scanf("%d", &a); 69 | printf("Stmt-8 check finish!\n"); //[7] 70 | 71 | /*[9]: 'printf' '('FormatString {',' Exp} ')'';' // 1.有Exp 2.无Exp */ 72 | printf("\n"); //[8] 73 | printf("FormatChar.\n"); //[9] 74 | printf("%d\n", a * 2 + 1); //[10] 75 | 76 | test(); 77 | 78 | return 0; 79 | } -------------------------------------------------------------------------------- /test/testfile29.txt: -------------------------------------------------------------------------------- 1 | const int A = 1; 2 | const int B = 2, C = 3, D = 4; 3 | 4 | int n = 10; 5 | int m = 20, p = 99, q = 66; 6 | 7 | void f0() { 8 | return; 9 | } 10 | 11 | int f1(int x) { 12 | if (x >= 0) { 13 | x = x + 1;;;; 14 | } 15 | 16 | if (x <= 0) { 17 | x = x + 1; 18 | } else { 19 | } 20 | 21 | { 22 | x = x + 1; 23 | } 24 | 25 | while (x < 0) { 26 | x = -x; 27 | } 28 | 29 | while (x > 0) { 30 | x = -x; 31 | continue; 32 | } 33 | 34 | while (x == 0) 35 | x = x + 1; 36 | 37 | while (x != 0) { 38 | break; 39 | } 40 | 41 | while (1) { 42 | while (!1) { 43 | } 44 | break; 45 | } 46 | 47 | return x; 48 | } 49 | 50 | int f2(int x, int y, int z) { 51 | int a; 52 | a = x + n * B - m / C + z % (A + B) + 233 + -y + f1(y); 53 | a = a * +A; 54 | return x + y + a; 55 | } 56 | 57 | int gi() { 58 | int r; 59 | r = getint(); 60 | printf("Got a number: "); 61 | printf("%d!\n", r); 62 | return r; 63 | } 64 | 65 | int main() { 66 | printf("19373348\n"); 67 | 68 | int a = f2(1, 2, gi()); 69 | f0(); 70 | printf("%d\n", a); 71 | printf("%d\n", f1(gi())); 72 | int b = f2(a, A, gi()), c, d; 73 | printf("%d\n", b); 74 | c = f2(b, n, gi()); 75 | printf("%d\n", c); 76 | printf("%d\n", f2(c, gi(), q)); 77 | d = 0; 78 | return d; 79 | } 80 | -------------------------------------------------------------------------------- /test/testfile3.txt: -------------------------------------------------------------------------------- 1 | //#include 2 | const int a = 9; 3 | const int b = a; 4 | int d = a; 5 | 6 | //int getint(){ 7 | // int n; 8 | // scanf("%d",&n); 9 | // return n; 10 | // 11 | //} 12 | int mycheck(int a){ 13 | if (a == 1) return 10; 14 | if (a==2) return 20; 15 | if (a==4) return 40 ; 16 | else if (a==5) return 50; 17 | else if (a == 6) return 60; 18 | if (a==3) return 30 ;else return 99 ; 19 | return 0; 20 | return 1; 21 | } 22 | 23 | void testGetInt(){ 24 | int i = 0; 25 | 26 | while(i<9){ 27 | int temp; 28 | temp = getint(); 29 | printf("%d\n",temp); 30 | i = i + 1; 31 | } 32 | } 33 | 34 | 35 | int main(){ 36 | const int in1 = 89; 37 | const int in2 = in1; 38 | int vin1 = in1; 39 | int vin2 = vin1; 40 | int vin3 = in2; 41 | int vin4 = a + b; 42 | int vin5 = d; 43 | 44 | 45 | printf("%d\n",19231177); 46 | testGetInt(); 47 | int a = 1; 48 | while(a<10){ 49 | printf("%d\n",mycheck(a)); 50 | if (mycheck(a) < mycheck(mycheck(a)/20) ){ 51 | printf("appear\n"); 52 | }else{ 53 | printf("not\n"); 54 | } 55 | a = a + 1; 56 | } 57 | int a1 = 1,a2 = 2,a3 = 3; 58 | printf("print123\n"); 59 | printf("%d%d%d\n",1,2,3); 60 | printf("/****/%d%d%d\n",1,a2,a3); 61 | printf("/**/%d%d%d\n",a1,a2,a3); 62 | printf("print return value :\n"); 63 | int temp = 5; 64 | printf("in : 5 ,out = %d\n",mycheck(5)); 65 | printf("in : 5 ,out = %d\n",mycheck(temp)); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /test/testfile31.txt: -------------------------------------------------------------------------------- 1 | const int a0 = 1; 2 | const int b0 = 2, c0 = 3; 3 | int a1 = 0, b1 = 0; 4 | int c1 = 0; 5 | 6 | const int s1[3] = {1,2,3}; 7 | const int s2[2][2] = {{1,2}, {3,4}}; 8 | int s3[3] = {1,2,3}; 9 | int s4[2][2] = {{1,2}, {3,4}}; 10 | 11 | int dealSet0(int s1[], int s2[][2]) { 12 | int i = 0, sum = 0; 13 | while (i<2) { 14 | sum = sum + s1[i] + s2[i][0] + s2[i][1]; 15 | i = i + 1; 16 | } 17 | return sum; 18 | } 19 | 20 | int getnumber() { 21 | int instantA,instantB,instantC; 22 | instantA = 1 + a0 + (b0 + c0); 23 | instantB = instantA * instantA / instantA % b0; 24 | instantB = instantA - (instantB + c0); 25 | if (b1b1!=a1>a1==b1<=b1==b1>=b1) { 26 | instantC = 1; 27 | } 28 | printf("A = %d\n", instantA); 29 | printf("B = %d\n", instantB); 30 | printf("C = %d\n", instantC); 31 | return 1; 32 | } 33 | 34 | int addself(int a) { 35 | return a+a; 36 | } 37 | 38 | int multself(int a) { 39 | return a*a; 40 | } 41 | 42 | void none(){ 43 | return; 44 | } 45 | 46 | void minus(int a, int b) { 47 | } 48 | 49 | int main() { 50 | int value; 51 | int i = 0, cycle; 52 | ; 53 | {} 54 | printf("19231007\n"); 55 | printf("input recurrent cycle:\n"); 56 | cycle = getint(); 57 | printf("cycle is: %d\n", cycle); 58 | 59 | printf("input initial value:\n"); 60 | value = getint(); 61 | printf("initial value is: %d\n", value); 62 | 63 | if (!0) { 64 | printf("your input is good\n"); 65 | while (i 2 | const int _a = 3+2+-+2%2/1,_12 = 85; 3 | const int a=3+(1*2)/2*_a; 4 | const int arrayA[2] = {1,2}; 5 | const int array_b[2][1] = {{1},{arrayA[0]}}; 6 | int array_c[2] = {1,2}; 7 | int array_d[2][1] = {{1},{2}}; 8 | int b=12,c_1; 9 | int d = _a*_a+_a; 10 | int func1(int c){ 11 | return 1; 12 | } 13 | void func2(){ 14 | return; 15 | } 16 | void _func1 (){ 17 | const int _a = 10,_12 = 12; 18 | int array_d[2][1] = {{1},{array_c[1]}}; 19 | int a=3+(1*20)/2*_a; 20 | int b=121,c_1; 21 | int d = _a*_a+_a+arrayA[0]; 22 | printf("_a=%d\n",_a); 23 | printf("_a=%d\n",a); 24 | if(a==0) 25 | a = a+-+1; 26 | else 27 | //scanf("%d",&c_1); 28 | //c_1 = getint(); 29 | //scanf("%d",&c_1); 30 | a = 1; 31 | while(a<10 && a>0 ){ 32 | a=a+1; 33 | if(a==5 || a==6 && a!=5*1%5 && a<10 && a>5 && a>=5 && a<=6 &&!(1-1)&&a )break; 34 | else; 35 | a=a+1; 36 | } 37 | while(a<10){ 38 | a=a+1; 39 | if(a==5) continue; 40 | } 41 | _a+1; 42 | b=_a%2; 43 | return; 44 | } 45 | int main(){ 46 | int x; 47 | printf("19231150\n"); 48 | //scanf("%d",&x); 49 | x=getint(); 50 | printf("what you enter in is:%d\n",x); 51 | printf("_a=%d\n",_a); 52 | printf("array_c[0] is:%d\n",array_c[0]); 53 | printf("array_d[0][0] is:%d\n",array_d[0][0]); 54 | printf("array_d[1][0] is:%d\n",array_d[1][0]); 55 | _func1(); 56 | int i=2; 57 | int ext; 58 | while(i1){} 64 | if (1<1 == 1<=1) {} 65 | if (1<=1 != 1>= 1) {} 66 | if (!1) {} 67 | if (1 && 1) {}; 68 | if (1 ||1 ) {}; 69 | 70 | while (0) break ; 71 | while (0) continue; 72 | 73 | 74 | 75 | var01 = getint(); 76 | var1DArr01[0] = 0; 77 | var2DArr01[0][0] = 0; 78 | 79 | printf("1"); 80 | printf("%d", var01); 81 | 82 | printf("1"); 83 | printf("1"); 84 | printf("1"); 85 | printf("1"); 86 | printf("1"); 87 | printf("1"); 88 | printf("1"); 89 | 90 | 0; 91 | return 0; 92 | 93 | } 94 | -------------------------------------------------------------------------------- /test/testfile37.txt: -------------------------------------------------------------------------------- 1 | const int a = 1; 2 | const int b = 2 * 6 + 1, _a_b[2%2+1] = {5}, _a_3_b[2/2+1][2*1-1] = {{0},{3452%2+4*3}}; 3 | int c, d[2%2+1], _a_4_b[2/2+1][2*1-1] = {{0},{3452%2+4*3}}, z; 4 | int _d_e = 1; 5 | int glo_1, glo_2, glo_3, glo_4, glo_5, glo_6, glo_7, glo_8, glo_9; 6 | 7 | int test_1(int var_1, int var_2[], int var_3[][1]){ 8 | c = var_1 + var_2[0] + var_3[0][0] + 3452340 + 0; 9 | return c; 10 | } 11 | 12 | int test_2(int var_6){ 13 | z = var_6; 14 | return (z + 6%4/2) * 2; 15 | } 16 | 17 | void test_3() { 18 | } 19 | 20 | void test_4() { 21 | return; 22 | } 23 | 24 | int test_5() { 25 | return 2; 26 | } 27 | 28 | int main() { 29 | const int f = 1; 30 | const int g = 2 * 6 + 1, _a_b_[2%2+1] = {a}, _a_3_b_d[2/2+1][2*1-1] = {{0},{3452%2+4*3}}; 31 | int c, d[2%2+1], _a_4_b_d[2/2+1][2*1-1] = {{0},{3452%2+4*3}}; 32 | int _d_e_g = (_a_3_b_d[0][0] + _a_4_b_d[0][0]) * b + 1; 33 | d[0] = 1; 34 | _a_4_b_d[0][0] = 1; 35 | c = _d_e_g + _a_4_b_d[0][0]; 36 | c = +-+d[0]; 37 | 5 * 5 + 1; 38 | glo_1 = getint(); 39 | glo_2 = getint(); 40 | glo_3 = getint(); 41 | glo_4 = getint(); 42 | glo_5 = getint(); 43 | glo_6 = getint(); 44 | glo_7 = getint(); 45 | glo_8 = getint(); 46 | glo_9 = getint(); 47 | c = -+-test_5() + test_1(c, d, _a_4_b_d) + test_1(_a_4_b_d[0][0], _a_4_b_d[0], _a_4_b_d) - test_1(_a_4_b_d[0][0], _a_4_b_d[0], _a_4_b_d); 48 | z = test_2(c); 49 | while(glo_9 >= 0 || 0 && 1 || !0 && glo_1 < 0 && 0 <= glo_8 && glo_7 > 0 && glo_6 != glo_5 && (glo_4 % 2 / 2 + test_1(c, d, _a_4_b_d) + test_1(_a_4_b_d[0][0], _a_4_b_d[0], _a_4_b_d) - test_1(_a_4_b_d[0][0], _a_4_b_d[0], _a_4_b_d) * 20) && glo_6 == glo_6 ) { 50 | if(glo_2>1) { 51 | glo_2 = glo_2 - 1; 52 | glo_9 = glo_9 - 1; 53 | continue; 54 | } else { 55 | glo_2 = glo_2 - 1; 56 | glo_1 = glo_1 - 1; 57 | if (glo_1 < 0) { 58 | glo_8 = glo_8 - 1; 59 | break; 60 | } 61 | else { 62 | ; 63 | } 64 | } 65 | } 66 | printf("19231076\n"); 67 | printf(">/!~varg1 is:%d;~\n", glo_1); 68 | printf(">/!~varg2 is:%d;~\n", glo_2); 69 | printf(">/!~varg3 is:%d;~\n", glo_3); 70 | printf(">/!~varg4 is:%d;~\n", glo_4); 71 | printf(">/!~varg5 is:%d;~\n", glo_5); 72 | printf(">/!~varg6 is:%d;~\n", glo_6); 73 | printf(">/!~varg7 is:%d;~\n", glo_7); 74 | printf(">/!~varg8 is:%d;~\n", glo_8); 75 | printf(">/!~varg9 is:%d;~\n", glo_9); 76 | return 0; 77 | } -------------------------------------------------------------------------------- /test/testfile38.txt: -------------------------------------------------------------------------------- 1 | void fun_1(){ 2 | return; 3 | } 4 | 5 | void fun_2(int a){ 6 | return; 7 | } 8 | 9 | void fun_3(int a, int b){ 10 | fun_2(a); 11 | } 12 | 13 | void fun_4(int a, int b, int c){ 14 | if(a <= b || a < b || a > b || a >= b){ 15 | if(b <= c && b == a && a != c){ 16 | 17 | } 18 | } 19 | 20 | } 21 | 22 | void fun_5(int a[], int b[][2]){ 23 | int t1 = 2, t2 = 1, t3; 24 | fun_4(t1, t2, t3 - t2); 25 | if(t1 + t2 == 0){ 26 | fun_4(t1, t2, t3); 27 | }else if(t2 + t2 != 0){ 28 | fun_4(t2, t1, t3); 29 | if(t3 == 1){ 30 | t3 = 0; 31 | } 32 | }else{ 33 | return; 34 | } 35 | } 36 | 37 | 38 | void fun_6(){ 39 | int var_1; 40 | var_1 = 1 + 1; 41 | var_1 = 1 + 1 * 3; 42 | var_1 = 3 * (var_1 / (-2) + 1 % (23 * +2)); 43 | int a[2][2] = {{1,2}, {1,2}}; 44 | // func_5(a[1], a); 45 | var_1 = var_1 + var_1 / var_1; 46 | { 47 | int var_1; 48 | int var_2[23]; 49 | 50 | var_1 = 0; 51 | var_1 = 3 % 5; 52 | var_1 = var_1 * 5; 53 | } 54 | return; 55 | } 56 | 57 | void fun_7(int a[]){ 58 | 59 | } 60 | 61 | int funInt(int a, int b[][2]){ 62 | int var_1[2][2], var_2[1]; 63 | var_1[1][1] = 2; 64 | var_2[0] = 3; 65 | int c = 0, d = 0; 66 | int i = 0; 67 | if(c == 0){ 68 | c = 1; 69 | } 70 | while(i < a){ 71 | if(i == c + d){ 72 | break; 73 | } else { 74 | continue; 75 | } 76 | i = i + 1; 77 | } 78 | 79 | while(i < !a || i >= 0){ 80 | if(i != 0){ 81 | i = i - 2; 82 | } 83 | i = i - 1; 84 | } 85 | return 1; 86 | } 87 | 88 | // MainFuncDef 89 | int main(){ 90 | int n; 91 | ; 92 | n = getint(); 93 | fun_6(); 94 | int a[2][2]; 95 | int b[2]; 96 | fun_7(a[1]); 97 | fun_7(b); 98 | printf("19373385"); 99 | printf("2"); 100 | printf("3"); 101 | printf("4"); 102 | printf("5"); 103 | printf("6"); 104 | printf("7"); 105 | printf("8"); 106 | printf("9"); 107 | printf("%d", n); 108 | return 0; 109 | } -------------------------------------------------------------------------------- /test/testfile4.txt: -------------------------------------------------------------------------------- 1 | const int max_number = 100; 2 | const int min_number = 0; 3 | int a = 0; 4 | int main(){ 5 | printf("19231204"); 6 | printf("\n"); 7 | int score; 8 | int b = 30; 9 | score = getint(); 10 | if (score >= min_number){ 11 | printf("The score is %d.\n",score); 12 | } 13 | a = 4 / 2; 14 | if (a == 2){ 15 | printf("Yes!\n"); 16 | } 17 | a = 3 + 5; 18 | if (a != 8){ 19 | printf("No!\n"); 20 | }else{ 21 | printf("Yes!\n"); 22 | } 23 | a = 10 * 10; 24 | if (a <= max_number){ 25 | printf("Yes!\n"); 26 | } 27 | if (a < max_number){ 28 | printf("Yes!\n"); 29 | } 30 | if (a > b) { 31 | printf("No!"); 32 | }else { 33 | printf("Yes!"); 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /test/testfile40.txt: -------------------------------------------------------------------------------- 1 | const int MAX = 6; 2 | 3 | int main() 4 | { 5 | printf("19182616\n"); 6 | int a = 0; 7 | int num = 0; 8 | int i = 2; 9 | int c,d; 10 | int j = 1, sum; 11 | int k; 12 | num = getint(); 13 | c = getint(); 14 | d = getint(); 15 | sum = getint(); 16 | k = getint(); 17 | if(num >= 2) 18 | { 19 | while(i0 && d>0) 36 | { 37 | printf("c and d are both positive\n"); 38 | } 39 | else 40 | { 41 | printf("Neither c nor d are positive\n"); 42 | } 43 | 44 | while(1) 45 | { 46 | sum = sum + 1; 47 | printf("%d\n", sum); 48 | j = j + 1; 49 | if(j > 3)break; 50 | } 51 | while(k= 3){ 5 | printf("%d\n",id); 6 | i = i - 1; 7 | } 8 | while(i > 0){ 9 | printf("%d\n",id); 10 | i = i - 1; 11 | } 12 | int j = 0; 13 | while(j <= 3){ 14 | printf("%d\n",id); 15 | j = j + 1; 16 | } 17 | while(j < 5){ 18 | printf("%d\n",id); 19 | j = j + 1; 20 | } 21 | return; 22 | } 23 | 24 | int playID(int id) 25 | { 26 | int i = 0; 27 | while(i < id){ 28 | i = i + 1; 29 | if(i == 711){ 30 | continue; 31 | } 32 | if(i == 19182622){ 33 | break; 34 | } 35 | } 36 | return i; 37 | } 38 | int block(){return 1;} 39 | 40 | int main() 41 | { 42 | const int id = 19182622; 43 | printMyID(id); 44 | while(0); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /test/testfile43.txt: -------------------------------------------------------------------------------- 1 | const int Mod = 10007; 2 | 3 | const int Map[2][2] = { 4 | {1, 1}, 5 | {1, 0} 6 | }, Bound[2] = {2, 2}; 7 | 8 | int T, f[2] = {1, 0}; 9 | int ori[2][2] = { 10 | {1, 0}, 11 | {0, 0} 12 | }; 13 | 14 | void calc(int a[][2], int b[][2]) { 15 | int i = 0, j = 0, k = 0; 16 | int Map[2][2]; 17 | while (i < Bound[0]) { 18 | j = 0; 19 | while (j < Bound[1]) { 20 | k = 0; 21 | int sum = 0; 22 | while (k < Bound[0]) { 23 | sum = (sum + a[i][k] * b[k][j] % Mod) % Mod; 24 | k = k + 1; 25 | } 26 | Map[i][j] = sum; 27 | j = j + 1; 28 | } 29 | i = i + 1; 30 | } 31 | i = 0; 32 | j = 0; 33 | while (i < Bound[0]) { 34 | while (j < Bound[1]) { 35 | a[i][j] = Map[i][j]; 36 | j = j + 1; 37 | } 38 | i = i + 1; 39 | } 40 | } 41 | 42 | int getans(int a[]) { 43 | int i = 0, sum = 0; 44 | while (i < Bound[0]) { 45 | sum = sum + a[i]; 46 | i = i + 1; 47 | } 48 | return sum; 49 | } 50 | 51 | void Copy(int a[][2]) { 52 | a[0][0] = Map[0][0]; a[0][1] = Map[0][1]; 53 | a[1][0] = Map[1][0]; a[1][1] = Map[1][1]; 54 | } 55 | 56 | int main() { 57 | printf("19373459\n"); 58 | T = getint(); 59 | int temp[2][2]; 60 | Copy(temp); 61 | while (T) { 62 | if (T / 2 * 2 != T) { 63 | calc(ori, temp); 64 | } else {} 65 | T = T / 2; 66 | calc(temp, temp); 67 | } 68 | int i = 0; 69 | while (i < Bound[0]) { 70 | printf("f%d: %d\n", f[i], getans(ori[i])); 71 | i = i + 1; 72 | } 73 | printf("ori00:%d\n", ori[0][0]); 74 | return 0; 75 | } -------------------------------------------------------------------------------- /test/testfile46.txt: -------------------------------------------------------------------------------- 1 | int array[100][2]; 2 | int len = 0; 3 | 4 | void swap(int i, int j) { 5 | printf("swap <%d %d> <-> <%d %d>\n", array[i][0], array[i][1], array[j][0], array[j][1]); 6 | int tmp[2] = {array[i][0], array[i][1]}; 7 | array[i][0] = array[j][0]; 8 | array[i][1] = array[j][1]; 9 | array[j][0] = tmp[0]; 10 | array[j][1] = tmp[1]; 11 | return; 12 | } 13 | 14 | void sort() { 15 | int i, j; 16 | i = 0; 17 | while (i < len - 1) { 18 | int f = 0; 19 | j = i + 1; 20 | while (j < len) { 21 | if (array[i][1] < array[j][1]) { 22 | swap(i, j); 23 | f = 1; 24 | } else if (array[i][1] == array[j][1]) { 25 | if (array[i][0] > array[j][0]) { 26 | swap(i, j); 27 | f = 1; 28 | } 29 | } 30 | j = j + 1; 31 | } 32 | if (!f) { 33 | printf("%d\n", i); 34 | return; 35 | } 36 | i = i + 1; 37 | } 38 | printf("%d\n", i); 39 | return; 40 | } 41 | 42 | int main() { 43 | len = getint(); 44 | int i; 45 | printf("19373372\n"); 46 | i = 0; 47 | { 48 | int i = 100; 49 | } 50 | while (i < len) { 51 | int x; 52 | x = getint(); 53 | int y; 54 | y = getint(); 55 | printf("input > %d %d\n", x, y); 56 | array[i][0] = x; 57 | array[i][1] = y; 58 | i = i + 1; 59 | } 60 | i = 0; 61 | while (i < len) { 62 | printf("%d -> %d\n", array[i][0], array[i][1]); 63 | i = i + 1; 64 | } 65 | sort(); 66 | i = 0; 67 | while (i < len) { 68 | printf("%d -> %d\n", array[i][0], array[i][1]); 69 | i = i + 1; 70 | } 71 | printf("");printf("");printf("");printf("");printf(""); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /test/testfile5.txt: -------------------------------------------------------------------------------- 1 | const int zero = 0,one = +1, minus_one = -1; 2 | const int two = 2; 3 | const int times = zero; 4 | int while_times = zero; 5 | int groups = 10; 6 | 7 | int get_max(int a,int b,int c){ 8 | a/b; 9 | int max = a; 10 | if(b > max){ 11 | max = b; 12 | if(c > max){ 13 | max = c; 14 | } 15 | } 16 | return max; 17 | } 18 | 19 | void while_times_cal(){ 20 | while_times = while_times + 1; 21 | return; 22 | } 23 | 24 | int is_prime(int in){ 25 | int result = zero; 26 | int item = two; 27 | { 28 | 29 | } 30 | if(in == 2){ 31 | result = one; 32 | ; 33 | }else{ 34 | while(item < in){ 35 | while_times_cal(); 36 | if(in%item == 0){ 37 | result = zero; 38 | break; 39 | } 40 | result = one; 41 | item = item + 1; 42 | } 43 | } 44 | return result; 45 | } 46 | 47 | 48 | 49 | int main(){ 50 | int input; 51 | printf("16061069\n"); 52 | printf("input an integer,judge if it is prime number,10 groups in total\n"); 53 | while(groups != 0){ 54 | while_times_cal(); 55 | input = getint(); 56 | if(input <= 0){ 57 | printf("input > 0 is needed\n"); 58 | groups = groups - 1; 59 | continue; 60 | } 61 | if(input == 1){ 62 | printf("1 is not concerned\n"); 63 | groups = groups - 1; 64 | continue; 65 | }else{ 66 | if(is_prime(input) >= 1){ 67 | printf("%d is a prime number\n",input); 68 | }else{ 69 | printf("%d is not a prime number\n",input); 70 | } 71 | } 72 | groups = groups - 1; 73 | } 74 | printf("while times is %d in total\n",while_times); 75 | return 0; 76 | } -------------------------------------------------------------------------------- /test/testfile51.txt: -------------------------------------------------------------------------------- 1 | const int zhenghw = 100; 2 | const int ID = 19373339; 3 | const int abc123 = 1, abc124 = 2, abc125 = +3; 4 | int a; 5 | int b,c,d,f,g; 6 | int AA; 7 | int e = 66; 8 | const int a3[2][2] = {{1,2},{3,4}}; 9 | const int h3 = 6; 10 | int b3,c3,d3; 11 | int e3[4],f3[3],ii3[2][2] = {{9,8},{7,6}}; 12 | int g3[2] = {1,2}; 13 | int funcAA(int x[], int y[]) { 14 | int tempp; 15 | tempp = x[0] + y[0]; 16 | return tempp; 17 | } 18 | int func1 (int paramA) { 19 | int declInBlock; 20 | int temp = 2; 21 | declInBlock = abc123; 22 | declInBlock = +3; 23 | if (e) temp = 1; 24 | else temp = 2; 25 | if (e == 66) temp = 3; 26 | else temp = 4; 27 | if (e != abc123) temp = 5; 28 | else temp=6; 29 | if (e > 1) temp = 7; 30 | if (e + 4 < 80) temp = 8; 31 | if (e <= 90) temp = 9; 32 | if (e >= 100) temp = 10; 33 | if (e * 2 == 140) temp = 11; 34 | if (e / 2 == 33) temp = 12; 35 | if (e % abc124 == 1) temp = 13; 36 | if (!(0 + 1)) temp = 14; 37 | while (e - 1 >= 0) { 38 | int bbb = e - 1; 39 | e = bbb; 40 | break; 41 | } 42 | while (1 == 0) continue; 43 | return (-temp); 44 | } 45 | 46 | void func2 () { 47 | ; 48 | return; 49 | } 50 | 51 | int func3 (int xxx, int yyy) { 52 | { 53 | } 54 | return (4); 55 | } 56 | 57 | int func4 () { 58 | return 1; 59 | } 60 | 61 | int main() { 62 | a = +-zhenghw; 63 | b = abc123 + abc124 + abc124; 64 | c = abc123 + abc124*abc125; 65 | d = func3(abc123, 1); 66 | f = func1(abc124); 67 | g= func4(); 68 | b3 = a3[0][1]; 69 | c3 = a3[1][0]; 70 | d3 = a3[1][1]; 71 | int temp3,inn; 72 | int j3[2][3] = {{ii3[0][0],2,ii3[0][1]},{4,ii3[1][0],ii3[1][1]}}; 73 | temp3 = funcAA(ii3[0], j3[1]); 74 | func2(); 75 | inn = getint(); 76 | printf("19373339"); 77 | printf("\n"); 78 | printf("%d\n",b3); 79 | printf("%d\n",c3); 80 | printf("%d\n",d3); 81 | printf("%d\n",temp3); 82 | printf("%d\n",g3[3/3]); 83 | printf("123456\n"); 84 | printf("123456\n"); 85 | printf("123456\n"); 86 | return 0; 87 | } -------------------------------------------------------------------------------- /test/testfile52.txt: -------------------------------------------------------------------------------- 1 | void quickSort(int arr[], int left, int right) { 2 | if (left >= right) { 3 | return; 4 | } 5 | int i = left, j = right, base, temp; 6 | base = arr[left]; 7 | while (i < j) { 8 | while (arr[j] >= base && i < j) { 9 | j = j - 1; 10 | } 11 | while (arr[i] <= base && i < j) { 12 | i = i + 1; 13 | } 14 | if (i < j) { 15 | temp = arr[i]; 16 | arr[i] = arr[j]; 17 | arr[j] = temp; 18 | } 19 | } 20 | arr[left] = arr[i]; 21 | arr[i] = base; 22 | quickSort(arr, left, i - 1); 23 | quickSort(arr, i + 1, right); 24 | } 25 | 26 | int main() { 27 | printf("19373611\n"); 28 | printf("19373611\n"); 29 | printf("19373611\n"); 30 | const int length = 5; 31 | const int array_const[5] = {1, 2, 3, 4, 5}; 32 | int array[5]; 33 | printf("Please input base number a:\n"); 34 | int a, b; 35 | a = getint(); 36 | printf("Please input base number b:\n"); 37 | b = getint(); 38 | array[0] = a + b; 39 | array[1] = a - b; 40 | array[2] = a * b; 41 | if (!b) { 42 | array[3] = array_const[3]; 43 | array[4] = array_const[4]; 44 | printf("error.\n"); 45 | } else { 46 | array[3] = a / b; 47 | array[4] = a % b; 48 | } 49 | quickSort(array, 0, 4); 50 | int i = 0; 51 | while (i < 5) { 52 | printf("The No.%d number is %d.\n", i, array[i]); 53 | i = i + 1; 54 | } 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /test/testfile53.txt: -------------------------------------------------------------------------------- 1 | const int globalConst1 = 11, globalConst2 = 22; 2 | const int globalConstArray2[3][2] = {{6,7},{8,9},{10,11}}; 3 | const int globalConstArray1[3] = {10,20,30}; 4 | int globalVirable1 = 111, globalVirable2; 5 | void assignmentArray(int n,int array1[], int array2[][2]){ 6 | int i = 0; 7 | int j = i; 8 | int m = n - 1; 9 | while(i<3 && i<=m){ 10 | array1[i] = globalConstArray1[i] / 10 - 10; 11 | i = i + 1; 12 | } 13 | i = i - 1; 14 | while(i>=0){ 15 | array1[i] = globalConstArray1[i] / 10 - 10; 16 | while(1){ 17 | if(j < 2){ 18 | array2[i][j] = globalConstArray2[i][j] % 6 + 6; 19 | j = j + 1; 20 | continue; 21 | } 22 | else{ 23 | j = 0; 24 | printf("[%d][%d]:%d|%d , [%d]:%d|%d\n",i,j,array2[i][j],globalConstArray2[i][j],i,array1[i],globalConstArray1[i]); 25 | break; 26 | } 27 | } 28 | i = i - 1; 29 | } 30 | return; 31 | } 32 | int main(){ 33 | printf("19373063\n"); 34 | int localVirable1 = globalConst1; 35 | int localVirableArray1[3],localVirableArray2[3][2]; 36 | int localVirable2 = globalConstArray2[2][0]; 37 | int n = 3; 38 | printf("1.localVirable1 = %d,localVirable2 = %d \n2.\n",localVirable1, localVirable2); 39 | 40 | assignmentArray(n,localVirableArray1, localVirableArray2); 41 | printf("%d?\n",localVirableArray2[1][1]); 42 | printf("3.function of kinds of parameters testified\n"); 43 | ; 44 | if(1>0) 45 | { 46 | 47 | } 48 | printf("4.non exp testified\n"); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /test/testfile58.txt: -------------------------------------------------------------------------------- 1 | const int month_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; 2 | 3 | 4 | int get_days(int year,int month,int day){ 5 | int flag = 0; 6 | int i = 0; 7 | int days = 0; 8 | if(!(year%4) && (year%100)){ 9 | flag = 1; 10 | }else{ 11 | if(year%400 == 0){ 12 | flag = 1; 13 | } 14 | } 15 | while(i < month - 1){ 16 | days = days + month_days[i]; 17 | i = i + 1; 18 | } 19 | days = days + day; 20 | if(month > 2){ 21 | days = days + flag; 22 | } 23 | return days; 24 | } 25 | 26 | int main(){ 27 | int groups; 28 | int birth_year; 29 | int birth_month; 30 | int birth_day; 31 | int cur_year; 32 | int cur_month; 33 | int cur_day; 34 | int year; 35 | printf("16061069\n"); 36 | printf("input groups\n"); 37 | groups = getint(); 38 | while(groups != 0){ 39 | int live_days = 0; 40 | printf("input your birth year,month,day,ep:1998 1 1\n"); 41 | birth_year = getint(); 42 | birth_month = getint(); 43 | birth_day = getint(); 44 | printf("input current year,month,day,ep:2020 2 2\n"); 45 | cur_year = getint(); 46 | cur_month = getint(); 47 | cur_day = getint(); 48 | year = birth_year; 49 | if(cur_year < birth_year){ 50 | printf("please input time legally\n"); 51 | }else{ 52 | while(year < cur_year){ 53 | if(year%400 == 0){ 54 | live_days = live_days + 366; 55 | }else{ 56 | if(!(year%100) || (year%4)){ 57 | live_days = live_days + 365; 58 | }else{ 59 | live_days = live_days + 366; 60 | } 61 | } 62 | year = year + 1; 63 | } 64 | live_days = live_days - get_days(birth_year,birth_month,birth_day) + get_days(cur_year,cur_month,cur_day); 65 | if(live_days >= 0){ 66 | printf("you have lived %d days\n",live_days); 67 | }else{ 68 | printf("please input time legally\n"); 69 | } 70 | } 71 | groups = groups - 1; 72 | } 73 | return 0; 74 | } -------------------------------------------------------------------------------- /test/testfile59.txt: -------------------------------------------------------------------------------- 1 | 2 | //test exp 3 | //test exp 4 | //test exp 5 | //test exp 6 | const int ca = 1, caa = 1; 7 | const int cc[3] = { 1,2,3 }; 8 | const int ce[2][3] = { {1,2,3},{4,5,6} }; 9 | int aa = 1, aaa = 1; 10 | int ab; 11 | int ac[10]; 12 | int ad[3] = { 1,2,3 }; 13 | int ae[10][10]; 14 | int af[2][3] = { {1,2,3},{4,5,6} }; 15 | int func1() { 16 | return 0; 17 | } 18 | int func2(int a) { 19 | 20 | int b = 1; 21 | int c = a * b; 22 | int d = c + a * b; 23 | int e = d / c; 24 | int f = e % d; 25 | int g = func1(); 26 | int h = g; 27 | if (!g) 28 | h = 1; 29 | int i = -a; 30 | int j = +b; 31 | int k[10]; 32 | int l[3] = { 1,2,3 }; 33 | int m[10][10]; 34 | int n[2][3] = { {1,2,3},{4,5,6} }; 35 | c = a - b; 36 | g = n[0][1]; 37 | i = l[1]; 38 | if (a == b && c == d) 39 | a = 1; 40 | if (a == b || c == d) 41 | a = 1; 42 | if (a == b) 43 | c = 1; 44 | if (a != b) 45 | c = 1; 46 | f = a; 47 | 48 | return f; 49 | } 50 | //test func 51 | //test func 52 | //test func 53 | //test func 54 | void func3(int a[]) { 55 | return; 56 | } 57 | void func4(int a, int b[][3]) { 58 | printf("%d\n", a); 59 | } 60 | void func5(int a, int b) { 61 | 62 | func3(ad); 63 | } 64 | int func6(int a, int b) { 65 | func5(12, 13); 66 | func5(ce[0][1], ce[0][2]); 67 | return 0; 68 | } 69 | int func7() { 70 | int a = 1; 71 | if(a == 100) { 72 | a = 1; 73 | } 74 | if (a >= 1) a = 1; 75 | if (a <= 1) a = 1; 76 | if (a > 1) a = 1; 77 | if (a < 1) a = 1; 78 | if (a == 1) a = 2; 79 | else a = 3; 80 | if (a != 1) a = 2; 81 | else a = 3; 82 | while (a == 5) 83 | a = 4; 84 | while (a == 6) 85 | break; 86 | while (a == 100) 87 | continue; 88 | a = getint(); 89 | return a; 90 | } 91 | void func10() { 92 | 93 | } 94 | int main() 95 | { 96 | printf("19373734\n"); 97 | printf("19373734\n"); 98 | printf("19373734\n"); 99 | printf("19373734\n"); 100 | printf("19373734\n"); 101 | printf("19373734\n"); 102 | printf("19373734\n"); 103 | printf("19373734\n"); 104 | printf("19373734\n"); 105 | printf("19373734"); 106 | int number1 = func2(3); 107 | int b = (12 + 3); 108 | ; 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /test/testfile6.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guo-yanpei/SysY-compiler/8a6017b4a9349ed6ed9166482e776bd82fda3bb8/test/testfile6.txt -------------------------------------------------------------------------------- /test/testfile60.txt: -------------------------------------------------------------------------------- 1 | int getlogictrue() { 2 | return 1; 3 | } 4 | int main() { 5 | printf("19231258 "); 6 | if (!0 == 1 && 1 == 1) { 7 | printf("OK1\n"); 8 | } 9 | if (1 == 1 && 1 == 1 && 1 == 1 || 2 == 1 && 1 == 1) { 10 | printf("OK2\n"); 11 | } 12 | if (+-1 == -+-+0 && 1 == 1 && 2 == 2) { 13 | ; 14 | } else { 15 | printf("OK3\n"); 16 | } 17 | if (1 || 2 == 3) { 18 | printf("OK4\n"); 19 | } 20 | if (0 || 0 && 3) { 21 | ; 22 | } else { 23 | printf("OK5\n"); 24 | } 25 | if (0 || 1 && 2) { 26 | printf("OK6\n"); 27 | } 28 | if (1 >= 0 == 1 && 1 || 2 && 3 == 1 > 0) { 29 | printf("OK7\n"); 30 | } else { 31 | printf("OK7\n"); 32 | } 33 | if (1 + 1 > 0 != !1 && 7 || 8 && !7 == 0) { 34 | printf("OK8\n"); 35 | } 36 | if (!(getlogictrue() * 1) >= 0) { 37 | printf("OK9\n"); 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /test/testfile64.txt: -------------------------------------------------------------------------------- 1 | const int a=0; 2 | const int b=10,c=100; 3 | const int A[2]={0,1}; 4 | const int B[5][5]={{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; 5 | int p =0; 6 | void Func5(int g) 7 | { 8 | return ; 9 | } 10 | void Func4(int N[][2]) 11 | { 12 | printf("Func4OK\n"); 13 | return ; 14 | } 15 | void Func3(int M[]) 16 | { 17 | printf("Func3OK\n"); 18 | return ; 19 | } 20 | int Func2(int z) 21 | { 22 | z=z*(z/z)%z; 23 | printf("%d",z); 24 | printf("Func2OK\n"); 25 | return z; 26 | } 27 | int Func1(int x,int y) 28 | { 29 | if(x!=y) 30 | { 31 | if(x>=y) 32 | { 33 | printf("fb:%d\n",x); 34 | return x; 35 | } 36 | else 37 | { 38 | printf("sb:%d\n",y); 39 | return y; 40 | } 41 | } 42 | printf("Func1OK\n"); 43 | return -1; 44 | } 45 | void Func0() 46 | { 47 | int m,n,i=0,j; 48 | int C[2]; 49 | int D[2][2]; 50 | m=getint(); 51 | n=getint(); 52 | while(i<2||i<2) 53 | { 54 | C[i]=getint(); 55 | i=i+1; 56 | 57 | } 58 | printf("CscanfOK\n"); 59 | while(!1); 60 | +8; 61 | i=0; 62 | 63 | while(i<=1) 64 | { 65 | j=0; 66 | while(j<2) 67 | { 68 | D[i][j]=getint(); 69 | j=j+1; 70 | } 71 | i=i+1; 72 | } 73 | printf("DscanfOK\n"); 74 | m=Func1(m,n); 75 | if(m==-1) 76 | { 77 | printf("same\n"); 78 | return ; 79 | } 80 | i=0; 81 | while(1) 82 | { 83 | if(i==0&&i>-1) 84 | { 85 | i=i-1; 86 | printf("continueOK\n"); 87 | continue; 88 | printf("continueGG\n"); 89 | } 90 | printf("breakOK\n"); 91 | break; 92 | printf("breakGG\n"); 93 | } 94 | i=1; 95 | i=Func2(i); 96 | Func3(C); 97 | Func4(D); 98 | Func5(C[0]); 99 | return ; 100 | } 101 | int main() 102 | { 103 | printf("19182619\n"); 104 | Func0(); 105 | printf("end"); 106 | return 0; 107 | } -------------------------------------------------------------------------------- /test/testfile66.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | const int c_common = 10, c_array[3] = {1, 2, 3}; 4 | const int c_matrix[3][3] = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}; 5 | int v_common, v_array[3], v_matrix[3][3]; 6 | int v_commonInit = 10; 7 | int v_arrayInit[3] = {1, 2, 3}; 8 | int v_matrixInit[3][3] = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}; 9 | 10 | 11 | 12 | void print(int output) { 13 | printf("\nf2() flag : %d", output); 14 | return; 15 | } 16 | 17 | int f0() { 18 | return 1; 19 | } 20 | 21 | int f1(int a[], int b[][3], int c[]) { 22 | return a[0] + b[0][0] + c[0]; 23 | } 24 | 25 | int f2(int a, int b) { 26 | int c; 27 | int flag; 28 | int a1[2] = {1, 2}; 29 | int a2[3][3] = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}; 30 | c = -(a*b) + a/b + (a%b) -1 + f1(a1, a2, a2[0]) - +f0(); 31 | 32 | if (a > 10 && b > 10) { 33 | print(1); 34 | flag = 0; 35 | } 36 | if (a > 10 && b <= 10) { 37 | print(2); 38 | flag = 0; 39 | } 40 | if (a <= 10 && b <= 10) { 41 | print(3); 42 | flag = 1; 43 | } 44 | if (a <= 10 && b > 10) { 45 | print(4); 46 | flag = 1; 47 | } 48 | return flag; 49 | } 50 | 51 | int main() { 52 | printf("19373528 "); 53 | ; 54 | { 55 | } 56 | int a,b,flag1,flag2,flag,i; 57 | a = getint(); 58 | b = getint(); 59 | i=0; 60 | if (a != b) { 61 | while (i < 3) { 62 | int c1; 63 | int c2; 64 | c1 = a + b; 65 | c2 = a - b; 66 | 67 | flag1=f2(c1,c2); 68 | flag2=f2(c2,c1); 69 | 70 | if (flag1 == 0 || !flag2) { 71 | flag = 0; 72 | } else { 73 | flag = 1; 74 | } 75 | printf("\nflag = %d : c1 = %d, c2 = %d", flag, c1, c2); 76 | i = i + 1; 77 | a = a + 5; 78 | b = b + 5; 79 | if (i >= 10) { 80 | continue; 81 | } 82 | if (i < 0) { 83 | break; 84 | } 85 | } 86 | } 87 | return 0; 88 | } 89 | 90 | 91 | -------------------------------------------------------------------------------- /test/testfile67.txt: -------------------------------------------------------------------------------- 1 | 2 | void de() 3 | { 4 | return; 5 | } 6 | 7 | int keke(int i,int j) 8 | { 9 | i=i+j; 10 | return 0; 11 | } 12 | 13 | int jian() 14 | { 15 | int x,y,z; 16 | x=getint(); 17 | y=getint(); 18 | z=x-y; 19 | return z; 20 | } 21 | 22 | int main() 23 | { 24 | printf("19182620\n"); 25 | int a,b,c,d, e,f,g=+1,h,j,k,l,o=-1; 26 | int i=2,n,m,flag=0; 27 | n=getint(); 28 | while(i=e) 71 | { 72 | if(e<=f) 73 | { 74 | if(f!=g) 75 | { 76 | if(c>1) 77 | { 78 | a=1; 79 | } 80 | 81 | 82 | } 83 | 84 | } 85 | } 86 | } 87 | keke(a,b); 88 | 89 | printf("%d\n%d\n%d\n%d\n",d,e,f,g); 90 | printf("19182620\n"); 91 | printf("19182620\n"); 92 | printf("19182620\n"); 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /test/testfile68.txt: -------------------------------------------------------------------------------- 1 | int main(){ 2 | printf("19182636\n"); 3 | 4 | int a = 5; 5 | int b = 6; 6 | int s1[6] = {0,1,2,3,6,12}; 7 | int s2[2][3] = {{10,11,12},{13,14,15}}; 8 | 9 | if(!s1[0]==1 && !(s1[0]*2) && !0){ 10 | printf("! is ok\n"); 11 | } else { 12 | printf("! has problem\n"); 13 | } 14 | 15 | if(a+b < s1[5]&& s1[3]-s1[0] < s2[1][2]/a==0 && bs1[1]){ 16 | printf("< is ok\n"); 17 | } else { 18 | printf("< has problem\n"); 19 | } 20 | 21 | if(a*b > s1[2]*s2[1][2]==0 && (s2[0][1]-s1[2])%a > s2[1][2]/a && s1[5]>a+b+2>0==0 && s1[5]>a+b < s1[1]==0){ 22 | printf("> is ok\n"); 23 | } else { 24 | printf("> has problem\n"); 25 | } 26 | 27 | if(a<=b && a<=b+s1[1] && a<=b<=s1[0]==0 && a<=b>=s1[2]<1){ 28 | printf("<= is ok\n"); 29 | } else { 30 | printf("<= has problem\n"); 31 | } 32 | 33 | if(b>=a+1 && b>=s1[4]+s1[1]!=1 && a<=b>=s1[1]+1==0 && a>=b>=s1[0]<=s1[0]!=1){ 34 | printf(">= is ok\n"); 35 | } else{ 36 | printf(">= has problem\n"); 37 | } 38 | 39 | if(a != b && a==b==0 && a>b == a=b == s1[2] != s1[0] != s1[4]){ 40 | printf("== != is ok\n"); 41 | } else { 42 | printf("== != has problem\n"); 43 | } 44 | 45 | if(s1[3] == s2[0][1] || a>=b || a+7%4 < b*3){ 46 | printf("or is ok\n"); 47 | } else { 48 | printf("or has problem\n"); 49 | } 50 | 51 | if(a/2 s1[5] && b > s1[5]){ 52 | printf("the priority of and/or is ok\n"); 53 | } else { 54 | printf("the priority of and/or has problem\n"); 55 | } 56 | 57 | if(a < b || +a){ 58 | a = a + 1; 59 | if(s1[0]= 0 && y < 5) { 25 | return 1; 26 | } 27 | return 0; 28 | } 29 | 30 | int check(int x, int y) { 31 | if (0 > x || 5 <= x || !Check(y)) return 0; 32 | if (Map[x][y]) { 33 | return 1; 34 | } else return 0; 35 | return 0; 36 | } 37 | 38 | void dfs(int x, int y, int base, int height[][5]) { 39 | if (val[x][y] != 0) { 40 | return; 41 | } 42 | val[x][y] = CalcMod(base * height[x][y] % Mod + Mod); 43 | if (val[x][y] == 0) val[x][y] = 1; 44 | int t1 = 0, t2 = 0; 45 | if (check(x + dx[0], y + dy[0])) { 46 | dfs(x + dx[0], y + dy[0], val[x][y], height); 47 | t1 = 1; 48 | } 49 | if (check(x + dx[1], y + dy[1])) { 50 | dfs(x + dx[1], y + dy[1], val[x][y], height); 51 | t2 = 1; 52 | } 53 | if (t1 != 0 || t2 != 0) return; 54 | val[x][y] = -1; 55 | } 56 | 57 | int main() { 58 | printf("19373459\n"); 59 | int i = 0, j; 60 | int height[5][5]; 61 | while (i < 5) { 62 | j = 0; 63 | while (j < 5) { 64 | height[i][j] = getint(); 65 | j = j + 1; 66 | } 67 | i = i + 1; 68 | if (i == 5) break; 69 | else continue; 70 | } 71 | i = 0; 72 | ; 73 | i; 74 | while(i < 2) { 75 | strP[i] = getint(); 76 | i = i + 1; 77 | } 78 | base = getint(); 79 | dfs(strP[0], strP[1], base, height); 80 | printf("val20: %d, val21:%d, val22:%d, val23:%d, val24:%d\n", val[2][0], val[2][1], val[2][2], val[2][3], val[2][4]); 81 | printf("val30: %d, val32: %d, val34: %d\n", val[3][0], val[3][2], val[3][4]); 82 | printf("val40: %d, val42:%d, val43:%d, val44:%d\n", val[4][0], val[4][2], val[4][3], val[4][4]); 83 | return 0; 84 | } -------------------------------------------------------------------------------- /test/testfile73.txt: -------------------------------------------------------------------------------- 1 | const int hehe[2][7] = {{1, 2, 5, 10, 20, 50, 100}, {1, 5, 10, 20, 50, 100, 200}}; 2 | 3 | int min(int a, int b) { 4 | if (a > b) return b; 5 | return a; 6 | } 7 | 8 | int inner1(int dp[][501], int target, int size, int num[]) { 9 | int i = 1, j = 1; 10 | while (i < size) { 11 | j = -+-1; 12 | while (j <= target) { 13 | if (j < num[i]) { 14 | dp[i][j] = dp[i - 1][j]; 15 | }else if(j == num[i]) { 16 | dp[i][j] = 1; 17 | }else{ 18 | if(dp[i - 1][j] != 0 && dp[i][j - num[i]] != 0) { 19 | dp[i][j] = min(dp[i-1][j], dp[i][j - num[i]]+1); 20 | }else{ 21 | if (dp[i - 1][j]!=0) dp[i][j] = dp[i - 1][j]; 22 | else dp[i][j] = dp[i][j - num[i]]; 23 | } 24 | } 25 | j = j + 1; 26 | } 27 | 28 | i = i + 1; 29 | } 30 | return dp[size - 1][target]; 31 | } 32 | 33 | int smallmoney(int num[], int target, int size) { 34 | int dp[11][501]; 35 | int i = 1, j = 1; 36 | 37 | dp[0][0] = 0; 38 | while (i <= target) { 39 | if (i % num[0] == 0) { 40 | dp[0][i] = i/num[0]; 41 | } else { 42 | dp[0][i] = 0; 43 | } 44 | i = i + 1; 45 | } 46 | i = 1; 47 | while (i < size) { 48 | dp[i][0] = 0; 49 | i = i + 1; 50 | } 51 | 52 | return inner1(dp, target, size, num); 53 | } 54 | 55 | int main() { 56 | int nums[2][7] = {{1, 2, 5, 10, 20, 50, 100}, {1, 5, 10, 20, 50, 100, 200}}; 57 | int i = 0, size = 9, max = 501; 58 | 59 | printf("19373487\n"); 60 | 61 | while (i < size) { 62 | int n; 63 | n = getint(); 64 | if (n >= max || n < 0) 65 | printf("out of range!!\n"); 66 | else { 67 | printf("the result is:%d\n", smallmoney(nums[0], n, 7)); 68 | } 69 | i = i + 1; 70 | } 71 | 72 | return 0; 73 | } -------------------------------------------------------------------------------- /test/testfile74.txt: -------------------------------------------------------------------------------- 1 | 2 | const int eye[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, D = 3; 3 | const int base[3] = {1, 0, 0}; 4 | int va[3][3], vb[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, vc[3] = {4, 5, 6}, vd[3], badname; 5 | 6 | int vdot(int x[], int y[]) { 7 | int d = 0, sum = 0; 8 | int useless; 9 | while (1 || 0) { 10 | sum = sum + x[d] * y[d] / 1; 11 | useless = sum % 2 - 3; 12 | d = d + 1; 13 | if (d == D || 0 && ! - sum < 7 + 1 || + sum > 8 - 1) break; 14 | else if (1) continue; 15 | else ; 16 | } 17 | return sum; 18 | } 19 | 20 | void print_id(int id) { 21 | printf("%d\n", id); 22 | if (0 && 4 > 5) return; 23 | } 24 | 25 | 26 | void print_vec(int x[]) { 27 | printf("%d %d %d\n", x[0], x[1], x[2]); 28 | } 29 | 30 | void uselessfunc() { 31 | int i = 0; 32 | i = i + 2 - 5 %2; 33 | if (i >= 3); 34 | } 35 | 36 | void print_mat(int m[][3]) { 37 | int dt = 0; 38 | while (!0) { 39 | print_vec(m[dt]); 40 | dt = dt + 1; 41 | if (dt < 3 && 1) {continue;} 42 | else if (1)break; 43 | else {} 44 | } 45 | } 46 | int main() { 47 | print_id(19373311); 48 | int d1 = 0; 49 | while (d1 != D || d1 < D) { 50 | int d2 = 0; 51 | while (d2 <= D - 1) { 52 | va[d1][d2] = getint(); 53 | d2 = d2 + 1; 54 | } 55 | d1 = d1 + 1; 56 | } 57 | int cd = 5; 58 | while (cd) { 59 | cd = cd - 1; 60 | printf("123\n"); 61 | } 62 | print_vec(vc); 63 | int ans; 64 | ans = vdot(va[0], vc); 65 | printf("%d\n", ans); 66 | ans = vdot(va[1], vb[0]); 67 | printf("%d\n", ans); 68 | ans = vdot(va[2], vb[0]); 69 | printf("%d\n", ans); 70 | ans = vdot(va[0], vb[2]); 71 | printf("%d\n", ans); 72 | ans = vdot(va[0], vb[1]); 73 | printf("%d\n", ans); 74 | print_mat(vb); 75 | uselessfunc(); 76 | return 0; 77 | } 78 | 79 | -------------------------------------------------------------------------------- /test/testfile79.txt: -------------------------------------------------------------------------------- 1 | const int global_const_var = 0; 2 | int glbal_var; 3 | int f() { 4 | return 0; 5 | } 6 | int g(int x) { 7 | int g_var; 8 | g_var = 3; 9 | return x*g_var; 10 | } 11 | void h(int x,int y[],int z[][3]) { 12 | return; 13 | } 14 | int func() { 15 | int func_var1; 16 | func_var1 = getint(); 17 | printf("123456789\n"); 18 | printf("%d\n",func_var1); 19 | func_var1 = 1+1; 20 | ; 21 | 3; 22 | { 23 | } 24 | if(1) { 25 | } 26 | if(1) { 27 | } else { 28 | } 29 | while(1){ 30 | if(1){ 31 | break; 32 | } 33 | continue; 34 | } 35 | return 1; 36 | } 37 | int main() { 38 | printf("19373421\n"); 39 | const int const_var1 = 1, const_var2 = 2; 40 | const int const_array1[2] = {1,2}; 41 | const int const_array2[2] = {}; 42 | const int const_array3[2][2] = {{1,1},{2,2}}; 43 | printf("const_var1+const_var2=:%d\n",const_var1+const_var2); 44 | printf("const_array1[1]=:%d\n",const_array1[1]); 45 | int var1; 46 | int var2,var3; 47 | int var4 = 1; 48 | int array1[2]; 49 | int array2[2] = {3,4}; 50 | int array4[2][2]; 51 | int array5[2][2] = {{3,3},{4,4}}; 52 | int array6[3][3] = {{1,1,1},{2,2,2},{3,3,3}}; 53 | printf("array2[0]:=%d\n",array2[0]); 54 | var1 = 1; 55 | array2[1] = 1; 56 | array5[1][1] = 1; 57 | (1); 58 | var1; 59 | 3; 60 | f(); 61 | g(1); 62 | printf("g(2):=%d\n",g(2)); 63 | h(1,array2,array6); 64 | -3; 65 | +3; 66 | h(1,array6[1],array6); 67 | 3+3*3+3/3-3%3; 68 | if(1<2){ 69 | if(1>2){ 70 | if(1<=2){ 71 | if(1>=2){ 72 | if(!1){ 73 | } 74 | } 75 | } 76 | } 77 | } 78 | if(1<2){ 79 | if(1<2==1){ 80 | if(3!=4){ 81 | } 82 | } 83 | } 84 | if(1!=2&&3==3){ 85 | if(1==2||3==4){ 86 | } 87 | } 88 | func(); 89 | var1 = getint(); 90 | var2 = getint(); 91 | printf("%d\n%d\n",var1+var2,var1*var2); 92 | printf("AK!!!"); 93 | return 0; 94 | } -------------------------------------------------------------------------------- /test/testfile8.txt: -------------------------------------------------------------------------------- 1 | int aa(int b[][3]) 2 | { 3 | int w; 4 | w=b[0][0]+b[0][1]+b[0][2]+b[1][0]+b[1][1]+b[1][2]; 5 | return w; 6 | } 7 | int main() 8 | { 9 | printf("19182650\n"); 10 | const int a[2][3]={{1,2,3},{4,5,6}}; 11 | int b[2][3]={{1,2,3},{4,5,6}}; 12 | int c[2][3]; 13 | int d; 14 | d=aa(b); 15 | c[0][0]=b[0][0]; 16 | c[0][1]=b[0][1]; 17 | c[0][2]=b[0][2]; 18 | c[1][0]=b[1][0]; 19 | c[1][1]=b[1][1]; 20 | c[1][2]=b[1][2]; 21 | printf("%d\n",a[0][0]); 22 | printf("%d\n",a[0][1]); 23 | printf("%d\n",a[0][2]); 24 | printf("%d\n",a[1][0]); 25 | printf("%d\n",a[1][1]); 26 | printf("%d\n",a[1][2]); 27 | printf("%d\n",d); 28 | printf("%d\n",c[0][0]); 29 | printf("%d\n",c[1][2]); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /test/testfile9.txt: -------------------------------------------------------------------------------- 1 | const int a = 1; 2 | const int b = 2 * 6 + 1, _a_b[2%2+1] = {5}, _a_3_b[2/2+1][2*1-1] = {{0},{3452%2+4*3}}; 3 | int c, d[2%2+1], _a_4_b[2/2+1][2*1-1] = {{0},{3452%2+4*3}}, z; 4 | int _d_e = 1; 5 | int glo_1, glo_2, glo_3, glo_4, glo_5, glo_6, glo_7, glo_8, glo_9; 6 | 7 | void test_1(int var_1, int var_2[], int var_3[][1]){ 8 | c = var_1 + var_2[0] + var_3[0][0]; 9 | 10 | } 11 | 12 | int main() { 13 | const int f = 1; 14 | const int g = 2 * 6 + 1, _a_b_[2%2+1] = {a}, _a_3_b_d[2/2+1][2*1-1] = {{0},{3452%2+4*3}}; 15 | int c, d[2%2+1], _a_4_b_d[2/2+1][2*1-1] = {{0},{3452%2+4*3}}; 16 | int _d_e_g = (_a_3_b_d[0][0] + _a_4_b_d[0][0]) * b + 1; 17 | d[0] = 1; 18 | _a_4_b_d[0][0] = 1; 19 | c = _d_e_g + _a_4_b_d[0][0]; 20 | glo_1 = getint(); 21 | glo_2 = getint(); 22 | glo_3 = getint(); 23 | glo_4 = getint(); 24 | glo_5 = getint(); 25 | glo_6 = getint(); 26 | glo_7 = getint(); 27 | glo_8 = getint(); 28 | glo_9 = getint(); 29 | test_1(c, d, _a_4_b_d); 30 | while(glo_9 > 0 || glo_8 > 0 && glo_7 > 0) { 31 | if(glo_2>2) { 32 | glo_2 = glo_2 - 1; 33 | continue; 34 | } else { 35 | glo_2 = glo_2 - 1; 36 | glo_1 = glo_1 - 1; 37 | if (glo_1 < 0) 38 | break; 39 | } 40 | } 41 | printf("19231076\n"); 42 | printf(">/!~varg1 is:%d;~\n", glo_1); 43 | printf(">/!~varg2 is:%d;~\n", glo_2); 44 | printf(">/!~varg3 is:%d;~\n", glo_3); 45 | printf(">/!~varg4 is:%d;~\n", glo_4); 46 | printf(">/!~varg5 is:%d;~\n", glo_5); 47 | printf(">/!~varg6 is:%d;~\n", glo_6); 48 | printf(">/!~varg7 is:%d;~\n", glo_7); 49 | printf(">/!~varg8 is:%d;~\n", glo_8); 50 | printf(">/!~varg9 is:%d;~\n", glo_9); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /语法解析重构.md: -------------------------------------------------------------------------------- 1 | CompUnit -> { VarDecl | FuncDef } 2 | 3 | VarDecl -> [const] 'int' VarDef { ',' VarDef} ';' 4 | 5 | VarDef -> Ident { '[' Expr ']' } [ '=' InitVal ] 6 | 7 | InitVal -> Expr | '{' [ InitVal { ',' InitVal }] '}' 8 | 9 | FuncDef -> FuncType `Indent | 'main'` '(' [FuncFParams] ')' Block 10 | 11 | FuncType -> 'void' | 'int' 12 | 13 | FuncFParams -> FuncFParam { ',' FuncFParam } 14 | 15 | FuncFParam -> 'int' Ident [ '[' ']' { '[' Expr ']' } ] 16 | 17 | Block -> '{' { `VarDecl | Stmt` } '}' 18 | 19 | Stmt -> AssignStmt | ExprStmt | Block | IfStmt | WhileStmt | BreakStmt | ContinueStmt | ReturnStmt | PrintfStmt 20 | 21 | AssignStmt -> Expr '=' `Expr | getint() '(' ')' ';'` 22 | 23 | ExprStmt -> [Expr] ';' 24 | 25 | IfStmt -> 'if' '(' Exp ')' Stmt [ 'else' Stmt ] 26 | 27 | WhileStmt -> 'while' '(' Expr ')' Stmt 28 | 29 | BreakStmt -> 'break' ';' 30 | 31 | ContinueStmt -> 'continue' ';' 32 | 33 | ReturnStmt -> 'return' [Expr] ';' 34 | 35 | PrintfStmt -> 'printf' '(' FormatString {, Expr} ')' ';' 36 | 37 | Expr -> PrefixExpr | BinExpr 38 | 39 | BinExpr -> `PrefixExpr | BinExpr` `'||' | '&&' | '!=' | '==' | '<' | '>' | '<=' | '>=' | '+' | '-' | '*' | '/' | '%'` `PrefixExpr | BinExpr` 40 | 41 | VarExpr -> Ident { '[' Expr ']' } 42 | 43 | PrefixExpr -> [PrefixOP] `'(' Expr ')' | LiteralExpr | FuncExpr | VarExpr` 44 | 45 | FuncExpr -> Ident '(' [FuncArgs] ')' 46 | 47 | FuncArgs -> Expr { ',' Expr } 48 | 49 | PrefixOP -> { '+' | '-' | '!' }$_1$ 50 | 51 | --------------------------------------------------------------------------------