├── Assignment 1 ├── mylexer.l └── myparser.y ├── Assignment 2 ├── mylexer.l └── myparser.y ├── Assignment 3 ├── mylexer.l └── myparser.y ├── Assignment 4 ├── TreeNode.h ├── TreeNodeBuild.cpp ├── TreeNodeCode.cpp ├── TreeNodeDisplay.cpp ├── TreeNodeTypeCheck.cpp ├── VarList.cpp ├── VarList.h ├── mylexer.l └── myparser.y ├── LICENSE └── README.md /Assignment 1/mylexer.l: -------------------------------------------------------------------------------- 1 | %{ 2 | /**************************************************************************** 3 | mylexer.l 4 | ParserWizard generated Lex file. 5 | 6 | Date: 2018��11��1�� 7 | ****************************************************************************/ 8 | 9 | #define _CRT_SECURE_NO_WARNINGS 10 | #pragma warning(disable:4996) 11 | #include "myparser.h" 12 | #include 13 | 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | int num_lines = 0; 19 | 20 | // const char* tokenStr[] = {}; 21 | struct var 22 | { 23 | string Name; 24 | void *Value; 25 | int Type; 26 | struct var *Next; 27 | }; 28 | 29 | class Var_list 30 | { 31 | public: 32 | struct var *head; 33 | int max; 34 | struct var *temp; 35 | Var_list() 36 | { 37 | max = 0; 38 | } 39 | int set_var(char* name) 40 | { 41 | string s_name = name; 42 | this->temp = this->head; 43 | //查看该变量是否已经存在 44 | int i = max; 45 | while( this->temp != NULL) 46 | { 47 | i--; 48 | if( temp->Name == s_name ) 49 | { 50 | return i; 51 | } 52 | temp = temp->Next; 53 | } 54 | this->temp = this->head; 55 | this->head = new struct var; 56 | this->head->Name = s_name; 57 | this->head->Next = temp; 58 | max++; 59 | 60 | return max-1; 61 | } 62 | }m_list; 63 | 64 | // 65 | 66 | static void print_token(string token, char* text) 67 | { 68 | // cout<<1; 69 | if(token == "SEMICOLON") 70 | cout< 11 | #include 12 | // #include 13 | 14 | using namespace std; 15 | 16 | 17 | 18 | 19 | %} 20 | 21 | ///////////////////////////////////////////////////////////////////////////// 22 | // declarations section 23 | 24 | // parser name 25 | %name myparser 26 | 27 | // class definition 28 | { 29 | // place any extra class members here 30 | 31 | } 32 | 33 | // constructor 34 | { 35 | // place any extra initialisation code here 36 | } 37 | 38 | // destructor 39 | { 40 | // place any extra cleanup code here 41 | } 42 | 43 | // attribute type 44 | %include { 45 | #ifndef YYSTYPE 46 | #define YYSTYPE int 47 | #endif 48 | } 49 | 50 | // place any declarations here 51 | 52 | %token NUMBER //十进制数 53 | %token ID 54 | %token ADD SUB // 加减 55 | %token MUL DIV //乘除 56 | %token LE RE //括号 57 | %token MOD INC DEC 58 | 59 | %token MAIN INT CHAR IF ELSE WHILE FOR 60 | 61 | %token LBRACE RBRACE LPAREN RPAREN LBRACKET RBRACKET SEMICOLON 62 | 63 | %% 64 | 65 | ///////////////////////////////////////////////////////////////////////////// 66 | // rules section 67 | 68 | // place your YACC rules here (there must be at least one) 69 | 70 | Grammar 71 | : /* empty */ 72 | ; 73 | 74 | %% 75 | 76 | ///////////////////////////////////////////////////////////////////////////// 77 | // programs section 78 | 79 | // int main(void) 80 | // { 81 | // int n = 1; 82 | // mylexer lexer; 83 | // myparser parser; 84 | // // parser.yycreate(); 85 | // if(lexer.yycreate()) 86 | // { 87 | // freopen("a.txt","r",stdin); 88 | // n = lexer.yylex(); 89 | // freopen("CON","r",stdin); 90 | // } 91 | // // if (parser.yycreate(&lexer)) { 92 | // // if (lexer.yycreate(&parser)) { 93 | // // n = parser.yyparse(); 94 | // // } 95 | // // } 96 | // return n; 97 | // } 98 | 99 | -------------------------------------------------------------------------------- /Assignment 2/mylexer.l: -------------------------------------------------------------------------------- 1 | %{ 2 | /**************************************************************************** 3 | mylexer.l 4 | ParserWizard generated Lex file. 5 | 6 | Date: 2018��11��1�� 7 | ****************************************************************************/ 8 | 9 | #define _CRT_SECURE_NO_WARNINGS 10 | #pragma warning(disable:4996) 11 | #include "myparser.h" 12 | #include 13 | 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | string token_text = "Hello"; 19 | 20 | int num_lines = 1; 21 | 22 | 23 | 24 | %} 25 | 26 | ///////////////////////////////////////////////////////////////////////////// 27 | // declarations section 28 | 29 | //语句类型 30 | 31 | MAIN "main" 32 | INT "int" 33 | CHAR "char" 34 | IF "if" 35 | ELSE "else" 36 | WHILE "while" 37 | FOR "for" 38 | 39 | STR \"[^\"]*\" 40 | LETTER '[^'\n]' 41 | ANNOTATION (\/\*(".*"|[^\*"]|(\*)+[^\/])*(\*)*\*\/)|(\/\/[^\n]*) 42 | ANNOTATIONO \/\/.*\n 43 | 44 | ID [_a-zA-Z][_a-zA-Z0-9]* 45 | 46 | NUMBER [0-9]+ 47 | 48 | LBRACE "{" 49 | RBRACE "}" 50 | LPAREN "(" 51 | RPAREN ")" 52 | LBRACKET "[" 53 | RBRACKET "]" 54 | 55 | //运算符 56 | 57 | //基本: 58 | 59 | 60 | ASSIGN "=" 61 | ADD "+" 62 | SUB "-" 63 | MUL "*" 64 | DIV "/" 65 | MOD "%" 66 | INC "++" 67 | DEC "--" 68 | //进阶: 69 | 70 | 71 | B_AND "&" 72 | B_IOR "|" 73 | B_EOR "^" 74 | B_OPP "~" 75 | M_LEFT "<<" 76 | M_RIGHT ">>" 77 | 78 | EQ "==" 79 | GRT ">" 80 | LET "<" 81 | GRE ">=" 82 | LEE "<=" 83 | NE "!=" 84 | 85 | AND "&&" 86 | OR "||" 87 | NOT "!" 88 | 89 | COMMA "," 90 | SEMICOLON ";" 91 | 92 | 93 | 94 | // lexical analyser name 95 | %name mylexer 96 | 97 | // class definition 98 | { 99 | // place any extra class members here 100 | public: 101 | static int getToken(); 102 | } 103 | 104 | // constructor 105 | { 106 | // place any extra initialisation code here 107 | } 108 | 109 | // destructor 110 | { 111 | // place any extra cleanup code here 112 | } 113 | 114 | // place any declarations here 115 | 116 | %% 117 | 118 | ///////////////////////////////////////////////////////////////////////////// 119 | // rules section 120 | 121 | 122 | 123 | // place your Lex rules here 124 | " " {} 125 | "\n" {++num_lines;} 126 | "\t" {} 127 | "" {} 128 | {MAIN} {return MAIN;} 129 | {INT} {return INT;} 130 | {CHAR} {return CHAR;} 131 | {IF} {return IF;} 132 | {ELSE} {return ELSE;} 133 | {WHILE} {return WHILE;} 134 | {FOR} {return FOR;} 135 | 136 | {STR} {return STR;} 137 | {LETTER} {token_text = yytext;return LETTER;} 138 | {ANNOTATION} {} 139 | {ANNOTATIONO} {} 140 | 141 | {ID} {token_text = yytext;return ID;} 142 | 143 | {NUMBER} {token_text = yytext;return NUMBER;} 144 | 145 | {LBRACE} {return LBRACE;} 146 | {RBRACE} {return RBRACE;} 147 | {LPAREN} {return LPAREN;} 148 | {RPAREN} {return RPAREN;} 149 | {LBRACKET} {return LBRACKET;} 150 | {RBRACKET} {return RBRACKET;} 151 | 152 | {ASSIGN} {return ASSIGN;} 153 | {ADD} {return ADD;} 154 | {SUB} {return SUB;} 155 | {MUL} {return MUL;} 156 | {DIV} {return DIV;} 157 | {MOD} {return MOD;} 158 | {INC} {return INC;} 159 | {DEC} {return DEC;} 160 | 161 | {B_AND} {return B_AND;} 162 | {B_IOR} {return B_IOR;} 163 | {B_EOR} {return B_EOR;} 164 | {B_OPP} {return B_OPP;} 165 | {M_LEFT} {return M_LEFT;} 166 | {M_RIGHT} {return M_RIGHT;} 167 | 168 | {EQ} {return EQ;} 169 | {GRT} {return GRT;} 170 | {LET} {return LET;} 171 | {GRE} {return GRE;} 172 | {LEE} {return LEE;} 173 | {NE} {return NE;} 174 | 175 | {AND} {return AND;} 176 | {OR} {return OR;} 177 | {NOT} {return NOT;} 178 | 179 | {COMMA} {return COMMA;} 180 | {SEMICOLON} {return SEMICOLON;} 181 | 182 | 183 | %% 184 | 185 | ///////////////////////////////////////////////////////////////////////////// 186 | // programs section 187 | -------------------------------------------------------------------------------- /Assignment 2/myparser.y: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chendante/Principles-of-Compilers-Assignments/eb2e33c0bf122ab93ee2a576306cf236c544fa44/Assignment 2/myparser.y -------------------------------------------------------------------------------- /Assignment 3/mylexer.l: -------------------------------------------------------------------------------- 1 | %{ 2 | /**************************************************************************** 3 | mylexer.l 4 | ParserWizard generated Lex file. 5 | 6 | Date: 2018��11��1�� 7 | ****************************************************************************/ 8 | 9 | #define _CRT_SECURE_NO_WARNINGS 10 | #pragma warning(disable:4996) 11 | #include "myparser.h" 12 | #include 13 | 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | string token_text = "Hello"; 19 | 20 | int num_lines = 0; 21 | %} 22 | 23 | ///////////////////////////////////////////////////////////////////////////// 24 | // declarations section 25 | 26 | //语句类型 27 | 28 | MAIN "main" 29 | INT "int" 30 | CHAR "char" 31 | IF "if" 32 | ELSE "else" 33 | WHILE "while" 34 | FOR "for" 35 | 36 | STR \"[^\"]*\" 37 | LETTER '[^'\n]' 38 | ANNOTATION (\/\*(".*"|[^\*"]|(\*)+[^\/])*(\*)*\*\/)|(\/\/[^\n]*) 39 | ANNOTATIONO \/\/.*\n 40 | 41 | ID [_a-zA-Z][_a-zA-Z0-9]* 42 | 43 | NUMBER [0-9]+ 44 | 45 | LBRACE "{" 46 | RBRACE "}" 47 | LPAREN "(" 48 | RPAREN ")" 49 | LBRACKET "[" 50 | RBRACKET "]" 51 | 52 | //运算符 53 | 54 | //基本: 55 | 56 | 57 | ASSIGN "=" 58 | ADD "+" 59 | SUB "-" 60 | MUL "*" 61 | DIV "/" 62 | MOD "%" 63 | INC "++" 64 | DEC "--" 65 | //进阶: 66 | 67 | 68 | B_AND "&" 69 | B_IOR "|" 70 | B_EOR "^" 71 | B_OPP "~" 72 | M_LEFT "<<" 73 | M_RIGHT ">>" 74 | 75 | EQ "==" 76 | GRT ">" 77 | LET "<" 78 | GRE ">=" 79 | LEE "<=" 80 | NE "!=" 81 | 82 | AND "&&" 83 | OR "||" 84 | NOT "!" 85 | 86 | COMMA "," 87 | SEMICOLON ";" 88 | 89 | 90 | 91 | // lexical analyser name 92 | %name mylexer 93 | 94 | // class definition 95 | { 96 | // place any extra class members here 97 | public: 98 | static int getToken(); 99 | } 100 | 101 | // constructor 102 | { 103 | // place any extra initialisation code here 104 | } 105 | 106 | // destructor 107 | { 108 | // place any extra cleanup code here 109 | } 110 | 111 | // place any declarations here 112 | 113 | %% 114 | 115 | ///////////////////////////////////////////////////////////////////////////// 116 | // rules section 117 | 118 | 119 | 120 | // place your Lex rules here 121 | " " {} 122 | "\n" {++num_lines;} 123 | "\t" {} 124 | "" {} 125 | {MAIN} {return MAIN;} 126 | {INT} {return INT;} 127 | {CHAR} {return CHAR;} 128 | {IF} {return IF;} 129 | {ELSE} {return ELSE;} 130 | {WHILE} {return WHILE;} 131 | {FOR} {return FOR;} 132 | 133 | {STR} {return STR;} 134 | {LETTER} {token_text = yytext;return LETTER;} 135 | {ANNOTATION} {} 136 | {ANNOTATIONO} {} 137 | 138 | {ID} {token_text = yytext;return ID;} 139 | 140 | {NUMBER} {token_text = yytext;return NUMBER;} 141 | 142 | {LBRACE} {return LBRACE;} 143 | {RBRACE} {return RBRACE;} 144 | {LPAREN} {return LPAREN;} 145 | {RPAREN} {return RPAREN;} 146 | {LBRACKET} {return LBRACKET;} 147 | {RBRACKET} {return RBRACKET;} 148 | 149 | {ASSIGN} {return ASSIGN;} 150 | {ADD} {return ADD;} 151 | {SUB} {return SUB;} 152 | {MUL} {return MUL;} 153 | {DIV} {return DIV;} 154 | {MOD} {return MOD;} 155 | {INC} {return INC;} 156 | {DEC} {return DEC;} 157 | 158 | {B_AND} {return B_AND;} 159 | {B_IOR} {return B_IOR;} 160 | {B_EOR} {return B_EOR;} 161 | {B_OPP} {return B_OPP;} 162 | {M_LEFT} {return M_LEFT;} 163 | {M_RIGHT} {return M_RIGHT;} 164 | 165 | {EQ} {return EQ;} 166 | {GRT} {return GRT;} 167 | {LET} {return LET;} 168 | {GRE} {return GRE;} 169 | {LEE} {return LEE;} 170 | {NE} {return NE;} 171 | 172 | {AND} {return AND;} 173 | {OR} {return OR;} 174 | {NOT} {return NOT;} 175 | 176 | {COMMA} {return COMMA;} 177 | {SEMICOLON} {return SEMICOLON;} 178 | 179 | 180 | %% 181 | 182 | ///////////////////////////////////////////////////////////////////////////// 183 | // programs section 184 | -------------------------------------------------------------------------------- /Assignment 3/myparser.y: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chendante/Principles-of-Compilers-Assignments/eb2e33c0bf122ab93ee2a576306cf236c544fa44/Assignment 3/myparser.y -------------------------------------------------------------------------------- /Assignment 4/TreeNode.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "VarList.h" 3 | #include 4 | #include "myparser.h" 5 | #include 6 | #define MAXCHILDREN 5 7 | using namespace std; 8 | 9 | extern VarList m_list; 10 | extern int num_lines; 11 | extern int node_num; 12 | extern stringstream code; 13 | extern int tmp_var_num; 14 | extern int label_num; 15 | 16 | class TreeNode 17 | { 18 | public: 19 | int line; // 第几行 20 | int num; //节点编号 21 | int tmp_var; // 临时变量 22 | int label_true; 23 | int label_false; 24 | union{int op; //操作符类型 token 25 | int val; //int常数值 26 | char c_value; 27 | char* name; //ID名 28 | } attr; 29 | 30 | NodeKind nodekind; //节点类型 31 | union{StmtKind stmt; ExpKind expr;} kind; 32 | TreeNode* children[MAXCHILDREN]; //子节点 33 | TreeNode* sibling; //兄弟节点用到地方: stmts 34 | 35 | ExpType type; //类型 36 | 37 | TreeNode(); 38 | 39 | //向兄弟链表的最后一位加个弟弟 40 | void newBrother(TreeNode* bro); 41 | 42 | static TreeNode* newStmtNode(StmtKind kind); 43 | 44 | static TreeNode* newExprNode(ExpKind kind); 45 | 46 | static TreeNode* newOpNode(int token); 47 | 48 | // 单目运算符的语句 49 | static TreeNode* newSingleNode(int token,TreeNode* fr); 50 | 51 | // 双目运算符的语句 52 | static TreeNode* newDoubleNode(int token,TreeNode* fr,TreeNode* sc); 53 | 54 | // 字母节点 55 | static TreeNode* newLetterNode(string str); 56 | 57 | // 数字节点 58 | static TreeNode* newIntNode(string str); 59 | 60 | // 变量节点 61 | static TreeNode* newIdNode(string str); 62 | 63 | // **************************************************************** 64 | // 自此之下为用于后序遍历整棵树,打印节点信息所写函数 65 | // 展示每个节点 66 | void display(); 67 | 68 | void printNode(); 69 | 70 | void printType(); 71 | 72 | void printStmt(); 73 | 74 | void printExp(); 75 | 76 | void printOp(); 77 | 78 | void printId(); 79 | 80 | void printConst(); 81 | 82 | // ********************************************************************** 83 | //打印错误信息 84 | static void typecheckError(TreeNode* node,string error_info); 85 | 86 | static void typecheckError_line(TreeNode* node,string error_info); 87 | 88 | // 类型检查 89 | void typecheckStart(); 90 | 91 | void typecheckNode(); 92 | 93 | void typecheckStmt(); 94 | 95 | void typecheckExp(); 96 | 97 | void typecheckOp(); 98 | 99 | //调用这个函数之前已经经过了类型检查 100 | void Calculate(); 101 | 102 | // ********************************************************************** 103 | // 以下为生成汇编代码 104 | static void genHeader(); 105 | static void genDecl(); 106 | void genMain(); 107 | void genCode(); 108 | void genExpr(); 109 | void genOp(); 110 | void genStmt(); 111 | // void genCode(); 112 | string genValue(); 113 | string genLabelTrue(); 114 | string genLabelFalse(); 115 | }; -------------------------------------------------------------------------------- /Assignment 4/TreeNodeBuild.cpp: -------------------------------------------------------------------------------- 1 | #include "TreeNode.h" 2 | 3 | TreeNode::TreeNode() 4 | { 5 | this->num = node_num++; 6 | this->line = num_lines; 7 | for (int i = 0; ichildren[i] = NULL; 10 | } 11 | this->sibling = NULL; 12 | this->nodekind = EMPTY; 13 | } 14 | 15 | //向兄弟链表的最后一位加个弟弟 16 | void TreeNode::newBrother(TreeNode* bro) 17 | { 18 | TreeNode *tmp = this; 19 | while(tmp->sibling != NULL) 20 | { 21 | tmp = tmp->sibling; 22 | } 23 | tmp->sibling = bro; 24 | } 25 | 26 | TreeNode* TreeNode::newStmtNode(StmtKind kind) 27 | { 28 | TreeNode* t = new TreeNode(); 29 | t->nodekind = StmtK; 30 | t->kind.stmt = kind; 31 | if(kind == IfK||kind == ForK||kind == WhileK) 32 | { 33 | t->label_true = label_num; 34 | label_num++; 35 | t->label_false = label_num; 36 | label_num++; 37 | } 38 | return t; 39 | } 40 | 41 | TreeNode* TreeNode::newExprNode(ExpKind kind) 42 | { 43 | TreeNode* t = new TreeNode(); 44 | t->nodekind = ExpK; 45 | t->kind.expr = kind; 46 | t->type = Void; 47 | return t; 48 | } 49 | 50 | TreeNode* TreeNode::newOpNode(int token) 51 | { 52 | TreeNode* res = newExprNode(OpK); 53 | res->attr.op = token; 54 | return res; 55 | } 56 | 57 | // 单目运算符的语句 58 | TreeNode* TreeNode::newSingleNode(int token,TreeNode* fr) 59 | { 60 | TreeNode* res = newOpNode(token); 61 | res->children[0] = fr; 62 | res->tmp_var = tmp_var_num; 63 | tmp_var_num++; 64 | return res; 65 | } 66 | 67 | // 双目运算符的语句 68 | TreeNode* TreeNode::newDoubleNode(int token,TreeNode* fr,TreeNode* sc) 69 | { 70 | TreeNode* res = newOpNode(token); 71 | res->children[0] = fr; 72 | res->children[1] = sc; 73 | res->tmp_var = tmp_var_num; 74 | tmp_var_num++; 75 | return res; 76 | } 77 | 78 | // 字母节点 79 | TreeNode* TreeNode::newLetterNode(string str) 80 | { 81 | TreeNode* res = newExprNode(ConstK); 82 | res->attr.c_value = str[1]; 83 | res->type = Char; 84 | return res; 85 | } 86 | 87 | // 数字节点 88 | TreeNode* TreeNode::newIntNode(string str) 89 | { 90 | TreeNode* res = newExprNode(ConstK); 91 | res->attr.val = stoi(str); 92 | res->type = Integer; 93 | return res; 94 | } 95 | 96 | // 变量节点 97 | TreeNode* TreeNode::newIdNode(string str) 98 | { 99 | TreeNode* res = newExprNode(IdK); 100 | res->type = Void; 101 | res->attr.name = (char*)(m_list.GetID(str)->name.data()); 102 | return res; 103 | } -------------------------------------------------------------------------------- /Assignment 4/TreeNodeCode.cpp: -------------------------------------------------------------------------------- 1 | #include "TreeNode.h" 2 | 3 | void TreeNode::genHeader() 4 | { 5 | code<<"\t.586\n"<children[0]->genCode(); 29 | code<<"\tinvoke ExitProcess, 0"<nodekind) 36 | { 37 | case StmtK: 38 | this->genStmt(); 39 | break; 40 | case ExpK: 41 | this->genExpr(); 42 | break; 43 | case TypeK: 44 | break; 45 | } 46 | if(sibling != NULL) 47 | { 48 | sibling->genCode(); 49 | } 50 | } 51 | 52 | void TreeNode::genDecl() 53 | { 54 | code<<"\n\n\t.data"<second->type) 60 | { 61 | case Integer: 62 | { 63 | code<<"\t\t_"<first<<" DWORD 0"<first<<" DWORD 0"<kind.expr) 87 | { 88 | case IdK: 89 | res = "_"+string(attr.name); 90 | break; 91 | case ConstK: 92 | switch(type) 93 | { 94 | case Char: 95 | r = attr.c_value; 96 | cout << attr.c_value; 97 | res = to_string(r); 98 | break; 99 | default: 100 | res = to_string(attr.val); 101 | break; 102 | } 103 | break; 104 | case OpK: 105 | res = "t"+to_string(this->tmp_var); 106 | break; 107 | case AssignK: 108 | return children[0]->genValue(); 109 | } 110 | return res; 111 | } 112 | 113 | void TreeNode::genExpr() 114 | { 115 | switch(this->kind.expr) 116 | { 117 | case OpK: 118 | this->genOp(); 119 | break; 120 | case IdK: 121 | break; 122 | case ConstK: 123 | break; 124 | } 125 | 126 | } 127 | 128 | void TreeNode::genOp() 129 | { 130 | TreeNode* child1 = this->children[0]; 131 | TreeNode* child2 = this->children[1]; 132 | child1->genCode(); 133 | if(child2 != NULL) 134 | { 135 | child2->genCode(); 136 | } 137 | code << "\tMOV eax,"<genValue()<attr.op) 139 | { 140 | case ADD: 141 | code << "\tADD eax,"<genValue()<genValue()<< endl; 145 | break; 146 | case MUL: 147 | code << "\tIMUL eax, "<genValue()<< endl; 148 | break; 149 | case DIV: 150 | code<<"\tMOV edx, 0"<genValue()<genValue()<kind.expr == IdK) 165 | { 166 | code<<"\tMOV "<genValue()<<",eax"<kind.expr == IdK) 172 | { 173 | code<<"\tMOV "<genValue()<<",eax"<genValue()<genValue()<genValue()<genValue()<genValue()<genValue()<genValue()<genValue()<genValue()<genValue()<genValue()<genValue()<<", 0"<num<<"2"<genValue()<<", 0"<num<<"2"<num<<"3"<num<<"2"<num<<"3"<genValue()<<", 0"<num<<"2"<genValue()<<", 0"<num<<"3"<num<<"2"<num<<"4"<num<<"3"<num<<"4"<genValue()<< ", eax" << endl; 256 | } 257 | 258 | string TreeNode::genLabelTrue() 259 | { 260 | string res; 261 | res = "@label_"+to_string(this->label_true); 262 | return res; 263 | } 264 | 265 | string TreeNode::genLabelFalse() 266 | { 267 | string res; 268 | res = "@label_"+to_string(this->label_false); 269 | return res; 270 | } 271 | 272 | void TreeNode::genStmt() 273 | { 274 | TreeNode* child1 = this->children[0]; 275 | TreeNode* child2 = this->children[1]; 276 | TreeNode* child3 = this->children[2]; 277 | TreeNode* child4 = this->children[3]; 278 | TreeNode* child5 = this->children[4]; 279 | switch(this->kind.stmt) 280 | { 281 | case IfK: 282 | child1->genCode(); 283 | code<<"\tcmp "<genValue()<<", 0"<genLabelTrue()<genCode(); 286 | code<genLabelTrue()<<":"<label_true<genLabelFalse()<<":"<genCode(); 292 | code<genLabelTrue()<<":"<genCode(); 294 | code<<"\tcmp "<genValue()<<", 0"<genLabelFalse()<genCode(); 301 | } 302 | code<<"\tjmp "<genLabelTrue()<genLabelFalse()<<":"<genCode(); 307 | } 308 | if(child3 != NULL) 309 | { 310 | child3->genCode(); 311 | } 312 | code<genLabelTrue()<<":"<genCode(); 316 | } 317 | else{ 318 | typecheckError_line(this," for error child2 is NULL"); 319 | } 320 | code<<"\tcmp "<genValue()<<", 0"<genLabelFalse()<kind.expr != IdK) 325 | { 326 | typecheckError_line(child1,"right of assign should be id"); 327 | return; 328 | } 329 | child2->genCode(); 330 | code<<"\tMOV eax, "<genValue()<genValue()<<", eax"<type == Char) 336 | { 337 | code<<"\tinvoke crt_scanf, SADD(\"%c\",0), addr "<genValue()<type == Integer) 340 | { 341 | code<<"\tinvoke crt_scanf, SADD(\"%d\",0), addr "<genValue()<genValue()<type == Char) 349 | { 350 | code<<"\tinvoke crt_printf, SADD(\"%c\",0), eax"<genCode(); 361 | break; 362 | case StmtsK: 363 | child1->genCode(); 364 | break; 365 | } 366 | } -------------------------------------------------------------------------------- /Assignment 4/TreeNodeDisplay.cpp: -------------------------------------------------------------------------------- 1 | #include "TreeNode.h" 2 | 3 | // 展示每个节点 4 | void TreeNode::display() 5 | { 6 | for(int i=0;ichildren[i] != NULL) 9 | this->children[i]->display(); 10 | } 11 | this->printNode(); 12 | if(this->sibling != NULL) 13 | { 14 | this->sibling->display(); 15 | } 16 | } 17 | 18 | void TreeNode::printNode() 19 | { 20 | cout<num<<":\t"; 21 | switch(this->nodekind) 22 | { 23 | case StmtK: 24 | this->printStmt(); 25 | break; 26 | case ExpK: 27 | this->printExp(); 28 | break; 29 | case TypeK: 30 | this->printType(); 31 | break; 32 | } 33 | cout<<"Children: "; 34 | for(int i=0;ichildren[i] != NULL) 37 | cout<<" "<children[i]->num; 38 | } 39 | if(this->sibling !=NULL) 40 | { 41 | cout<<" "<sibling->num; 42 | } 43 | cout<type) 50 | { 51 | case Integer: 52 | cout<<"Integer\t"; 53 | break; 54 | case Char: 55 | cout<<"Char\t\t"; 56 | break; 57 | } 58 | } 59 | 60 | void TreeNode::printStmt() 61 | { 62 | string stmt_list[] = {"If Stmt","While Stmt","For Stmt","Assign Stmt","Input Stmt","Output Stmt","Decl Stmt","Stmts"}; 63 | cout<kind.stmt]<<"\t\t\t"; 64 | } 65 | 66 | void TreeNode::printExp() 67 | { 68 | switch(this->kind.expr) 69 | { 70 | case OpK: 71 | cout<<"Expr\t\t"; 72 | this->printOp(); 73 | break; 74 | case ConstK: 75 | this->printConst(); 76 | break; 77 | case IdK: 78 | cout<<"ID\t\t"; 79 | this->printId(); 80 | break; 81 | } 82 | } 83 | 84 | void TreeNode::printOp() 85 | { 86 | switch(this->attr.op) 87 | { 88 | case ADD: 89 | cout<<"OP: +\t\t"; 90 | break; 91 | case SUB: 92 | cout<<"OP: -\t\t"; 93 | break; 94 | case MUL: 95 | cout<<"OP: *\t\t"; 96 | break; 97 | case DIV: 98 | cout<<"OP: /\t\t"; 99 | break; 100 | case MOD: 101 | cout<<"OP: %\t\t"; 102 | break; 103 | case USUB: 104 | cout<<"OP: -\t\t"; 105 | break; 106 | case INC: 107 | cout<<"OP: ++\t\t"; 108 | break; 109 | case DEC: 110 | cout<<"OP: --\t\t"; 111 | break; 112 | case M_LEFT: 113 | cout<<"OP: <<\t\t"; 114 | break; 115 | case M_RIGHT: 116 | cout<<"OP: >>\t\t"; 117 | break; 118 | case EQ: 119 | cout<<"OP: ==\t\t"; 120 | break; 121 | case GRT: 122 | cout<<"OP: >\t\t"; 123 | break; 124 | case LET: 125 | cout<<"OP: <\t\t"; 126 | break; 127 | case GRE: 128 | cout<<"OP: >=\t\t"; 129 | break; 130 | case LEE: 131 | cout<<"OP: <=\t\t"; 132 | break; 133 | case NE: 134 | cout<<"OP: !=\t\t"; 135 | break; 136 | case AND: 137 | cout<<"OP: &&\t\t"; 138 | break; 139 | case OR: 140 | cout<<"OP: ||\t\t"; 141 | break; 142 | case NOT: 143 | cout<<"OP: !\t\t"; 144 | break; 145 | case B_AND: 146 | cout<<"OP: &\t\t"; 147 | break; 148 | case B_EOR: 149 | cout<<"OP: ^\t\t"; 150 | break; 151 | case B_IOR: 152 | cout<<"OP: |\t\t"; 153 | break; 154 | case B_OPP: 155 | cout<<"OP: ~\t\t"; 156 | break; 157 | } 158 | } 159 | 160 | void TreeNode::printId() 161 | { 162 | cout<attr.name)<<"\t\t"; 163 | } 164 | 165 | void TreeNode::printConst() 166 | { 167 | switch(this->type) 168 | { 169 | case Char: 170 | cout<<"Char\t\t"; 171 | cout<attr.c_value<<"\t\t"; 172 | break; 173 | case Integer: 174 | cout<<"Integer\t\t"; 175 | cout<attr.val<<"\t\t"; 176 | break; 177 | } 178 | } -------------------------------------------------------------------------------- /Assignment 4/TreeNodeTypeCheck.cpp: -------------------------------------------------------------------------------- 1 | #include "TreeNode.h" 2 | 3 | void TreeNode::typecheckError(TreeNode* node,string error_info) 4 | { 5 | cout<<"error in node:"<num<<" error info: "<line<<" error info: "<children[i] != NULL) 19 | { 20 | this->children[i]->typecheckStart(); 21 | } 22 | } 23 | if(this->sibling != NULL) 24 | { 25 | this->sibling->typecheckStart(); 26 | } 27 | this->typecheckNode(); 28 | } 29 | 30 | void TreeNode::typecheckNode() 31 | { 32 | switch(this->nodekind) 33 | { 34 | case StmtK: 35 | this->typecheckStmt(); 36 | break; 37 | case ExpK: 38 | this->typecheckExp(); 39 | break; 40 | case TypeK: 41 | //this->typecheckType(); 42 | break; 43 | } 44 | } 45 | 46 | void TreeNode::typecheckStmt() 47 | { 48 | switch(this->kind.stmt) 49 | { 50 | case IfK: 51 | { 52 | if(this->children[0] != NULL) 53 | { 54 | if(this->children[0]->type != Boolean) 55 | { 56 | typecheckError(this, "if check is not Boolean"); 57 | } 58 | } 59 | break; 60 | } 61 | case WhileK: 62 | { 63 | if(this->children[0] != NULL) 64 | { 65 | if(this->children[0]->type != Boolean) 66 | { 67 | typecheckError(this, "while check is not Boolean"); 68 | } 69 | } 70 | break; 71 | } 72 | case ForK: 73 | { 74 | if(this->children[1] != NULL) 75 | { 76 | if(this->children[0]->type != Boolean) 77 | { 78 | typecheckError(this, "for check is not Boolean"); 79 | } 80 | } 81 | } 82 | case AssignK: 83 | { 84 | if(this->children[0]->type != this->children[1]->type) 85 | { 86 | typecheckError(this,"assign stmt type error"); 87 | } 88 | else 89 | { 90 | this->type = this->children[0]->type; 91 | } 92 | break; 93 | } 94 | case InputK: 95 | break; 96 | case OutputK: 97 | break; 98 | case DeclK: 99 | { 100 | // ExpType tmp_type = this->children[0]->type; 101 | // TreeNode* tmp_node = this->children[1]; 102 | // while(tmp_node != NULL) 103 | // { 104 | // tmp_node->type = tmp_type; 105 | // tmp_node = tmp_node->sibling; 106 | // m_list.addType(tmp_node->attr.name, tmp_type); 107 | // } 108 | break; 109 | } 110 | case StmtsK: 111 | break; 112 | } 113 | } 114 | 115 | void TreeNode::typecheckExp() 116 | { 117 | switch(this->kind.expr) 118 | { 119 | case OpK: 120 | this->typecheckOp(); 121 | break; 122 | case ConstK: 123 | break; 124 | case IdK: 125 | // 当遇见一个Id,如果该id节点没有类型,则说明该id节点不是在声明语句中的 126 | // 遇见这种情况,说明我们需要检查该节点之前出现过没有声明语句 127 | // 即在符号表中查看该节点是否已经addtype 128 | if( this->type == Void) 129 | { 130 | ExpType tmp_type = m_list.getType(this->attr.name); 131 | if( tmp_type == Void) 132 | { 133 | typecheckError(this,string(this->attr.name) + " didn't declare"); 134 | } 135 | else 136 | { 137 | this->type = tmp_type; 138 | } 139 | } 140 | else 141 | { 142 | // 当该节点有类型,则为其在符号表中添加类型 143 | // 当没有成功时,说明之前已经有类型被赋予了 144 | if(!m_list.addType(this->attr.name,this->type)) 145 | { 146 | typecheckError(this,string(this->attr.name) + " duplicate declare"); 147 | } 148 | } 149 | break; 150 | } 151 | } 152 | 153 | void TreeNode::typecheckOp() 154 | { 155 | switch(this->attr.op) 156 | { 157 | case ADD: 158 | case SUB: 159 | case MUL: 160 | case DIV: 161 | case MOD: 162 | case USUB: 163 | case INC: 164 | case DEC: 165 | case M_LEFT: 166 | case M_RIGHT: 167 | case B_AND: 168 | case B_EOR: 169 | case B_IOR: 170 | case B_OPP: 171 | this->type = Integer; 172 | if(this->children[0]->type == Integer && 173 | (this->children[1] == NULL || this->children[1]->type == Integer)) 174 | { 175 | this->Calculate(); 176 | } 177 | else 178 | { 179 | typecheckError(this,"expr op error"); 180 | } 181 | break; 182 | case EQ: 183 | case GRT: 184 | case LET: 185 | case GRE: 186 | case LEE: 187 | case NE: 188 | this->type = Boolean; 189 | if(this->children[0]->type == Integer && 190 | (this->children[1] == NULL || this->children[1]->type == Integer)) 191 | { 192 | this->Calculate(); 193 | } 194 | else 195 | { 196 | typecheckError(this,"expr op error"); 197 | } 198 | break; 199 | case AND: 200 | case OR: 201 | case NOT: 202 | this->type = Boolean; 203 | if(this->children[0]->type == Boolean && 204 | (this->children[1] == NULL || this->children[1]->type == Boolean)) 205 | { 206 | this->Calculate(); 207 | } 208 | else 209 | { 210 | typecheckError(this,"expr op error"); 211 | } 212 | break; 213 | } 214 | } 215 | 216 | //调用这个函数之前已经经过了类型检查 217 | void TreeNode::Calculate() 218 | { 219 | // 检查是否所有节点都可以进行计算 220 | for(int i=0 ; ichildren[i] != NULL && this->children[i]->kind.expr != ConstK) 224 | { 225 | return; 226 | } 227 | } 228 | 229 | // 经过上面的检查,发现所有节点都是固定值节点,那么该节点也必然可以计算为固定值 230 | this->kind.expr = ConstK; 231 | switch(this->attr.op) 232 | { 233 | case ADD: 234 | this->attr.val = this->children[0]->attr.val + this->children[1]->attr.val; 235 | break; 236 | case SUB: 237 | this->attr.val = this->children[0]->attr.val - this->children[1]->attr.val; 238 | break; 239 | case MUL: 240 | this->attr.val = this->children[0]->attr.val * this->children[1]->attr.val; 241 | break; 242 | case DIV: 243 | this->attr.val = this->children[0]->attr.val / this->children[1]->attr.val; 244 | break; 245 | case MOD: 246 | this->attr.val = this->children[0]->attr.val % this->children[1]->attr.val; 247 | break; 248 | case USUB: 249 | this->attr.val = -(this->children[0]->attr.val); 250 | break; 251 | case INC: 252 | this->attr.val = this->children[0]->attr.val + 1; 253 | break; 254 | case DEC: 255 | this->attr.val = this->children[0]->attr.val - 1; 256 | break; 257 | case M_LEFT: 258 | this->attr.val = this->children[0]->attr.val << this->children[1]->attr.val; 259 | break; 260 | case M_RIGHT: 261 | this->attr.val = this->children[0]->attr.val >> this->children[1]->attr.val; 262 | break; 263 | case EQ: 264 | this->attr.val = this->children[0]->attr.val == this->children[1]->attr.val; 265 | break; 266 | case GRT: 267 | this->attr.val = this->children[0]->attr.val > this->children[1]->attr.val; 268 | break; 269 | case LET: 270 | this->attr.val = this->children[0]->attr.val < this->children[1]->attr.val; 271 | break; 272 | case GRE: 273 | this->attr.val = this->children[0]->attr.val >= this->children[1]->attr.val; 274 | break; 275 | case LEE: 276 | this->attr.val = this->children[0]->attr.val <= this->children[1]->attr.val; 277 | break; 278 | case NE: 279 | this->attr.val = this->children[0]->attr.val != this->children[1]->attr.val; 280 | break; 281 | case AND: 282 | this->attr.val = this->children[0]->attr.val && this->children[1]->attr.val; 283 | break; 284 | case OR: 285 | this->attr.val = this->children[0]->attr.val || this->children[1]->attr.val; 286 | break; 287 | case NOT: 288 | this->attr.val = !this->children[0]->attr.val; 289 | break; 290 | case B_AND: 291 | this->attr.val = this->children[0]->attr.val & this->children[1]->attr.val; 292 | break; 293 | case B_EOR: 294 | this->attr.val = this->children[0]->attr.val ^ this->children[1]->attr.val; 295 | break; 296 | case B_IOR: 297 | this->attr.val = this->children[0]->attr.val | this->children[1]->attr.val; 298 | break; 299 | case B_OPP: 300 | this->attr.val = ~this->children[0]->attr.val; 301 | break; 302 | } 303 | for(int i=0;ichildren[i] = NULL; 306 | } 307 | cout<<"calculate : "<<" type: "; 308 | if( this->type == Boolean) 309 | { 310 | cout<<"Boolean"; 311 | } 312 | else if(this->type == Integer) 313 | { 314 | cout<<"Integer"; 315 | } 316 | cout<<"value: "<attr.val<second; 8 | } 9 | Var* res = new Var(); 10 | res->name = name; 11 | res->type = Void; 12 | this->table[name] = res; 13 | return res; 14 | } 15 | 16 | Var* VarList::GetID(char* c_name) 17 | { 18 | string name = c_name; 19 | if(table.find(name) != table.end()) 20 | { 21 | return table.find(name)->second; 22 | } 23 | Var* res = new Var(); 24 | res->name = name; 25 | res->type = Void; 26 | this->table[name] = res; 27 | return res; 28 | } 29 | 30 | bool VarList::IsPresence(char* c_name) 31 | { 32 | string name = c_name; 33 | if(table.find(name) != table.end()) 34 | { 35 | return true; 36 | } 37 | else 38 | { 39 | return false; 40 | } 41 | } 42 | 43 | bool VarList::addType(char* c_name, ExpType type) 44 | { 45 | string name = c_name; 46 | Var* tmp; 47 | if(table.find(name) != table.end()) 48 | { 49 | tmp = table.find(name)->second; 50 | if(tmp->type == Void) 51 | { 52 | tmp->type = type; 53 | return true; 54 | } 55 | } 56 | return false; 57 | } 58 | 59 | ExpType VarList::getType(char* c_name) 60 | { 61 | string name = c_name; 62 | Var* tmp; 63 | if(table.find(name) != table.end()) 64 | { 65 | tmp = table.find(name)->second; 66 | return tmp->type; 67 | } 68 | return Void; 69 | } -------------------------------------------------------------------------------- /Assignment 4/VarList.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | typedef enum{n_main,braced_stmt,stmts,stmt,decl_stmt,type,id,assign_stmt,expr,factor,n_num,letter,while_stmt,if_stmt,for_stmt} Nonterminal; 8 | // 当节点类型为type时,才用到TypeK 9 | typedef enum {StmtK,ExpK,TypeK,EMPTY} NodeKind; 10 | typedef enum {IfK,WhileK,ForK,AssignK,InputK,OutputK,DeclK,StmtsK} StmtKind; 11 | typedef enum {OpK,ConstK,IdK} ExpKind; 12 | typedef enum {Void,Integer,Char,Boolean} ExpType; 13 | 14 | class Var 15 | { 16 | public: 17 | string name; 18 | int num; //某类型的第几个变量 19 | ExpType type; //变量类型 20 | Var(){type = Void;} 21 | }; 22 | 23 | //定义符号表 24 | typedef map VarTable; 25 | 26 | class VarList 27 | { 28 | public: 29 | VarTable table; 30 | 31 | VarList(){} 32 | Var* GetID(string name); 33 | Var* GetID(char* c_name); 34 | bool IsPresence(char* c_name); 35 | bool addType(char* c_name, ExpType type); 36 | ExpType getType(char* c_name); 37 | }; -------------------------------------------------------------------------------- /Assignment 4/mylexer.l: -------------------------------------------------------------------------------- 1 | %{ 2 | /**************************************************************************** 3 | mylexer.l 4 | ParserWizard generated Lex file. 5 | 6 | Date: 2018��11��1�� 7 | ****************************************************************************/ 8 | 9 | #define _CRT_SECURE_NO_WARNINGS 10 | #pragma warning(disable:4996) 11 | #include "myparser.h" 12 | #include 13 | 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | string token_text = "Hello"; 19 | 20 | int num_lines = 0; 21 | %} 22 | 23 | ///////////////////////////////////////////////////////////////////////////// 24 | // declarations section 25 | 26 | //语句类型 27 | 28 | MAIN "main" 29 | INT "int" 30 | CHAR "char" 31 | IF "if" 32 | ELSE "else" 33 | WHILE "while" 34 | FOR "for" 35 | 36 | INPUT "input" 37 | OUTPUT "output" 38 | 39 | STR \"[^\"]*\" 40 | LETTER '[^'\n]' 41 | ANNOTATION (\/\*(".*"|[^\*"]|(\*)+[^\/])*(\*)*\*\/)|(\/\/[^\n]*) 42 | ANNOTATIONO \/\/.*\n 43 | 44 | ID [_a-zA-Z][_a-zA-Z0-9]* 45 | 46 | NUMBER [0-9]+ 47 | 48 | LBRACE "{" 49 | RBRACE "}" 50 | LPAREN "(" 51 | RPAREN ")" 52 | LBRACKET "[" 53 | RBRACKET "]" 54 | 55 | //运算符 56 | 57 | //基本: 58 | 59 | 60 | ASSIGN "=" 61 | ADD "+" 62 | SUB "-" 63 | MUL "*" 64 | DIV "/" 65 | MOD "%" 66 | INC "++" 67 | DEC "--" 68 | //进阶: 69 | 70 | 71 | B_AND "&" 72 | B_IOR "|" 73 | B_EOR "^" 74 | B_OPP "~" 75 | M_LEFT "<<" 76 | M_RIGHT ">>" 77 | 78 | EQ "==" 79 | GRT ">" 80 | LET "<" 81 | GRE ">=" 82 | LEE "<=" 83 | NE "!=" 84 | 85 | AND "&&" 86 | OR "||" 87 | NOT "!" 88 | 89 | COMMA "," 90 | SEMICOLON ";" 91 | 92 | 93 | 94 | // lexical analyser name 95 | %name mylexer 96 | 97 | // class definition 98 | { 99 | // place any extra class members here 100 | public: 101 | static int getToken(); 102 | } 103 | 104 | // constructor 105 | { 106 | // place any extra initialisation code here 107 | } 108 | 109 | // destructor 110 | { 111 | // place any extra cleanup code here 112 | } 113 | 114 | // place any declarations here 115 | 116 | %% 117 | 118 | ///////////////////////////////////////////////////////////////////////////// 119 | // rules section 120 | 121 | 122 | 123 | // place your Lex rules here 124 | " " {} 125 | "\n" {++num_lines;} 126 | "\t" {} 127 | "" {} 128 | {MAIN} {return MAIN;} 129 | {INT} {return INT;} 130 | {CHAR} {return CHAR;} 131 | {IF} {return IF;} 132 | {ELSE} {return ELSE;} 133 | {WHILE} {return WHILE;} 134 | {FOR} {return FOR;} 135 | 136 | {INPUT} {return INPUT;} 137 | {OUTPUT} {return OUTPUT;} 138 | 139 | {STR} {return STR;} 140 | {LETTER} {token_text = yytext;return LETTER;} 141 | {ANNOTATION} {} 142 | {ANNOTATIONO} {} 143 | 144 | {ID} {token_text = yytext;return ID;} 145 | 146 | {NUMBER} {token_text = yytext;return NUMBER;} 147 | 148 | {LBRACE} {return LBRACE;} 149 | {RBRACE} {return RBRACE;} 150 | {LPAREN} {return LPAREN;} 151 | {RPAREN} {return RPAREN;} 152 | {LBRACKET} {return LBRACKET;} 153 | {RBRACKET} {return RBRACKET;} 154 | 155 | {ASSIGN} {return ASSIGN;} 156 | {ADD} {return ADD;} 157 | {SUB} {return SUB;} 158 | {MUL} {return MUL;} 159 | {DIV} {return DIV;} 160 | {MOD} {return MOD;} 161 | {INC} {return INC;} 162 | {DEC} {return DEC;} 163 | 164 | {B_AND} {return B_AND;} 165 | {B_IOR} {return B_IOR;} 166 | {B_EOR} {return B_EOR;} 167 | {B_OPP} {return B_OPP;} 168 | {M_LEFT} {return M_LEFT;} 169 | {M_RIGHT} {return M_RIGHT;} 170 | 171 | {EQ} {return EQ;} 172 | {GRT} {return GRT;} 173 | {LET} {return LET;} 174 | {GRE} {return GRE;} 175 | {LEE} {return LEE;} 176 | {NE} {return NE;} 177 | 178 | {AND} {return AND;} 179 | {OR} {return OR;} 180 | {NOT} {return NOT;} 181 | 182 | {COMMA} {return COMMA;} 183 | {SEMICOLON} {return SEMICOLON;} 184 | 185 | 186 | %% 187 | 188 | ///////////////////////////////////////////////////////////////////////////// 189 | // programs section 190 | -------------------------------------------------------------------------------- /Assignment 4/myparser.y: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chendante/Principles-of-Compilers-Assignments/eb2e33c0bf122ab93ee2a576306cf236c544fa44/Assignment 4/myparser.y -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Principles-of-Compilers-Assignments 2 | 南开大学计算机学院“编译系统原理”课程作业。 3 | 4 | ## 作业信息: 5 | 最终实现利用yacc和lex工具,完成一个简单的c编译器。 6 | 7 | ## 环境调试: 8 | 参看[小白说编译原理-6-lex和yacc环境配置-多图](https://blog.csdn.net/lpstudy/article/details/51330063) 9 | --------------------------------------------------------------------------------