├── .gitignore ├── C-Compiler ├── .vs │ └── C-Compiler │ │ └── v14 │ │ └── .suo ├── C-Compiler.VC.db ├── C-Compiler.sln └── C-Compiler │ ├── 0 prog-resource.txt │ ├── 1 prog-step1.txt │ ├── 10 stack_content.txt │ ├── 11 symbol_table.txt │ ├── 12 middle_code.txt │ ├── 13 ad_midcode.txt │ ├── 2 prog-clear.txt │ ├── 3-lexical.txt │ ├── 4 grammar.txt │ ├── 5 ll1_grammar.txt │ ├── 6 ad_grammar.txt │ ├── 7 vn_vtlist.txt │ ├── 8 first_follow.txt │ ├── 9 mtable.txt │ ├── C-Compiler.vcxproj │ ├── C-Compiler.vcxproj.filters │ ├── global.cpp │ ├── global.h │ ├── grammar-analysis.cpp │ ├── grammar-analysis.h │ ├── include.h │ ├── lexical-analysis.cpp │ ├── lexical-analysis.h │ ├── main.cpp │ ├── optimize.cpp │ ├── optimize.h │ ├── pretreat.cpp │ ├── pretreat.h │ ├── symbol.cpp │ └── symbol.h ├── README.md ├── 程序函数及变量定义.docx ├── 编译原理.docx └── 编译原理课设实验报告(语法及符号表).docx /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /C-Compiler/.vs/C-Compiler/v14/.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/.vs/C-Compiler/v14/.suo -------------------------------------------------------------------------------- /C-Compiler/C-Compiler.VC.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler.VC.db -------------------------------------------------------------------------------- /C-Compiler/C-Compiler.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "C-Compiler", "C-Compiler\C-Compiler.vcxproj", "{51635098-93F4-4AE9-94A5-7AD4CB37DB0B}" 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 | {51635098-93F4-4AE9-94A5-7AD4CB37DB0B}.Debug|x64.ActiveCfg = Debug|x64 17 | {51635098-93F4-4AE9-94A5-7AD4CB37DB0B}.Debug|x64.Build.0 = Debug|x64 18 | {51635098-93F4-4AE9-94A5-7AD4CB37DB0B}.Debug|x86.ActiveCfg = Debug|Win32 19 | {51635098-93F4-4AE9-94A5-7AD4CB37DB0B}.Debug|x86.Build.0 = Debug|Win32 20 | {51635098-93F4-4AE9-94A5-7AD4CB37DB0B}.Release|x64.ActiveCfg = Release|x64 21 | {51635098-93F4-4AE9-94A5-7AD4CB37DB0B}.Release|x64.Build.0 = Release|x64 22 | {51635098-93F4-4AE9-94A5-7AD4CB37DB0B}.Release|x86.ActiveCfg = Release|Win32 23 | {51635098-93F4-4AE9-94A5-7AD4CB37DB0B}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/0 prog-resource.txt: -------------------------------------------------------------------------------- 1 | /* A program to perform Euclid's 2 | Algorithm to compute gcd. */ 3 | int gcd (int u, int v) 4 | { if (v == 0) return u ; 5 | else return gcd(v,u-u/v*v); 6 | /* u-u/v*v == u mod v */ 7 | 8 | } 9 | /**/ 10 | void main(void) 11 | { int x; int y; 12 | x = input(); y = input(); 13 | output ( gcd ( x , y ) ) ; 14 | x=1+1; 15 | } -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/1 prog-step1.txt: -------------------------------------------------------------------------------- 1 | 2 | int gcd (int u, int v) 3 | { if (v == 0) return u ; 4 | else return gcd(v,u-u/v*v); 5 | 6 | 7 | } 8 | 9 | void main(void) 10 | { int x; int y; 11 | x = input(); y = input(); 12 | output ( gcd ( x , y ) ) ; 13 | x=1+1; 14 | } -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/10 stack_content.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/10 stack_content.txt -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/11 symbol_table.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/11 symbol_table.txt -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/12 middle_code.txt: -------------------------------------------------------------------------------- 1 | (100) Prog gcd 2 | (101) T0 := v == 0 3 | (102) if T0==1 Goto 104 4 | (103) Goto 106 5 | (104) return u 6 | (105) Goto 113 7 | (106) T1 := v * v 8 | (107) T2 := u / T1 9 | (108) T3 := u - T2 10 | (109) param v 11 | (110) param T3 12 | (111) T4 := call gcd 13 | (112) return T4 14 | (113) Prog main 15 | (114) T5 := call input 16 | (115) x := T5 17 | (116) T6 := call input 18 | (117) y := T6 19 | (118) param x 20 | (119) param y 21 | (120) T7 := call gcd 22 | (121) param T7 23 | (122) T8 := call output 24 | (123) T9 := 1 + 1 25 | (124) x := T9 26 | -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/13 ad_midcode.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/13 ad_midcode.txt -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/2 prog-clear.txt: -------------------------------------------------------------------------------- 1 | int gcd (int u, int v) 2 | { if (v == 0) return u ; 3 | else return gcd(v,u-u/v*v); 4 | } 5 | void main(void) 6 | { int x; int y; 7 | x = input(); y = input(); 8 | output ( gcd ( x , y ) ) ; 9 | x=1+1; 10 | } -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/3-lexical.txt: -------------------------------------------------------------------------------- 1 | int RESERVED 1 1 2 | gcd IDF 1 5 3 | ( SOP 1 9 4 | int RESERVED 1 10 5 | u ID 1 14 6 | , SOP 1 15 7 | int RESERVED 1 17 8 | v ID 1 21 9 | ) SOP 1 22 10 | { SOP 2 1 11 | if RESERVED 2 3 12 | ( SOP 2 6 13 | v ID 2 7 14 | == COP 2 9 15 | 0 NUM 2 12 16 | ) SOP 2 13 17 | return RESERVED 2 15 18 | u ID 2 22 19 | ; EOP 2 24 20 | else RESERVED 3 1 21 | return RESERVED 3 6 22 | gcd IDF 3 13 23 | ( SOP 3 16 24 | v ID 3 17 25 | , SOP 3 18 26 | u ID 3 19 27 | - OOP 3 20 28 | u ID 3 21 29 | / OOP 3 22 30 | v ID 3 23 31 | * OOP 3 24 32 | v ID 3 25 33 | ) SOP 3 26 34 | ; EOP 3 27 35 | } SOP 4 1 36 | void RESERVED 5 1 37 | main IDF 5 6 38 | ( SOP 5 10 39 | void RESERVED 5 11 40 | ) SOP 5 15 41 | { SOP 6 1 42 | int RESERVED 6 3 43 | x ID 6 7 44 | ; EOP 6 8 45 | int RESERVED 6 10 46 | y ID 6 14 47 | ; EOP 6 15 48 | x ID1 7 1 49 | = AOP 7 3 50 | input IDF 7 5 51 | ( SOP 7 10 52 | ) SOP 7 11 53 | ; EOP 7 12 54 | y ID1 7 14 55 | = AOP 7 16 56 | input IDF 7 18 57 | ( SOP 7 23 58 | ) SOP 7 24 59 | ; EOP 7 25 60 | output IDF 8 1 61 | ( SOP 8 8 62 | gcd IDF 8 10 63 | ( SOP 8 14 64 | x ID 8 16 65 | , SOP 8 18 66 | y ID 8 20 67 | ) SOP 8 22 68 | ) SOP 8 24 69 | ; EOP 8 26 70 | x ID1 9 1 71 | = AOP 9 2 72 | 1 NUM 9 3 73 | + OOP 9 4 74 | 1 NUM 9 5 75 | ; EOP 9 6 76 | } SOP 10 1 77 | # # 11 1 78 | -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/4 grammar.txt: -------------------------------------------------------------------------------- 1 | S -> program 2 | program -> declaration-list 3 | declaration-list -> declaration-list declaration | declaration 4 | declaration -> var-declaration | fun-declaration 5 | var-declaration -> type-specifier ID ; | type-specifier ID [ NUM ] ; 6 | type-specifier -> int | void 7 | fun-declaration -> type-specifier IDF ( params ) | compound-stmt 8 | params -> params-list | void 9 | params-list -> params-list , param | param 10 | param -> type-specifier ID | type-specifier ID [ ] 11 | compound-stmt -> { local-declarations statement-list } 12 | local-declarations -> local-declarations var-declaration | empty 13 | statement-list -> statement-list statement | empty 14 | statement -> expression-stmt | compound-stmt | selection-stmt | iteration-stmt | return-stmt 15 | expression-stmt -> expression ; | ; 16 | selection-stmt -> if ( expression ) statement | if ( expression ) statement else statement 17 | iteration-stmt -> while ( expression ) statement 18 | return-stmt -> return ; | return expression ; 19 | expression -> var1 = expression | simple-expression 20 | var -> ID | ID [ expression ] 21 | var1 -> ID1 | ID1 [ expression ] 22 | simple-expression -> additive-expression relop additive-expression | additive-expression 23 | relop -> <= | < | > | >= | == | != 24 | additive-expression -> additive-expression addop term | term 25 | addop -> + | - 26 | term -> term mulop factor | factor 27 | mulop -> * | / 28 | factor -> ( expression ) | var | call | NUM 29 | call -> IDF ( args ) 30 | args -> arg-list | empty 31 | arg-list -> arg-list , expression | expression -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/5 ll1_grammar.txt: -------------------------------------------------------------------------------- 1 | S -> program 2 | program -> declaration-list 3 | declaration-list -> declaration declaration-list' 4 | declaration-list' -> declaration declaration-list' | empty 5 | declaration -> var-declaration | fun-declaration 6 | var-declaration -> type-specifier ID var-declaration' 7 | var-declaration' -> ; | [ NUM ] ; 8 | type-specifier -> int | void 9 | fun-declaration -> type-specifier IDF ( params ) | compound-stmt 10 | params -> params-list | void 11 | params-list -> param params-list' 12 | params-list' -> , param params-list' | empty 13 | param -> type-specifier ID param' 14 | param' -> empty | [ ] 15 | compound-stmt -> { local-declarations statement-list } 16 | local-declarations -> empty | local-declarations' 17 | local-declarations' -> var-declaration local-declarations' | empty 18 | statement-list -> empty | statement-list' 19 | statement-list' -> statement statement-list' | empty 20 | statement -> expression-stmt | compound-stmt | selection-stmt | iteration-stmt | return-stmt 21 | expression-stmt -> expression ; | ; 22 | selection-stmt -> if ( expression ) statement selection-stmt' 23 | selection-stmt' -> empty | else statement 24 | iteration-stmt -> while ( expression ) statement 25 | return-stmt -> return return-stmt' 26 | return-stmt' -> ; | expression ; 27 | expression -> var1 = expression | simple-expression 28 | var -> ID var' 29 | var' -> empty | [ expression ] 30 | var1 -> ID1 var1' 31 | var1' -> empty | [ expression ] 32 | simple-expression -> additive-expression simple-expression' 33 | simple-expression' -> relop additive-expression | empty 34 | relop -> <= | < | > | >= | == | != 35 | additive-expression -> term additive-expression' 36 | additive-expression' -> addop term additive-expression' | empty 37 | addop -> + | - 38 | term -> factor term' 39 | term' -> mulop factor term' | empty 40 | mulop -> * | / 41 | factor -> ( expression ) | var | call | NUM 42 | call -> IDF ( args ) 43 | args -> arg-list | empty 44 | arg-list -> expression arg-list' 45 | arg-list' -> , expression arg-list' | empty 46 | -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/6 ad_grammar.txt: -------------------------------------------------------------------------------- 1 | 1 S -> program 2 | 2 program -> declaration-list 3 | 3 declaration-list -> declaration declaration-list' 4 | 4 declaration-list' -> declaration declaration-list' 5 | 5 declaration-list' -> empty 6 | 6 declaration -> var-declaration 7 | 7 declaration -> fun-declaration 8 | 8 var-declaration -> type-specifier ID var-declaration' 9 | 9 var-declaration' -> ; 10 | 10 var-declaration' -> [ NUM ] ; 11 | 11 type-specifier -> int 12 | 12 type-specifier -> void 13 | 13 fun-declaration -> type-specifier IDF ( params ) 14 | 14 fun-declaration -> compound-stmt 15 | 15 params -> params-list 16 | 16 params -> void 17 | 17 params-list -> param params-list' 18 | 18 params-list' -> , param params-list' 19 | 19 params-list' -> empty 20 | 20 param -> type-specifier ID param' 21 | 21 param' -> empty 22 | 22 param' -> [ ] 23 | 23 compound-stmt -> { local-declarations statement-list } 24 | 24 local-declarations -> empty 25 | 25 local-declarations -> local-declarations' 26 | 26 local-declarations' -> var-declaration local-declarations' 27 | 27 local-declarations' -> empty 28 | 28 statement-list -> empty 29 | 29 statement-list -> statement-list' 30 | 30 statement-list' -> statement statement-list' 31 | 31 statement-list' -> empty 32 | 32 statement -> expression-stmt 33 | 33 statement -> compound-stmt 34 | 34 statement -> selection-stmt 35 | 35 statement -> iteration-stmt 36 | 36 statement -> return-stmt 37 | 37 expression-stmt -> expression ; 38 | 38 expression-stmt -> ; 39 | 39 selection-stmt -> if ( expression ) statement selection-stmt' 40 | 40 selection-stmt' -> empty 41 | 41 selection-stmt' -> else statement 42 | 42 iteration-stmt -> while ( expression ) statement 43 | 43 return-stmt -> return return-stmt' 44 | 44 return-stmt' -> ; 45 | 45 return-stmt' -> expression ; 46 | 46 expression -> var1 = expression 47 | 47 expression -> simple-expression 48 | 48 var -> ID var' 49 | 49 var' -> empty 50 | 50 var' -> [ expression ] 51 | 51 var1 -> ID1 var1' 52 | 52 var1' -> empty 53 | 53 var1' -> [ expression ] 54 | 54 simple-expression -> additive-expression simple-expression' 55 | 55 simple-expression' -> relop additive-expression 56 | 56 simple-expression' -> empty 57 | 57 relop -> <= 58 | 58 relop -> < 59 | 59 relop -> > 60 | 60 relop -> >= 61 | 61 relop -> == 62 | 62 relop -> != 63 | 63 additive-expression -> term additive-expression' 64 | 64 additive-expression' -> addop term additive-expression' 65 | 65 additive-expression' -> empty 66 | 66 addop -> + 67 | 67 addop -> - 68 | 68 term -> factor term' 69 | 69 term' -> mulop factor term' 70 | 70 term' -> empty 71 | 71 mulop -> * 72 | 72 mulop -> / 73 | 73 factor -> ( expression ) 74 | 74 factor -> var 75 | 75 factor -> call 76 | 76 factor -> NUM 77 | 77 call -> IDF ( args ) 78 | 78 args -> arg-list 79 | 79 args -> empty 80 | 80 arg-list -> expression arg-list' 81 | 81 arg-list' -> , expression arg-list' 82 | 82 arg-list' -> empty 83 | -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/7 vn_vtlist.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/7 vn_vtlist.txt -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/8 first_follow.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/8 first_follow.txt -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/9 mtable.txt: -------------------------------------------------------------------------------- 1 | vnnum: 45 2 | vtnum: 30 3 | ID ; [ NUM ] int void IDF ( ) , { } if else while return = ID1 <= < > >= == != + - * / # 4 | S 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 | program 0 0 0 0 0 2 2 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 | declaration-list 0 0 0 0 0 3 3 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 | declaration-list' 0 0 0 0 0 4 4 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 8 | declaration 0 0 0 0 0 7 7 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 | var-declaration 0 0 0 0 0 8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 | var-declaration' 0 9 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 | type-specifier 0 0 0 0 0 11 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12 | fun-declaration 0 0 0 0 0 13 13 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 | params 0 0 0 0 0 15 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 14 | params-list 0 0 0 0 0 17 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 | params-list' 0 0 0 0 0 0 0 0 0 19 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 | param 0 0 0 0 0 20 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 | param' 0 0 22 0 0 0 0 0 0 21 21 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18 | compound-stmt 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 | local-declarations 24 24 0 24 0 25 25 24 24 0 0 24 24 24 0 24 24 0 24 0 0 0 0 0 0 0 0 0 0 0 20 | local-declarations' 27 27 0 27 0 26 26 27 27 0 0 27 27 27 0 27 27 0 27 0 0 0 0 0 0 0 0 0 0 0 21 | statement-list 29 29 0 29 0 0 0 29 29 0 0 29 28 29 0 29 29 0 29 0 0 0 0 0 0 0 0 0 0 0 22 | statement-list' 30 30 0 30 0 0 0 30 30 0 0 30 31 30 0 30 30 0 30 0 0 0 0 0 0 0 0 0 0 0 23 | statement 32 32 0 32 0 0 0 32 32 0 0 33 0 34 0 35 36 0 32 0 0 0 0 0 0 0 0 0 0 0 24 | expression-stmt 37 38 0 37 0 0 0 37 37 0 0 0 0 0 0 0 0 0 37 0 0 0 0 0 0 0 0 0 0 0 25 | selection-stmt 0 0 0 0 0 0 0 0 0 0 0 0 0 39 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 | selection-stmt' 40 40 0 40 0 0 0 40 40 0 0 40 40 40 41 40 40 0 40 0 0 0 0 0 0 0 0 0 0 0 27 | iteration-stmt 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 | return-stmt 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 43 0 0 0 0 0 0 0 0 0 0 0 0 0 29 | return-stmt' 45 44 0 45 0 0 0 45 45 0 0 0 0 0 0 0 0 0 45 0 0 0 0 0 0 0 0 0 0 0 30 | expression 47 0 0 47 0 0 0 47 47 0 0 0 0 0 0 0 0 0 46 0 0 0 0 0 0 0 0 0 0 0 31 | var 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 | var' 0 49 50 0 49 0 0 0 0 49 49 0 0 0 0 0 0 0 0 49 49 49 49 49 49 49 49 49 49 0 33 | var1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51 0 0 0 0 0 0 0 0 0 0 0 34 | var1' 0 0 53 0 0 0 0 0 0 0 0 0 0 0 0 0 0 52 0 0 0 0 0 0 0 0 0 0 0 0 35 | simple-expression 54 0 0 54 0 0 0 54 54 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 36 | simple-expression' 0 56 0 0 56 0 0 0 0 56 56 0 0 0 0 0 0 0 0 55 55 55 55 55 55 0 0 0 0 0 37 | relop 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 57 58 59 60 61 62 0 0 0 0 0 38 | additive-expression 63 0 0 63 0 0 0 63 63 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 39 | additive-expression'0 65 0 0 65 0 0 0 0 65 65 0 0 0 0 0 0 0 0 65 65 65 65 65 65 64 64 0 0 0 40 | addop 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 66 67 0 0 0 41 | term 68 0 0 68 0 0 0 68 68 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42 | term' 0 70 0 0 70 0 0 0 0 70 70 0 0 0 0 0 0 0 0 70 70 70 70 70 70 70 70 69 69 0 43 | mulop 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 71 72 0 44 | factor 74 0 0 76 0 0 0 75 73 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 | call 0 0 0 0 0 0 0 77 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 | args 78 0 0 78 0 0 0 78 78 79 0 0 0 0 0 0 0 0 78 0 0 0 0 0 0 0 0 0 0 0 47 | arg-list 80 0 0 80 0 0 0 80 80 0 0 0 0 0 0 0 0 0 80 0 0 0 0 0 0 0 0 0 0 0 48 | arg-list' 0 0 0 0 0 0 0 0 0 82 81 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49 | -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/C-Compiler.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {51635098-93F4-4AE9-94A5-7AD4CB37DB0B} 23 | Win32Proj 24 | CCompiler 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | 87 | 88 | Level3 89 | Disabled 90 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 91 | true 92 | 93 | 94 | Console 95 | true 96 | 97 | 98 | 99 | 100 | 101 | 102 | Level3 103 | Disabled 104 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 105 | true 106 | 107 | 108 | Console 109 | true 110 | 111 | 112 | 113 | 114 | Level3 115 | 116 | 117 | MaxSpeed 118 | true 119 | true 120 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 121 | true 122 | 123 | 124 | Console 125 | true 126 | true 127 | true 128 | 129 | 130 | 131 | 132 | Level3 133 | 134 | 135 | MaxSpeed 136 | true 137 | true 138 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 139 | true 140 | 141 | 142 | Console 143 | true 144 | true 145 | true 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/C-Compiler.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 源文件 20 | 21 | 22 | -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/global.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/global.cpp -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/global.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/global.h -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/grammar-analysis.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/grammar-analysis.cpp -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/grammar-analysis.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/grammar-analysis.h -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/include.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | #include "pretreat.h" 19 | #include "lexical-analysis.h" 20 | #include "grammar-analysis.h" 21 | #include "symbol.h" 22 | #include "optimize.h" -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/lexical-analysis.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/lexical-analysis.cpp -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/lexical-analysis.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/lexical-analysis.h -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/main.cpp -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/optimize.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/optimize.cpp -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/optimize.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/optimize.h -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/pretreat.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/pretreat.cpp -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/pretreat.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/pretreat.h -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/symbol.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/symbol.cpp -------------------------------------------------------------------------------- /C-Compiler/C-Compiler/symbol.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/C-Compiler/C-Compiler/symbol.h -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # From NUAA 2017 编译原理 2 | # C-Compiler 3 | 一个C-的编译器 4 | 5 | # 流程 6 | * 词法分析-语法分析-语义分析-代码优化 7 | * 使用有限状态自动机提取token 8 | * 使用ll(1)文法分析语法 9 | * 建立语法分析树 构建语义动作 10 | * 生成三地址代码 使用DAG图进行优化 11 | 12 | # 文件信息 13 | * 数字开头的为各步骤输出内容 14 | * global. 定义全局变量或函数 15 | * grammar-analysis. 处理语法 16 | * lexical-analysis. 处理词法 17 | * optimize. 进行优化 18 | * pretreat. 实现各种操作的预处理 19 | * symbol. 产生输入程序的符号表 20 | 21 | # 使用 22 | * 在0 prog-resource.txt中输入原始代码 23 | * 打开C-Compiler.sln编译所有代码运行 24 | * 语法规则见文档 25 | -------------------------------------------------------------------------------- /程序函数及变量定义.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/程序函数及变量定义.docx -------------------------------------------------------------------------------- /编译原理.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/编译原理.docx -------------------------------------------------------------------------------- /编译原理课设实验报告(语法及符号表).docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/D0ub1ePieR/C-Compiler/f5c3b6fcf5257a0f2ccfafc9b960c226d5922be5/编译原理课设实验报告(语法及符号表).docx --------------------------------------------------------------------------------