├── .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