├── .gitignore ├── C0.html ├── FinalProject └── 编译课程设计 │ ├── Debug │ ├── 编译课程设计.exe │ ├── 编译课程设计.ilk │ └── 编译课程设计.pdb │ ├── 编译课程设计.sln │ └── 编译课程设计 │ ├── ConstValue.cpp │ ├── ConstValue.h │ ├── Debug │ ├── ConstValue.obj │ ├── LexicalAnalysis.obj │ ├── SymbolTable.obj │ ├── SyntaxAnalysis.obj │ ├── error.obj │ ├── globalFunction.obj │ ├── main.obj │ ├── vc141.idb │ ├── vc141.pdb │ ├── 编译课程设计.Build.CppClean.log │ ├── 编译课程设计.log │ └── 编译课程设计.tlog │ │ ├── CL.command.1.tlog │ │ ├── CL.read.1.tlog │ │ ├── CL.write.1.tlog │ │ ├── link.command.1.tlog │ │ ├── link.read.1.tlog │ │ ├── link.write.1.tlog │ │ └── 编译课程设计.lastbuildstate │ ├── FinalProject.vcxproj │ ├── FinalProject.vcxproj.filters │ ├── FinalProject.vcxproj.user │ ├── LexicalAnalysis.cpp │ ├── LexicalAnalysis.h │ ├── ReadMe.txt │ ├── SymbolTable.cpp │ ├── SymbolTable.h │ ├── SyntaxAnalysis.cpp │ ├── SyntaxAnalysis.h │ ├── error.cpp │ ├── error.h │ ├── errorTest │ └── t1.txt │ ├── globalFunction.cpp │ ├── globalFunction.h │ ├── main.cpp │ ├── mips.asm │ ├── normalTest │ ├── globalTest(带注释).c │ ├── globalTest(标准C测试).c │ ├── globalTest(标准C测试).exe │ ├── globalTest.c │ ├── globalTest_result.txt │ ├── optimize.c │ ├── t1.txt │ ├── t1_result.txt │ ├── t2.txt │ └── t3.txt │ ├── op_mips.asm │ ├── op_tmpCode.txt │ ├── optimize.cpp │ ├── optimize.h │ ├── thinking.txt │ ├── tmpCode.txt │ ├── 代码优化.md │ ├── 编译课程设计.vcxproj │ └── 编译课程设计.vcxproj.filters ├── MIPS32.pdf ├── Pascal保留字.txt ├── README.md ├── pascal_S.pas ├── 文法解读 ├── result.txt ├── test.txt ├── 扩充的C0文法.txt └── 文法解读.docx ├── 简单词法分析程序 ├── 简易词法分析正式文档.docx ├── 简易词法分析程序.c └── 简易词法分析要求.txt └── 编译器设计文档.docx /.gitignore: -------------------------------------------------------------------------------- 1 | FinalProject/编译课程设计/.vs 2 | -------------------------------------------------------------------------------- /C0.html: -------------------------------------------------------------------------------- 1 | <程序> ::= [<常量说明>][<变量说明>]{<有返回值函数定义>|<无返回值函数定义>}<主函数> 2 | <加法运算符> ::= +|- 3 | <乘法运算符> ::= *|/ 4 | <关系运算符> ::= <|<=|>|>=|!=|== 5 | <字母> ::= _|a|...|z|A|...|Z 6 | <数字> ::= 0|<非零数字> 7 | <非零数字> ::= 1|...|9 8 | <字符> ::= '<加法运算符>'|'<乘法运算符>'|'<字母>'|'<数字>' 9 | <字符串> ::= "{十进制编码为32,33,35-126的ASCII字符}" 10 | <常量说明> ::= const<常量定义>;{ const<常量定义>;} 11 | <常量定义> ::= int<标识符>=<整数>{,<标识符>=<整数>} 12 | | char<标识符>=<字符>{,<标识符>=<字符>} 13 | <无符号整数> ::= <非零数字>{<数字>} 14 | <整数> ::= [+|-]<无符号整数>|0 15 | <标识符> ::= <字母>{<字母>|<数字>} 16 | <声明头部> ::= int<标识符> |char<标识符> 17 | <变量说明> ::= <变量定义>;{<变量定义>;} 18 | <变量定义> ::= <类型标识符>(<标识符>|<标识符>‘[’<无符号整数>‘]’){,(<标识符>|<标识符>‘[’<无符号整数>‘]’ )} 19 | <常量> ::= <整数>|<字符> 20 | <类型标识符> ::= int | char 21 | <有返回值函数定义> ::= <声明头部>‘(’<参数>‘)’ ‘{’<复合语句>‘}’|<声明头部>‘{’<复合语句>‘}’ //第一种选择为有参数的情况,第二种选择为无参数的情况 22 | 23 | <无返回值函数定义> ::= void<标识符>(’<参数>‘)’‘{’<复合语句>‘}’| void<标识符>{’<复合语句>‘}’//第一种选择为有参数的情况,第二种选择为无参数的情况 24 | 25 | <复合语句> ::= [<常量说明>][<变量说明>]<语句列> 26 | <参数> ::= <参数表> 27 | <参数表> ::= <类型标识符><标识符>{,<类型标识符><标识符>} 28 | <主函数> ::= void main‘(’‘)’‘{’<复合语句>‘}’ 29 | <表达式> ::= [+|-]<项>{<加法运算符><项>} 30 | <项> ::= <因子>{<乘法运算符><因子>} 31 | <因子> ::= <标识符>|<标识符>‘[’<表达式>‘]’|‘(’<表达式>‘)’|<整数>|<字符>|<有返回值函数调用语句> 32 | <语句> ::= <条件语句>|<循环语句>| ‘{’<语句列>‘}’|<有返回值函数调用语句>; 33 | |<无返回值函数调用语句>;|<赋值语句>;|<读语句>;|<写语句>;|<空>;|<情况语句>|<返回语句>; 34 | <赋值语句> ::= <标识符>=<表达式>|<标识符>‘[’<表达式>‘]’=<表达式> 35 | <条件语句>::= if ‘(’<条件>‘)’<语句>else<语句> 36 | <条件> ::= <表达式><关系运算符><表达式>|<表达式> //表达式为0条件为假,否则为真 37 | <循环语句> ::= while ‘(’<条件>‘)’<语句> 38 | <情况语句> ::= switch ‘(’<表达式>‘)’ ‘{’<情况表>[<缺省>] ‘}’ 39 | <情况表> ::= <情况子语句>{<情况子语句>} 40 | <情况子语句> ::= case<常量>:<语句> 41 | <缺省> ::= default : <语句> 42 | <有返回值函数调用语句> ::= <标识符>‘(’<值参数表>‘)’|<标识符> //第一种选择为有参数的情况,第二种选择为无参数的情况 43 | <无返回值函数调用语句> ::= <标识符>‘(’<值参数表>‘)’|<标识符> //第一种选择为有参数的情况,第二种选择为无参数的情况 44 | <值参数表> ::= <表达式>{,<表达式>} 45 | <语句列> ::= {<语句>} 46 | <读语句> ::= scanf ‘(’<标识符>{,<标识符>}‘)’ 47 | <写语句> ::= printf ‘(’ <字符串>,<表达式> ‘)’| printf ‘(’<字符串> ‘)’| printf ‘(’<表达式>‘)’ 48 | <返回语句> ::= return[‘(’<表达式>‘)’] 49 | 50 | 附加说明: 51 | 52 | (1)char类型的表达式,用字符的ASCII码对应的整数参加运算,在写语句中输出字符 53 | 54 | (2)标识符不区分大小写字母 55 | 56 | (3)写语句中的字符串原样输出 57 | 58 | (4)情况语句中,switch后面的表达式和case后面的常量只允许出现int和char类型;每个情况子语句执行完毕后,不继续执行后面的情况子语句 59 | 60 | (5)数组的下标从0开始 61 | 62 | switch-case有关: 63 | case的类型与switch的表达式类型要求一致,否则报错。case的常量根据switch中表达式的类型来决定是整型还是字符型。switch中表达式只有在单独是字符型变量的时候按字符型处理,只要进行过运算的,都按整型处理。 64 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/Debug/编译课程设计.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/Debug/编译课程设计.exe -------------------------------------------------------------------------------- /FinalProject/编译课程设计/Debug/编译课程设计.ilk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/Debug/编译课程设计.ilk -------------------------------------------------------------------------------- /FinalProject/编译课程设计/Debug/编译课程设计.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/Debug/编译课程设计.pdb -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26730.16 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "编译课程设计", "编译课程设计\编译课程设计.vcxproj", "{9BFF5025-D6ED-4183-B1C4-3C77B7E9235E}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {9BFF5025-D6ED-4183-B1C4-3C77B7E9235E}.Debug|x64.ActiveCfg = Debug|x64 17 | {9BFF5025-D6ED-4183-B1C4-3C77B7E9235E}.Debug|x64.Build.0 = Debug|x64 18 | {9BFF5025-D6ED-4183-B1C4-3C77B7E9235E}.Debug|x86.ActiveCfg = Debug|Win32 19 | {9BFF5025-D6ED-4183-B1C4-3C77B7E9235E}.Debug|x86.Build.0 = Debug|Win32 20 | {9BFF5025-D6ED-4183-B1C4-3C77B7E9235E}.Release|x64.ActiveCfg = Release|x64 21 | {9BFF5025-D6ED-4183-B1C4-3C77B7E9235E}.Release|x64.Build.0 = Release|x64 22 | {9BFF5025-D6ED-4183-B1C4-3C77B7E9235E}.Release|x86.ActiveCfg = Release|Win32 23 | {9BFF5025-D6ED-4183-B1C4-3C77B7E9235E}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {BD6830C0-C6ED-4080-8095-D6E108903AC5} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/ConstValue.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2017-11-22 4 | ** @location:BUAA 5 | */ 6 | #include "ConstValue.h" 7 | #include 8 | using namespace std; 9 | //保留字 10 | const char * keyWordsArr[KEY_NUM] = { 11 | "const", "int", "char", "void", 12 | "main" , "if" , "else", "switch", 13 | "case" , "default", "while", "scanf", 14 | "printf", "return" 15 | }; 16 | //记忆符(凡是带SY后缀的都是保留字的记忆符) 17 | const char * SymbolArr[SYM_NUM] = { 18 | "CONSTSY", "INTSY", "CHARSY", "VOIDSY", 19 | "MAINSY", "IFSY", "ELSESY", "SWITCHSY", 20 | "CASESY", "DEFAULTSY", "WHILESY", "SCANFSY", 21 | "PRINTFSY", "RETURNSY", "IDENTIFIER", "INTNUM", 22 | "STRING", "CHAR", "ADD", "SUB", 23 | "MULT", "DIV", "LESS", "LESSEQ", 24 | "EQUAL", "MOREEQ", "NOTEQ", "MORE", 25 | "COMMA", "COLON", "SEMI", "LSBRACKET", 26 | "RSBRACKET", "LMBRACKET", "RMBRACKET", "LBBRACKET", 27 | "RBBRACKET", "ASSIGN" 28 | }; 29 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/ConstValue.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2017-11-22 4 | ** @location:BUAA 5 | */ 6 | /*相关全局常量的定义 7 | *%1:关键词(保留字) 8 | *%2:标识符类别标识 9 | *%3:可用字符的范围 10 | */ 11 | #ifndef CONSTVALUE_H 12 | #define CONSTVALUE_H 13 | #define KEY_NUM 14 //保留字数量定义 14 | #define SYM_NUM 38 //记忆符数量 15 | #define _CRT_SECURE_NO_WARNINGS 16 | #include 17 | #include //使用sprintf 18 | using namespace std; 19 | //枚举记忆符对应的类别编码 20 | enum SymbolCode{ 21 | CONSTSY, INTSY, CHARSY, VOIDSY, 22 | MAINSY, IFSY, ELSESY, SWITCHSY, 23 | CASESY, DEFAULTSY, WHILESY, SCANFSY, 24 | PRINTFSY, RETURNSY, IDENTIFIER, INTNUM, 25 | STRING, CHAR, ADD, SUB, 26 | MULT, DIV, LESS, LESSEQ, 27 | EQUAL, MOREEQ, NOTEQ, MORE, 28 | COMMA, COLON, SEMI, LSBRACKET, 29 | RSBRACKET, LMBRACKET, RMBRACKET, LBBRACKET, 30 | RBBRACKET, ASSIGN 31 | }; 32 | 33 | //枚举相关类型 34 | enum ItemType { 35 | Constant,//常量 36 | Variable,//变量 37 | Function,//函数 38 | Parament//参数 39 | }; 40 | //对于int char string 41 | enum ValueType { 42 | IntType, 43 | CharType, 44 | StringType 45 | }; 46 | //对于函数是否具有返回值 47 | enum FunctionType { 48 | VoidType, 49 | ReturnIntType, 50 | ReturnCharType 51 | }; 52 | /*四元式结构体总设计 53 | 1.值参传入push x,push y 54 | 2.调用函数 call add 55 | 3.赋值语句 i = ret i = t1 + 1 56 | 4.条件判断 x == y x<=y 57 | 5.纯标签Label1: 58 | 6.跳转语句 goto label1 bnz label1 ... 59 | 7.函数返回 ret x ret 60 | 8.函数声明 int x 61 | 9.参数表 param x 62 | 10.print "xxxx" print 'c' print 23 print a 63 | 11.read int a, read char c 64 | */ 65 | enum TmpCodeType { 66 | ValueParamDeliver, 67 | FunctionCall, 68 | AssignState, 69 | Label, 70 | FunctionDef, 71 | ParamDef, 72 | Jump, 73 | BEZ, 74 | BNZ, 75 | BLZ, 76 | BLEZ, 77 | BGZ, 78 | BGEZ, 79 | ReadChar, 80 | ReadInt, 81 | PrintStr, 82 | PrintChar, 83 | PrintInt, 84 | PrintId, 85 | ReturnInt, 86 | ReturnChar, 87 | ReturnId, 88 | ReturnEmpty, 89 | OverProcedure 90 | }; 91 | //四元式结构体 92 | struct FourYuanItem { 93 | TmpCodeType type; 94 | ValueType valueType;//参数,print语句表达式输出 95 | FunctionType funcType; 96 | string target; 97 | string index1; 98 | bool isTargetArr; 99 | bool isLeftArr; 100 | bool isNotPrintCharId;//打印的是不是char类型的id 101 | string left; 102 | string index2; 103 | string right; 104 | char op; 105 | }; 106 | //中缀表达式转逆波兰表达式栈的项结构体 107 | struct PostfixItem { 108 | ValueType type; 109 | string str; 110 | int number; 111 | bool isNotCharVar;//是否是char型变量或者说是char型数组某个元素 112 | bool isNotOperator;//如果是char型常量,是不是运算符 113 | }; 114 | 115 | /* 116 | 可用字符32,33 35-126 117 | */ 118 | #define CHAR1 32 119 | #define CHAR2 33 120 | #define CHAR3 35 121 | #define CHAR4 126 122 | 123 | #endif -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/ConstValue.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/ConstValue.obj -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/LexicalAnalysis.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/LexicalAnalysis.obj -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/SymbolTable.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/SymbolTable.obj -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/SyntaxAnalysis.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/SyntaxAnalysis.obj -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/error.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/error.obj -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/globalFunction.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/globalFunction.obj -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/main.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/main.obj -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/vc141.idb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/vc141.idb -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/vc141.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/vc141.pdb -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.Build.CppClean.log: -------------------------------------------------------------------------------- 1 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\vc141.pdb 2 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\vc141.idb 3 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\constvalue.obj 4 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\error.obj 5 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\lexicalanalysis.obj 6 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\main.obj 7 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\symboltable.obj 8 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\syntaxanalysis.obj 9 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\globalfunction.obj 10 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\globalfunction.obj.enc 11 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\debug\编译课程设计.pdb 12 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\debug\编译课程设计.exe 13 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\debug\编译课程设计.ilk 14 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\编译课程设计.tlog\cl.command.1.tlog 15 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\编译课程设计.tlog\cl.read.1.tlog 16 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\编译课程设计.tlog\cl.write.1.tlog 17 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\编译课程设计.tlog\link.command.1.tlog 18 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\编译课程设计.tlog\link.read.1.tlog 19 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\debug\编译课程设计.tlog\link.write.1.tlog 20 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.log: -------------------------------------------------------------------------------- 1 |  globalFunction.cpp 2 | SyntaxAnalysis.cpp 3 | h:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\buaa_complier_design\finalproject\编译课程设计\编译课程设计\lexicalanalysis.h : warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失 4 | 正在生成代码... 5 | 正在跳过...(未检测到相关更改) 6 | main.cpp 7 | 编译课程设计.vcxproj -> H:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\BUAA_Complier_Design\FinalProject\编译课程设计\Debug\编译课程设计.exe 8 | 编译课程设计.vcxproj -> H:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\BUAA_Complier_Design\FinalProject\编译课程设计\Debug\编译课程设计.pdb (Partial PDB) 9 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/CL.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/CL.command.1.tlog -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/CL.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/CL.read.1.tlog -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/CL.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/CL.write.1.tlog -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/link.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/link.command.1.tlog -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/link.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/link.read.1.tlog -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/link.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/link.write.1.tlog -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/Debug/编译课程设计.tlog/编译课程设计.lastbuildstate: -------------------------------------------------------------------------------- 1 | #TargetFrameworkVersion=v4.0:PlatformToolSet=v141:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=10.0.15063.0 2 | Debug|Win32|H:\本地与远端github连接库集合\编译器课程设计与远端github连接仓库\BUAA_Complier_Design\FinalProject\编译课程设计\| 3 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/FinalProject.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {93991D27-B4C8-4DD1-AF06-E07FA3541460} 15 | Win32Proj 16 | FinalProject 17 | 10.0.16299.0 18 | 19 | 20 | 21 | Application 22 | true 23 | Unicode 24 | v141 25 | 26 | 27 | Application 28 | false 29 | true 30 | Unicode 31 | v141 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | true 45 | 46 | 47 | false 48 | 49 | 50 | 51 | 52 | 53 | Level3 54 | Disabled 55 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 56 | 57 | 58 | Console 59 | true 60 | 61 | 62 | 63 | 64 | Level3 65 | 66 | 67 | MaxSpeed 68 | true 69 | true 70 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 71 | 72 | 73 | Console 74 | true 75 | true 76 | true 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/FinalProject.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 源文件 20 | 21 | 22 | 源文件 23 | 24 | 25 | 源文件 26 | 27 | 28 | 源文件 29 | 30 | 31 | 源文件 32 | 33 | 34 | 源文件 35 | 36 | 37 | 源文件 38 | 39 | 40 | 源文件 41 | 42 | 43 | 44 | 45 | 头文件 46 | 47 | 48 | 头文件 49 | 50 | 51 | 头文件 52 | 53 | 54 | 头文件 55 | 56 | 57 | 头文件 58 | 59 | 60 | 头文件 61 | 62 | 63 | 头文件 64 | 65 | 66 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/FinalProject.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | false 5 | 6 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/LexicalAnalysis.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2017-11-22 4 | ** @location:BUAA 5 | */ 6 | //实现文件 7 | #include "LexicalAnalysis.h" 8 | #include 9 | #include //文件操作 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | //初始化 16 | LexicalAnalysis::LexicalAnalysis(const Error & error):myError(error){ 17 | fileLength = 0; 18 | index = 0; 19 | globalChar = '\0'; 20 | globalNumber = 0; 21 | lineCount = 1; 22 | finish = false; 23 | isNextSym = false; 24 | } 25 | //文件读取 26 | void LexicalAnalysis::readFile(string FilePath){ 27 | ifstream fileIn(FilePath,ios::in); 28 | if(!fileIn){ 29 | cout<<"Sorry,File Not exist."<beg(fileIn), end; 34 | fileContents = string(beg, end); 35 | fileIn.close(); 36 | fileLength = fileContents.size(); 37 | //检测文本是否存在中文日文...--->char值小于-1 38 | for(int i=0;i= fileLength){ 56 | finish = true;////此时代表已经完全分析完所有的词法成分,而且预读的也已经全部处理了 57 | return false; 58 | } 59 | //否则,进行单词读取 60 | char Array[maxWordLength] = {'\0'}; 61 | char temp = getChar(); 62 | //空白字符跳过 63 | while(isspace(temp)){ 64 | if(temp=='\n')//换行 65 | lineCount++; 66 | temp = getChar(); 67 | } 68 | //读到文件结束 69 | if(temp==EOF){ 70 | finish = true;//此时代表已经完全分析完所有的词法成分,而且预读的也已经全部处理了 71 | return false; 72 | } 73 | if(isalpha(temp) || temp=='_'){//标识符或者保留字 74 | while(isalnum(temp) || temp=='_'){ 75 | Array[strlen(Array)] = temp; 76 | temp = getChar(); 77 | } 78 | if(temp != EOF){ 79 | retract(); 80 | } 81 | int resultValue = reserver(Array); 82 | if(resultValue == 0){ 83 | globalSymbol = IDENTIFIER; 84 | //全体小写化,统一处理 85 | toLow(Array); 86 | globalString = Array; 87 | }else{ 88 | globalSymbol = (SymbolCode)(resultValue-1); 89 | } 90 | }else if(isdigit(temp)){//数字 91 | while(isdigit(temp)){ 92 | Array[strlen(Array)] = temp; 93 | temp = getChar(); 94 | } 95 | if(temp != EOF) 96 | retract(); 97 | globalNumber = atoi(Array); 98 | globalSymbol = INTNUM; 99 | }else if(temp=='+'){ 100 | globalChar = '+'; 101 | globalSymbol = ADD; 102 | }else if(temp=='-'){ 103 | globalChar = '-'; 104 | globalSymbol = SUB; 105 | }else if(temp=='*'){ 106 | globalChar = '*'; 107 | globalSymbol = MULT; 108 | }else if(temp=='/'){ 109 | globalChar = '/'; 110 | globalSymbol = DIV; 111 | }else if(temp=='<'){ 112 | temp = getChar(); 113 | if(temp!='='){ 114 | globalChar = '<'; 115 | globalSymbol = LESS; 116 | retract(); 117 | }else{ 118 | globalString = "<="; 119 | globalSymbol = LESSEQ; 120 | } 121 | }else if(temp=='>'){ 122 | temp = getChar(); 123 | if(temp!='='){ 124 | globalChar = '>'; 125 | globalSymbol = MORE; 126 | retract(); 127 | }else{ 128 | globalString = ">="; 129 | globalSymbol = MOREEQ; 130 | } 131 | }else if(temp=='!'){ 132 | temp = getChar(); 133 | if(temp!='='){ 134 | myError.LexicalAnalysisError(NotEqualSymIllegal,lineCount); 135 | while(temp!='\n' && temp!=';'){ 136 | temp = getChar(); 137 | } 138 | if(temp=='\n') 139 | retract(); 140 | nextSym(); 141 | return false; 142 | }else{ 143 | globalString = "!="; 144 | globalSymbol = NOTEQ; 145 | } 146 | }else if(temp==','){ 147 | globalChar = ','; 148 | globalSymbol = COMMA; 149 | }else if(temp==':'){ 150 | globalChar = ':'; 151 | globalSymbol = COLON; 152 | }else if(temp==';'){ 153 | globalChar = ';'; 154 | globalSymbol = SEMI; 155 | }else if(temp=='('){ 156 | globalChar = '('; 157 | globalSymbol = LSBRACKET; 158 | }else if(temp==')'){ 159 | globalChar = ')'; 160 | globalSymbol = RSBRACKET; 161 | }else if(temp=='['){ 162 | globalChar = '['; 163 | globalSymbol = LMBRACKET; 164 | }else if(temp==']'){ 165 | globalChar = ']'; 166 | globalSymbol = RMBRACKET; 167 | }else if(temp=='{'){ 168 | globalChar = '{'; 169 | globalSymbol = LBBRACKET; 170 | }else if(temp=='}'){ 171 | globalChar = '}'; 172 | globalSymbol = RBBRACKET; 173 | }else if(temp=='='){ 174 | temp = getChar(); 175 | if(temp!='='){ 176 | globalChar = '='; 177 | globalSymbol = ASSIGN; 178 | retract(); 179 | }else{ 180 | globalString = "=="; 181 | globalSymbol = EQUAL; 182 | } 183 | }else if(temp=='\''){ 184 | temp = getChar(); 185 | if(temp=='+'||temp=='-'||temp=='*'||temp=='/'|| temp=='_'|| 186 | (temp>='A' && temp<='Z') || (temp>='a' && temp<='z') || (temp>='0' && temp<='9')){ 187 | globalChar = temp; 188 | temp = getChar(); 189 | if(temp!='\''){ 190 | myError.LexicalAnalysisError(SingleCharIllegal,lineCount); 191 | while(temp!='\n' && temp!=';'){ 192 | temp = getChar(); 193 | } 194 | if(temp=='\n') 195 | retract(); 196 | nextSym(); 197 | return false; 198 | } 199 | globalSymbol = CHAR; 200 | }else{ 201 | myError.LexicalAnalysisError(SingleCharIllegal,lineCount); 202 | while(temp!='\n' && temp!=';'){ 203 | temp = getChar(); 204 | } 205 | if(temp=='\n') 206 | retract(); 207 | nextSym(); 208 | return false; 209 | } 210 | }else if(temp=='"'){ 211 | temp = getChar(); 212 | while(temp==CHAR1 || temp==CHAR2 || (temp>=CHAR3 && temp<=CHAR4)){ 213 | Array[strlen(Array)] = temp; 214 | temp = getChar(); 215 | } 216 | if(temp=='"'){ 217 | globalString = Array; 218 | globalSymbol = STRING; 219 | }else{ 220 | myError.LexicalAnalysisError(StringIllegal,lineCount); 221 | while(temp!='\n' && temp!=';'){ 222 | temp = getChar(); 223 | } 224 | if(temp=='\n') 225 | retract(); 226 | nextSym(); 227 | return false; 228 | } 229 | }else{ 230 | myError.LexicalAnalysisError(ContentIllegal,lineCount); 231 | return false; 232 | } 233 | return true; 234 | } 235 | //查找保留字 236 | int LexicalAnalysis::reserver(char* target){ 237 | for(int i=0;i='A' && *target<='Z') 248 | *target = *target + 'a' - 'A'; 249 | target++; 250 | } 251 | } 252 | //跳读函数 253 | void LexicalAnalysis::skipRead(char end){ 254 | if(globalChar != end){ 255 | char x = getChar(); 256 | while(!(x==EOF || x==end || x=='\n')) 257 | x = getChar(); 258 | if(x == '\n'){ 259 | retract(); 260 | } 261 | } 262 | nextSym(); 263 | } -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/LexicalAnalysis.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2017-11-22 4 | ** @location:BUAA 5 | */ 6 | /*LexicalAnalysis是词法分析程序,主要任务是分析单词类别,将识别出的内容 7 | * 传递给语法分析程序 8 | */ 9 | #ifndef LEXICALANALYSIS_H 10 | #define LEXICALANALYSIS_H 11 | #include 12 | #include "ConstValue.h" 13 | #include "error.h" 14 | using namespace std; 15 | 16 | extern const char * keyWordsArr[KEY_NUM]; 17 | extern const char * SymbolArr[SYM_NUM]; 18 | 19 | class LexicalAnalysis 20 | { 21 | private: 22 | string fileContents;//编译文件的全部内容 23 | long fileLength;//编译文件的大小 24 | int index; //索引 25 | char globalChar;//存放当前读进的字符 26 | string globalString;//存放当前读进的字符串 27 | int globalNumber;//存放当前读进的整数 28 | enum SymbolCode globalSymbol;//当前所识别单词的类型 29 | static const int maxWordLength = 1024;//一个单词最大长度 30 | int lineCount;//行计数器,目的在于发现非法字符提示所在文件的行数 31 | const Error & myError;//错误处理类引用,全局公用 32 | bool finish;//是否已经读完 33 | bool isNextSym;//需不需要调用nextSym的时候不实际调用(语法分析错误处理,自动补足缺失的括号冒号逗号分号等情况) 34 | /*关于isFinish的解释: 35 | 由于每个递归下降子程序结束前必然会预读,isFinish设置的是 36 | 所有预读全部处理完的结束标志 37 | */ 38 | void retract(){//回退一个字符 39 | index--; 40 | } 41 | //读取字符 42 | char getChar(){ 43 | if(index >= fileLength){ 44 | return EOF; 45 | } 46 | return fileContents[index++]; 47 | } 48 | //小写化字符串 49 | void toLow(char* target); 50 | //查找保留字表 51 | int reserver(char* target); 52 | public: 53 | //有参构造函数 54 | LexicalAnalysis(const Error & error); 55 | //读取文件的全部内容到fileContents中 56 | void readFile(string FilePath); 57 | //语法分析程序调用,读取一个单词 58 | bool nextSym(); 59 | //检查是否读到文件的末尾,对于连续的symbolCode相同判断文件是否分析完的标志 60 | bool isFinish(){ 61 | return finish; 62 | } 63 | //设置下次读取不是实际读取单词 64 | void setNextSym(){ 65 | isNextSym = true; 66 | } 67 | //返回相关的值 68 | //class的属性get集 69 | char getGlobalChar(){ 70 | return globalChar; 71 | } 72 | string getGlobalString(){ 73 | return globalString; 74 | } 75 | int getGlobalNumber(){ 76 | return globalNumber; 77 | } 78 | enum SymbolCode getGlobalSymbol(){ 79 | return globalSymbol; 80 | } 81 | int getLineCount(){ 82 | return lineCount; 83 | } 84 | int getPoint(){ 85 | return index; 86 | } 87 | //重新设置index的值 88 | void resetPoint(int value){ 89 | index = value; 90 | } 91 | //重新设置symbol ---实际只涉及INTSY CHARSY 92 | void resetSymbol(SymbolCode symbol){ 93 | globalSymbol = symbol; 94 | } 95 | //设置跳读的函数 96 | void skipRead(char end); 97 | }; 98 | 99 | #endif -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | 控制台应用程序:[!output PROJECT_NAME] 项目概述 3 | ======================================================================== 4 | 5 | 应用程序向导已为您创建了此 [!output PROJECT_NAME] 应用程序。 6 | 7 | 本文件概要介绍组成 [!output PROJECT_NAME] 应用程序的每个文件的内容。 8 | 9 | 10 | 这是使用应用程序向导生成的 VC++ 项目的主项目文件,其中包含生成该文件的 Visual C++ 的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。 11 | 12 | 这是使用“应用程序向导”生成的 VC++ 项目筛选器文件。它包含有关项目文件与筛选器之间的关联信息。在 IDE 中,通过这种关联,在特定节点下以分组形式显示具有相似扩展名的文件。例如,“.cpp”文件与“源文件”筛选器关联。 13 | 14 | 这是主应用程序源文件。 15 | 16 | ///////////////////////////////////////////////////////////////////////////// 17 | 其他标准文件: 18 | 19 | StdAfx.h, StdAfx.cpp 20 | 这些文件用于生成名为 [!output PROJECT_NAME].pch 的预编译头 (PCH) 文件和名为 StdAfx.obj 的预编译类型文件。 21 | 22 | ///////////////////////////////////////////////////////////////////////////// 23 | 其他注释: 24 | 25 | 应用程序向导使用“TODO:”注释来指示应添加或自定义的源代码部分。 26 | 27 | ///////////////////////////////////////////////////////////////////////////// 28 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/SymbolTable.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2017-12-04 4 | ** @location:BUAA 5 | */ 6 | #include "SymbolTable.h" 7 | #include 8 | 9 | using namespace std; 10 | 11 | int SymbolTableItem::numberCount = 0; 12 | 13 | //全局符号表 14 | vector globalSymbolTable; 15 | 16 | SymbolTableItem::SymbolTableItem(string id,string funcName){ 17 | order = numberCount++; 18 | functionName = funcName; 19 | identifier = id; 20 | length = 0;//默认为0 21 | weight = 0; 22 | } -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/SymbolTable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2017-12-04 4 | ** @location:BUAA 5 | */ 6 | //符号表类 7 | #ifndef SYMBOLTABLE_H 8 | #define SYMBOLTABLE_H 9 | 10 | #include "ConstValue.h" 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | //全局作用域名统一规定为GLOBAL 16 | class SymbolTableItem 17 | { 18 | private: 19 | 20 | static int numberCount; 21 | 22 | string identifier;//标识符 23 | int order; // 符号表所在的项号,从0开始 24 | string functionName;//符号表所属函数作用域 25 | ItemType itemType;//符号表项类型 26 | ValueType valueType;//值类型 27 | FunctionType functionType;//函数类型 28 | 29 | int constIntValue;//常量整数 30 | char constCharValue;//字符常量 31 | 32 | int length;//数组的长度,变量设置为0 33 | 34 | int weight;//只针对函数内部的简单变量以及参数有效 35 | 36 | public: 37 | SymbolTableItem(string id,string funcName); 38 | int getOrder() { 39 | return order; 40 | } 41 | string getId() { 42 | return identifier; 43 | } 44 | string getFuncName() { 45 | return functionName; 46 | } 47 | ItemType getItemType() { 48 | return itemType; 49 | } 50 | ValueType getValueType() { 51 | return valueType; 52 | } 53 | FunctionType getFuncType() { 54 | return functionType; 55 | } 56 | int getArrSize() { 57 | return length; 58 | } 59 | int getConstInt() { 60 | return constIntValue; 61 | } 62 | char getConstChar() { 63 | return constCharValue; 64 | } 65 | //set 66 | void setItemType(ItemType type) { 67 | itemType = type; 68 | } 69 | void setValueType(ValueType type) { 70 | valueType = type; 71 | } 72 | void setArrSize(int size) { 73 | length = size; 74 | } 75 | void setConstInt(int value) { 76 | constIntValue = value; 77 | } 78 | void setConstChar(char value) { 79 | constCharValue = value; 80 | } 81 | void setFuncType(FunctionType type) { 82 | functionType = type; 83 | } 84 | void addWeight(int num) { 85 | weight += num; 86 | } 87 | int getWeight() { 88 | return weight; 89 | } 90 | }; 91 | 92 | #endif -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/SyntaxAnalysis.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2017-11-22 4 | ** @location:BUAA 5 | */ 6 | #ifndef SYNTAXANALYSIS_H 7 | #define SYNTAXANALYSIS_H 8 | #define _CRT_SECURE_NO_WARNINGS 9 | #include "LexicalAnalysis.h" 10 | #include "error.h" 11 | #include "ConstValue.h" 12 | #include "SymbolTable.h" 13 | #include "globalFunction.h" 14 | #include 15 | using namespace std; 16 | 17 | extern vector globalSymbolTable; //使用全局的符号表 18 | extern vector globalTmpCodeArr;//使用全局中间代码表 19 | 20 | //函数存在多返回值的结构体设计 21 | //表达式返回值 22 | struct ExpRet { 23 | string name;//表达式返回的名字--->统一规定,表达式都需要有一个返回变量名(临时变量) 24 | bool isSurable;//是否确定 25 | ValueType type;//值类型 26 | int number; 27 | char character; 28 | bool isEmpty;//是否是空的 29 | }; 30 | //情况子语句返回 31 | struct CaseRet { 32 | int constValue; 33 | bool recognize; 34 | string label; 35 | }; 36 | 37 | class SyntaxAnalysis 38 | { 39 | private: 40 | Error & myError;//公用错误处理 41 | LexicalAnalysis & myLexicalAnalysis;//公用词法分析 42 | bool isMainVoid; //main函数分析的前缀是否是void 43 | //相关递归下降子程序需要返回的值 44 | int return_integer;//ZSQX_integer()需要返回的识别的整数值 45 | string return_declare_funcName; 46 | SymbolCode relation;//关系运算符 47 | vector noUseCache;//无用的cache 48 | bool haveReturn;//有参函数是否有有效的return语句(必须有效) 49 | // 50 | int getLineNumber(){//获取词法分析当前行 51 | return myLexicalAnalysis.getLineCount(); 52 | } 53 | //下面是填符号表,查符号表语义分析相关的操作函数 54 | //填符号表函数,采用函数重载 55 | void pushItem(string id, string functionName, int num);//常量 整数 56 | void pushItem(string id, string functionName, char character);//常量 char 57 | void pushItem(string id, string functionName, ValueType valueType, int size);//变量 数组 58 | void pushItem(string id, string functionName, FunctionType funcType);//函数 59 | void pushItem(string id, string functionName, ItemType itemType, ValueType valueType);//变量与参数 60 | //检查是否可以填表 61 | bool isAbleInsert(string id, string functionName); 62 | //引用变量函数等需要检查是否定义 63 | //bool isDefined(string id, string functionName); 64 | //标识符检查--->因子项中 65 | int idCheckInFactor(string identifier,string funcName); 66 | //标识符检查--->语句中 67 | void idCheckInState(string identifier); 68 | //<标识符>‘[’<表达式>‘]’检查--->因子项与赋值语句中 69 | int idArrExpCheck(string identifier,string funcName,bool expSurable,int index = 0); 70 | //<标识符>‘(’<值参数表>‘)’--->若是因子项中的(表达式中的,需要判断是否是有返回值) 71 | void funcCallCheck(string identifier,bool isInExp,vector paramType); 72 | //类型检查 73 | void checkTypeMatch(ValueType s_type, ValueType e_type) { 74 | if (s_type == CharType && e_type == IntType) { 75 | myError.SemanticAnalysisError(AssignIntToCharError,getLineNumber(),""); 76 | } 77 | } 78 | //对赋值语句单纯的标识符的检查 79 | int checkAssignId(string identifier,string funcName); 80 | //检查switch的case语句是否出现相同的值 81 | void checkCase(vector cases); 82 | void checkSwitchType(ValueType s_type, ValueType e_type) { 83 | if (s_type != e_type) { 84 | myError.SemanticAnalysisError(CaseTypeNotMatchError,getLineNumber(),""); 85 | } 86 | } 87 | 88 | //返回语句检查 89 | void checkReturn(string funcName);//无返回值的return 90 | void checkReturn(string funcName,ValueType retType); 91 | 92 | //优化部分的函数 93 | void addWeight(int order,int weight);//需要检查是否符合要求 94 | 95 | public: 96 | //标准构造函数 97 | SyntaxAnalysis(Error & myError,LexicalAnalysis & myAnalysis); 98 | //程序入口函数 99 | void startAnalysis(); 100 | //下面是所有的递归下降子程序法定义的分析过程 101 | //<程序> ::= [<常量说明>][<变量说明>][<函数定义部分>] <主函数> 102 | bool ZSQX_procedure(); 103 | //<常量说明> ::= const<常量定义>;{ const<常量定义>;} 104 | bool ZSQX_constDescription(string funcName); 105 | /*<常量定义> ::= int<标识符>=<整数>{,<标识符>=<整数>} 106 | | char<标识符>=<字符>{,<标识符>=<字符>} */ 107 | bool ZSQX_constDefinition(string funcName); 108 | //<变量说明> ::= <变量定义>;{<变量定义>;} 109 | bool ZSQX_varDescription(bool isGlobal, string funcName); 110 | /*<变量定义> ::= <类型标识符>(<标识符>|<标识符>‘[’<无符号整数>‘]’) 111 | {,(<标识符>|<标识符>‘[’<无符号整数>‘]’ )}*/ 112 | bool ZSQX_varDefinition(string funcName); 113 | //<函数定义部分> ::= {<有返回值函数定义>|<无返回值函数定义>} 114 | bool ZSQX_functionDefinition(); 115 | /*<有返回值函数定义> ::= <声明头部>‘(’<参数表>‘)’ ‘{’<复合语句>‘}’|<声明头部>‘{’<复合语句>‘}’*/ 116 | bool ZSQX_haveReturnValueFunctionDefinition(); 117 | //<无返回值函数定义> ::= bool<标识符>(’<参数表>‘)’‘{’<复合语句>‘}’| bool<标识符>{’<复合语句>‘}’ 118 | bool ZSQX_noReturnValueFunctionDefinition(); 119 | //<参数表> ::= <类型标识符><标识符>{,<类型标识符><标识符>} 120 | bool ZSQX_paramTable(string funcName); 121 | //<复合语句> ::= [<常量说明>][<变量说明>]{<语句>} 122 | bool ZSQX_compoundStatement(string funcName); 123 | //<表达式> ::= [+|-]<项>{<加法运算符><项>} 124 | ExpRet ZSQX_expression(string funcName,bool isCache,vector & cache,int weight); 125 | //<项> ::= <因子>{<乘法运算符><因子>} 126 | bool ZSQX_item(vector &, string funcName, bool isCache, vector & cache,int weight); 127 | //<因子> ::= <标识符>[‘(’<值参数表>‘)’]|<标识符>‘[’<表达式>‘]’|‘(’<表达式>‘)’|<整数>|<字符> 128 | bool ZSQX_factor(vector &, string funcName, bool isCache, vector & cache, int weight); 129 | /*<语句> ::= <条件语句>|<循环语句>| ‘{’{<语句>}‘}’|<标识符>[‘(’<值参数表>‘)’]; 130 | |<赋值语句>;|<读语句>;|<写语句>;|<空>;|<情况语句>|<返回语句>;*/ 131 | bool ZSQX_statement(string funcName, bool isCache, vector & cache, int weight); 132 | //<赋值语句> ::= <标识符>=<表达式>|<标识符>‘[’<表达式>‘]’=<表达式> 133 | bool ZSQX_assignStatement(string funcName,string, bool isCache, vector & cache, int weight); 134 | //<条件语句>::= if ‘(’<条件>‘)’<语句>else<语句> 135 | bool ZSQX_conditionStatement(string funcName, bool isCache, vector & cache, int weight); 136 | //<条件> ::= <表达式><关系运算符><表达式>|<表达式> 137 | string ZSQX_condition(string funcName, bool isCache, vector & cache, int weight); 138 | //<循环语句> ::= while ‘(’<条件>‘)’<语句> 139 | bool ZSQX_loopStatement(string funcName, bool isCache, vector & cache, int weight); 140 | //<情况语句> ::= switch ‘(’<表达式>‘)’ ‘{’<情况表>[<缺省>] ‘}’ 141 | bool ZSQX_situationStatement(string funcName, bool isCache, vector & cache, int weight); 142 | //<情况表> ::= <情况子语句>{<情况子语句>} 143 | vector ZSQX_situationTable(string funcName,string endLabel,ValueType type,vector &, int weight); 144 | //<情况子语句> ::= case<常量>:<语句> 145 | CaseRet ZSQX_situationSonStatement(string funcName,string endLabel, ValueType type,vector &, int weight); 146 | //<缺省> ::= default : <语句> 147 | bool ZSQX_default(string funcName, bool isCache, vector & cache, int weight); 148 | //<值参数表> ::= <表达式>{,<表达式>} 149 | vector ZSQX_valueParamTable(string funcName, bool isCache, vector & cache, int weight); 150 | //<读语句> ::= scanf ‘(’<标识符>{,<标识符>}‘)’ 151 | bool ZSQX_readStatement(string funcName, bool isCache, vector & cache, int weight); 152 | //<写语句> ::= printf ‘(’ <字符串>,<表达式> ‘)’| printf ‘(’<字符串> ‘)’| printf ‘(’<表达式>‘)’ 153 | bool ZSQX_writeStatement(string funcName, bool isCache, vector & cache, int weight); 154 | //<返回语句> ::= return[‘(’<表达式>‘)’] 155 | bool ZSQX_returnStatement(string funcName, bool isCache, vector & cache, int weight); 156 | //<整数> ::= [+|-]<无符号整数>|0 157 | bool ZSQX_integer(); 158 | //<声明头部> ::= int <标识符> |char <标识符> 159 | bool ZSQX_declareHead(); 160 | 161 | }; 162 | 163 | #endif -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/error.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/error.cpp -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/error.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2017-11-22 4 | ** @location:BUAA 5 | */ 6 | //错误处理程序 7 | #ifndef ERROR_H 8 | #define ERROR_H 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | //词法分析错误种类枚举 15 | /* 16 | *1.单字符char不合法 17 | *2.字符串不合法 18 | *3.整个程序出现非法字符 19 | */ 20 | enum LexicalErrorCode{ 21 | SingleCharIllegal, 22 | StringIllegal, 23 | ContentIllegal, 24 | NotEqualSymIllegal 25 | }; 26 | //语法分析错误种类枚举 27 | /* 28 | 1.文件为空error 29 | 2.main函数后多余内容 30 | 3.没有void main函数 31 | 4.语法缺少部分结构,或者结构不匹配 32 | 5.数组声明中数组的长度不是无符号整数(>0) 33 | 6.0的前面不可以有正负号修饰 34 | */ 35 | enum SyntaxErrorCode{ 36 | EmptyFileError,//文件为空 37 | AfterMainContentError,//main函数后多余的内容 38 | NoMainFunctionError,//无void main函数 39 | LackComposedPartError,//缺少相关组成部分 40 | NotUnsignedIntegerError,//不是无符号整数 41 | ZeroPrefixSignError,//0前不可以有正负号修饰 42 | GeneralError // 43 | }; 44 | 45 | //语义分析的相关错误 46 | /* 47 | 1.声明定义冲突(作用域问题) 48 | 49 | 2.引用变量,变量未定义 50 | 51 | 3.引用数组,数组越界 52 | 4.引用函数,函数未定义 53 | 5.单个语句只能是函数 54 | 6.调用函数,无参却传参数 55 | 7.调用函数,值参数个数不对 56 | 8.调用函数,值参数类型不对 57 | 9.赋值对象不是普通变量(包含了多种错误情况) 58 | 10.赋值类型不匹配(int 给 char赋值) 59 | 11.引用标识符,标识符对应类型不匹配 60 | 12.值参数表不可以为空 61 | 13.case出现相同的值 62 | 14.除数不可以为0 63 | */ 64 | enum SemanticErrorCode { 65 | DeclareConflictError, 66 | NotDefinitionError, 67 | FuncNotDefineError, 68 | ArrIndexOutOfRangeError, 69 | TypeNotMatchError, 70 | AssignIntToCharError, 71 | StateIdNotMatchError, 72 | NeedValueButVoidFuncError, 73 | NoneValueParamError, 74 | NoneParamButDeliverError, 75 | ParamNumNotMatchError, 76 | ParamTypeNotMatchError, 77 | AssignObjectNotVar, 78 | CaseSameValueError, 79 | CaseTypeNotMatchError 80 | }; 81 | /*与return语句有关的error 82 | 1.无返回值存在有返回值的return 83 | 2.有返回值存在无返回值的return 84 | 3.有返回值不存在return 85 | 4.有返回值存在return类型不兼容 86 | 5.有返回值存在分支导致无返回值 87 | */ 88 | enum ReturnRelatedError { 89 | VoidButReturnValueError, 90 | CharButReturnIntError, 91 | ExistNoneReturnError, 92 | ExistReturnEmptyError 93 | }; 94 | 95 | class Error 96 | { 97 | private: 98 | 99 | public: 100 | //词法分析错误处理程序 101 | void LexicalAnalysisError(LexicalErrorCode errorCode,int lineNumber)const; 102 | //语法分析错误处理程序 103 | void SyntaxAnalysisError(SyntaxErrorCode errorCode,int lineNumber); 104 | void SyntaxAnalysisError(SyntaxErrorCode errorCode,int lineNumber,string info); 105 | //语义分析错误处理程序 106 | void SemanticAnalysisError(SemanticErrorCode errorCode,int lineNumber,string identifier);//带标识符 107 | //关于函数返回语句相关错误 108 | void ReturnStatementError(ReturnRelatedError errorCode,int lineNumber,string funcName); 109 | }; 110 | #endif -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/errorTest/t1.txt: -------------------------------------------------------------------------------- 1 | const int g_const_int1 = 23,g_const_int2 = 24; 2 | const char g_const_char1 = 'a',g_const_char2 = 'm'; 3 | 4 | int g_var_int1,g_var_int2; 5 | int g_var_int_arr[12],g_var_int3; 6 | char g_var_char1,g_var_char2; 7 | char g_var_char_arr[12],g_vat_char3; 8 | 9 | int Fibnaci(int n){ 10 | if(n<=2) 11 | return (1); 12 | else 13 | return (Fibnaci(n-1)+Fibnaci(n-2)); 14 | } 15 | 16 | void printGlobalVarValue{ 17 | printf("g_var_int1:",g_var_int1); 18 | printf("g_var_int2:",g_var_int2); 19 | printf("g_var_char1:",g_var_char1); 20 | printf("g_var_char2:",g_var_char2); 21 | printf("------------------------"); 22 | } 23 | 24 | void printGlobalArrValue(int loopLength){ 25 | int i; 26 | i = 0; 27 | printf("g_var_int_arr:"); 28 | wHIle(i='n') 121 | printf("bigger than n or equal to n"); 122 | else 123 | printf("smaller than n"); 124 | 125 | if(target>'n') 126 | printf("bigger than n"); 127 | else 128 | printf("not bigger than n"); 129 | 130 | if(target!='n') 131 | printf("not equal to n"); 132 | else 133 | printf("equal to n"); 134 | 135 | printf("------------------------"); 136 | 137 | } 138 | 139 | int BBB(int x,int y,char z){ 140 | return (x+y+z); 141 | } 142 | 143 | void main(){ 144 | int i; 145 | int flag1,flag2; 146 | int myFib,inputFib; 147 | CHAR relationFalg; 148 | 149 | g_var_int1 = g_const_int1 + g_const_int2; 150 | g_var_int2 = g_const_int2 - g_const_int1; 151 | g_var_char1 = g_const_char1; 152 | g_var_char2 = g_const_char2; 153 | 154 | printGlobalVarValue; 155 | 156 | g_var_int1 = g_const_int1 * 2; 157 | g_var_int2 = g_const_int2 / 3; 158 | 159 | g_var_int1 = returnTwll; 160 | g_var_int1 = returnTen(); 161 | g_var_int1 = printGlobalVarValue; 162 | g_var_char1 = VVV(); 163 | g_var_int1 = BBB(1,6,7); 164 | g_var_int1 = BBB(3,5); 165 | g_var_int1 = BBB(1,3,'+'); 166 | g_var_int1 = BBB(); 167 | 168 | printGlobalVarValue; 169 | 170 | i = 0; 171 | while(i<=11){ 172 | g_var_int_arr[i] = i; 173 | g_var_char_arr[i] = 'x'; 174 | i = i + 1; 175 | } 176 | g_var_int_arr[3] = g_var_int_arr[0] + 3; 177 | printGlobalArrValue(12); 178 | 179 | flag1 = 1;flag2 = g_var_int_arr[2]; 180 | testSwitchCase(flag1,flag2); 181 | testSwitchCase(flag1-1,flag2-1); 182 | testSwitchCase('a'-'d',flag2+4); 183 | testSwitchCase('a'-'c',flag2+3); 184 | testSwitchCase(111,222); 185 | 186 | myFib = Fibnaci(1); 187 | printf("Fib(1):",myFib); 188 | myFib = Fibnaci(2); 189 | printf("Fib(2):",myFib); 190 | myFib = Fibnaci(8); 191 | printf("Fib(8):",myFib); 192 | 193 | g_var_int1 = (1*(1 + 3) * 'a'*(1) + g_var_int_arr[4]) / (returnTwo(1)); 194 | g_var_int2 = (666/g_var_int_arr[2])/111*(returnTen(2)); 195 | printGlobalVarValue; 196 | printf(-3*-4++7+-6); 197 | 198 | relationFalg = 'a'; 199 | testIfElse(relationFalg); 200 | 201 | relationFalg = 'n'; 202 | testIfElse(relationFalg); 203 | 204 | relationFalg = 's'; 205 | testIfElse(relationFalg); 206 | 207 | printf("Please enter the number of items you want to calculate the Fibonacci sequence:"); 208 | 209 | scanf(inputFib); 210 | 211 | myFib = Fibnaci(inputFib); 212 | 213 | printf("the Fib number you want to get is:",myFib); 214 | 215 | { 216 | printf("lalalalala."); 217 | scanf(testIfElse,returnTen,returnTwo); 218 | { 219 | printf("sum is:",testIfElse + returnTen + returnTwo); 220 | printf("procedure is Over, Thank you."); 221 | } 222 | } 223 | 224 | return; 225 | } 226 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/globalFunction.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2017-12-10 4 | ** @location:BUAA 5 | */ 6 | #ifndef GLOBALFUNCTION_H 7 | #define GLOBALFUNCTION_H 8 | #define _CRT_SECURE_NO_WARNINGS 9 | #include "ConstValue.h" 10 | #include "SymbolTable.h" 11 | #include 12 | #include 13 | #include "optimize.h" 14 | using namespace std; 15 | 16 | //函数声明 17 | string generateLabel(); 18 | string generateVar(); 19 | bool isStringDigit(string); 20 | int stringToInt(string); 21 | //文件操作 22 | //将中间代码写入到文件中 23 | void writeTmpCodeToFile(); 24 | void op_writeTmpCodeToFile(); 25 | //将中间代码翻译成最终的mips32汇编语言代码 26 | void generateMipsCode(); 27 | void op_generateMipsCode(); 28 | void generateData(ofstream &); 29 | void generateText(ofstream &); 30 | void op_generateText(ofstream &); 31 | //表达式的相关计算处理 32 | void turnToPostfixExp(vector, vector &); 33 | string calculateExp(vector &,bool &,ValueType &,int &,int,bool,vector &,string); 34 | 35 | //取消字符串中的转义字符 36 | void cancelEscapeChar(string & target); 37 | #endif // !GLOBALFUNCTION_H 38 | 39 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2017-11-22 4 | ** @location:BUAA 5 | */ 6 | /*相关说明: 7 | #1编译程序的整体入口 8 | #2标识符不区分大小写 9 | */ 10 | #include 11 | #include 12 | #include "error.h" 13 | #include "ConstValue.h" 14 | #include "LexicalAnalysis.h" 15 | #include "SyntaxAnalysis.h" 16 | #include "globalFunction.h" 17 | #include "optimize.h" 18 | #include 19 | 20 | using namespace std; 21 | 22 | string compilerFilePath; 23 | 24 | extern const char * keyWordsArr[KEY_NUM]; 25 | extern const char * SymbolArr[SYM_NUM]; 26 | extern bool ErrorFlag; 27 | 28 | int main(void){ 29 | //欢迎界面 30 | cout<<"WelCome to use my compiler."<= 0 ."); 163 | return (0); 164 | }else{ 165 | if(item == 0) 166 | return (1); 167 | else 168 | return (calculateFactorial(item-1)*item); 169 | } 170 | } 171 | //计算斐波那契,第二种递归 172 | int Fibonaci(int n){ 173 | if(n<=2){ 174 | if((n+1)<=1){ 175 | printf("The Fibonaci number must >=1."); 176 | return (0); 177 | }else{ 178 | ; 179 | // 测试空语句 180 | } 181 | return (1); 182 | } 183 | else 184 | return (Fibonaci(n-1)+Fibonaci(n-2)); 185 | } 186 | 187 | //超复杂表达式计算函数,定义辅助的返回函数 188 | char return100{ 189 | return ('d'); 190 | } 191 | 192 | int returnIntValue{ 193 | return (1 + 'c' + Fibonaci(8) + calculateFactorial(4)); //return 145 194 | } 195 | 196 | void complexExpression{ 197 | int x1,x2,x3,x4,x5; 198 | //5个混合型极其复杂的表达式计算 199 | x1 = (3+4-7*Fibonaci(9))/2-(calculateFactorial(5)-87*2+67)/(return100-50/3-12)+(34-67)/2*6; 200 | printf("complex --- x1:",x1); 201 | //6层括号嵌套 202 | x2 = ( 333-222 + ( 1 + ( 2 + ( 3 * ( 89 - 56 * (3-3*6+Fibonaci(7) +Fibonaci(1) -2 ) + calculateFactorial(3) ) -3 ) + 4 ) -0*(0+0-0-0) - 1 ) + 2 ) - calculateFactorial(4)/4*111+Fibonaci(6); 203 | printf("complex --- x2:",x2); 204 | //带字符的混合运算 205 | x3 = ('a'+90-'0'+(Fibonaci('a'-91))) + calculateFactorial(Fibonaci(Fibonaci(Fibonaci(Fibonaci(2))))) + 666*3/222; 206 | printf("complex --- x3:",x3); 207 | //带数组运算 208 | x4 = (666+888)/111 - return100/50 + gInt2[Fibonaci(3)] - gInt5[calculateFactorial(3)] + 5000; 209 | printf("complex --- x4:",x4); 210 | //混合型全种类计算 211 | x5 = (calculateFactorial(5) - 67)*'a'+ x1 - x3 + 4666 + 2333/32 + 87 +'9' + gCh1 + '8'*'_' + '*'/'4' + gInt3[3+'f'-'e'] + (((((5+5)))))*100 - return100 + Fibonaci(5); 212 | printf("complex --- x5:",x5); 213 | } 214 | 215 | //复杂形式的if-else测试 216 | void complexIf_Else{ 217 | int i; 218 | i = 4; 219 | while(i>=0){ 220 | if(i == 4){ 221 | printf("In If-Else i = 4"); 222 | }else{ 223 | if(i == 3){ 224 | printf("In If-Else i = 3"); 225 | }else{ 226 | if(i == 2){ 227 | printf("In If-Else i = 2"); 228 | }else{ 229 | if(i == 1){ 230 | printf("In If-Else i = 1"); 231 | }else{ 232 | printf("Test If-Else Over."); 233 | } 234 | } 235 | } 236 | } 237 | i = i - 1; 238 | } 239 | } 240 | //复杂形式的switch-case测试 241 | void complexSwitch_Case{ 242 | int k; 243 | k = 1; 244 | while(k<=4){ 245 | switch(k){ 246 | //简单情况 247 | case 1:{ 248 | printf("Welcome"); 249 | printf("Welcome"); 250 | } 251 | //嵌套if-else 252 | case 2:{ 253 | if(k-2){ 254 | printf("False"); 255 | }else{ 256 | printf("True"); 257 | } 258 | } 259 | //嵌套while 260 | case 3:{ 261 | gInt1 = 3; 262 | while(gInt1){ 263 | printf("Tiga"); 264 | gInt1 = gInt1 - 1; 265 | } 266 | } 267 | //带default的switch嵌套 268 | case 4:{ 269 | switch(k+1){ 270 | case 2:printf("Sally"); 271 | case 3:printf("TAT_-_"); 272 | case 4:printf("My Sniper"); 273 | default:printf("CoCo"); 274 | } 275 | } 276 | } 277 | k = k + 1; 278 | } 279 | } 280 | //复杂形式的while循环测试 281 | void complexWhile{ 282 | int i,j,k; 283 | i = 0; 284 | while(i<3){ 285 | j = 0; 286 | while(j<3){ 287 | k = 0; 288 | while(k < 3){ 289 | printf("At test While:",i*9 + j*3 + k+1); 290 | k = k + 1; 291 | } 292 | j = j + 1; 293 | } 294 | i = i + 1; 295 | } 296 | } 297 | //多层嵌套测试,计算两个整数的加减 298 | int _Multi_layer(int a1,int a2){ 299 | return (a1+a2); 300 | } 301 | 302 | void main(){ 303 | //main函数设置多个return点,根据输入的值确定何时return 304 | int returnScanfSymbol; 305 | int giveValueToGlobalVar;//与函数同名的变量 306 | int testAllDefine[10];//与函数同名的数组 307 | char calculateFactorial; 308 | char Fibonaci[10]; 309 | int i; 310 | int int1,int2,int3; 311 | char char1,char2,char3; 312 | 313 | giveValueToGlobalVar; 314 | testAllDefine; 315 | //以上做了同名测试,也做了变量定义不同作用域的测试 316 | //多参数传参测试 317 | printf("many params1:",returnAdd(1,26,33,445,512,656,744,834,954,1021)); 318 | printf("many params2:",returnMix('a','b','c','4','e','+','-','*','/','_')); 319 | 320 | //测试params2 321 | i = 0; 322 | printf("The All Value Of GGG:"); 323 | while(i<10){ 324 | printf(GGG[i]); 325 | i = i + 1; 326 | } 327 | 328 | //多return点测试,使用if-else结构结合scanf 329 | printf("Hello Boy, you need to be careful"); 330 | printf("If you input 0 ---> end the procedure"); 331 | printf("If you input 1 ---> we return the number of 1+1"); 332 | printf("If you input 2 ---> we return the number of 2*2"); 333 | printf("If you input others ---> we carry out the next procedure"); 334 | printf("Hey,Input int number to choose:"); 335 | scanf(returnScanfSymbol); 336 | if(returnScanfSymbol == 0){ 337 | printf("You mean to exit.Ok -_-"); 338 | return; 339 | }else{ 340 | if(returnScanfSymbol == 1){ 341 | printf("The result 1+1 is:",1+1); 342 | return; 343 | }else{ 344 | if(returnScanfSymbol == 2){ 345 | printf("The result 2*2 is:",2*2); 346 | return; 347 | }else{ 348 | printf("Next the trip."); 349 | } 350 | } 351 | } 352 | //表达式计算 353 | complexExpression; 354 | //运算符与'运算符混合型' 355 | printf("complex expression add --- > x6:",(3*'*'+'/'-'-'+'a'*'+'+909/'_'+'/'*3+'+'*3)); 356 | //scanf多形式测试 357 | printf("Test scanf"); 358 | scanf(int1,int2,int3); 359 | printf("int1:",int1); 360 | printf("int2:",int2); 361 | printf("int3:",int3); 362 | 363 | scanf(char1,char2,char3); 364 | printf("char1:",char1); 365 | printf("char2:",char2); 366 | printf("char3:",char3); 367 | 368 | scanf(int1,char1,int2,char2,int3,char3); 369 | printf("int1:",int1); 370 | printf("int2:",int2); 371 | printf("int3:",int3); 372 | printf("char1:",char1); 373 | printf("char2:",char2); 374 | printf("char3:",char3); 375 | 376 | //printf多形式测试(测试打印字符) 377 | printf("Test printf character"); 378 | printf("return100:",return100);//函数调用返回 379 | printf("gCh2[1]:",gCh2['b'-'a']);//数组 380 | printf("gCh1:",gCh1);//变量 381 | printf("const1:",'y');//常量 382 | printf("const2:",char4);//常量 383 | //值参数多层嵌套函数返回值 384 | printf("multi_layer:",_Multi_layer(Fibonaci(calculateFactorial(3)),_Multi_layer(_Multi_layer(_Multi_layer(2333,6666+'a'),calculateFactorial(7)),Fibonaci(8)))); 385 | //测试if-else连环嵌套 386 | complexIf_Else; 387 | //测试while连环嵌套 388 | complexWhile; 389 | //测试switch连环嵌套(重点测试) 390 | complexSwitch_Case; 391 | printf("Congratulations,you passed all tests."); 392 | return; 393 | } -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/normalTest/globalTest(标准C测试).c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | const int int1 = 666,int2 = +233,int3 = -1000,int4 = 0; 4 | const char char1 = '1',char2 = '_',char3='+',char4 = 'a'; 5 | 6 | 7 | int gInt1,gInt2[10],gInt3[10],gInt4,gInt5[10]; 8 | char gCh1,gCh2[10],gCh3[10],gCh4,gCh5[10]; 9 | 10 | char GGG[10]; 11 | 12 | 13 | void testAllDefine(){ 14 | const int int1=333,int2 = +789,int3 = -7125,int4 = -1; 15 | 16 | const char char1 = '0',char2 = '-',char3='*',char4 = 'D',char5 = 'g', 17 | char6 = '+',char7 = '/',char8 = '9',char9 = 'Z',char10 = 'y'; 18 | 19 | int gInt1,gInt2[10],gInt3[10],gInt4,gInt5[10]; 20 | 21 | char gCh1,gCh2[10],gCh3[10],gCh4,gCh5[10]; 22 | 23 | int i; 24 | 25 | 26 | gInt1 = 10*1; 27 | gInt4 = 10*4; 28 | gCh1 = '0'; 29 | gCh4 = '9'; 30 | 31 | i = (0+0*0-0-(0-'a'+'b'-1)*34445); 32 | while(i<(333-222-111+19-9+gCh4-'8'-1)){ 33 | gInt2[i] = 2*10+i; 34 | gInt3[i] = 3*10+i; 35 | gInt5[i] = 5*10+i; 36 | gCh2[i] = 'a'; 37 | gCh3[i] = 'b'; 38 | gCh5[i] = 'c'; 39 | i = i + '8' - '7'; 40 | } 41 | 42 | 43 | printf("int1:%d\n",int1); 44 | printf("int2:%d\n",int2); 45 | printf("int3:%d\n",int3); 46 | printf("int4:%d\n",int4); 47 | 48 | printf("char1:%c\n",char1); 49 | printf("char2:%c\n",char2); 50 | printf("char3:%c\n",char3); 51 | printf("char4:%c\n",char4); 52 | 53 | printf("gInt1:%d\n",gInt1); 54 | printf("gInt4:%d\n",gInt4); 55 | 56 | printf("gCh1:%c\n",gCh1); 57 | printf("gCh4:%c\n",gCh4); 58 | 59 | i = 0; 60 | printf("gInt2[]:\n"); 61 | while(i<10){ 62 | i = i + 1; 63 | printf("%d\n",gInt2[i-1]); 64 | } 65 | i = 0; 66 | printf("gInt3[]:\n"); 67 | while(i<10){ 68 | i = i + 1; 69 | printf("%d\n",gInt3[i-1]); 70 | } 71 | i = 0; 72 | printf("gInt5[]:\n"); 73 | while(i<10){ 74 | i = i + 1; 75 | printf("%d\n",gInt5[i-1]); 76 | } 77 | 78 | printf("gCh2[3]:%c\n",gCh2[3]); 79 | printf("gCh2[6]:%c\n",gCh2[6]); 80 | 81 | printf("gCh3[3]:%c\n",gCh3[3]); 82 | printf("gCh3[6]:%c\n",gCh3[6]); 83 | 84 | printf("gCh5[3]:%c\n",gCh5[3]); 85 | printf("gCh5[6]:%c\n",gCh5[6]); 86 | 87 | } 88 | 89 | void giveValueToGlobalVar(){ 90 | int i; 91 | 92 | gInt1 = 100*1; 93 | gInt4 = 100*4; 94 | gCh1 = 'D'; 95 | gCh4 = 'F'; 96 | 97 | i = 0; 98 | while(i<10){ 99 | gInt2[i] = 2*100+i; 100 | gInt3[i] = 3*100+i; 101 | gInt5[i] = 5*100+i; 102 | gCh2[i] = 'i'; 103 | gCh3[i] = 'j'; 104 | gCh5[i] = 'k'; 105 | i = i + 'B' - 'A'; 106 | } 107 | } 108 | 109 | int returnAdd(int int1,int int2,int int3,int int4,int int5,int int6,int int7,int int8,int int9,int int10){ 110 | int gInt2; 111 | gInt2 = +int1+int2+int3+int4+int5+int6+int7+int8+int9+int10; 112 | return (gInt2); 113 | } 114 | 115 | int returnMix(char char1,char char2,char char3,char char4,char char5,char char6,char char7,char char8,char char9,char char10){ 116 | int gCh2,i; 117 | gCh2 = char1+char2+char3+char4+char5-char6*char7+char8-char9+char10/2; 118 | i = 0; 119 | while(i<(100/10)){ 120 | switch(i){ 121 | case 0: 122 | GGG[0] = char1; 123 | break; 124 | case 1: 125 | GGG[1] = char2; 126 | break; 127 | case 2: 128 | GGG[2] = char3; 129 | break; 130 | case 3: 131 | GGG[3] = char4; 132 | break; 133 | case 4: 134 | GGG[4] = char5; 135 | break; 136 | case 5: 137 | GGG[5] = char6; 138 | break; 139 | case 6: 140 | GGG[6] = char7; 141 | break; 142 | case 7: 143 | GGG[7] = char8; 144 | break; 145 | case 8: 146 | GGG[8] = char9; 147 | break; 148 | case 9: 149 | GGG[9] = char10; 150 | break; 151 | default:printf("Are you kidding me?\n");break; 152 | } 153 | i = i + (100/100); 154 | } 155 | return (gCh2); 156 | } 157 | 158 | char MixParam(int int1,char char1,int int2,char char2){ 159 | if(int1+char1){ 160 | return ('1'); 161 | } 162 | else{ 163 | if(int2+char2) 164 | return ('2'); 165 | else 166 | return ('0'); 167 | } 168 | } 169 | 170 | int calculateFactorial(int item){ 171 | if(item < 0 ){ 172 | printf("The factorial number must >= 0 .\n"); 173 | return (0); 174 | }else{ 175 | if(item == 0) 176 | return (1); 177 | else 178 | return (calculateFactorial(item-1)*item); 179 | } 180 | } 181 | 182 | int Fibonaci(int n){ 183 | if(n<=2){ 184 | if((n+1)<=1){ 185 | printf("The Fibonaci number must >=1.\n"); 186 | return (0); 187 | }else{ 188 | ; 189 | 190 | } 191 | return (1); 192 | } 193 | else 194 | return (Fibonaci(n-1)+Fibonaci(n-2)); 195 | } 196 | 197 | 198 | char return100(){ 199 | return ('d'); 200 | } 201 | 202 | int returnIntValue(){ 203 | return (1 + 'c' + Fibonaci(8) + calculateFactorial(4)); 204 | } 205 | 206 | void complexExpression(){ 207 | int x1,x2,x3,x4,x5; 208 | 209 | x1 = (3+4-7*Fibonaci(9))/2-(calculateFactorial(5)-87*2+67)/(return100()-50/3-12)+(34-67)/2*6; 210 | printf("complex --- x1:%d\n",x1); 211 | 212 | x2 = ( 333-222 + ( 1 + ( 2 + ( 3 * ( 89 - 56 * (3-3*6+Fibonaci(7) +Fibonaci(1) -2 ) + calculateFactorial(3) ) -3 ) + 4 ) -0*(0+0-0-0) - 1 ) + 2 ) - calculateFactorial(4)/4*111+Fibonaci(6); 213 | printf("complex --- x2:%d\n",x2); 214 | 215 | x3 = ('a'+90-'0'+(Fibonaci('a'-91))) + calculateFactorial(Fibonaci(Fibonaci(Fibonaci(Fibonaci(2))))) + 666*3/222; 216 | printf("complex --- x3:%d\n",x3); 217 | 218 | x4 = (666+888)/111 - return100()/50 + gInt2[Fibonaci(3)] - gInt5[calculateFactorial(3)] + 5000; 219 | printf("complex --- x4:%d\n",x4); 220 | 221 | x5 = (calculateFactorial(5) - 67)*('a')+ x1 - x3 + 4666 + 2333/32 + 87 +'9' + gCh1 + ('8')*('_') + ('*')/('4') + gInt3[3+'f'-'e'] + return100() + (((((5+5)))))*100 - 100 + Fibonaci(5); 222 | printf("complex --- x5:%d\n",x5); 223 | } 224 | 225 | 226 | void complexIf_Else(){ 227 | int i; 228 | i = 4; 229 | while(i>=0){ 230 | if(i == 4){ 231 | if(i==4){ 232 | printf("In If-Else i = 4 haha\n"); 233 | } 234 | printf("In If-Else i = 4\n"); 235 | }else{ 236 | if(i == 3){ 237 | printf("In If-Else i = 3\n"); 238 | }else{ 239 | if(i == 2){ 240 | printf("In If-Else i = 2\n"); 241 | }else{ 242 | if(i == 1){ 243 | printf("In If-Else i = 1\n"); 244 | }else{ 245 | printf("Test If-Else Over.\n"); 246 | } 247 | } 248 | } 249 | } 250 | i = i - 1; 251 | } 252 | } 253 | 254 | void complexSwitch_Case(){ 255 | int k; 256 | k = 1; 257 | while(k<=4){ 258 | switch(k){ 259 | case 1:{ 260 | printf("Welcome\n"); 261 | printf("Welcome\n"); 262 | break; 263 | } 264 | case 2:{ 265 | if(k-2){ 266 | printf("False\n"); 267 | }else{ 268 | printf("True\n"); 269 | } 270 | break; 271 | } 272 | case 3:{ 273 | gInt1 = 3; 274 | while(gInt1){ 275 | printf("Tiga\n"); 276 | gInt1 = gInt1 - 1; 277 | } 278 | break; 279 | } 280 | case 4:{ 281 | switch(k+1){ 282 | case 2:printf("Sally\n");break; 283 | case 3:printf("TAT_-_\n");break; 284 | case 4:printf("My Sniper\n");break; 285 | default:printf("CoCo\n");break; 286 | } 287 | break; 288 | } 289 | } 290 | k = k + 1; 291 | } 292 | } 293 | 294 | void complexWhile(){ 295 | int i,j,k; 296 | i = 0; 297 | while(i<3){ 298 | j = 0; 299 | while(j<3){ 300 | k = 0; 301 | while(k < 3){ 302 | printf("At test While:%d\n",i*9 + j*3 + k+1); 303 | k = k + 1; 304 | } 305 | j = j + 1; 306 | } 307 | i = i + 1; 308 | } 309 | } 310 | 311 | int _Multi_layer(int a1,int a2){ 312 | return (a1+a2); 313 | } 314 | 315 | int main(){ 316 | 317 | int returnScanfSymbol; 318 | int _giveValueToGlobalVar; 319 | int _testAllDefine[10]; 320 | char _calculateFactorial; 321 | char _Fibonaci[10]; 322 | int i; 323 | int int1,int2,int3,char1,char2,char3; 324 | 325 | giveValueToGlobalVar(); 326 | testAllDefine(); 327 | 328 | printf("many params1:%d\n",returnAdd(1,26,33,445,512,656,744,834,954,1021)); 329 | printf("many params2:%d\n",returnMix('a','b','c','4','e','+','-','*','/','_')); 330 | 331 | i = 0; 332 | printf("The All Value Of GGG:\n"); 333 | while(i<10){ 334 | printf("%c\n",GGG[i]); 335 | i = i + 1; 336 | } 337 | 338 | 339 | printf("Hello Boy, you need to be careful\n"); 340 | printf("If you input 0 ---> end the procedure\n"); 341 | printf("If you input 1 ---> we return the number of 1+1\n"); 342 | printf("If you input 2 ---> we return the number of 2*2\n"); 343 | printf("If you input others ---> we carry out the next procedure\n"); 344 | printf("Hey,Input int number to choose:\n"); 345 | scanf("%d",&returnScanfSymbol); 346 | if(returnScanfSymbol == 0){ 347 | printf("You mean to exit.Ok -_-\n"); 348 | return 0; 349 | }else{ 350 | if(returnScanfSymbol == 1){ 351 | printf("The result 1+1 is:%d\n",1+1); 352 | return 0; 353 | }else{ 354 | if(returnScanfSymbol == 2){ 355 | printf("The result 2*2 is:%d\n",2*2); 356 | return 0; 357 | }else{ 358 | printf("Next the trip.\n"); 359 | } 360 | } 361 | } 362 | 363 | 364 | complexExpression(); 365 | 366 | printf("complex expression add --- > x6:%d\n",(3*'*'+'/'-'-'+'a'*'+'+909/'_'+'/'*3+'+'*3)); 367 | 368 | printf("Test scanf\n"); 369 | scanf("%d %d %d",&int1,&int2,&int3); 370 | printf("int1:%d\n",int1); 371 | printf("int2:%d\n",int2); 372 | printf("int3:%d\n",int3); 373 | getchar(); 374 | scanf("%c%c%c",&char1,&char2,&char3); 375 | printf("char1:%c\n",char1); 376 | printf("char2:%c\n",char2); 377 | printf("char3:%c\n",char3); 378 | 379 | scanf("%d",&int1); 380 | getchar(); 381 | scanf("%c",&char1); 382 | scanf("%d",&int2); 383 | getchar(); 384 | scanf("%c",&char2); 385 | scanf("%d",&int3); 386 | getchar(); 387 | scanf("%c",&char3); 388 | printf("int1:%d\n",int1); 389 | printf("int2:%d\n",int2); 390 | printf("int3:%d\n",int3); 391 | printf("char1:%c\n",char1); 392 | printf("char2:%c\n",char2); 393 | printf("char3:%c\n",char3); 394 | 395 | 396 | printf("Test printf character\n"); 397 | printf("return100:%c\n",return100()); 398 | printf("gCh2[1]:%c\n",gCh2['b'-'a']); 399 | printf("gCh1:%c\n",gCh1); 400 | printf("const1:%c\n",'y'); 401 | printf("const2:%c\n",char4); 402 | 403 | printf("multi_layer:%d\n",_Multi_layer(Fibonaci(calculateFactorial(3)),_Multi_layer(_Multi_layer(_Multi_layer(2333,6666+'a'),calculateFactorial(7)),Fibonaci(8)))); 404 | 405 | complexIf_Else(); 406 | 407 | complexWhile(); 408 | 409 | complexSwitch_Case(); 410 | printf("Congratulations,you passed all tests.\n"); 411 | 412 | return 0; 413 | } 414 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/normalTest/globalTest(标准C测试).exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/FinalProject/编译课程设计/编译课程设计/normalTest/globalTest(标准C测试).exe -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/normalTest/globalTest.c: -------------------------------------------------------------------------------- 1 | 2 | const int int1 = 666,int2 = +233,int3 = -1000,int4 = 0; 3 | const char char1 = '1',char2 = '_',char3='+',char4 = 'a'; 4 | 5 | 6 | int gInt1,gInt2[10],gInt3[10],gInt4,gInt5[10]; 7 | char gCh1,gCh2[10],gCh3[10],gCh4,gCh5[10]; 8 | 9 | char GGG[10]; 10 | 11 | 12 | void testAllDefine{ 13 | const int int1=333,int2 = +789,int3 = -7125,int4 = -1; 14 | 15 | const char char1 = '0',char2 = '-',char3='*',char4 = 'D',char5 = 'g', 16 | char6 = '+',char7 = '/',char8 = '9',char9 = 'Z',char10 = 'y'; 17 | 18 | int gInt1,gInt2[10],gInt3[10],gInt4,gInt5[10]; 19 | 20 | char gCh1,gCh2[10],gCh3[10],gCh4,gCh5[10]; 21 | 22 | int i; 23 | 24 | 25 | gInt1 = 10*1; 26 | gInt4 = 10*4; 27 | gCh1 = '0'; 28 | gCh4 = '9'; 29 | 30 | i = (0+0*0-0-(0-'a'+'b'-1)*34445); 31 | while(i<(333-222-111+19-9+gCh4-'8'-1)){ 32 | gInt2[2*i/2] = 2*10+i; 33 | gInt3[3*i/3] = 3*10+i; 34 | gInt5[5*i/5] = 5*10+i; 35 | gCh2[i] = 'a'; 36 | gCh3[i] = 'b'; 37 | gCh5[i] = 'c'; 38 | i = i + '8' - '7'; 39 | } 40 | 41 | 42 | printf("int1:",int1); 43 | printf("int2:",int2); 44 | printf("int3:",int3); 45 | printf("int4:",int4); 46 | 47 | printf("char1:",char1); 48 | printf("char2:",char2); 49 | printf("char3:",char3); 50 | printf("char4:",char4); 51 | 52 | printf("gInt1:",gInt1); 53 | printf("gInt4:",gInt4); 54 | 55 | printf("gCh1:",gCh1); 56 | printf("gCh4:",gCh4); 57 | 58 | i = 0; 59 | printf("gInt2[]:"); 60 | while(i<10){ 61 | i = i + 1; 62 | printf(gInt2[i-1]); 63 | } 64 | i = 0; 65 | printf("gInt3[]:"); 66 | while(i<10){ 67 | i = i + 1; 68 | printf(gInt3[i-1]); 69 | } 70 | i = 0; 71 | printf("gInt5[]:"); 72 | while(i<10){ 73 | i = i + 1; 74 | printf(gInt5[i-1]); 75 | } 76 | 77 | printf("gCh2[3]:",gCh2[3]); 78 | printf("gCh2[6]:",gCh2[6]); 79 | 80 | printf("gCh3[3]:",gCh3[3]); 81 | printf("gCh3[6]:",gCh3[6]); 82 | 83 | printf("gCh5[3]:",gCh5[3]); 84 | printf("gCh5[6]:",gCh5[6]); 85 | 86 | } 87 | 88 | void giveValueToGlobalVar{ 89 | int i; 90 | 91 | gInt1 = 100*1; 92 | gInt4 = 100*4; 93 | gCh1 = 'D'; 94 | gCh4 = 'F'; 95 | 96 | i = 0; 97 | while(i<10){ 98 | gInt2[i] = 2*100+i; 99 | gInt3[i] = 3*100+i; 100 | gInt5[i] = 5*100+i; 101 | gCh2[i] = 'i'; 102 | gCh3[i] = 'j'; 103 | gCh5[i] = 'k'; 104 | i = i + 'B' - 'A'; 105 | } 106 | } 107 | 108 | int returnAdd(int int1,int int2,int int3,int int4,int int5,int int6,int int7,int int8,int int9,int int10){ 109 | int gInt2; 110 | gInt2 = +int1+int2+int3+int4+int5+int6+int7+int8+int9+int10; 111 | return (gInt2); 112 | } 113 | 114 | int returnMix(char char1,char char2,char char3,char char4,char char5,char char6,char char7,char char8,char char9,char char10){ 115 | int gCh2,i; 116 | i = 0; 117 | gCh2 = char1+char2+char3+char4+char5-char6*char7+char8-char9+char10/2; 118 | while(i<(100/10)){ 119 | switch(i){ 120 | case 0: 121 | GGG[0] = char1; 122 | case 1: 123 | GGG[1] = char2; 124 | case 2: 125 | GGG[2] = char3; 126 | case 3: 127 | GGG[3] = char4; 128 | case 4: 129 | GGG[4] = char5; 130 | case 5: 131 | GGG[5] = char6; 132 | case 6: 133 | GGG[6] = char7; 134 | case 7: 135 | GGG[7] = char8; 136 | case 8: 137 | GGG[8] = char9; 138 | case 9: 139 | GGG[9] = char10; 140 | default:printf("Are you kidding me?\n"); 141 | } 142 | i = i + (100/100); 143 | } 144 | return (gCh2); 145 | } 146 | 147 | char MixParam(int int1,char char1,int int2,char char2){ 148 | if(int1+char1){ 149 | return ('1'); 150 | } 151 | else{ 152 | if(int2+char2) 153 | return ('2'); 154 | else 155 | return ('0'); 156 | } 157 | } 158 | 159 | int calculateFactorial(int item){ 160 | if(item < 0 ){ 161 | printf("The factorial number must >= 0 ."); 162 | return (0); 163 | }else{ 164 | if(item == 0) 165 | return (1); 166 | else 167 | return (calculateFactorial(item-1)*item); 168 | } 169 | } 170 | 171 | int Fibonaci(int n){ 172 | if(n<=2){ 173 | if((n+1)<=1){ 174 | printf("The Fibonaci number must >=1."); 175 | return (0); 176 | }else{ 177 | ; 178 | 179 | } 180 | return (1); 181 | } 182 | else 183 | return (Fibonaci(n-1)+Fibonaci(n-2)); 184 | } 185 | 186 | 187 | char return100{ 188 | return ('d'); 189 | } 190 | 191 | int returnIntValue{ 192 | return (1 + 'c' + Fibonaci(8) + calculateFactorial(4)); 193 | } 194 | 195 | void complexExpression{ 196 | int x1,x2,x3,x4,x5; 197 | 198 | x1 = (3+4-7*Fibonaci(9))/2-(calculateFactorial(5)-87*2+67)/(return100-50/3-12)+(34-67)/2*6; 199 | printf("complex --- x1:",x1); 200 | 201 | x2 = ( 333-222 + ( 1 + ( 2 + ( 3 * ( 89 - 56 * (3-3*6+Fibonaci(7) +Fibonaci(1) -2 ) + calculateFactorial(3) ) -3 ) + 4 ) -0*(0+0-0-0) - 1 ) + 2 ) - calculateFactorial(4)/4*111+Fibonaci(6); 202 | printf("complex --- x2:",x2); 203 | 204 | x3 = ('a'+90-'0'+(Fibonaci('a'-91))) + calculateFactorial(Fibonaci(Fibonaci(Fibonaci(Fibonaci(2))))) + 666*3/222; 205 | printf("complex --- x3:",x3); 206 | 207 | x4 = (666+888)/111 - return100/50 + gInt2[Fibonaci(3)] - gInt5[calculateFactorial(3)] + 5000; 208 | printf("complex --- x4:",x4); 209 | 210 | x5 = (calculateFactorial(5) - 67)*'a'+ x1 - x3 + 4666 + 2333/32 + 87 +'9' + gCh1 + '8'*'_' + '*'/'4' + gInt3[3+'f'-'e'] + (((((5+5)))))*100 - return100 + Fibonaci(5); 211 | printf("complex --- x5:",x5); 212 | } 213 | 214 | 215 | void complexIf_Else{ 216 | int i; 217 | i = 4; 218 | while(i>=0){ 219 | if(i == 4){ 220 | printf("In If-Else i = 4"); 221 | if(i == 4){ 222 | printf("In If-Else i = 4 again"); 223 | }else{ 224 | ; 225 | } 226 | }else{ 227 | if(i == 3){ 228 | printf("In If-Else i = 3"); 229 | if(i!=4){ 230 | printf("In If-Else i = 3 again"); 231 | }else{ 232 | ; 233 | } 234 | }else{ 235 | if(i == 2){ 236 | printf("In If-Else i = 2"); 237 | if(i-2){ 238 | printf("In If-Else i = 2 again"); 239 | }else{ 240 | ; 241 | } 242 | }else{ 243 | if(i == 1){ 244 | printf("In If-Else i = 1"); 245 | }else{ 246 | printf("Test If-Else Over."); 247 | } 248 | } 249 | } 250 | } 251 | i = i - 1; 252 | } 253 | } 254 | 255 | void complexSwitch_Case{ 256 | int k; 257 | k = 1; 258 | while(k<=4){ 259 | switch(k){ 260 | 261 | case 1:{ 262 | printf("Welcome"); 263 | printf("Welcome"); 264 | } 265 | 266 | case 2:{ 267 | if(k-2){ 268 | printf("False"); 269 | }else{ 270 | printf("True"); 271 | } 272 | } 273 | 274 | case 3:{ 275 | gInt1 = 3; 276 | while(gInt1){ 277 | printf("Tiga"); 278 | gInt1 = gInt1 - 1; 279 | } 280 | } 281 | 282 | case 4:{ 283 | switch(k+1){ 284 | case 2:printf("Sally"); 285 | case 3:printf("TAT_-_"); 286 | case 4:printf("My Sniper"); 287 | default:printf("CoCo"); 288 | } 289 | } 290 | } 291 | k = k + 1; 292 | } 293 | } 294 | 295 | void complexWhile{ 296 | int i,j,k; 297 | i = 0; 298 | while(i<3){ 299 | j = 0; 300 | while(j<3){ 301 | k = 0; 302 | while(k < 3){ 303 | printf("At test While:",i*9 + j*3 + k+1); 304 | k = k + 1; 305 | } 306 | j = j + 1; 307 | } 308 | i = i + 1; 309 | } 310 | } 311 | 312 | int _Multi_layer(int a1,int a2){ 313 | return (a1+a2); 314 | } 315 | 316 | void main(){ 317 | 318 | int returnScanfSymbol; 319 | int giveValueToGlobalVar; 320 | int testAllDefine[10]; 321 | char calculateFactorial; 322 | char Fibonaci[10]; 323 | int i; 324 | int int1,int2,int3; 325 | char char1,char2,char3; 326 | 327 | giveValueToGlobalVar; 328 | testAllDefine; 329 | 330 | printf("many params1:",returnAdd(1,26,33,445,512,656,744,834,954,1021)); 331 | printf("many params2:",returnMix('a','b','c','4','e','+','-','*','/','_')); 332 | 333 | 334 | i = 0; 335 | printf("The All Value Of GGG:"); 336 | while(i<10){ 337 | printf(GGG[i]); 338 | i = i + 1; 339 | } 340 | 341 | 342 | printf("Hello Boy, you need to be careful"); 343 | printf("If you input 0 ---> end the procedure"); 344 | printf("If you input 1 ---> we return the number of 1+1"); 345 | printf("If you input 2 ---> we return the number of 2*2"); 346 | printf("If you input others ---> we carry out the next procedure"); 347 | printf("Hey,Input int number to choose:"); 348 | scanf(returnScanfSymbol); 349 | if(returnScanfSymbol == 0){ 350 | printf("You mean to exit.Ok -_-"); 351 | return; 352 | }else{ 353 | if(returnScanfSymbol == 1){ 354 | printf("The result 1+1 is:",1+1); 355 | return; 356 | }else{ 357 | if(returnScanfSymbol == 2){ 358 | printf("The result 2*2 is:",2*2); 359 | return; 360 | }else{ 361 | printf("Next the trip."); 362 | } 363 | } 364 | } 365 | 366 | complexExpression; 367 | 368 | printf("complex expression add --- > x6:",(3*'*'+'/'-'-'+'a'*'+'+909/'_'+'/'*3+'+'*3)); 369 | printf("complex expression add --- > x7:",('-'-6*'/'+'-'*5+'+'+'*')); 370 | printf("Test scanf"); 371 | scanf(int1,int2,int3); 372 | printf("int1:",int1); 373 | printf("int2:",int2); 374 | printf("int3:",int3); 375 | 376 | scanf(char1,char2,char3); 377 | printf("char1:",char1); 378 | printf("char2:",char2); 379 | printf("char3:",char3); 380 | 381 | scanf(int1,char1,int2,char2,int3,char3); 382 | printf("int1:",int1); 383 | printf("int2:",int2); 384 | printf("int3:",int3); 385 | printf("char1:",char1); 386 | printf("char2:",char2); 387 | printf("char3:",char3); 388 | 389 | 390 | printf("Test printf character"); 391 | printf("return100:",return100); 392 | printf("gCh2[1]:",gCh2['b'-'a']); 393 | printf("gCh1:",gCh1); 394 | printf("const1:",'y'); 395 | printf("const2:",char4); 396 | 397 | printf("multi_layer:",_Multi_layer(Fibonaci(calculateFactorial(3)),_Multi_layer(_Multi_layer(_Multi_layer(2333,6666+'a'),calculateFactorial(7)),Fibonaci(8)))); 398 | 399 | complexIf_Else; 400 | 401 | complexWhile; 402 | 403 | complexSwitch_Case; 404 | 405 | printf("input a char:"); 406 | scanf(char1); 407 | while(char1!='d'){ 408 | switch(char1){ 409 | case 'a':printf("b--Fib(10):",Fibonaci(10)); 410 | case 'b':printf("b--Fib(9):",Fibonaci(9)); 411 | case 'c':printf("a--Fib(8):",Fibonaci(8)); 412 | } 413 | printf("input a char:"); 414 | scanf(char1); 415 | } 416 | 417 | printf("Congratulations,you passed all tests."); 418 | 419 | return; 420 | } -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/normalTest/globalTest_result.txt: -------------------------------------------------------------------------------- 1 | int1:333 2 | int2:789 3 | int3:-7125 4 | int4:-1 5 | char1:0 6 | char2:- 7 | char3:* 8 | char4:D 9 | gInt1:10 10 | gInt4:40 11 | gCh1:0 12 | gCh4:9 13 | gInt2[]: 14 | 20 15 | 21 16 | 22 17 | 23 18 | 24 19 | 25 20 | 26 21 | 27 22 | 28 23 | 29 24 | gInt3[]: 25 | 30 26 | 31 27 | 32 28 | 33 29 | 34 30 | 35 31 | 36 32 | 37 33 | 38 34 | 39 35 | gInt5[]: 36 | 50 37 | 51 38 | 52 39 | 53 40 | 54 41 | 55 42 | 56 43 | 57 44 | 58 45 | 59 46 | gCh2[3]:a 47 | gCh2[6]:a 48 | gCh3[3]:b 49 | gCh3[6]:b 50 | gCh5[3]:c 51 | gCh5[6]:c 52 | many params1:5226 53 | many params2:-1446 54 | The All Value Of GGG: 55 | a 56 | b 57 | c 58 | 4 59 | e 60 | + 61 | - 62 | * 63 | / 64 | _ 65 | Hello Boy, you need to be careful 66 | If you input 0 ---> end the procedure 67 | If you input 1 ---> we return the number of 1+1 68 | If you input 2 ---> we return the number of 2*2 69 | If you input others ---> we carry out the next procedure 70 | Hey,Input int number to choose: 71 | 72 | //输入0 73 | You mean to exit.Ok -_- 74 | //程序结束 75 | //输入1 76 | The result 1+1 is:2 77 | //程序结束 78 | //输入2 79 | The result 2*2 is:4 80 | //程序结束 81 | //输入其他 82 | Next the trip. 83 | complex --- x1:-211 84 | complex --- x2:247 85 | complex --- x3:157 86 | complex --- x4:4708 87 | complex --- x5:16252 88 | //继续下面的运行 89 | 90 | complex expression add --- > x6:4578 91 | complex expression add --- > x7:73 92 | Test scanf 93 | 94 | //输入模块省略,自行检查 95 | Test printf character 96 | return100:d 97 | gCh2[1]:i 98 | gCh1:D 99 | const1:y 100 | const2:a 101 | multi_layer:14165 102 | In If-Else i = 4 103 | In If-Else i = 3 104 | In If-Else i = 2 105 | In If-Else i = 1 106 | Test If-Else Over. 107 | At test While:1 108 | At test While:2 109 | At test While:3 110 | At test While:4 111 | At test While:5 112 | At test While:6 113 | At test While:7 114 | At test While:8 115 | At test While:9 116 | At test While:10 117 | At test While:11 118 | At test While:12 119 | At test While:13 120 | At test While:14 121 | At test While:15 122 | At test While:16 123 | At test While:17 124 | At test While:18 125 | At test While:19 126 | At test While:20 127 | At test While:21 128 | At test While:22 129 | At test While:23 130 | At test While:24 131 | At test While:25 132 | At test While:26 133 | At test While:27 134 | Welcome 135 | Welcome 136 | False 137 | Tiga 138 | Tiga 139 | Tiga 140 | Sally 141 | TAT_-_ 142 | My Sniper 143 | CoCo 144 | True 145 | Tiga 146 | Tiga 147 | Tiga 148 | TAT_-_ 149 | My Sniper 150 | CoCo 151 | Tiga 152 | Tiga 153 | Tiga 154 | My Sniper 155 | CoCo 156 | CoCo 157 | Congratulations,you passed all tests. 158 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/normalTest/optimize.c: -------------------------------------------------------------------------------- 1 | const int MAX_NUM = 1024 ; 2 | 3 | void complete_num{ 4 | int k[1024]; 5 | int i,j,n,s,x1; 6 | int m,k2,h,leap,x2; 7 | 8 | j=2; 9 | while(j< MAX_NUM){ 10 | n = -1; 11 | s = j; 12 | 13 | i=1; 14 | while(i= 1024) 21 | printf("OVERFLOW! "); 22 | else 23 | k[n] = i; 24 | }else{ 25 | ; 26 | } 27 | i = i + 1; 28 | } 29 | 30 | if(s==0){ 31 | printf("complete number: ",j); 32 | i = 0; 33 | while(i<=n){ 34 | printf(" ",k[i]); 35 | i = i + 1; 36 | } 37 | printf(" ") ; 38 | }else{ 39 | ; 40 | } 41 | j = j + 1; 42 | } 43 | printf("---------------------------------------------------------------"); 44 | 45 | h = 0 ; 46 | leap = 1 ; 47 | 48 | m = 2; 49 | while(m <= MAX_NUM){ 50 | k2 = m - 1; 51 | i = 2; 52 | while(i<=k2){ 53 | x2 = (m/i)*i ; 54 | if( x2 == m){ 55 | leap=0; 56 | }else{ 57 | ; 58 | } 59 | i = i + 1; 60 | } 61 | if(leap == 1){ 62 | printf(" ",m); 63 | h = h + 1; 64 | x2 = (h/10)*10 ; 65 | if( x2 == h) 66 | printf(" "); 67 | else 68 | ; 69 | }else{ 70 | ; 71 | } 72 | leap=1; 73 | m = m + 1; 74 | } 75 | 76 | printf("The total is ",h); 77 | } 78 | 79 | 80 | 81 | void main(){ 82 | complete_num; 83 | } -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/normalTest/t1.txt: -------------------------------------------------------------------------------- 1 | const int g_const_int1 = 23,g_const_int2 = 24; 2 | const char g_const_char1 = 'a',g_const_char2 = 'm'; 3 | 4 | int g_var_int1,g_var_int2; 5 | int g_var_int_arr[12],g_var_int3; 6 | char g_var_char1,g_var_char2; 7 | char g_var_char_arr[12],g_vat_char3; 8 | 9 | int Fibnaci(int n){ 10 | if(n<=2) 11 | return (1); 12 | else 13 | return (Fibnaci(n-1)+Fibnaci(n-2)); 14 | } 15 | 16 | void printGlobalVarValue{ 17 | printf("g_var_int1:",g_var_int1); 18 | printf("g_var_int2:",g_var_int2); 19 | printf("g_var_char1:",g_var_char1); 20 | printf("g_var_char2:",g_var_char2); 21 | printf("------------------------"); 22 | } 23 | 24 | void printGlobalArrValue(int loopLength){ 25 | int i; 26 | i = 0; 27 | printf("g_var_int_arr:"); 28 | wHIle(i='n') 105 | printf("bigger than n or equal to n"); 106 | else 107 | printf("smaller than n"); 108 | 109 | if(target>'n') 110 | printf("bigger than n"); 111 | else 112 | printf("not bigger than n"); 113 | 114 | if(target!='n') 115 | printf("not equal to n"); 116 | else 117 | printf("equal to n"); 118 | 119 | printf("------------------------"); 120 | 121 | } 122 | 123 | void main(){ 124 | int i; 125 | int flag1,flag2; 126 | int myFib,inputFib; 127 | CHAR relationFalg; 128 | int testIfElse,returnTen,returnTwo; 129 | 130 | g_var_int1 = g_const_int1 + g_const_int2; 131 | g_var_int2 = g_const_int2 - g_const_int1; 132 | g_var_char1 = g_const_char1; 133 | g_var_char2 = g_const_char2; 134 | 135 | printGlobalVarValue; 136 | 137 | g_var_int1 = g_const_int1 * 2; 138 | g_var_int2 = g_const_int2 / 3; 139 | 140 | printGlobalVarValue; 141 | 142 | i = 0; 143 | while(i<=11){ 144 | g_var_int_arr[i] = i; 145 | g_var_char_arr[i] = 'x'; 146 | i = i + 1; 147 | } 148 | g_var_int_arr[3] = g_var_int_arr[0] + 3; 149 | printGlobalArrValue(12); 150 | 151 | flag1 = 1;flag2 = g_var_int_arr[2]; 152 | testSwitchCase(flag1,flag2); 153 | testSwitchCase(flag1-1,flag2-1); 154 | testSwitchCase('a'-'d',flag2+4); 155 | testSwitchCase('a'-'c',flag2+3); 156 | testSwitchCase(111,222); 157 | 158 | myFib = Fibnaci(1); 159 | printf("Fib(1):",myFib); 160 | myFib = Fibnaci(2); 161 | printf("Fib(2):",myFib); 162 | myFib = Fibnaci(8); 163 | printf("Fib(8):",myFib); 164 | 165 | g_var_int1 = (1*(1 + 3) * 'a'*(1) + g_var_int_arr[4]) / (returnTwo(1)); 166 | g_var_int2 = (666/g_var_int_arr[2])/111*(returnTen(2)); 167 | printGlobalVarValue; 168 | printf(-3*-4++7+-6); 169 | 170 | relationFalg = 'a'; 171 | testIfElse(relationFalg); 172 | 173 | relationFalg = 'n'; 174 | testIfElse(relationFalg); 175 | 176 | relationFalg = 's'; 177 | testIfElse(relationFalg); 178 | 179 | printf("Please enter the number of items you want to calculate the Fibonacci sequence:"); 180 | 181 | scanf(inputFib); 182 | 183 | myFib = Fibnaci(inputFib); 184 | 185 | printf("the Fib number you want to get is:",myFib); 186 | 187 | { 188 | printf("lalalalala."); 189 | scanf(testIfElse,returnTen,returnTwo); 190 | { 191 | printf("sum is:",testIfElse + returnTen + returnTwo); 192 | printf("procedure is Over, Thank you."); 193 | } 194 | } 195 | 196 | return; 197 | } 198 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/normalTest/t1_result.txt: -------------------------------------------------------------------------------- 1 | g_var_int1:47 2 | g_var_int2:1 3 | g_var_char1:a 4 | g_var_char2:m 5 | ------------------------ 6 | g_var_int1:46 7 | g_var_int2:8 8 | g_var_char1:a 9 | g_var_char2:m 10 | ------------------------ 11 | g_var_int_arr: 12 | 0 13 | 1 14 | 2 15 | 3 16 | 4 17 | 5 18 | 6 19 | 7 20 | 8 21 | 9 22 | 10 23 | 11 24 | g_var_char_arr: 25 | x 26 | x 27 | x 28 | x 29 | x 30 | x 31 | x 32 | x 33 | x 34 | x 35 | x 36 | x 37 | Welcome to BUAA. 38 | g_var_int_arr: 39 | 0 40 | 1 41 | 2 42 | g_var_char_arr: 43 | x 44 | x 45 | x 46 | Sally Go2. 47 | Lala 48 | DiDi 49 | Hello World. 50 | g_var_int1:46 51 | g_var_int2:8 52 | g_var_char1:a 53 | g_var_char2:m 54 | ------------------------ 55 | Sally Go1. 56 | g_var_int1:46 57 | g_var_int2:8 58 | g_var_char1:a 59 | g_var_char2:m 60 | ------------------------ 61 | Excuse Me. 62 | BoBo 63 | CoCo 64 | Excuse Me. 65 | mmppppooodd 66 | Excuse Me. 67 | a 68 | Fib(1):1 69 | Fib(2):1 70 | Fib(8):21 71 | g_var_int1:196 72 | g_var_int2:30 73 | g_var_char1:a 74 | g_var_char2:m 75 | ------------------------ 76 | 13 77 | smaller than n 78 | smaller than n or equal to n 79 | not equal to n 80 | smaller than n 81 | not bigger than n 82 | not equal to n 83 | ------------------------ 84 | not smaller than n 85 | smaller than n or equal to n 86 | equal to n 87 | bigger than n or equal to n 88 | not bigger than n 89 | equal to n 90 | ------------------------ 91 | not smaller than n 92 | bigger than n 93 | not equal to n 94 | bigger than n or equal to n 95 | bigger than n 96 | not equal to n 97 | ------------------------ 98 | Please enter the number of items you want to calculate the Fibonacci sequence: 99 | //输入斐波那契数列的项数,for example:20 100 | 20 101 | the Fib number you want to get is:6765 102 | lalalalala. 103 | //输入三个整数10 20 30 104 | 10 20 30 105 | sum is:60 106 | procedure is Over, Thank you. 107 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/normalTest/t2.txt: -------------------------------------------------------------------------------- 1 | int getA(int a1,int a2){ 2 | a1 = 10*10; 3 | a2 = a1+a1+a1+a1; 4 | return (a2); 5 | } 6 | 7 | void main(){ 8 | int a,b,c,d; 9 | a = 2; 10 | b = 4; 11 | c = 6; 12 | d = 8; 13 | 14 | printf("a:",a); 15 | printf("b:",b); 16 | printf("c:",c); 17 | printf("d:",d); 18 | 19 | printf("DAG"); 20 | a = 2*b + 3*d-c + b*2; 21 | b = 3*c + d-4 + a + 3*c; 22 | c = 56 + 3*c + c*3 +678; 23 | d = 200+getA(10,209)*2; 24 | 25 | printf("a:",a); 26 | printf("b:",b); 27 | printf("c:",c); 28 | printf("d:",d); 29 | } -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/normalTest/t3.txt: -------------------------------------------------------------------------------- 1 | int Fibonaci(int n){ 2 | if(n<=2){ 3 | if((n+1)<=1){ 4 | printf("The Fibonaci number must >=1."); 5 | return (0); 6 | }else{ 7 | ; 8 | 9 | } 10 | return (1); 11 | } 12 | else 13 | return (Fibonaci(n-1)+Fibonaci(n-2)); 14 | } 15 | 16 | void complexSwitch_Case{ 17 | int k; 18 | k = 1; 19 | while(k<=4){ 20 | switch(k){ 21 | 22 | case 1:{ 23 | printf("Welcome"); 24 | printf("Welcome"); 25 | } 26 | 27 | case 2:{ 28 | if(k-2){ 29 | printf("False"); 30 | }else{ 31 | printf("True"); 32 | } 33 | } 34 | 35 | case 3:{ 36 | gInt1 = 3; 37 | while(gInt1){ 38 | printf("Tiga"); 39 | gInt1 = gInt1 - 1; 40 | } 41 | } 42 | 43 | case 4:{ 44 | switch(k+1){ 45 | case 2:printf("Sally"); 46 | case 3:printf("TAT_-_"); 47 | case 4:printf("My Sniper"); 48 | default:printf("CoCo"); 49 | } 50 | } 51 | } 52 | k = k + 1; 53 | } 54 | } 55 | 56 | void complexWhile{ 57 | int i,j,k; 58 | i = 0; 59 | while(i<3){ 60 | j = 0; 61 | while(j<3){ 62 | k = 0; 63 | while(k < 3){ 64 | printf("At test While:",i*9 + j*3 + k+1); 65 | k = k + 1; 66 | } 67 | j = j + 1; 68 | } 69 | i = i + 1; 70 | } 71 | } 72 | 73 | 74 | void complexIf_Else{ 75 | int i; 76 | i = 4; 77 | while(i>=0){ 78 | if(i == 4){ 79 | printf("In If-Else i = 4"); 80 | if(i == 4){ 81 | printf("In If-Else i = 4 again"); 82 | }else{ 83 | ; 84 | } 85 | }else{ 86 | if(i == 3){ 87 | printf("In If-Else i = 3"); 88 | if(i!=4){ 89 | printf("In If-Else i = 3 again"); 90 | }else{ 91 | ; 92 | } 93 | }else{ 94 | if(i == 2){ 95 | printf("In If-Else i = 2"); 96 | if(i-2){ 97 | printf("In If-Else i = 2 again"); 98 | }else{ 99 | ; 100 | } 101 | }else{ 102 | if(i == 1){ 103 | printf("In If-Else i = 1"); 104 | }else{ 105 | printf("Test If-Else Over."); 106 | } 107 | } 108 | } 109 | } 110 | i = i - 1; 111 | } 112 | } 113 | 114 | void main(){ 115 | printf("test if-else"); 116 | complexIf_Else; 117 | printf("test while"); 118 | complexWhile; 119 | printf("test switch"); 120 | complexSwitch_Case; 121 | 122 | printf("Fib(10):",Fibonaci(10)); 123 | printf("Fib(13):",Fibonaci(13)); 124 | printf("Fib(16):",Fibonaci(16)); 125 | } 126 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/op_mips.asm: -------------------------------------------------------------------------------- 1 | .data 2 | String7:.asciiz "OVERFLOW! " 3 | String8:.asciiz "complete number: " 4 | String9:.asciiz " " 5 | String10:.asciiz "---------------------------------------------------------------" 6 | String11:.asciiz " " 7 | String12:.asciiz "The total is " 8 | .globl main 9 | .text 10 | complete_num: 11 | sw $k0 0($sp) 12 | sw $k1 4($sp) 13 | sw $t4 8($sp) 14 | sw $t5 12($sp) 15 | sw $t6 16($sp) 16 | sw $t7 20($sp) 17 | sw $t8 24($sp) 18 | sw $t9 28($sp) 19 | sw $s0 32($sp) 20 | sw $s1 36($sp) 21 | sw $s2 40($sp) 22 | sw $s3 44($sp) 23 | sw $s4 48($sp) 24 | sw $s5 52($sp) 25 | sw $s6 56($sp) 26 | sw $s7 60($sp) 27 | addiu $sp $sp 64 28 | sw $fp 4($sp) 29 | move $fp $sp 30 | sw $ra 0($fp) 31 | addiu $sp $fp 8 32 | addiu $sp $sp 4136 33 | move $k0 $sp 34 | move $k1 $0 35 | addiu $sp $sp 4 36 | addiu $k1 $0 7 37 | li $t1 2 38 | move $s2 $t1 39 | Label1: 40 | li $t1 1024 41 | move $t4 $t1 42 | move $t1 $s2 43 | move $t2 $t4 44 | subu $t1 $t1 $t2 45 | move $t5 $t1 46 | move $a1 $t5 47 | bgez $a1 Label2 48 | li $t1 -1 49 | move $s1 $t1 50 | move $t1 $s2 51 | move $s4 $t1 52 | li $t1 1 53 | move $s0 $t1 54 | Label3: 55 | move $t1 $s0 56 | move $t2 $s2 57 | subu $t1 $t1 $t2 58 | move $t4 $t1 59 | move $a1 $t4 60 | bgez $a1 Label4 61 | move $t1 $s2 62 | move $t2 $s0 63 | div $t1 $t2 64 | mflo $t1 65 | move $t4 $t1 66 | move $t1 $t4 67 | move $t5 $t1 68 | move $t1 $t5 69 | move $t2 $s0 70 | mult $t1 $t2 71 | mflo $t1 72 | move $t6 $t1 73 | move $t1 $t6 74 | move $t7 $t1 75 | move $t1 $t7 76 | move $s6 $t1 77 | move $t1 $s6 78 | move $t2 $s2 79 | subu $t1 $t1 $t2 80 | move $t8 $t1 81 | move $a1 $t8 82 | bne $a1 $0 Label5 83 | move $t1 $s1 84 | li $t2 1 85 | addu $t1 $t1 $t2 86 | move $t4 $t1 87 | move $t1 $t4 88 | move $t5 $t1 89 | move $t1 $t5 90 | move $s1 $t1 91 | move $t1 $s4 92 | move $t2 $s0 93 | subu $t1 $t1 $t2 94 | move $t6 $t1 95 | move $t1 $t6 96 | move $t7 $t1 97 | move $t1 $t7 98 | move $s4 $t1 99 | li $t1 1024 100 | move $t8 $t1 101 | move $t1 $s1 102 | move $t2 $t8 103 | subu $t1 $t1 $t2 104 | move $t9 $t1 105 | move $a1 $t9 106 | bltz $a1 Label7 107 | la $a0 String7 108 | li $v0 4 109 | syscall 110 | li $a0 10 111 | li $v0 11 112 | syscall 113 | j Label8 114 | Label7: 115 | addiu $t0 $fp 8 116 | move $t1 $s1 117 | li $t2 4 118 | mult $t1 $t2 119 | mflo $t1 120 | addu $t0 $t0 $t1 121 | move $t1 $s0 122 | sw $t1 0($t0) 123 | Label8: 124 | j Label6 125 | Label5: 126 | Label6: 127 | move $t1 $s0 128 | li $t2 1 129 | addu $t1 $t1 $t2 130 | move $t4 $t1 131 | move $t1 $t4 132 | move $t5 $t1 133 | move $t1 $t5 134 | move $s0 $t1 135 | j Label3 136 | Label4: 137 | li $t1 0 138 | move $t6 $t1 139 | move $t1 $s4 140 | move $t2 $t6 141 | subu $t1 $t1 $t2 142 | move $t7 $t1 143 | move $a1 $t7 144 | bne $a1 $0 Label9 145 | la $a0 String8 146 | li $v0 4 147 | syscall 148 | addu $a0 $s2 $0 149 | li $v0 1 150 | syscall 151 | li $a0 10 152 | li $v0 11 153 | syscall 154 | li $t1 0 155 | move $s0 $t1 156 | Label11: 157 | move $t1 $s0 158 | move $t2 $s1 159 | subu $t1 $t1 $t2 160 | move $t4 $t1 161 | move $a1 $t4 162 | bgtz $a1 Label12 163 | la $a0 String9 164 | li $v0 4 165 | syscall 166 | addiu $t1 $fp 8 167 | move $t2 $s0 168 | li $t3 4 169 | mult $t2 $t3 170 | mflo $t2 171 | addu $t1 $t1 $t2 172 | lw $t1 0($t1) 173 | move $t4 $t1 174 | move $a0 $t4 175 | li $v0 1 176 | syscall 177 | li $a0 10 178 | li $v0 11 179 | syscall 180 | move $t1 $s0 181 | li $t2 1 182 | addu $t1 $t1 $t2 183 | move $t4 $t1 184 | move $t1 $t4 185 | move $t5 $t1 186 | move $t1 $t5 187 | move $s0 $t1 188 | j Label11 189 | Label12: 190 | la $a0 String9 191 | li $v0 4 192 | syscall 193 | li $a0 10 194 | li $v0 11 195 | syscall 196 | j Label10 197 | Label9: 198 | Label10: 199 | move $t1 $s2 200 | li $t2 1 201 | addu $t1 $t1 $t2 202 | move $t4 $t1 203 | move $t1 $t4 204 | move $t5 $t1 205 | move $t1 $t5 206 | move $s2 $t1 207 | j Label1 208 | Label2: 209 | la $a0 String10 210 | li $v0 4 211 | syscall 212 | li $a0 10 213 | li $v0 11 214 | syscall 215 | addiu $t0 $fp 4132 216 | li $t1 0 217 | sw $t1 0($t0) 218 | li $t1 1 219 | move $s7 $t1 220 | li $t1 2 221 | move $s3 $t1 222 | Label13: 223 | li $t1 1024 224 | move $t6 $t1 225 | move $t1 $s3 226 | move $t2 $t6 227 | subu $t1 $t1 $t2 228 | move $t7 $t1 229 | move $a1 $t7 230 | bgtz $a1 Label14 231 | move $t1 $s3 232 | li $t2 1 233 | subu $t1 $t1 $t2 234 | move $t4 $t1 235 | move $t1 $t4 236 | move $t5 $t1 237 | addiu $t0 $fp 4128 238 | move $t1 $t5 239 | sw $t1 0($t0) 240 | li $t1 2 241 | move $s0 $t1 242 | Label15: 243 | move $t1 $s0 244 | addiu $t2 $fp 4128 245 | lw $t2 0($t2) 246 | subu $t1 $t1 $t2 247 | move $t6 $t1 248 | move $a1 $t6 249 | bgtz $a1 Label16 250 | move $t1 $s3 251 | move $t2 $s0 252 | div $t1 $t2 253 | mflo $t1 254 | move $t4 $t1 255 | move $t1 $t4 256 | move $t5 $t1 257 | move $t1 $t5 258 | move $t2 $s0 259 | mult $t1 $t2 260 | mflo $t1 261 | move $t6 $t1 262 | move $t1 $t6 263 | move $t7 $t1 264 | move $t1 $t7 265 | move $s5 $t1 266 | move $t1 $s5 267 | move $t2 $s3 268 | subu $t1 $t1 $t2 269 | move $t8 $t1 270 | move $a1 $t8 271 | bne $a1 $0 Label17 272 | li $t1 0 273 | move $s7 $t1 274 | j Label18 275 | Label17: 276 | Label18: 277 | move $t1 $s0 278 | li $t2 1 279 | addu $t1 $t1 $t2 280 | move $t4 $t1 281 | move $t1 $t4 282 | move $t5 $t1 283 | move $t1 $t5 284 | move $s0 $t1 285 | j Label15 286 | Label16: 287 | li $t1 1 288 | move $t6 $t1 289 | move $t1 $s7 290 | move $t2 $t6 291 | subu $t1 $t1 $t2 292 | move $t7 $t1 293 | move $a1 $t7 294 | bne $a1 $0 Label19 295 | la $a0 String11 296 | li $v0 4 297 | syscall 298 | addu $a0 $s3 $0 299 | li $v0 1 300 | syscall 301 | li $a0 10 302 | li $v0 11 303 | syscall 304 | addiu $t1 $fp 4132 305 | lw $t1 0($t1) 306 | li $t2 1 307 | addu $t1 $t1 $t2 308 | move $t4 $t1 309 | move $t1 $t4 310 | move $t5 $t1 311 | addiu $t0 $fp 4132 312 | move $t1 $t5 313 | sw $t1 0($t0) 314 | addiu $t1 $fp 4132 315 | lw $t1 0($t1) 316 | li $t2 10 317 | div $t1 $t2 318 | mflo $t1 319 | move $t6 $t1 320 | move $t1 $t6 321 | move $t7 $t1 322 | move $t1 $t7 323 | li $t2 10 324 | mult $t1 $t2 325 | mflo $t1 326 | move $t8 $t1 327 | move $t1 $t8 328 | move $t9 $t1 329 | move $t1 $t9 330 | move $s5 $t1 331 | move $t0 $k0 332 | addiu $t0 $t0 0 333 | move $t1 $s5 334 | addiu $t2 $fp 4132 335 | lw $t2 0($t2) 336 | subu $t1 $t1 $t2 337 | sw $t1 0($t0) 338 | move $a1 $k0 339 | addiu $a1 $a1 0 340 | lw $a1 0($a1) 341 | bne $a1 $0 Label21 342 | la $a0 String9 343 | li $v0 4 344 | syscall 345 | li $a0 10 346 | li $v0 11 347 | syscall 348 | j Label22 349 | Label21: 350 | Label22: 351 | j Label20 352 | Label19: 353 | Label20: 354 | li $t1 1 355 | move $s7 $t1 356 | move $t1 $s3 357 | li $t2 1 358 | addu $t1 $t1 $t2 359 | move $t4 $t1 360 | move $t1 $t4 361 | move $t5 $t1 362 | move $t1 $t5 363 | move $s3 $t1 364 | j Label13 365 | Label14: 366 | la $a0 String12 367 | li $v0 4 368 | syscall 369 | addiu $a0 $fp 4132 370 | lw $a0 0($a0) 371 | li $v0 1 372 | syscall 373 | li $a0 10 374 | li $v0 11 375 | syscall 376 | move $sp $fp 377 | lw $s7 -4($sp) 378 | lw $s6 -8($sp) 379 | lw $s5 -12($sp) 380 | lw $s4 -16($sp) 381 | lw $s3 -20($sp) 382 | lw $s2 -24($sp) 383 | lw $s1 -28($sp) 384 | lw $s0 -32($sp) 385 | lw $t9 -36($sp) 386 | lw $t8 -40($sp) 387 | lw $t7 -44($sp) 388 | lw $t6 -48($sp) 389 | lw $t5 -52($sp) 390 | lw $t4 -56($sp) 391 | lw $k1 -60($sp) 392 | lw $k0 -64($sp) 393 | addiu $sp $sp -64 394 | lw $ra 0($fp) 395 | lw $fp 4($fp) 396 | jr $ra 397 | main: 398 | li $fp 268501212 399 | addiu $sp $fp 8 400 | addiu $sp $sp 0 401 | move $k0 $sp 402 | move $k1 $0 403 | jal complete_num 404 | #accomplish generate mips code. 405 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/op_tmpCode.txt: -------------------------------------------------------------------------------- 1 | void complete_num() 2 | G4j = 2 + 0 3 | Label1: 4 | T1 = 1024 + 0 5 | T2 = G4j - T1 6 | BGEZ T2 Label2 7 | G5n = -1 + 0 8 | G6s = G4j + 0 9 | G3i = 1 + 0 10 | Label3: 11 | T1 = G3i - G4j 12 | BGEZ T1 Label4 13 | T1 = G4j / G3i 14 | T2 = T1 + 0 15 | T3 = T2 * G3i 16 | T4 = T3 + 0 17 | G7x1 = T4 + 0 18 | T5 = G7x1 - G4j 19 | BNZ T5 Label5 20 | T1 = G5n + 1 21 | T2 = T1 + 0 22 | G5n = T2 + 0 23 | T3 = G6s - G3i 24 | T4 = T3 + 0 25 | G6s = T4 + 0 26 | T5 = 1024 + 0 27 | T6 = G5n - T5 28 | BLZ T6 Label7 29 | Print string "OVERFLOW! " 30 | New Line. 31 | Jump Label8 32 | Label7: 33 | G2k[G5n] = G3i + 0 34 | Label8: 35 | Jump Label6 36 | Label5: 37 | Label6: 38 | T1 = G3i + 1 39 | T2 = T1 + 0 40 | G3i = T2 + 0 41 | Jump Label3 42 | Label4: 43 | T3 = 0 + 0 44 | T4 = G6s - T3 45 | BNZ T4 Label9 46 | Print string "complete number: " 47 | Print id G4j 48 | New Line. 49 | G3i = 0 + 0 50 | Label11: 51 | T1 = G3i - G5n 52 | BGZ T1 Label12 53 | Print string " " 54 | T1 = G2k[G3i] 55 | Print id T1 56 | New Line. 57 | T1 = G3i + 1 58 | T2 = T1 + 0 59 | G3i = T2 + 0 60 | Jump Label11 61 | Label12: 62 | Print string " " 63 | New Line. 64 | Jump Label10 65 | Label9: 66 | Label10: 67 | T1 = G4j + 1 68 | T2 = T1 + 0 69 | G4j = T2 + 0 70 | Jump Label1 71 | Label2: 72 | Print string "---------------------------------------------------------------" 73 | New Line. 74 | G10h = 0 + 0 75 | G11leap = 1 + 0 76 | G8m = 2 + 0 77 | Label13: 78 | T3 = 1024 + 0 79 | T4 = G8m - T3 80 | BGZ T4 Label14 81 | T1 = G8m - 1 82 | T2 = T1 + 0 83 | G9k2 = T2 + 0 84 | G3i = 2 + 0 85 | Label15: 86 | T3 = G3i - G9k2 87 | BGZ T3 Label16 88 | T1 = G8m / G3i 89 | T2 = T1 + 0 90 | T3 = T2 * G3i 91 | T4 = T3 + 0 92 | G12x2 = T4 + 0 93 | T5 = G12x2 - G8m 94 | BNZ T5 Label17 95 | G11leap = 0 + 0 96 | Jump Label18 97 | Label17: 98 | Label18: 99 | T1 = G3i + 1 100 | T2 = T1 + 0 101 | G3i = T2 + 0 102 | Jump Label15 103 | Label16: 104 | T3 = 1 + 0 105 | T4 = G11leap - T3 106 | BNZ T4 Label19 107 | Print string " " 108 | Print id G8m 109 | New Line. 110 | T1 = G10h + 1 111 | T2 = T1 + 0 112 | G10h = T2 + 0 113 | T3 = G10h / 10 114 | T4 = T3 + 0 115 | T5 = T4 * 10 116 | T6 = T5 + 0 117 | G12x2 = T6 + 0 118 | T7 = G12x2 - G10h 119 | BNZ T7 Label21 120 | Print string " " 121 | New Line. 122 | Jump Label22 123 | Label21: 124 | Label22: 125 | Jump Label20 126 | Label19: 127 | Label20: 128 | G11leap = 1 + 0 129 | T1 = G8m + 1 130 | T2 = T1 + 0 131 | G8m = T2 + 0 132 | Jump Label13 133 | Label14: 134 | Print string "The total is " 135 | Print id G10h 136 | New Line. 137 | Ret 138 | void main() 139 | Call complete_num 140 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/optimize.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2018-1-12 4 | ** @location:BUAA 5 | ** @func:优化函数 6 | */ 7 | #include 8 | #include 9 | #include "optimize.h" 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | extern vector globalTmpCodeArr; 16 | extern vector globalSymbolTable; 17 | extern map maxTempOrderMap; 18 | 19 | map> functionVarIndexMap; 20 | map varToRegisterMap; 21 | 22 | vector funcNameTable;//函数名表 23 | //删除公共子表达式后的四元式表 24 | vector optimizeTmpCodeArr; 25 | //原四元式的基本块划分 26 | map> initBlockDivision; 27 | 28 | string intToStr(int number) { 29 | char x[10] = { '\0' }; 30 | sprintf_s(x,"%d",number); 31 | return string(x); 32 | } 33 | /*划分基本块函数 34 | *mode为true,表示对原始的进行划分,false对优化的进行 35 | */ 36 | void divideBlock() { 37 | map> & objMap = initBlockDivision; 38 | vector objVector = globalTmpCodeArr; 39 | /* 40 | 划分规则: 41 | 1.所有函数的第一条语句为开始语句 42 | 2.所有跳转语句的下一条语句为开始语句 43 | 3.所有的跳转语句的目标语句是开始语句 44 | 4.scanf语句,print语句,返回语句,整体的函数调用均单独分块 45 | */ 46 | //遍历整个四元式数组 47 | string funcName = "GLOBAL"; 48 | map>::iterator iter; 49 | /* 50 | 标志flag,用于划分基本块 51 | */ 52 | bool callFlag = false; 53 | bool expFlag = false;//连续的表达式块 54 | for (unsigned int i = 0; i < objVector.size(); i++) { 55 | FourYuanItem item = objVector.at(i); 56 | switch (item.type) { 57 | case FunctionDef: { 58 | expFlag = false; 59 | funcName = item.target; 60 | vector t; 61 | Block x; x.index = i; x.type = FuncBlock; 62 | t.push_back(x); 63 | objMap.insert(map>::value_type(funcName, t)); 64 | iter = objMap.find(funcName); 65 | funcNameTable.push_back(funcName); 66 | break; 67 | } 68 | case ValueParamDeliver: { 69 | expFlag = false; 70 | if (callFlag) 71 | break; 72 | callFlag = true; 73 | Block x; x.index = i; x.type = CallBlock; 74 | iter->second.push_back(x); 75 | break; 76 | } 77 | case AssignState: { 78 | //如果是获取返回值的ret型,则不用管,否则去新建一个块 79 | if (item.isTargetArr) { 80 | expFlag = false; 81 | Block x; x.index = i; x.type = ArrayBlock; 82 | iter->second.push_back(x); 83 | break; 84 | } 85 | if (item.left == "Ret") { 86 | break; 87 | } 88 | if (!expFlag) { 89 | expFlag = true; 90 | Block x; x.index = i; x.type = DAGBlock; 91 | iter->second.push_back(x); 92 | } 93 | break; 94 | } 95 | case Label: { 96 | expFlag = false; 97 | Block x; 98 | x.index = i; 99 | x.type = LabelBlock; 100 | iter->second.push_back(x); 101 | break; 102 | } 103 | case FunctionCall: { 104 | //将callFlag设置为false 105 | if (callFlag) { 106 | callFlag = false; 107 | } 108 | else { 109 | Block x; x.index = i; x.type = CallBlock; 110 | iter->second.push_back(x); 111 | } 112 | break; 113 | } 114 | case Jump: 115 | case BEZ: 116 | case BNZ: 117 | case BLZ: 118 | case BLEZ: 119 | case BGZ: 120 | case BGEZ: { 121 | expFlag = false; 122 | Block x; x.index = i; x.type = BranchBlock; 123 | iter->second.push_back(x); 124 | break; 125 | } 126 | case ReadChar: 127 | case ReadInt: { 128 | expFlag = false; 129 | Block x; x.index = i; x.type = ReadBlock; 130 | iter->second.push_back(x); 131 | break; 132 | } 133 | case PrintChar: 134 | case PrintStr: 135 | case PrintInt: 136 | case PrintId: { 137 | expFlag = false; 138 | Block x; x.index = i; x.type = WriteBlock; 139 | iter->second.push_back(x); 140 | break; 141 | } 142 | case ReturnChar: 143 | case ReturnEmpty: 144 | case ReturnInt: 145 | case ReturnId: { 146 | expFlag = false; 147 | Block x; x.index = i; x.type = RetBlock; 148 | iter->second.push_back(x); 149 | break; 150 | } 151 | } 152 | } 153 | 154 | } 155 | 156 | bool isIn(vector &obj,int target) { 157 | for (unsigned int i = 0; i < obj.size(); i++) { 158 | if (target == obj.at(i)) 159 | return true; 160 | } 161 | return false; 162 | } 163 | 164 | void doFillCalSequ(vector & calSequ,vector & myDAG,vector& interNodes,int code) { 165 | //检查code是否加入队列 166 | if (isIn(calSequ, code)) 167 | return; 168 | //检查该节点的所有父节点是否全部在calSequ里面 169 | bool flag = true; 170 | for (unsigned int j = 0; j < myDAG.at(code).parent.size(); j++) { 171 | if (!isIn(calSequ, myDAG.at(code).parent.at(j))) { 172 | flag = false; 173 | break; 174 | } 175 | } 176 | if (!flag) 177 | return; 178 | //将该节点加入队列 179 | calSequ.push_back(code); 180 | //下面检查该节点的左右子节点(都要检查) 181 | DAGNode node = myDAG.at(code); 182 | DAGNode left = myDAG.at(node.left); 183 | DAGNode right = myDAG.at(node.right); 184 | if (!node.isLeftLeaf) { 185 | if (isIn(interNodes, left.code)) 186 | doFillCalSequ(calSequ,myDAG,interNodes,left.code); 187 | } 188 | if (!node.isRightLeaf) { 189 | if (isIn(interNodes, right.code)) 190 | doFillCalSequ(calSequ,myDAG,interNodes,right.code); 191 | } 192 | } 193 | 194 | //搜索DAG图 195 | int searchDAG(vector & myDAG,int v1,int v2,char op) { 196 | for (unsigned int i = 0; i < myDAG.size(); i++) { 197 | DAGNode node = myDAG.at(i); 198 | if (node.isLeftLeaf && node.isRightLeaf) 199 | continue; 200 | if (!((myDAG.at(node.left).code == v1 && myDAG.at(node.right).code == v2) || 201 | (myDAG.at(node.left).code == v2 && myDAG.at(node.right).code == v1 && (op == '+' || op == '*')))) 202 | continue; 203 | if (node.op == op) 204 | return i; 205 | } 206 | return myDAG.size(); 207 | } 208 | 209 | //删除公共子表达式 210 | void deletePubExp(int startIndex,int loopEnd,string funcName) { 211 | NodeTable myTable; 212 | vector myDAG; 213 | vector cache; 214 | //Step1:建立DAG图以及结点表 215 | for (unsigned int i = startIndex; i < (unsigned)loopEnd; i++) { 216 | FourYuanItem item = globalTmpCodeArr.at(i); 217 | if (item.type == AssignState) { 218 | //如果实Ret 219 | int v1, v2, v3; 220 | map::iterator iter; 221 | //复杂在数组问题 222 | //检查左节点,如果左节点是数组 223 | if (item.isLeftArr) { 224 | iter = myTable.table.find(item.left); 225 | if (iter == myTable.table.end()) { 226 | DAGNode newNode1; 227 | v1 = myDAG.size(); 228 | newNode1.code = v1; 229 | newNode1.isLeftLeaf = newNode1.isRightLeaf = true; 230 | newNode1.value = item.left; 231 | newNode1.isInitial = false; 232 | myDAG.push_back(newNode1); 233 | myTable.table.insert(map::value_type(item.left,v1)); 234 | } 235 | else { 236 | v1 = iter->second; 237 | } 238 | //寻找v2 239 | iter = myTable.table.find(item.index2); 240 | if (iter == myTable.table.end()) { 241 | DAGNode newNode2; 242 | v2 = myDAG.size(); 243 | newNode2.code = v2; 244 | newNode2.isLeftLeaf = newNode2.isRightLeaf = true; 245 | newNode2.value = item.index2; 246 | if (item.index2.at(0) == 'G') 247 | newNode2.isInitial = true; 248 | else 249 | newNode2.isInitial = false; 250 | myDAG.push_back(newNode2); 251 | myTable.table.insert(map::value_type(item.index2,v2)); 252 | } 253 | else { 254 | v2 = iter->second; 255 | } 256 | //针对target,查询DAG图 257 | v3 = searchDAG(myDAG,v1,v2,'['); 258 | if (v3 == myDAG.size()) { 259 | DAGNode newNode3; 260 | newNode3.code = v3; 261 | newNode3.op = '['; 262 | newNode3.left = v1; 263 | newNode3.right = v2; 264 | newNode3.isLeftLeaf = newNode3.isRightLeaf = false; 265 | newNode3.isInitial = false; 266 | newNode3.target = item.target; 267 | myDAG.at(v1).parent.push_back(v3); 268 | myDAG.at(v2).parent.push_back(v3); 269 | myDAG.push_back(newNode3); 270 | } 271 | else { 272 | myDAG.at(v3).target = item.target; 273 | } 274 | iter = myTable.table.find(item.target); 275 | if (iter == myTable.table.end()) { 276 | myTable.table.insert(map::value_type(item.target,v3)); 277 | } 278 | else { 279 | iter->second = v3; 280 | } 281 | } 282 | else { 283 | iter = myTable.table.find(item.left); 284 | if (iter == myTable.table.end()) { 285 | v1 = myDAG.size(); 286 | DAGNode newNode1; 287 | newNode1.code = v1; 288 | newNode1.isLeftLeaf = newNode1.isRightLeaf = true; 289 | newNode1.value = item.left; 290 | if (item.left.at(0) == 'G') 291 | newNode1.isInitial = true; 292 | else 293 | newNode1.isInitial = false; 294 | myDAG.push_back(newNode1); 295 | myTable.table.insert(map::value_type(item.left, v1)); 296 | } 297 | else { 298 | v1 = iter->second; 299 | } 300 | iter = myTable.table.find(item.right); 301 | if (iter == myTable.table.end()) { 302 | v2 = myDAG.size(); 303 | DAGNode newNode2; 304 | newNode2.code = v2; 305 | newNode2.isLeftLeaf = newNode2.isRightLeaf = true; 306 | newNode2.value = item.right; 307 | if (item.right.at(0) == 'G') 308 | newNode2.isInitial = true; 309 | else 310 | newNode2.isInitial = false; 311 | myDAG.push_back(newNode2); 312 | myTable.table.insert(map::value_type(item.right, v2)); 313 | } 314 | else { 315 | v2 = iter->second; 316 | } 317 | v3 = searchDAG(myDAG,v1,v2,item.op); 318 | if (v3 == myDAG.size()) { 319 | DAGNode newNode3; 320 | newNode3.code = v3; 321 | newNode3.op = item.op; 322 | newNode3.left = v1; 323 | newNode3.right = v2; 324 | newNode3.isInitial = false; 325 | newNode3.isLeftLeaf = newNode3.isRightLeaf = false; 326 | newNode3.target = item.target; 327 | myDAG.push_back(newNode3); 328 | myDAG.at(v1).parent.push_back(v3); 329 | myDAG.at(v2).parent.push_back(v3); 330 | } 331 | else { 332 | myDAG.at(v3).target = item.target; 333 | } 334 | iter = myTable.table.find(item.target); 335 | if (iter == myTable.table.end()) { 336 | myTable.table.insert(map::value_type(item.target, v3)); 337 | } 338 | else { 339 | iter->second = v3; 340 | } 341 | } 342 | } 343 | } 344 | //Step2:遍历DAG图生成逆序的计算顺序 345 | /*Step2.1:遍历所有可能需要保存初始值的变量,观察它们所对应的变量在节点表中的位置是否发生变化, 346 | * 如果未发生变化,则不需要先生成保存的四元式代码 347 | */ 348 | //Step2.2:找出所有的中间节点 349 | vector interNodes; 350 | map::iterator itr = maxTempOrderMap.find(funcName); 351 | int tmpCount = itr->second+1;//保存的临时变量从当前函数最大临时变量计数器加一开始 352 | for (unsigned int i = 0; i < myDAG.size(); i++) { 353 | DAGNode node = myDAG.at(i); 354 | if (node.isLeftLeaf && node.isRightLeaf && node.isInitial) {//叶子结点 355 | map::iterator iter = myTable.table.find(node.value); 356 | if (iter->second != node.code) {//需要保存 357 | FourYuanItem fourItem; 358 | fourItem.isTargetArr = fourItem.isLeftArr = false; 359 | fourItem.type = AssignState; 360 | fourItem.target = "T" + intToStr(tmpCount++); 361 | fourItem.left = node.value; 362 | fourItem.right = "0"; 363 | fourItem.op = '+'; 364 | cache.push_back(fourItem); 365 | myDAG.at(i).value = fourItem.target; 366 | } 367 | } 368 | if (!node.isLeftLeaf && !node.isRightLeaf) 369 | interNodes.push_back(node.code); 370 | } 371 | //检查中间节点数,如果是跟优化前一样的化,不优化 372 | if ((loopEnd - startIndex) == interNodes.size()) { 373 | for (unsigned int p = startIndex; p < loopEnd; p++) { 374 | optimizeTmpCodeArr.push_back(globalTmpCodeArr.at(p)); 375 | } 376 | return; 377 | } 378 | else { 379 | for (unsigned int p = 0; p < cache.size(); p++) { 380 | optimizeTmpCodeArr.push_back(cache.at(p)); 381 | } 382 | } 383 | 384 | //Step2.3:针对所有的中间节点,访问节点表,找出最终选择生成的变量名(变量/临时,变量必生成) 385 | for (unsigned int i = 0; i < interNodes.size(); i++) { 386 | vector vars; 387 | bool isVarIn = false; 388 | map::iterator iter = myTable.table.begin(); 389 | while (iter != myTable.table.end()) { 390 | if (iter->second == interNodes.at(i)) { 391 | if (iter->first.at(0) == 'G') 392 | isVarIn = true; 393 | vars.push_back(iter->first); 394 | } 395 | iter++; 396 | } 397 | if (isVarIn) {//删除所有的临时变量 398 | for (unsigned int j = 0; j < vars.size(); j++) { 399 | if (vars.at(j).at(0) == 'T') { 400 | vars.erase(vars.begin() + j); 401 | j--;//弥补删除 402 | } 403 | } 404 | } 405 | else { 406 | if (vars.size() > 1) { 407 | vars.erase(vars.begin() + 1, vars.end()); 408 | } 409 | else if (vars.size() == 0) 410 | vars.push_back(myDAG.at(interNodes.at(i)).target); 411 | } 412 | //将DAG中对应节点的value设置为vars的第一个 413 | myDAG.at(interNodes.at(i)).value = vars.at(0); 414 | //加入final 415 | myTable.final.insert(map>::value_type(interNodes.at(i),vars)); 416 | } 417 | //Step2.4:遍历所有的中间节点,生成实质逆序的计算顺序 418 | vector calSequ; 419 | while (calSequ.size() != interNodes.size()) {//只要不等于,就继续扫描 420 | for (unsigned int i = 0; i < interNodes.size();i++) { 421 | int code = interNodes.at(i); 422 | doFillCalSequ(calSequ,myDAG,interNodes,code); 423 | } 424 | } 425 | //Step3:逆转计算序列 426 | reverse(calSequ.begin(),calSequ.end()); 427 | //Step4:生成新的四元式 428 | FourYuanItem four; 429 | for (unsigned int i = 0; i < calSequ.size(); i++) { 430 | int code = calSequ.at(i); 431 | map>::iterator iter = myTable.final.find(code); 432 | vector myVars; myVars.assign(iter->second.begin(),iter->second.end()); 433 | DAGNode curr = myDAG.at(code); 434 | DAGNode left = myDAG.at(curr.left); 435 | DAGNode right = myDAG.at(curr.right); 436 | char op = myDAG.at(code).op; 437 | 438 | four.type = AssignState; 439 | switch (op) { 440 | case '+': 441 | case '-': 442 | case '*': 443 | case '/': 444 | four.target = curr.value; 445 | four.isTargetArr = four.isLeftArr = false; 446 | four.left = left.value; 447 | four.right = right.value; 448 | four.op = op; 449 | optimizeTmpCodeArr.push_back(four); 450 | break; 451 | case '[': 452 | four.target = curr.value; 453 | four.isTargetArr = false; 454 | four.isLeftArr = true; 455 | four.left = left.value; 456 | four.index2 = right.value; 457 | optimizeTmpCodeArr.push_back(four); 458 | break; 459 | } 460 | } 461 | } 462 | 463 | //生成新的四元式集合 464 | void generateNewTmpCode() { 465 | for (unsigned int i = 0; i < funcNameTable.size(); i++) { 466 | string funcName = funcNameTable.at(i); 467 | map>::iterator iter = initBlockDivision.find(funcName); 468 | vector obj = iter->second; 469 | for (unsigned int j = 0; j < obj.size(); j++) { 470 | Block block = obj.at(j); 471 | if (block.type == DAGBlock) { 472 | int end; 473 | if (j + 1 == obj.size()) { 474 | for (unsigned k = block.index;; k++) { 475 | FourYuanItem item; 476 | if (k != block.index) { 477 | if (k == globalTmpCodeArr.size()) { 478 | end = k; 479 | break; 480 | } 481 | else { 482 | item = globalTmpCodeArr.at(k); 483 | if (item.type == FunctionDef) { 484 | end = k; 485 | break; 486 | } 487 | } 488 | } 489 | } 490 | } 491 | else { 492 | end = obj.at(j + 1).index; 493 | } 494 | deletePubExp(block.index,end,funcName); 495 | } 496 | else { 497 | //全部原样写入 498 | unsigned begin = block.index; 499 | unsigned end; 500 | if (j + 1 == obj.size()) { 501 | for (unsigned int k = begin;; k++) { 502 | FourYuanItem item; 503 | if (k != begin) {//防止是空函数 504 | if (k == globalTmpCodeArr.size()) {//跑到文件结尾 505 | break; 506 | } 507 | else { 508 | item = globalTmpCodeArr.at(k); 509 | if (item.type == FunctionDef) { 510 | break; 511 | } 512 | } 513 | } 514 | optimizeTmpCodeArr.push_back(globalTmpCodeArr.at(k)); 515 | } 516 | } 517 | else { 518 | end = obj.at(j + 1).index; 519 | for (unsigned int k = begin; k < end; k++) { 520 | optimizeTmpCodeArr.push_back(globalTmpCodeArr.at(k)); 521 | } 522 | } 523 | } 524 | } 525 | } 526 | } 527 | 528 | void allocateRegister() { 529 | string funcName = "GLOBAL"; 530 | unsigned int i = 0; 531 | //先读完所有的global 532 | for (; i < globalSymbolTable.size(); i++) { 533 | if (globalSymbolTable.at(i).getItemType() != Function) 534 | continue; 535 | else 536 | break; 537 | } 538 | for (; i < globalSymbolTable.size(); i++) { 539 | SymbolTableItem item = globalSymbolTable.at(i); 540 | if (item.getItemType() == Function) { 541 | funcName = item.getId(); 542 | vectorx; 543 | functionVarIndexMap.insert(map < string, vector>::value_type(funcName,x)); 544 | } 545 | else if ((item.getItemType() == Parament) || (item.getItemType() == Variable 546 | && item.getArrSize() == 0)) { 547 | map>::iterator iter = functionVarIndexMap.find(funcName); 548 | iter->second.push_back(i); 549 | } 550 | } 551 | //遍历函数集 552 | for (unsigned int j = 0; j < funcNameTable.size(); j++) { 553 | map>::iterator iter = functionVarIndexMap.find(funcNameTable.at(j)); 554 | //排序 555 | if (iter->second.size() == 0)//无需排序,过 556 | continue; 557 | if (iter->second.size() == 1) { 558 | varToRegisterMap.insert(map::value_type( 559 | "G" + intToStr(iter->second.at(0)) + globalSymbolTable.at(iter->second.at(0)).getId(), 560 | "$s" + intToStr(0))); 561 | } 562 | for (unsigned int k = 0; k < iter->second.size()-1; k++) { 563 | for (unsigned int l = k + 1; l < iter->second.size(); l++) { 564 | int w1 = globalSymbolTable.at(iter->second.at(k)).getWeight(); 565 | int w2 = globalSymbolTable.at(iter->second.at(l)).getWeight(); 566 | if (w1 < w2) { 567 | int t = iter->second.at(k); 568 | iter->second.at(k) = iter->second.at(l); 569 | iter->second.at(l) = t; 570 | } 571 | } 572 | } 573 | //排序完成 574 | //依据排序好的map,为全局的变量分配寄存器 575 | for (unsigned int k = 0; k < iter->second.size() && k<8; k++) { 576 | varToRegisterMap.insert(map::value_type( 577 | "G"+intToStr(iter->second.at(k))+globalSymbolTable.at(iter->second.at(k)).getId(), 578 | "$s"+intToStr(k))); 579 | } 580 | } 581 | } 582 | 583 | //暴露给外部的优化启动函数 584 | void toDoOptimize() { 585 | //基于原始的四元式数组,划分基本块 586 | divideBlock(); 587 | //基于DAG图的新四元式集合生成 588 | generateNewTmpCode(); 589 | //遍历整个符号表,为每个函数的参与临时寄存器分配的变量分配寄存器$s0~$s7 590 | allocateRegister(); 591 | } 592 | 593 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/optimize.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** @author:止水清潇menghuanlater 3 | ** @date:2018-1-12 4 | ** @location:BUAA 5 | ** @func:优化定义头文件 6 | */ 7 | #ifndef OPTIMIZE_H 8 | #define OPTIMIZE_H 9 | #define _CRT_SECURE_NO_WARNINGS 10 | #include "ConstValue.h" 11 | #include "globalFunction.h" 12 | #include "SymbolTable.h" 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | //块的类型 19 | enum BlockType { 20 | FuncBlock, 21 | DAGBlock, 22 | CallBlock, 23 | ReadBlock, 24 | WriteBlock, 25 | RetBlock, 26 | LabelBlock, 27 | BranchBlock, 28 | ArrayBlock 29 | }; 30 | 31 | //定义优化需要的相关结构体 32 | struct Block { 33 | int index;//开始的索引 34 | BlockType type;//基本块类型 35 | }; 36 | 37 | //节点表 38 | struct NodeTable { 39 | map table; 40 | map> final;//对应中间节点,最终选择的 41 | }; 42 | 43 | //DAG图中的每个节点 44 | struct DAGNode { 45 | int code; 46 | int left; 47 | int right; 48 | char op;//+ - * / = [ 49 | string value;//针对叶子结点 50 | //记录其所有的父节点 51 | vector parent; 52 | //针对叶节点引用不在结点表中的变量(只针对变量) 53 | string target;//当中间节点啥都没有了的时候,保存最新的 54 | bool isInitial; 55 | bool isLeftLeaf; 56 | bool isRightLeaf; 57 | }; 58 | 59 | //唯一的暴露给外部的函数 60 | void toDoOptimize(); 61 | 62 | #endif -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/thinking.txt: -------------------------------------------------------------------------------- 1 | 语义检查: 2 | 函数返回return语句的关键点思考 3 | 1.无返回值的函数存在返回值语句 4 | 2.有返回值的函数: 5 | 1.是否有返回值 6 | 2.返回类型检查 7 | 3.返回逻辑点检查(是否存在分支导致无返回值)--->控制逻辑分析法 8 | 9 | case的几个数值不可以出现相同的情况 10 | 11 | 语义检查: 12 | 1.<标识符>‘[’<表达式>‘]’需要检查: 标识符对应的是不是数组 如果是数组,表达式对应的下标值是否超数组长度,不确定值的话报个warning 13 | 2.<标识符>[‘(’<值参数表>‘)’] --> 若是单纯的标识符,检查是不是普通变量、常量或者有返回值无参数的函数; 若是标识符(参数表)模式, 14 | 首先检查标识符是否是有返回值的,其次是否有参数,参数个数对不对,各参数类型是否匹配 15 | 3.<标识符>=<表达式>|<标识符>‘[’<表达式>‘]’=<表达式> --->检查:如果标识符或者标识符[index]为char类型,则=后面的表达式必须为单字符 16 | 4.<表达式>需要有一个最终的属性,即<表达式>最终是char还是int,如果只是单个的char则就为char,否则为int 17 | 其值能否确定 18 | 5.switch()中必须使用4中获得最终属性,传递给情况子语句,作为case的后面所带常量类型是否匹配的标准 19 | 6.scanf标识符必须为普通变量或者参数,其余的均为不可 20 | 7.类型检查 21 | 8.分语法结构检查 22 | 23 | switch(表达式) 24 | 25 | 26 | //语法结构检查存在的问题 27 | int x{ 28 | return 10; ----->此处报错不好,会直接跳过void main()的分析,为何会出现缺少semicolon的问题 29 | } 30 | 31 | void main(){ 32 | int a[23]; 33 | a[1] = x; 34 | printf("hahaha:",a[1]); 35 | return; 36 | } 37 | 38 | 39 | //表达式里面嵌入'*','+'等如何解决 -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/tmpCode.txt: -------------------------------------------------------------------------------- 1 | void complete_num() 2 | G4j = 2 + 0 3 | Label1: 4 | T1 = 1024 + 0 5 | T2 = G4j - T1 6 | BGEZ T2 Label2 7 | G5n = -1 + 0 8 | G6s = G4j + 0 9 | G3i = 1 + 0 10 | Label3: 11 | T1 = G3i - G4j 12 | BGEZ T1 Label4 13 | T1 = G4j / G3i 14 | T2 = T1 + 0 15 | T3 = T2 * G3i 16 | T4 = T3 + 0 17 | G7x1 = T4 + 0 18 | T5 = G7x1 - G4j 19 | BNZ T5 Label5 20 | T1 = G5n + 1 21 | T2 = T1 + 0 22 | G5n = T2 + 0 23 | T3 = G6s - G3i 24 | T4 = T3 + 0 25 | G6s = T4 + 0 26 | T5 = 1024 + 0 27 | T6 = G5n - T5 28 | BLZ T6 Label7 29 | Print string "OVERFLOW! " 30 | New Line. 31 | Jump Label8 32 | Label7: 33 | G2k[G5n] = G3i + 0 34 | Label8: 35 | Jump Label6 36 | Label5: 37 | Label6: 38 | T1 = G3i + 1 39 | T2 = T1 + 0 40 | G3i = T2 + 0 41 | Jump Label3 42 | Label4: 43 | T3 = 0 + 0 44 | T4 = G6s - T3 45 | BNZ T4 Label9 46 | Print string "complete number: " 47 | Print id G4j 48 | New Line. 49 | G3i = 0 + 0 50 | Label11: 51 | T1 = G3i - G5n 52 | BGZ T1 Label12 53 | Print string " " 54 | T1 = G2k[G3i] 55 | Print id T1 56 | New Line. 57 | T1 = G3i + 1 58 | T2 = T1 + 0 59 | G3i = T2 + 0 60 | Jump Label11 61 | Label12: 62 | Print string " " 63 | New Line. 64 | Jump Label10 65 | Label9: 66 | Label10: 67 | T1 = G4j + 1 68 | T2 = T1 + 0 69 | G4j = T2 + 0 70 | Jump Label1 71 | Label2: 72 | Print string "---------------------------------------------------------------" 73 | New Line. 74 | G10h = 0 + 0 75 | G11leap = 1 + 0 76 | G8m = 2 + 0 77 | Label13: 78 | T3 = 1024 + 0 79 | T4 = G8m - T3 80 | BGZ T4 Label14 81 | T1 = G8m - 1 82 | T2 = T1 + 0 83 | G9k2 = T2 + 0 84 | G3i = 2 + 0 85 | Label15: 86 | T3 = G3i - G9k2 87 | BGZ T3 Label16 88 | T1 = G8m / G3i 89 | T2 = T1 + 0 90 | T3 = T2 * G3i 91 | T4 = T3 + 0 92 | G12x2 = T4 + 0 93 | T5 = G12x2 - G8m 94 | BNZ T5 Label17 95 | G11leap = 0 + 0 96 | Jump Label18 97 | Label17: 98 | Label18: 99 | T1 = G3i + 1 100 | T2 = T1 + 0 101 | G3i = T2 + 0 102 | Jump Label15 103 | Label16: 104 | T3 = 1 + 0 105 | T4 = G11leap - T3 106 | BNZ T4 Label19 107 | Print string " " 108 | Print id G8m 109 | New Line. 110 | T1 = G10h + 1 111 | T2 = T1 + 0 112 | G10h = T2 + 0 113 | T3 = G10h / 10 114 | T4 = T3 + 0 115 | T5 = T4 * 10 116 | T6 = T5 + 0 117 | G12x2 = T6 + 0 118 | T7 = G12x2 - G10h 119 | BNZ T7 Label21 120 | Print string " " 121 | New Line. 122 | Jump Label22 123 | Label21: 124 | Label22: 125 | Jump Label20 126 | Label19: 127 | Label20: 128 | G11leap = 1 + 0 129 | T1 = G8m + 1 130 | T2 = T1 + 0 131 | G8m = T2 + 0 132 | Jump Label13 133 | Label14: 134 | Print string "The total is " 135 | Print id G10h 136 | New Line. 137 | Ret 138 | void main() 139 | Call complete_num 140 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/代码优化.md: -------------------------------------------------------------------------------- 1 | # 针对中间代码的优化 2 | 3 | 1.建立DAG图,消除公共子表达式(针对每一个```基本块```) 4 | 5 | 2.恒等式的删除,比如 x = x + 0; x = x - 0; a[3] = a[3] + 0; 6 | 7 | 3.临时变量的重复利用(一个函数内存在大量的临时变量只使用过一次就再也没有用的情况)--->针对```基本块```的重新规划 8 | 9 | 4.将临时变量的起始地址放到$k0寄存器中,$k1寄存器存放临时变量的最大编号 -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/编译课程设计.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {9BFF5025-D6ED-4183-B1C4-3C77B7E9235E} 24 | Win32Proj 25 | 编译课程设计 26 | 10.0.15063.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v141 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v141 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v141 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v141 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | true 75 | 76 | 77 | true 78 | 79 | 80 | false 81 | 82 | 83 | false 84 | 85 | 86 | 87 | Level3 88 | Disabled 89 | true 90 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 91 | Use 92 | 93 | 94 | true 95 | Console 96 | 97 | 98 | 99 | 100 | Level3 101 | Disabled 102 | true 103 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 104 | Use 105 | 106 | 107 | true 108 | Console 109 | 110 | 111 | 112 | 113 | Level3 114 | MaxSpeed 115 | true 116 | true 117 | true 118 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 119 | Use 120 | 121 | 122 | true 123 | true 124 | true 125 | Console 126 | 127 | 128 | 129 | 130 | Level3 131 | MaxSpeed 132 | true 133 | true 134 | true 135 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 136 | Use 137 | 138 | 139 | true 140 | true 141 | true 142 | Console 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | NotUsing 161 | 162 | 163 | NotUsing 164 | 165 | 166 | NotUsing 167 | 168 | 169 | NotUsing 170 | 171 | 172 | NotUsing 173 | 174 | 175 | NotUsing 176 | 177 | 178 | NotUsing 179 | 180 | 181 | 182 | 183 | 184 | -------------------------------------------------------------------------------- /FinalProject/编译课程设计/编译课程设计/编译课程设计.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 20 | 资源文件 21 | 22 | 23 | 资源文件 24 | 25 | 26 | 27 | 28 | 头文件 29 | 30 | 31 | 头文件 32 | 33 | 34 | 头文件 35 | 36 | 37 | 头文件 38 | 39 | 40 | 头文件 41 | 42 | 43 | 头文件 44 | 45 | 46 | 47 | 48 | 源文件 49 | 50 | 51 | 源文件 52 | 53 | 54 | 源文件 55 | 56 | 57 | 源文件 58 | 59 | 60 | 源文件 61 | 62 | 63 | 源文件 64 | 65 | 66 | 源文件 67 | 68 | 69 | -------------------------------------------------------------------------------- /MIPS32.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/MIPS32.pdf -------------------------------------------------------------------------------- /Pascal保留字.txt: -------------------------------------------------------------------------------- 1 | PASCAL保留字中,保留字分为6种共36个: 2 | 3 | (1)程序、函数、过程符号 4 | 5 | program,function,procedure 6 | 7 | (2)说明部分专用定义符号 8 | 9 | array,const,file,label,of,packed,record,set,type,var 10 | 11 | (3)语句专用符号 12 | 13 | case,do,downto,else,for,forward,goto,if,repeat,then,to until,while,with 14 | 15 | (4)运算符号 16 | 17 | and,div,in,mod ,not,or 18 | 19 | (5)分隔符号 20 | 21 | begin,end 22 | 23 | (6)空指针常量 24 | 25 | nil -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BUAA_Complier_Design 2 | 北航本科三年级编译课程设计
3 | (由于某些原因,优化部分代码丢失,未上传) 4 | -------------------------------------------------------------------------------- /文法解读/result.txt: -------------------------------------------------------------------------------- 1 | g_var_int1:47 2 | g_var_int2:1; 3 | g_var_char1:b 4 | g_var_char2:l 5 | ------------------------ 6 | g_var_int1:46 7 | g_var_int2:8 8 | g_var_char1:b 9 | g_var_char2:l 10 | ------------------------ 11 | g_var_int_arr: 12 | 0 13 | 1 14 | 2 15 | 3 16 | 4 17 | 5 18 | 6 19 | 7 20 | 8 21 | 9 22 | 10 23 | 11 24 | g_var_char_arr: 25 | a 26 | b 27 | c 28 | d 29 | e 30 | f 31 | g 32 | h 33 | i 34 | j 35 | k 36 | l 37 | Welcome to BUAA. 38 | g_var_int_arr: 39 | 0 40 | 1 41 | 2 42 | g_var_char_arr: 43 | a 44 | b 45 | c 46 | Sally Go2. 47 | Hello World. 48 | g_var_int1:46 49 | g_var_int2:8 50 | g_var_char1:b 51 | g_var_char2:l 52 | ------------------------ 53 | Excuse Me. 54 | Fib(1):1 55 | Fib(2):1 56 | Fib(8):21 57 | g_var_int1:196 58 | g_var_int2:30 59 | g_var_char1:b 60 | g_var_char2:l 61 | ------------------------ 62 | smaller than n 63 | smaller than n or equal to n 64 | not equal to n 65 | smaller than n 66 | not bigger than n 67 | ------------------------ 68 | not smaller than n 69 | smaller than n or equal to n 70 | equal to n 71 | bigger than n or equal to n 72 | not bigger than n 73 | ------------------------ 74 | not smaller than n 75 | bigger than n 76 | not equal to n 77 | bigger than n or equal to n 78 | bigger than n 79 | ------------------------ 80 | Please enter the number of items you want to calculate the Fibonacci sequence: 81 | //从这开始程序会去接收输入的数据,程序会根据输入的数值计算对应的斐波那契数,假设我们输入的数值是20 82 | the Fib number you want to get is:6765 -------------------------------------------------------------------------------- /文法解读/test.txt: -------------------------------------------------------------------------------- 1 | const int g_const_int1 = 23,g_const_int2 = 24; 2 | const char g_const_char1 = 'a',g_const_char2 = 'm'; 3 | 4 | int g_var_int1,g_var_int2; 5 | int g_var_int_arr[12]; 6 | int g_var_char1,g_var_char2; 7 | int g_var_char_arr[12]; 8 | 9 | void printGlobalVarValue(){ 10 | printf("g_var_int1:",g_var_int1); 11 | printf("g_var_int2:",g_var_int2); 12 | printf("g_var_char1:",g_var_char1); 13 | printf("g_var_char2:",g_var_char2); 14 | printf("------------------------"); 15 | } 16 | 17 | void printGlobalArrValue(int loopLength){ 18 | int i; 19 | i = 0; 20 | printf("g_var_int_arr:"); 21 | while(i='n') 79 | printf("bigger than n or equal to n"); 80 | else 81 | printf("smaller than n"); 82 | if(target>'n') 83 | printf("bigger than n"); 84 | else 85 | printf("not bigger than n"); 86 | 87 | printf("------------------------"); 88 | 89 | } 90 | 91 | void main(){ 92 | int i; 93 | int flag1,flag2; 94 | int myFib,inputFib; 95 | char relationFalg; 96 | g_var_int1 = g_const_int1 + g_const_int2; 97 | g_var_int2 = g_const_int2 - g_const_int1; 98 | g_var_char1 = g_const_char1 + 1; 99 | g_var_char2 = g_const_char2 - 1; 100 | 101 | printGlobalVarValue; 102 | 103 | g_var_int1 = g_const_int1 * 2; 104 | g_var_int2 = g_const_int2 / 3; 105 | 106 | printGlobalVarValue; 107 | 108 | i = 0; 109 | while(i<=11){ 110 | g_var_int_arr[i] = i; 111 | g_var_char_arr[i] = 'a' + i; 112 | i = i + 1; 113 | } 114 | printGlobalArrValue(12); 115 | 116 | flag1 = 1;flag2 = g_var_int_arr[2]; 117 | testSwitchCase(flag1,flag2); 118 | testSwitchCase(flag1-1,flag2-1); 119 | testSwitchCase('d'-'-',flag2-8); 120 | 121 | myFib = Fibnaci(1); 122 | printf("Fib(1):",myFib); 123 | myFib = Fibnaci(2); 124 | printf("Fib(2):",myFib); 125 | myFib = Fibnaci(8); 126 | printf("Fib(8):",myFib); 127 | 128 | g_var_int1 = ((1 + 3) * 'a' + g_var_int_arr[4]) / (returnTwo); 129 | g_const_int2 = (666/g_var_int_arr[2])/111*(returnTen); 130 | printGlobalVarValue; 131 | 132 | relationFalg = 'a'; 133 | testIfElse(relationFalg); 134 | 135 | relationFalg = 'n'; 136 | testIfElse(relationFalg); 137 | 138 | relationFalg = 's'; 139 | testIfElse(relationFalg); 140 | 141 | printf("Please enter the number of items you want to calculate the Fibonacci sequence:"); 142 | 143 | scanf(inputFib); 144 | 145 | myFib = Fibnaci(inputFib); 146 | 147 | printf("the Fib number you want to get is:",myFib); 148 | 149 | return; 150 | } 151 | -------------------------------------------------------------------------------- /文法解读/扩充的C0文法.txt: -------------------------------------------------------------------------------- 1 | <加法运算符> ::= +|- 2 | <乘法运算符> ::= *|/ 3 | <关系运算符> ::= <|<=|>|>=|!=|== 4 | <字母> ::= _|a|...|z|A|...|Z 5 | <数字> ::= 0|<非零数字> 6 | <非零数字> ::= 1|...|9 7 | <字符> ::= '<加法运算符>'|'<乘法运算符>'|'<字母>'|'<数字>' 8 | <字符串> ::= "{十进制编码为32,33,35-126的ASCII字符}" 9 | <程序> ::= [<常量说明>][<变量说明>]{<有返回值函数定义>|<无返回值函数定义>}<主函数> 10 | <常量说明> ::= const<常量定义>;{ const<常量定义>;} 11 | <常量定义> ::= int<标识符>=<整数>{,<标识符>=<整数>} 12 | | char<标识符>=<字符>{,<标识符>=<字符>} 13 | <无符号整数> ::= <非零数字>{<数字>} 14 | <整数> ::= [+|-]<无符号整数>|0 15 | <标识符> ::= <字母>{<字母>|<数字>} 16 | <声明头部> ::= int<标识符> |char<标识符> 17 | <变量说明> ::= <变量定义>;{<变量定义>;} 18 | <变量定义> ::= <类型标识符>(<标识符>|<标识符>‘[’<无符号整数>‘]’){,(<标识符>|<标识符>‘[’<无符号整数>‘]’ )} 19 | <常量> ::= <整数>|<字符> 20 | <类型标识符> ::= int | char 21 | <有返回值函数定义> ::= <声明头部>‘(’<参数>‘)’ ‘{’<复合语句>‘}’|<声明头部>‘{’<复合语句>‘}’ //第一种选择为有参数的情况,第二种选择为无参数的情况 22 | 23 | <无返回值函数定义> ::= void<标识符>‘(’<参数>‘)’‘{’<复合语句>‘}’| void<标识符>{’<复合语句>‘}’//第一种选择为有参数的情况,第二种选择为无参数的情况 24 | 25 | <复合语句> ::= [<常量说明>][<变量说明>]<语句列> 26 | <参数> ::= <参数表> 27 | <参数表> ::= <类型标识符><标识符>{,<类型标识符><标识符>}| <空> 28 | <主函数> ::= void main‘(’‘)’‘{’<复合语句>‘}’ 29 | <表达式> ::= [+|-]<项>{<加法运算符><项>} 30 | <项> ::= <因子>{<乘法运算符><因子>} 31 | <因子> ::= <标识符>|<标识符>‘[’<表达式>‘]’|‘(’<表达式>‘)’|<整数>|<字符>|<有返回值函数调用语句> 32 | <语句> ::= <条件语句>|<循环语句>| ‘{’<语句列>‘}’|<有返回值函数调用语句>; 33 | |<无返回值函数调用语句>;|<赋值语句>;|<读语句>;|<写语句>;|<空>;|<情况语句>|<返回语句>; 34 | <赋值语句> ::= <标识符>=<表达式>|<标识符>‘[’<表达式>‘]’=<表达式> 35 | <条件语句>::= if ‘(’<条件>‘)’<语句>else<语句> 36 | <条件> ::= <表达式><关系运算符><表达式>|<表达式> //表达式为0条件为假,否则为真 37 | <循环语句> ::= while ‘(’<条件>‘)’<语句> 38 | <情况语句> ::= switch ‘(’<表达式>‘)’ ‘{’<情况表>[<缺省>] ‘}’ 39 | <情况表> ::= <情况子语句>{<情况子语句>} 40 | <情况子语句> ::= case<常量>:<语句> 41 | <缺省> ::= default : <语句> 42 | <有返回值函数调用语句> ::= <标识符>‘(’<值参数表>‘)’|<标识符> //第一种选择为有参数的情况,第二种选择为无参数的情况 43 | <无返回值函数调用语句> ::= <标识符>‘(’<值参数表>‘)’|<标识符> //第一种选择为有参数的情况,第二种选择为无参数的情况 44 | <值参数表> ::= <表达式>{,<表达式>} 45 | <语句列> ::= {<语句>} 46 | <读语句> ::= scanf ‘(’<标识符>{,<标识符>}‘)’ 47 | <写语句> ::= printf ‘(’ <字符串>,<表达式> ‘)’| printf ‘(’<字符串> ‘)’| printf ‘(’<表达式>‘)’ 48 | <返回语句> ::= return[‘(’<表达式>‘)’] 49 | 50 | 附加说明: 51 | 52 | (1)char类型的表达式,用字符的ASCII码对应的整数参加运算,在写语句中输出字符 53 | 54 | (2)标识符不区分大小写字母 55 | 56 | (3)写语句中的字符串原样输出 57 | 58 | (4)情况语句中,switch后面的表达式和case后面的常量只允许出现int和char类型;每个情况子语句执行完毕后,不继续执行后面的情况子语句 59 | 60 | (5)数组的下标从0开始 61 | 62 | (6)输出字符时,表达式写为字符变量名即可输出字符,其余表达式全部输出 63 | 64 | 保留字集合{ 65 | const,int,char,void,main,if,else,switch,case,default,while,scanf,printf,return 66 | } -------------------------------------------------------------------------------- /文法解读/文法解读.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/文法解读/文法解读.docx -------------------------------------------------------------------------------- /简单词法分析程序/简易词法分析正式文档.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/简单词法分析程序/简易词法分析正式文档.docx -------------------------------------------------------------------------------- /简单词法分析程序/简易词法分析程序.c: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define MAX 4096 //设置输入字符串长度最大为4096 8 | #define MAX_WORD 66 //设置最大单词长度 9 | #define REMAIN 36 //设置保留字的个数 10 | //枚举类型,设置所有的类别编码 11 | enum Sym { 12 | NOTHING, 13 | PROGRAM, FUNCTION, PROCEDURE, ARRAY, CONST, 14 | _FILE, LABLE, PACKED, VAR, RECORD, 15 | SET, TYPE, OF, CASE, DO, 16 | DOWNTO, ELSE, FOR, GOTO, IF, 17 | REPEAT, THEN, TO, UNNTIL, WHILE, 18 | WITH, FORWARD, AND, NOT, OR, 19 | IN, DIV, MOD, BEGIN, END, 20 | NIL, ID, INT, ADD, MINUS, 21 | STAR, DIVI, LPAR, RPAR, COMMA, 22 | SEMI, COLON, ASSIGN, EQUAL 23 | }; 24 | 25 | const char * remainSym[REMAIN] = { 26 | "program","function","procedure","array","const", 27 | "file" ,"lable" ,"packed" ,"var" ,"record", 28 | "set" ,"type" ,"of" ,"case" ,"do", 29 | "downto" ,"else" ,"for" ,"goto" ,"if", 30 | "repeat" ,"then" ,"to" ,"until","while", 31 | "with" ,"forward" ,"and" ,"not" ,"or", 32 | "in" ,"div" ,"mod" ,"begin","end","nil" 33 | }; 34 | char g_char; //字符型全局变量 35 | char g_token[MAX_WORD];//字符数组 36 | int g_num;//读入的整型数值 37 | enum Sym g_symbol;//当前识别的单词类型 38 | char coding[MAX];//输入的源代码 39 | int index;//coding数组的游标 40 | 41 | /*functions*/ 42 | void getSym(); //识别组合单词符号 43 | void initial();//初始化全局变量,作为每一次调用getSym的先行函数 44 | int reserve();//查找是否保留字 45 | void error() {//程序分析出错处理-->代表源程序词法不合法 46 | printf("抱歉,词法分析发现存在不合法内容,程序终止\n"); 47 | exit(EXIT_SUCCESS); 48 | } 49 | char getNextChar() {//获取一个char 50 | return coding[index++]; 51 | } 52 | void retract() {//回退 53 | index--; 54 | } 55 | /*functions*/ 56 | 57 | int main(void) { 58 | printf("Hello,欢迎使用简易词法分析程序,请输入你所要分析的源代码(输入终止符表示结束ctrl-D/ctrl-Z)\n"); 59 | int ch; 60 | while ((ch = getchar()) != EOF) { 61 | coding[index++] = ch; 62 | } 63 | index = 0; 64 | while (getNextChar() != '\0') { 65 | retract(); 66 | getSym(); 67 | if (g_symbol == NOTHING) 68 | continue; 69 | else if (g_symbol > NOTHING && g_symbol <= NIL) 70 | printf("识别出保留字:%s\n", remainSym[g_symbol - 1]); 71 | else if (g_symbol == ID) 72 | printf("识别出标识符:%s\n", g_token); 73 | else if (g_symbol == INT) 74 | printf("识别出整常数:%d\n", g_num); 75 | else if (g_symbol == ADD) 76 | printf("识别出+\n"); 77 | else if (g_symbol == MINUS) 78 | printf("识别出-\n"); 79 | else if (g_symbol == STAR) 80 | printf("识别出*\n"); 81 | else if (g_symbol == DIVI) 82 | printf("识别出/\n"); 83 | else if (g_symbol == LPAR) 84 | printf("识别出(\n"); 85 | else if (g_symbol == RPAR) 86 | printf("识别出)\n"); 87 | else if (g_symbol == COMMA) 88 | printf("识别出,\n"); 89 | else if (g_symbol == SEMI) 90 | printf("识别出;\n"); 91 | else if (g_symbol == COLON) 92 | printf("识别出:\n"); 93 | else if (g_symbol == ASSIGN) 94 | printf("识别出赋值符号:=\n"); 95 | else if (g_symbol == EQUAL) 96 | printf("识别出相等符号=\n"); 97 | } 98 | printf("分析结束,谢谢使用\n"); 99 | return 0; 100 | } 101 | 102 | void getSym() { 103 | initial(); //初始化 104 | g_char = getNextChar(); 105 | while (isspace(g_char)) 106 | g_char = getNextChar(); 107 | if (isalpha(g_char)) { 108 | while (isalnum(g_char)) { 109 | strcat(g_token, &g_char); 110 | g_char = getNextChar(); 111 | } 112 | retract(); 113 | int resultValue = reserve(); 114 | 115 | if (resultValue == 0) g_symbol = ID; 116 | else g_symbol = resultValue; 117 | } 118 | else if (isdigit(g_char)) { 119 | while (isdigit(g_char)) { 120 | strcat(g_token, &g_char); 121 | g_char = getNextChar(); 122 | } 123 | retract(); 124 | g_num = atoi(g_token); 125 | g_symbol = INT; 126 | } 127 | else if (g_char == ':') { 128 | g_char = getNextChar(); 129 | if (g_char == '=') g_symbol = ASSIGN; 130 | else { 131 | retract(); 132 | g_symbol = COLON; 133 | } 134 | } 135 | else if (g_char == '=') g_symbol = EQUAL; 136 | else if (g_char == '+') g_symbol = ADD; 137 | else if (g_char == '-') g_symbol = MINUS; 138 | else if (g_char == '*') g_symbol = STAR; 139 | else if (g_char == '(') g_symbol = LPAR; 140 | else if (g_char == ')') g_symbol = RPAR; 141 | else if (g_char == ',') g_symbol = COMMA; 142 | else if (g_char == ';') g_symbol = SEMI; 143 | else if (g_char == '/') { 144 | g_char = getNextChar(); 145 | if (g_char == '*') { 146 | while (1) { 147 | g_char = getNextChar(); 148 | if (g_char == '*') { 149 | g_char = getNextChar(); 150 | if (g_char == '/') 151 | break; 152 | else 153 | continue; 154 | } 155 | } 156 | } 157 | else { 158 | retract(); 159 | g_symbol = DIVI; 160 | } 161 | } 162 | else error(); 163 | } 164 | 165 | void initial() { 166 | g_char = '\0'; 167 | memset(g_token, 0, MAX_WORD * sizeof(char)); 168 | g_num = -1; 169 | g_symbol = NOTHING; //设置为0,不在单词类别编码表内 170 | } 171 | 172 | int reserve() { 173 | int i; 174 | for (i = 0; i < REMAIN; i++) { 175 | if (_stricmp(remainSym[i], g_token) == 0) 176 | return (i + 1); 177 | } 178 | return 0; 179 | } 180 | -------------------------------------------------------------------------------- /简单词法分析程序/简易词法分析要求.txt: -------------------------------------------------------------------------------- 1 | 1.识别面向pascal 2 | 2.需要分析识别出标识符、无符号整数、单子符分界符、双字符分界符、注释符号 3 | -------------------------------------------------------------------------------- /编译器设计文档.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menghuanlater/BUAA_Complier_Design/5ac6b481ae66d6e1fa466ba96cbe7b9f445bbc70/编译器设计文档.docx --------------------------------------------------------------------------------