├── README.md ├── 《编译系统》习题答案-学长版.pptx └── 编译系统实验 ├── 实验一 ├── 代码 │ ├── 1.h │ ├── 1.l │ ├── 1.tab.c │ ├── 1.tab.h │ ├── 1.y │ ├── parser │ └── tree.c ├── 编译原理实验指导书-词法分析与语法分析.pdf ├── 编译系统实验一-1190201215-冯开来.docx └── 编译系统实验一-1190201215-冯开来.pdf ├── 实验三 ├── codes │ ├── intercodegenerate.h │ ├── lex.yy.c │ ├── lexical.l │ ├── main.c │ ├── makefile │ ├── multitree.h │ ├── out1 │ ├── out2 │ ├── parser │ ├── scanner │ ├── semantic.h │ ├── symboltable.h │ ├── syntax.tab.c │ ├── syntax.tab.h │ ├── syntax.y │ ├── test1.cmm │ └── test2.cmm ├── 编译原理实验指导书-中间代码生成.pdf ├── 编译系统实验三-1190201215-冯开来.docx └── 编译系统实验三-1190201215-冯开来.pdf └── 实验二 ├── lab2 ├── Code │ ├── Makefile │ ├── enum.h │ ├── input1.cmm │ ├── input10.cmm │ ├── input11.cmm │ ├── input12.cmm │ ├── input13.cmm │ ├── input14.cmm │ ├── input15.cmm │ ├── input16.cmm │ ├── input17.cmm │ ├── input2.cmm │ ├── input3.cmm │ ├── input4.cmm │ ├── input5.cmm │ ├── input6.cmm │ ├── input7.cmm │ ├── input8.cmm │ ├── input9.cmm │ ├── lex.yy.c │ ├── lexical.l │ ├── main.c │ ├── main.o │ ├── node.h │ ├── parser │ ├── scanner │ ├── semantic.c │ ├── semantic.h │ ├── semantic.o │ ├── syntax.tab.o │ ├── syntax.y │ └── test.cmm ├── Makefile ├── Tree.c ├── Tree.h ├── lexical.l ├── main.c ├── parser ├── semantic.c ├── semantic.h ├── syntax.output ├── syntax.tab.c ├── syntax.tab.h └── syntax.y ├── 编译原理实验指导书-语义分析(标注版).pdf ├── 编译系统实验二-1190201215-冯开来.docx └── 编译系统实验二-1190201215-冯开来.pdf /README.md: -------------------------------------------------------------------------------- 1 | # HIT2022CompilerPrinciple 2 | 哈工大2022春编译原理 3 | 4 | 仅供学习参考,不建议直接套用搬运 5 | 6 | 各位学弟学妹clone后star下啦~ 7 | 8 | 有问题欢迎联系carlo_fkl@163.com 9 | -------------------------------------------------------------------------------- /《编译系统》习题答案-学长版.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/《编译系统》习题答案-学长版.pptx -------------------------------------------------------------------------------- /编译系统实验/实验一/代码/1.h: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | # include 5 | # define maxNum 10 6 | extern int yylineno; 7 | extern char* yytext; 8 | extern int yylex(void); 9 | 10 | struct TREE{ 11 | int line; 12 | char* name; 13 | int n; // num of children 14 | struct TREE *child[maxNum]; 15 | union{ 16 | char* ID; 17 | int INT; 18 | float FLOAT; 19 | }; 20 | }; 21 | typedef struct TREE *treeNode; 22 | 23 | int hasFault; 24 | int nodeNum; 25 | treeNode nodeList[1000]; 26 | int nodeIsChild[1000]; 27 | 28 | int stoi(char * p); 29 | 30 | void setChildTag (treeNode node); 31 | 32 | treeNode newNode(char *name, int num, int yyline, treeNode childList[]); 33 | 34 | void preOrder(treeNode t, int level); 35 | 36 | void printTree(); 37 | 38 | void yyerror(char *msg); 39 | -------------------------------------------------------------------------------- /编译系统实验/实验一/代码/1.l: -------------------------------------------------------------------------------- 1 | %{ 2 | # include 3 | # include 4 | # include "1.h" 5 | # include "1.tab.h" 6 | %} 7 | 8 | %option yylineno 9 | 10 | /*整型*/ 11 | INT8 [+-]?0[1-7][0-7]* 12 | INT10 [+-]?(0|[1-9][0-9]*) 13 | INT16 [+-]?0[Xx][0-9a-fA-F]+ 14 | INT {INT8}|{INT10}|{INT16} 15 | /*浮点型*/ 16 | FLOAT ({INT10}\.[0-9]+)|([+-]?([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)[Ee][+-]?[0-9]+) 17 | /*数字*/ 18 | NUMBER {INT}|{FLOAT} 19 | /*标识符*/ 20 | ID [a-zA-Z_][a-zA-Z0-9_]* 21 | /*标点*/ 22 | SEMI ; 23 | COMMA , 24 | ASSIGNOP = 25 | ADD \+ 26 | SUB \- 27 | MUL \* 28 | DIV \/ 29 | AND && 30 | OR \|\| 31 | DOT \. 32 | NOT \! 33 | LP \( 34 | RP \) 35 | LB \[ 36 | RB \] 37 | LC \{ 38 | RC \} 39 | RELOP >|<|>=|<=|==|!= 40 | /*关键字*/ 41 | STRUCT struct 42 | RETURN return 43 | IF if 44 | ELSE else 45 | WHILE while 46 | TYPE int|float 47 | /*注释*/ 48 | LINE_COMMENT ("//"[.]*) 49 | BLOCK_COMMENT "/\*"[.]*"\*/" 50 | COMMENT {LINE_COMMENT}|{BLOCK_COMMENT} 51 | /*空格*/ 52 | SPACE [ \n\r\t]+ 53 | /*错误*/ 54 | AERROR . 55 | INT8_ERROR 0[0-7]*[8-9]+[0-7]* 56 | INT16_ERROR 0[Xx][a-fA-F0-9]*[g-zG-Z]+[a-fA-F0-9]* 57 | FLOAT_ERROR [0]+(0|[1-9][0-9]*)\.[0-9]+|\.[0-9]+|[0-9]+\.|[0-9]+[Ee][+-]?[0-9]*|([+-]?([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)[Ee][+-]?[0-9]+\.[0-9]+) 58 | NUM_ERROR {INT8_ERROR}|{INT16_ERROR}|{FLOAT_ERROR} 59 | ID_ERROR [0-9]+[a-zA-Z_]+ 60 | 61 | %% 62 | {SPACE} {} 63 | /* {COMMENT} {yylval.type_node = newNode("COMMENT", 0, yylineno, NULL); return COMMENT;} */ 64 | "//" { char c = input(); while (c != '\n') c = input(); } 65 | "/*" { 66 | int q = 0; 67 | char c; 68 | while(1){ 69 | if(q == 0){ 70 | c = input(); 71 | if(c == EOF) 72 | break; 73 | if(c == '*') 74 | q = 1; 75 | }else{ 76 | c = input(); 77 | if(c == EOF) 78 | break; 79 | if(c == '/') 80 | break; 81 | else if(c != '*') 82 | q = 0; 83 | } 84 | } 85 | } 86 | 87 | {TYPE} { yylval.type_node = newNode("TYPE", 0, yylineno, NULL); return TYPE;} 88 | {STRUCT} {yylval.type_node = newNode("STRUCT", 0, yylineno, NULL); return STRUCT;} 89 | {RETURN} {yylval.type_node = newNode("RETURN", 0, yylineno, NULL); return RETURN;} 90 | {IF} {yylval.type_node = newNode("IF", 0, yylineno, NULL); return IF;} 91 | {ELSE} {yylval.type_node = newNode("ELSE", 0, yylineno, NULL); return ELSE;} 92 | {WHILE} {yylval.type_node = newNode("WHILE", 0, yylineno, NULL); return WHILE;} 93 | 94 | {INT} {yylval.type_node = newNode("INT", 0, yylineno, NULL); return INT;} 95 | {FLOAT} {yylval.type_node = newNode("FLOAT", 0, yylineno, NULL); return FLOAT;} 96 | 97 | {SEMI} {yylval.type_node = newNode("SEMI", 0, yylineno, NULL); return SEMI;} 98 | {COMMA} {yylval.type_node = newNode("COMMA", 0, yylineno, NULL); return COMMA;} 99 | {ASSIGNOP} {yylval.type_node = newNode("ASSIGNOP", 0, yylineno, NULL); return ASSIGNOP;} 100 | {ADD} {yylval.type_node = newNode("ADD", 0, yylineno, NULL); return ADD;} 101 | {SUB} {yylval.type_node = newNode("SUB", 0, yylineno, NULL); return SUB;} 102 | {MUL} {yylval.type_node = newNode("MUL", 0, yylineno, NULL); return MUL;} 103 | {DIV} {yylval.type_node = newNode("DIV", 0, yylineno, NULL); return DIV;} 104 | {AND} {yylval.type_node = newNode("AND", 0, yylineno, NULL); return AND;} 105 | {DOT} {yylval.type_node = newNode("DOT", 0, yylineno, NULL); return DOT;} 106 | {NOT} {yylval.type_node = newNode("NOT", 0, yylineno, NULL); return NOT;} 107 | {LP} {yylval.type_node = newNode("LP", 0, yylineno, NULL); return LP;} 108 | {RP} {yylval.type_node = newNode("RP", 0, yylineno, NULL); return RP;} 109 | {LB} {yylval.type_node = newNode("LB", 0, yylineno, NULL); return LB;} 110 | {RB} {yylval.type_node = newNode("RB", 0, yylineno, NULL); return RB;} 111 | {LC} {yylval.type_node = newNode("LC", 0, yylineno, NULL); return LC;} 112 | {RC} {yylval.type_node = newNode("RC", 0, yylineno, NULL); return RC;} 113 | {RELOP} {yylval.type_node = newNode("RELOP", 0, yylineno, NULL); return RELOP;} 114 | 115 | {ID} {yylval.type_node = newNode("ID", 0, yylineno, NULL); return ID;} 116 | 117 | {AERROR} { 118 | hasFault = 1; 119 | printf("error type A at line %d: mysterious character '%s'\n", yylineno, yytext); 120 | } 121 | %% 122 | -------------------------------------------------------------------------------- /编译系统实验/实验一/代码/1.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 3.5.1. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2020 Free Software Foundation, 6 | Inc. 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . */ 20 | 21 | /* As a special exception, you may create a larger work that contains 22 | part or all of the Bison parser skeleton and distribute that work 23 | under terms of your choice, so long as that work isn't itself a 24 | parser generator using the skeleton or a modified version thereof 25 | as a parser skeleton. Alternatively, if you modify or redistribute 26 | the parser skeleton itself, you may (at your option) remove this 27 | special exception, which will cause the skeleton and the resulting 28 | Bison output files to be licensed under the GNU General Public 29 | License without this special exception. 30 | 31 | This special exception was added by the Free Software Foundation in 32 | version 2.2 of Bison. */ 33 | 34 | /* Undocumented macros, especially those whose name start with YY_, 35 | are private implementation details. Do not rely on them. */ 36 | 37 | #ifndef YY_YY_1_TAB_H_INCLUDED 38 | # define YY_YY_1_TAB_H_INCLUDED 39 | /* Debug traces. */ 40 | #ifndef YYDEBUG 41 | # define YYDEBUG 0 42 | #endif 43 | #if YYDEBUG 44 | extern int yydebug; 45 | #endif 46 | 47 | /* Token type. */ 48 | #ifndef YYTOKENTYPE 49 | # define YYTOKENTYPE 50 | enum yytokentype 51 | { 52 | INT = 258, 53 | FLOAT = 259, 54 | ID = 260, 55 | SEMI = 261, 56 | COMMA = 262, 57 | ASSIGNOP = 263, 58 | RELOP = 264, 59 | ADD = 265, 60 | SUB = 266, 61 | MUL = 267, 62 | DIV = 268, 63 | AND = 269, 64 | OR = 270, 65 | DOT = 271, 66 | NOT = 272, 67 | TYPE = 273, 68 | LP = 274, 69 | RP = 275, 70 | LB = 276, 71 | RB = 277, 72 | LC = 278, 73 | RC = 279, 74 | STRUCT = 280, 75 | RETURN = 281, 76 | IF = 282, 77 | ELSE = 283, 78 | WHILE = 284, 79 | COMMENT = 285, 80 | SPACE = 286, 81 | AERROR = 287, 82 | NUM_ERROR = 288, 83 | ID_ERROR = 289 84 | }; 85 | #endif 86 | 87 | /* Value type. */ 88 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 89 | union YYSTYPE 90 | { 91 | #line 7 "1.y" 92 | 93 | treeNode type_node; 94 | 95 | #line 96 "1.tab.h" 96 | 97 | }; 98 | typedef union YYSTYPE YYSTYPE; 99 | # define YYSTYPE_IS_TRIVIAL 1 100 | # define YYSTYPE_IS_DECLARED 1 101 | #endif 102 | 103 | 104 | extern YYSTYPE yylval; 105 | 106 | int yyparse (void); 107 | 108 | #endif /* !YY_YY_1_TAB_H_INCLUDED */ 109 | -------------------------------------------------------------------------------- /编译系统实验/实验一/代码/1.y: -------------------------------------------------------------------------------- 1 | %{ 2 | # include 3 | # include 4 | # include "1.h" 5 | %} 6 | 7 | %union{ 8 | treeNode type_node; 9 | } 10 | 11 | /*终结符*/ 12 | %token INT FLOAT ID 13 | %token SEMI COMMA ASSIGNOP RELOP ADD SUB MUL DIV 14 | %token AND OR DOT NOT TYPE LP RP LB RB LC RC 15 | %token STRUCT RETURN IF ELSE WHILE 16 | %token COMMENT SPACE AERROR NUM_ERROR ID_ERROR 17 | 18 | /*非终结符*/ 19 | %type Program ExtDefList ExtDef ExtDecList 20 | %type Specifier StructSpecifier OptTag Tag 21 | %type VarDec FunDec VarList ParamDec CompSt 22 | %type StmtList Stmt DefList Def DecList Dec Exp Args 23 | 24 | /*优先级*/ 25 | %right ASSIGNOP 26 | %left OR 27 | %left AND 28 | %left RELOP 29 | %left ADD SUB 30 | %left MUL DIV 31 | %right NOT 32 | %left LP RP RB LB DOT 33 | %nonassoc ELSE 34 | 35 | 36 | 37 | %% 38 | 39 | /*High-level Definitions*/ 40 | Program : ExtDefList {treeNode childList[1] = {$1}; $$= newNode("Program", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++; printTree();} 41 | ; 42 | ExtDefList : ExtDef ExtDefList {treeNode childList[2] = {$1, $2}; $$= newNode("ExtDefList", 2, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 43 | | {$$= newNode("ExtDefList", 0, -1, NULL);nodeList[nodeNum]=$$;nodeNum++;} 44 | ; 45 | ExtDef : Specifier ExtDecList SEMI {treeNode childList[3] = {$1, $2, $3}; $$= newNode("ExtDef", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 46 | | Specifier SEMI {treeNode childList[2] = {$1, $2}; $$= newNode("ExtDefList", 2, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 47 | | Specifier FunDec CompSt {treeNode childList[3] = {$1, $2, $3}; $$= newNode("ExtDef", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 48 | | error SEMI { } 49 | ; 50 | ExtDecList : VarDec {treeNode childList[1] = {$1}; $$= newNode("ExtDecList", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 51 | | VarDec COMMA ExtDecList {treeNode childList[3] = {$1, $2, $3}; $$= newNode("ExtDecList", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 52 | ; 53 | 54 | /*Specifiers*/ 55 | Specifier : TYPE {treeNode childList[1] = {$1}; $$= newNode("Specifier", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 56 | | StructSpecifier {treeNode childList[1] = {$1}; $$= newNode("Specifier", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 57 | ; 58 | StructSpecifier : STRUCT OptTag LC DefList RC {treeNode childList[5] = {$1, $2, $3, $4, $5}; $$= newNode("StructSpecifier", 5, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 59 | | STRUCT Tag {treeNode childList[2] = {$1, $2}; $$= newNode("StructSpecifier", 2, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 60 | ; 61 | OptTag : ID {treeNode childList[1] = {$1}; $$= newNode("OptTag", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 62 | | {$$= newNode("OptTag", 0, -1, NULL);nodeList[nodeNum]=$$;nodeNum++;} 63 | ; 64 | Tag : ID {treeNode childList[1] = {$1}; $$= newNode("Tag", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 65 | ; 66 | 67 | /*Declarators*/ 68 | VarDec : ID {treeNode childList[1] = {$1}; $$= newNode("VarDec", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 69 | | VarDec LB INT RB {treeNode childList[4] = {$1, $2, $3, $4}; $$= newNode("VarDec", 4, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 70 | ; 71 | FunDec : ID LP VarList RP {treeNode childList[4] = {$1, $2, $3, $4}; $$= newNode("FunDec", 4, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 72 | | ID LP RP {treeNode childList[3] = {$1, $2, $3}; $$= newNode("FunDec", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 73 | ; 74 | VarList : ParamDec COMMA VarList {treeNode childList[3] = {$1, $2, $3}; $$= newNode("VarList", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 75 | | ParamDec {treeNode childList[1] = {$1}; $$= newNode("VarList", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 76 | ; 77 | ParamDec : Specifier VarDec {treeNode childList[2] = {$1, $2}; $$= newNode("ParamDec", 2, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 78 | ; 79 | 80 | /* Statements*/ 81 | CompSt : LC DefList StmtList RC {treeNode childList[4] = {$1, $2, $3, $4}; $$= newNode("CompSt", 4, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 82 | //| LC DefList RC {treeNode childList[3] = {$1, $2, $3}; $$= newNode("CompSt", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 83 | ; 84 | StmtList : Stmt StmtList {treeNode childList[2] = {$1, $2}; $$= newNode("StmtList", 2, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 85 | | {$$= newNode("StmtList", 0, -1, NULL);nodeList[nodeNum]=$$;nodeNum++;} 86 | ; 87 | Stmt : Exp SEMI {treeNode childList[2] = {$1, $2}; $$= newNode("Stmt", 2, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 88 | | CompSt {treeNode childList[1] = {$1}; $$= newNode("Stmt", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 89 | | RETURN Exp SEMI {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Stmt", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 90 | | IF LP Exp RP Stmt {treeNode childList[5] = {$1, $2, $3, $4, $5}; $$= newNode("Stmt", 5, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 91 | | IF LP Exp RP Stmt ELSE Stmt {treeNode childList[7] = {$1, $2, $3, $4, $5, $6, $7}; $$= newNode("Stmt", 7, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 92 | | WHILE LP Exp RP Stmt {treeNode childList[5] = {$1, $2, $3, $4, $5}; $$= newNode("Stmt", 5, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 93 | | error SEMI { } 94 | ; 95 | 96 | /* Local Definitions*/ 97 | DefList : Def DefList {treeNode childList[2] = {$1, $2}; $$= newNode("DefList", 2, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 98 | | {$$= newNode("DefList", 0, -1, NULL);nodeList[nodeNum]=$$;nodeNum++;} 99 | ; 100 | Def : Specifier DecList SEMI {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Def", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 101 | ; 102 | DecList : Dec {treeNode childList[1] = {$1}; $$= newNode("DecList", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 103 | | Dec COMMA DecList {treeNode childList[3] = {$1, $2, $3}; $$= newNode("DecList", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 104 | ; 105 | Dec : VarDec {treeNode childList[1] = {$1}; $$= newNode("Dec", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 106 | | VarDec ASSIGNOP Exp {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Dec", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 107 | ; 108 | 109 | /*Expressions*/ 110 | Exp : Exp ASSIGNOP Exp {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 111 | | Exp AND Exp {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 112 | | Exp OR Exp {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 113 | | Exp RELOP Exp {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 114 | | Exp ADD Exp {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 115 | | Exp SUB Exp {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 116 | | Exp MUL Exp {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 117 | | Exp DIV Exp {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 118 | | LP Exp RP {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 119 | | SUB Exp {treeNode childList[2] = {$1, $2}; $$= newNode("Exp", 2, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 120 | | NOT Exp {treeNode childList[2] = {$1, $2}; $$= newNode("Exp", 2, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 121 | | ID LP Args RP {treeNode childList[4] = {$1, $2, $3, $4}; $$= newNode("Exp", 4, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 122 | | ID LP RP {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 123 | | Exp LB Exp RB {treeNode childList[4] = {$1, $2, $3, $4}; $$= newNode("Exp", 4, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 124 | | Exp DOT ID {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Exp", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 125 | | ID {treeNode childList[1] = {$1}; $$= newNode("Exp", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 126 | | INT {treeNode childList[1] = {$1}; $$= newNode("Exp", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 127 | | FLOAT {treeNode childList[1] = {$1}; $$= newNode("Exp", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 128 | ; 129 | Args : Exp COMMA Args {treeNode childList[3] = {$1, $2, $3}; $$= newNode("Args", 3, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 130 | | Exp {treeNode childList[1] = {$1}; $$= newNode("Args", 1, yylineno, childList);nodeList[nodeNum]=$$;nodeNum++;} 131 | ; 132 | 133 | /*ERRORS*/ 134 | /*Exp : Exp error LB Exp RB {} 135 | ; 136 | */ 137 | 138 | %% 139 | -------------------------------------------------------------------------------- /编译系统实验/实验一/代码/parser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验一/代码/parser -------------------------------------------------------------------------------- /编译系统实验/实验一/代码/tree.c: -------------------------------------------------------------------------------- 1 | # include "1.h" 2 | extern int yylineno; 3 | extern int yyparse(); 4 | extern void yyrestart(FILE *); 5 | 6 | // 进制转换 7 | int stoi(char * p){ 8 | int num = 0; 9 | // 16进制 10 | if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')){ 11 | p += 2; 12 | while (*p != '\0'){ 13 | if (*p >= '0' && *p <= '9') 14 | num = num * 16 + (*p -'0'); 15 | else 16 | num = num * 16 + (tolower(*p) - 'a' + 10); 17 | p ++; 18 | } 19 | } 20 | // 10进制 21 | else if (p[0] != '0' || (p[0] == '0' && p[1] == '\0')){ 22 | num = atoi(p); 23 | } 24 | // 8进制 25 | else { 26 | p += 1; 27 | while (*p != '\0') { 28 | num = num * 8 + (*p - '0'); 29 | p++; 30 | } 31 | } 32 | return num; 33 | } 34 | 35 | void setChildTag (treeNode node){ 36 | for (int i=0; i name = name; 54 | father -> n = num; 55 | // 有子节点,不是终结符 56 | if (num > 0){ 57 | temp = childList[0]; 58 | setChildTag(temp); 59 | father -> child[0] = temp; 60 | father -> line = temp -> line; 61 | for (int i=1; i child[i] = childList[i]; 65 | } 66 | } 67 | // 终结符、空的语法单元,空单元行号为-1 68 | else{ 69 | father -> line = yyline; 70 | //printf("the child is %d\n", father->n); 71 | if ((!strcmp(name, "ID"))) { 72 | //printf("u have enter %s into it\n", yytext); 73 | char *str; 74 | str = (char *)malloc(sizeof(char) * 40); 75 | strcpy(str, yytext); 76 | father->ID = str; 77 | } 78 | else if ((!strcmp(name, "TYPE"))) { 79 | //printf("u have enter %s into it\n", yytext); 80 | char *str; 81 | str = (char *)malloc(sizeof(char) * 40); 82 | strcpy(str, yytext); 83 | father->ID = str; 84 | } 85 | else if (!strcmp(name, "INT")){ 86 | father -> INT = stoi(yytext); 87 | //printf("%d\n", father -> INT); 88 | } 89 | else if (!strcmp(name, "FLOAT")){ 90 | father -> FLOAT = atof(yytext); 91 | } 92 | } 93 | return father; 94 | } 95 | 96 | void preOrder(treeNode t, int level){ 97 | if (t != NULL && t -> line != -1){ 98 | // 缩进 99 | for (int i=0; iname); 102 | if (!strcmp(t->name, "ID")){ 103 | printf(": %s", t->ID); 104 | } 105 | else if (!strcmp(t->name, "TYPE")) 106 | printf(": %s", t->ID); 107 | else if (!strcmp(t->name, "INT")){ 108 | printf(": %d", t->INT); 109 | } 110 | else if (!strcmp(t->name, "FLOAT")) 111 | printf(": %f", t->FLOAT); 112 | // 非叶节点,打印行号 113 | else if (t->n != 0) 114 | printf("(%d)", t->line); 115 | printf("\n"); 116 | for (int i=0; in; i++){ 117 | preOrder(t->child[i], level+1); 118 | } 119 | } 120 | } 121 | 122 | void yyerror(char *msg){ 123 | hasFault = 1; 124 | fprintf(stderr, "Error type B at Line %d: %s.\n", yylineno, msg); 125 | } 126 | 127 | void printTree(){ 128 | if (!hasFault) 129 | for (int j=0; j 4 | #include 5 | #include "syntax.tab.h" 6 | int lex_error = 0; 7 | %} 8 | 9 | digit [0-9] 10 | letter [_a-zA-Z] 11 | %option yylineno 12 | 13 | %% 14 | "while" { 15 | yylval.type_node = (node*)malloc(sizeof(node)); 16 | yylval.type_node->numchild = 0; 17 | yylval.type_node->name = "WHILE"; 18 | yylval.type_node->linenum=yylineno; 19 | return WHILE; 20 | } 21 | "else" { 22 | yylval.type_node = (node*)malloc(sizeof(node)); 23 | yylval.type_node->numchild = 0; 24 | yylval.type_node->name = "ELSE"; 25 | yylval.type_node->linenum=yylineno; 26 | return ELSE; 27 | } 28 | "if" { 29 | yylval.type_node = (node*)malloc(sizeof(node)); 30 | yylval.type_node->numchild = 0; 31 | yylval.type_node->name = "IF"; 32 | yylval.type_node->linenum=yylineno; 33 | return IF; 34 | } 35 | "return" { 36 | yylval.type_node = (node*)malloc(sizeof(node)); 37 | yylval.type_node->numchild = 0; 38 | yylval.type_node->name = "RETURN"; 39 | yylval.type_node->linenum=yylineno; 40 | return RETURN; 41 | } 42 | "struct" { 43 | yylval.type_node = (node*)malloc(sizeof(node)); 44 | yylval.type_node->numchild = 0; 45 | yylval.type_node->name = "STRUCT"; 46 | yylval.type_node->linenum=yylineno; 47 | return STRUCT; 48 | } 49 | "}" { 50 | yylval.type_node = (node*)malloc(sizeof(node)); 51 | yylval.type_node->numchild = 0; 52 | yylval.type_node->name = "RC"; 53 | yylval.type_node->linenum=yylineno; 54 | return RC; 55 | } 56 | "{" { 57 | yylval.type_node = (node*)malloc(sizeof(node)); 58 | yylval.type_node->numchild = 0; 59 | yylval.type_node->name = "LC"; 60 | yylval.type_node->linenum=yylineno; 61 | return LC; 62 | } 63 | "]" { 64 | yylval.type_node = (node*)malloc(sizeof(node)); 65 | yylval.type_node->numchild = 0; 66 | yylval.type_node->name = "RB"; 67 | yylval.type_node->linenum=yylineno; 68 | return RB; 69 | } 70 | "[" { 71 | yylval.type_node = (node*)malloc(sizeof(node)); 72 | yylval.type_node->numchild = 0; 73 | yylval.type_node->name = "LB"; 74 | yylval.type_node->linenum=yylineno; 75 | return LB; 76 | } 77 | ")" { 78 | yylval.type_node = (node*)malloc(sizeof(node)); 79 | yylval.type_node->numchild = 0; 80 | yylval.type_node->name = "RP"; 81 | yylval.type_node->linenum=yylineno; 82 | return RP; 83 | } 84 | "(" { 85 | yylval.type_node = (node*)malloc(sizeof(node)); 86 | yylval.type_node->numchild = 0; 87 | yylval.type_node->name = "LP"; 88 | yylval.type_node->linenum=yylineno; 89 | return LP; 90 | } 91 | "int"|"float" { 92 | yylval.type_node = (node*)malloc(sizeof(node)); 93 | yylval.type_node->numchild = 0; 94 | yylval.type_node->name = "TYPE"; 95 | yylval.type_node->typevalue = strdup(yytext); 96 | yylval.type_node->linenum=yylineno; 97 | return TYPE; 98 | } 99 | "!" { 100 | yylval.type_node = (node*)malloc(sizeof(node)); 101 | yylval.type_node->numchild = 0; 102 | yylval.type_node->name = "NOT"; 103 | yylval.type_node->linenum=yylineno; 104 | return NOT; 105 | } 106 | "." { 107 | yylval.type_node = (node*)malloc(sizeof(node)); 108 | yylval.type_node->numchild = 0; 109 | yylval.type_node->name = "DOT"; 110 | yylval.type_node->linenum=yylineno; 111 | return DOT; 112 | } 113 | "||" { 114 | yylval.type_node = (node*)malloc(sizeof(node)); 115 | yylval.type_node->numchild = 0; 116 | yylval.type_node->name = "OR"; 117 | yylval.type_node->linenum=yylineno; 118 | return OR; 119 | } 120 | "&&" { 121 | yylval.type_node = (node*)malloc(sizeof(node)); 122 | yylval.type_node->numchild = 0; 123 | yylval.type_node->name = "AND"; 124 | yylval.type_node->linenum=yylineno; 125 | return AND; 126 | } 127 | "/" { 128 | yylval.type_node = (node*)malloc(sizeof(node)); 129 | yylval.type_node->numchild = 0; 130 | yylval.type_node->name = "DIV"; 131 | yylval.type_node->linenum=yylineno; 132 | return DIV; 133 | } 134 | "*" { 135 | yylval.type_node = (node*)malloc(sizeof(node)); 136 | yylval.type_node->numchild = 0; 137 | yylval.type_node->name = "STAR"; 138 | yylval.type_node->linenum=yylineno; 139 | return STAR; 140 | } 141 | "-" { 142 | yylval.type_node = (node*)malloc(sizeof(node)); 143 | yylval.type_node->numchild = 0; 144 | yylval.type_node->name = "MINUS"; 145 | yylval.type_node->linenum=yylineno; 146 | return MINUS; 147 | } 148 | "+" { 149 | yylval.type_node = (node*)malloc(sizeof(node)); 150 | yylval.type_node->numchild = 0; 151 | yylval.type_node->name = "PLUS"; 152 | yylval.type_node->linenum=yylineno; 153 | return PLUS; 154 | } 155 | ">" { 156 | yylval.type_node = (node*)malloc(sizeof(node)); 157 | yylval.type_node->numchild = 0; 158 | yylval.type_node->name = "RELOP"; 159 | yylval.type_node->linenum=yylineno; 160 | yylval.type_node->relopvalue = 1; 161 | return RELOP; 162 | } 163 | "<" { 164 | yylval.type_node = (node*)malloc(sizeof(node)); 165 | yylval.type_node->numchild = 0; 166 | yylval.type_node->name = "RELOP"; 167 | yylval.type_node->linenum=yylineno; 168 | yylval.type_node->relopvalue = 2; 169 | return RELOP; 170 | } 171 | ">=" { 172 | yylval.type_node = (node*)malloc(sizeof(node)); 173 | yylval.type_node->numchild = 0; 174 | yylval.type_node->name = "RELOP"; 175 | yylval.type_node->linenum=yylineno; 176 | yylval.type_node->relopvalue = 3; 177 | return RELOP; 178 | } 179 | "<=" { 180 | yylval.type_node = (node*)malloc(sizeof(node)); 181 | yylval.type_node->numchild = 0; 182 | yylval.type_node->name = "RELOP"; 183 | yylval.type_node->linenum=yylineno; 184 | yylval.type_node->relopvalue = 4; 185 | return RELOP; 186 | } 187 | "==" { 188 | yylval.type_node = (node*)malloc(sizeof(node)); 189 | yylval.type_node->numchild = 0; 190 | yylval.type_node->name = "RELOP"; 191 | yylval.type_node->linenum=yylineno; 192 | yylval.type_node->relopvalue = 5; 193 | return RELOP; 194 | } 195 | "!=" { 196 | yylval.type_node = (node*)malloc(sizeof(node)); 197 | yylval.type_node->numchild = 0; 198 | yylval.type_node->name = "RELOP"; 199 | yylval.type_node->linenum=yylineno; 200 | yylval.type_node->relopvalue = 6; 201 | return RELOP; 202 | } 203 | "=" { 204 | yylval.type_node = (node*)malloc(sizeof(node)); 205 | yylval.type_node->numchild = 0; 206 | yylval.type_node->name = "ASSIGNOP"; 207 | yylval.type_node->linenum=yylineno; 208 | return ASSIGNOP; 209 | } 210 | "," { 211 | yylval.type_node = (node*)malloc(sizeof(node)); 212 | yylval.type_node->numchild = 0; 213 | yylval.type_node->name = "COMMA"; 214 | yylval.type_node->linenum=yylineno; 215 | return COMMA; 216 | } 217 | ";" { 218 | yylval.type_node = (node*)malloc(sizeof(node)); 219 | yylval.type_node->numchild = 0; 220 | yylval.type_node->name = "SEMI"; 221 | yylval.type_node->linenum=yylineno; 222 | return SEMI; 223 | } 224 | 0|[1-9]{digit}* { 225 | yylval.type_node = (node*)malloc(sizeof(node)); 226 | yylval.type_node->numchild = 0; 227 | yylval.type_node->name = "INT"; 228 | yylval.type_node->linenum=yylineno; 229 | yylval.type_node->intvalue = atoi(yytext); 230 | return INT; 231 | } 232 | 0[0-7]+ { 233 | yylval.type_node = (node*)malloc(sizeof(node)); 234 | yylval.type_node->numchild = 0; 235 | yylval.type_node->name = "INT"; 236 | yylval.type_node->linenum=yylineno; 237 | yylval.type_node->intvalue = (int)strtol(yytext,NULL,0); 238 | return INT; 239 | } 240 | (0x|0X)[A-Fa-f0-9]+ { 241 | yylval.type_node = (node*)malloc(sizeof(node)); 242 | yylval.type_node->numchild = 0; 243 | yylval.type_node->name = "INT"; 244 | yylval.type_node->linenum=yylineno; 245 | yylval.type_node->intvalue = (int)strtol(yytext,NULL,0); 246 | return INT; 247 | } 248 | {digit}+\.{digit}+ { 249 | yylval.type_node = (node*)malloc(sizeof(node)); 250 | yylval.type_node->numchild = 0; 251 | yylval.type_node->name = "FLOAT"; 252 | yylval.type_node->linenum=yylineno; 253 | yylval.type_node->floatvalue = atof(yytext); 254 | return FLOAT; 255 | } 256 | {digit}*\.{digit}+(E|e)(\+|\-)?{digit}+ { 257 | yylval.type_node = (node*)malloc(sizeof(node)); 258 | yylval.type_node->numchild = 0; 259 | yylval.type_node->name = "FLOAT"; 260 | yylval.type_node->linenum=yylineno; 261 | yylval.type_node->floatvalue = atof(yytext); 262 | return FLOAT; 263 | } 264 | {letter}({letter}|{digit})* { 265 | yylval.type_node = (node*)malloc(sizeof(node)); 266 | yylval.type_node->numchild = 0; 267 | yylval.type_node->name = "ID"; 268 | yylval.type_node->linenum=yylineno; 269 | yylval.type_node->IDvalue = strdup(yytext); 270 | return ID; 271 | } 272 | "//" { char c = input(); while (c != '\n') c = input(); } 273 | "/*" { char c = input(); TAB: while (c != '*') c = input(); c = input(); if (c != '/') goto TAB;} 274 | \n|\t|" " { } 275 | . { 276 | lex_error = 1; 277 | printf("Error type A at Line %d: Mysterious characters \"%s\"\n", yylineno,yytext); 278 | } 279 | %% 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | extern int syn_error; 5 | extern int lex_error; 6 | FILE* out; 7 | int main(int argc, char** argv) { 8 | //yydebug = 1; 9 | FILE* fp; 10 | if (argc <= 1) 11 | { 12 | return 1; 13 | } 14 | FILE* f = fopen(argv[1],"r"); 15 | if (!f) 16 | { 17 | perror(argv[1]); 18 | return 1; 19 | } 20 | if(argc > 2) 21 | { 22 | out = fopen(argv[2],"w"); 23 | } 24 | yyrestart(f); 25 | yyparse(); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | FLEX = flex 3 | BISON = bison 4 | 5 | parser: main.c lex.yy.c syntax.tab.c 6 | $(CC) main.c syntax.tab.c -lfl -o parser -g 7 | 8 | lex.yy.c: lexical.l 9 | $(FLEX) lexical.l 10 | 11 | syntax.tab.c: syntax.y 12 | $(BISON) -d -t syntax.y 13 | 14 | .PHONY: clean test 15 | test: 16 | ./parser test1.c 17 | ./parser test2.c 18 | ./parser test3.c 19 | ./parser test4.c 20 | ./parser test5.c 21 | ./parser test6.c 22 | ./parser test7.c 23 | ./parser test8.c 24 | ./parser test9.c 25 | ./parser test10.c 26 | clean: 27 | rm -f parser lex.yy.c syntax.tab.c syntax.tab.h syntax.output 28 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/multitree.h: -------------------------------------------------------------------------------- 1 | #ifndef NUM_TREE 2 | #define NUM_TREE 3 | #include 4 | #include 5 | #include 6 | 7 | typedef struct node_t 8 | { 9 | char* name; 10 | int numchild; 11 | char* typevalue; 12 | char* IDvalue; 13 | int intvalue; 14 | int relopvalue; 15 | float floatvalue; 16 | struct node_t** children; 17 | int linenum; 18 | int type ; 19 | int dim ; 20 | int arraytype; 21 | int canleft; 22 | } node; 23 | 24 | 25 | node* Create_new_node( char* nodename , int number , node** childs , int yylineno) 26 | { 27 | node* A = (node*)malloc(sizeof(node)); 28 | A->name = nodename; 29 | A->numchild = number; 30 | A->children = childs; 31 | A->linenum = yylineno; 32 | return A; 33 | } 34 | 35 | void Travel (node* root,int space) 36 | { 37 | int i = 0; 38 | for(int j = 0;j < space;j++) 39 | { 40 | printf(" "); 41 | } 42 | if (strcmp(root->name, "TYPE") == 0) 43 | { 44 | printf("%s: %s\n",root->name,root->typevalue); 45 | } 46 | else if (strcmp(root->name, "ID") == 0) 47 | { 48 | printf("%s: %s\n",root->name,root->IDvalue); 49 | } 50 | else if (strcmp(root->name, "INT") == 0) 51 | { 52 | printf("%s: %d\n",root->name,root->intvalue); 53 | } 54 | else if (strcmp(root->name, "FLOAT") == 0) 55 | { 56 | printf("%s: %f\n",root->name,root->floatvalue); 57 | } 58 | else if (strcmp(root->name, "SEMI") == 0) 59 | { 60 | printf("%s\n",root->name); 61 | } 62 | else if (strcmp(root->name, "COMMA") == 0) 63 | { 64 | printf("%s\n",root->name); 65 | } 66 | else if (strcmp(root->name, "ASSIGNOP") == 0) 67 | { 68 | printf("%s\n",root->name); 69 | } 70 | else if (strcmp(root->name, "RELOP") == 0) 71 | { 72 | printf("%s\n",root->name); 73 | } 74 | else if (strcmp(root->name, "PLUS") == 0) 75 | { 76 | printf("%s\n",root->name); 77 | } 78 | else if (strcmp(root->name, "MINUS") == 0) 79 | { 80 | printf("%s\n",root->name); 81 | } 82 | else if (strcmp(root->name, "STAR") == 0) 83 | { 84 | printf("%s\n",root->name); 85 | } 86 | else if (strcmp(root->name, "DIV") == 0) 87 | { 88 | printf("%s\n",root->name); 89 | } 90 | else if (strcmp(root->name, "AND") == 0) 91 | { 92 | printf("%s\n",root->name); 93 | } 94 | else if (strcmp(root->name, "OR") == 0) 95 | { 96 | printf("%s\n",root->name); 97 | } 98 | else if (strcmp(root->name, "DOT") == 0) 99 | { 100 | printf("%s\n",root->name); 101 | } 102 | else if (strcmp(root->name, "NOT") == 0) 103 | { 104 | printf("%s\n",root->name); 105 | } 106 | else if (strcmp(root->name, "LP") == 0) 107 | { 108 | printf("%s\n",root->name); 109 | } 110 | else if (strcmp(root->name, "RP") == 0) 111 | { 112 | printf("%s\n",root->name); 113 | } 114 | else if (strcmp(root->name, "LB") == 0) 115 | { 116 | printf("%s\n",root->name); 117 | } 118 | else if (strcmp(root->name, "RB") == 0) 119 | { 120 | printf("%s\n",root->name); 121 | } 122 | else if (strcmp(root->name, "LC") == 0) 123 | { 124 | printf("%s\n",root->name); 125 | } 126 | else if (strcmp(root->name, "RC") == 0) 127 | { 128 | printf("%s\n",root->name); 129 | } 130 | else if (strcmp(root->name, "STRUCT") == 0) 131 | { 132 | printf("%s\n",root->name); 133 | } 134 | else if (strcmp(root->name, "RETURN") == 0) 135 | { 136 | printf("%s\n",root->name); 137 | } 138 | else if (strcmp(root->name, "IF") == 0) 139 | { 140 | printf("%s\n",root->name); 141 | } 142 | else if (strcmp(root->name, "ELSE") == 0) 143 | { 144 | printf("%s\n",root->name); 145 | } 146 | else if (strcmp(root->name, "WHILE") == 0) 147 | { 148 | printf("%s\n",root->name); 149 | } 150 | else 151 | { 152 | printf("%s (%d)\n",root->name,root->linenum); 153 | } 154 | space +=2; 155 | while(i<(root->numchild)) 156 | { 157 | if(root->children[i] != NULL) 158 | { 159 | Travel (root->children[i],space); 160 | i++; 161 | }else i++; 162 | } 163 | } 164 | 165 | 166 | 167 | #define YYERROR_VERBOSE 1 168 | 169 | #endif 170 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/out1: -------------------------------------------------------------------------------- 1 | FUNCTION main : 2 | READ t1 3 | n := t1 4 | t2 := n 5 | t3 := #0 6 | IF t2 > t3 GOTO label1 7 | GOTO label2 8 | LABEL label1 : 9 | t4 := #1 10 | WRITE t4 11 | GOTO label3 12 | LABEL label2 : 13 | t5 := n 14 | t6 := #0 15 | IF t5 < t6 GOTO label4 16 | GOTO label5 17 | LABEL label4 : 18 | t8 := #1 19 | t7 := #0 - t8 20 | WRITE t7 21 | GOTO label6 22 | LABEL label5 : 23 | t9 := #0 24 | WRITE t9 25 | LABEL label6 : 26 | LABEL label3 : 27 | t10 := #0 28 | RETURN t10 29 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/out2: -------------------------------------------------------------------------------- 1 | FUNCTION fact : 2 | PARAM n 3 | t1 := n 4 | t2 := #1 5 | IF t1 == t2 GOTO label1 6 | GOTO label2 7 | LABEL label1 : 8 | t3 := n 9 | RETURN t3 10 | GOTO label3 11 | LABEL label2 : 12 | t5 := n 13 | t8 := n 14 | t9 := #1 15 | t7 := t8 - t9 16 | ARG t7 17 | t6 := CALL fact 18 | t4 := t5 * t6 19 | RETURN t4 20 | LABEL label3 : 21 | FUNCTION main : 22 | READ t10 23 | m := t10 24 | t11 := m 25 | t12 := #1 26 | IF t11 > t12 GOTO label4 27 | GOTO label5 28 | LABEL label4 : 29 | t14 := m 30 | ARG t14 31 | t13 := CALL fact 32 | result := t13 33 | GOTO label6 34 | LABEL label5 : 35 | t15 := #1 36 | result := t15 37 | LABEL label6 : 38 | t16 := result 39 | WRITE t16 40 | t17 := #0 41 | RETURN t17 42 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/parser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验三/codes/parser -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/scanner: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验三/codes/scanner -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/semantic.h: -------------------------------------------------------------------------------- 1 | #ifndef SEM 2 | #define SEM 3 | #include "multitree.h" 4 | typedef struct Type_* Type; 5 | typedef struct FieldList_* FieldList; 6 | typedef struct node1 7 | { 8 | char* symname; 9 | Type symtype; 10 | struct node1* next; 11 | } tablenode; 12 | 13 | struct Type_ 14 | { 15 | enum { BASIC, ARRAY, STRUCTURE, FUNC} kind; 16 | union 17 | { 18 | int basic; 19 | struct { Type elem; int size; } array; 20 | } u; 21 | }; 22 | 23 | struct Variable 24 | { 25 | char* name ; 26 | Type head ; 27 | int line; 28 | }; 29 | tablenode* chainheader; 30 | tablenode* Create_tablenode (char* nodename, Type nodetype, tablenode* chainheader) 31 | { 32 | tablenode* A = (tablenode*)malloc(sizeof(tablenode)); 33 | A->symname = nodename; 34 | A->symtype = nodetype; 35 | A->next = chainheader; 36 | return A; 37 | } 38 | 39 | int checkrepeat(char* nodename,int lineno) 40 | { 41 | tablenode* tmp = chainheader; 42 | while(tmp!=NULL) 43 | { 44 | if(strcmp(tmp->symname, nodename) == 0) 45 | { 46 | printf("Error type 3 at Line %d: Redefined variable\n", lineno); 47 | return 1; 48 | } 49 | tmp = tmp->next; 50 | } 51 | return 0; 52 | } 53 | 54 | struct Variable getVarDec(node* root,Type type) 55 | { 56 | if(strcmp(root->children[0]->name, "ID") == 0) 57 | { 58 | struct Variable array; 59 | array.name = root->children[0]->IDvalue; 60 | array.head = type; 61 | array.line = root->children[0]->linenum; 62 | return array; 63 | } 64 | else 65 | { 66 | struct Variable tmp; 67 | Type arraytype = (Type)malloc(sizeof(struct Type_)); 68 | arraytype->kind = ARRAY; 69 | arraytype->u.array.elem = type; 70 | arraytype->u.array.size = root->children[2]->intvalue; 71 | tmp = getVarDec(root->children[0],arraytype); 72 | return tmp; 73 | } 74 | 75 | } 76 | 77 | void dealparam(node* param) 78 | { 79 | if(strcmp(param->children[0]->children[0]->name, "TYPE") == 0) 80 | { 81 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 82 | nodetype->kind = BASIC; 83 | if(strcmp(param->children[0]->children[0]->typevalue, "int") == 0) 84 | { 85 | nodetype->u.basic = 0; 86 | } 87 | if(strcmp(param->children[0]->children[0]->typevalue, "float") == 0) 88 | { 89 | nodetype->u.basic = 1; 90 | } 91 | struct Variable array = getVarDec(param->children[1],nodetype); 92 | int check = checkrepeat(array.name,array.line); 93 | if(check == 0) 94 | { 95 | chainheader = Create_tablenode (array.name, array.head, chainheader); 96 | } 97 | } 98 | if(strcmp(param->children[0]->children[0]->name, "StructSpecifier") == 0) 99 | { 100 | if(strcmp(param->children[0]->children[0]->children[1]->name, "Tag") == 0) 101 | { 102 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 103 | nodetype->kind = STRUCTURE; 104 | int check = checkrepeat(param->children[0]->children[0]->children[1]->children[0]->IDvalue,param->children[0]->children[0]->children[1]->children[0]->linenum); 105 | if(check == 0) 106 | { 107 | chainheader = Create_tablenode (param->children[0]->children[0]->children[1]->children[0]->IDvalue, nodetype, chainheader); 108 | } 109 | } 110 | } 111 | 112 | } 113 | 114 | int lookuptable(char* name) 115 | { 116 | tablenode* tmp = chainheader; 117 | while(tmp!=NULL) 118 | { 119 | if(strcmp(tmp->symname, name) == 0) 120 | { 121 | if(tmp->symtype->kind == BASIC) 122 | { 123 | if(tmp->symtype->u.basic == 0) 124 | { 125 | return 0; 126 | } 127 | if(tmp->symtype->u.basic == 1) 128 | { 129 | return 1; 130 | } 131 | } 132 | if(tmp->symtype->kind == ARRAY) 133 | { 134 | return 1; 135 | } 136 | } 137 | tmp = tmp->next; 138 | } 139 | return 0; 140 | } 141 | 142 | void checkerror(node* root) 143 | { 144 | if(strcmp(root->name, "ExtDef") == 0) 145 | { 146 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 147 | if(strcmp(root->children[0]->children[0]->name, "TYPE") == 0) 148 | { 149 | nodetype->kind = BASIC; 150 | if(strcmp(root->children[0]->children[0]->typevalue, "int") == 0) 151 | { 152 | nodetype->u.basic = 0; 153 | } 154 | if(strcmp(root->children[0]->children[0]->typevalue, "float") == 0) 155 | { 156 | nodetype->u.basic = 1; 157 | } 158 | } 159 | if(strcmp(root->children[0]->children[0]->name, "StructSpecifier") == 0) 160 | { 161 | if(root->children[0]->children[0]->children[1] != NULL) 162 | { 163 | if(strcmp(root->children[0]->children[0]->children[1]->name, "Tag") == 0) 164 | { 165 | nodetype->kind = STRUCTURE; 166 | int check = checkrepeat(root->children[0]->children[0]->children[1]->children[0]->IDvalue,root->children[0]->children[0]->children[1]->children[0]->linenum); 167 | if(check == 0) 168 | { 169 | chainheader = Create_tablenode (root->children[0]->children[0]->children[1]->children[0]->IDvalue, nodetype, chainheader); 170 | } 171 | } 172 | if(strcmp(root->children[0]->children[0]->children[1]->name, "OptTag") == 0) 173 | { 174 | nodetype->kind = STRUCTURE; 175 | int check = checkrepeat(root->children[0]->children[0]->children[1]->children[0]->IDvalue,root->children[0]->children[0]->children[1]->children[0]->linenum); 176 | if(check == 0) 177 | { 178 | chainheader = Create_tablenode (root->children[0]->children[0]->children[1]->children[0]->IDvalue, nodetype, chainheader); 179 | } 180 | } 181 | } 182 | else 183 | { 184 | nodetype->kind = STRUCTURE; 185 | } 186 | } 187 | if(strcmp(root->children[1]->name, "ExtDecList") == 0) 188 | { 189 | node* tmp1 = root->children[1]; 190 | struct Variable array = getVarDec(tmp1->children[0],nodetype); 191 | int check = checkrepeat(array.name,array.line); 192 | if(check == 0) 193 | { 194 | chainheader = Create_tablenode (array.name, array.head, chainheader); 195 | } 196 | while(tmp1->numchild != 1) 197 | { 198 | tmp1 = tmp1->children[2]; 199 | struct Variable array1 = getVarDec(tmp1->children[0],nodetype); 200 | int check = checkrepeat(array1.name,array1.line); 201 | if(check == 0) 202 | { 203 | chainheader = Create_tablenode (array1.name, array1.head, chainheader); 204 | } 205 | } 206 | } 207 | else if(strcmp(root->children[1]->name, "FunDec") == 0) 208 | { 209 | Type funtype = (Type)malloc(sizeof(struct Type_)); 210 | funtype->kind = FUNC; 211 | int check = checkrepeat(root->children[1]->children[0]->IDvalue,root->children[1]->children[0]->linenum); 212 | if(check == 0) 213 | { 214 | chainheader = Create_tablenode (root->children[1]->children[0]->IDvalue, funtype, chainheader); 215 | } 216 | if(strcmp(root->children[1]->children[2]->name, "VarList") == 0) 217 | { 218 | node* tmp2 = root->children[1]->children[2]; 219 | dealparam(tmp2->children[0]); 220 | while(tmp2->numchild != 1) 221 | { 222 | tmp2 = tmp2->children[2]; 223 | dealparam(tmp2->children[0]); 224 | } 225 | } 226 | } 227 | else {} 228 | } 229 | 230 | else if(strcmp(root->name, "Def") == 0) 231 | { 232 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 233 | if(strcmp(root->children[0]->children[0]->name, "TYPE") == 0) 234 | { 235 | nodetype->kind = BASIC; 236 | if(strcmp(root->children[0]->children[0]->typevalue, "int") == 0) 237 | { 238 | nodetype->u.basic = 0; 239 | } 240 | if(strcmp(root->children[0]->children[0]->typevalue, "float") == 0) 241 | { 242 | nodetype->u.basic = 1; 243 | } 244 | } 245 | if(strcmp(root->children[0]->children[0]->name, "StructSpecifier") == 0) 246 | { 247 | if(root->children[0]->children[0]->children[1] != NULL) 248 | { 249 | if(strcmp(root->children[0]->children[0]->children[1]->name, "Tag") == 0) 250 | { 251 | nodetype->kind = STRUCTURE; 252 | int check = checkrepeat(root->children[0]->children[0]->children[1]->children[0]->IDvalue,root->children[0]->children[0]->children[1]->children[0]->linenum); 253 | if(check == 0) 254 | { 255 | chainheader = Create_tablenode (root->children[0]->children[0]->children[1]->children[0]->IDvalue, nodetype, chainheader); 256 | } 257 | } 258 | if(strcmp(root->children[0]->children[0]->children[1]->name, "OptTag") == 0) 259 | { 260 | nodetype->kind = STRUCTURE; 261 | int check = checkrepeat(root->children[0]->children[0]->children[1]->children[0]->IDvalue,root->children[0]->children[0]->children[1]->children[0]->linenum); 262 | if(check == 0) 263 | { 264 | chainheader = Create_tablenode (root->children[0]->children[0]->children[1]->children[0]->IDvalue, nodetype, chainheader); 265 | } 266 | } 267 | } 268 | else 269 | { 270 | nodetype->kind = STRUCTURE; 271 | } 272 | } 273 | node* tmp2 = root->children[1]; 274 | struct Variable array = getVarDec(tmp2->children[0]->children[0],nodetype); 275 | int check = checkrepeat(array.name,array.line); 276 | if(check == 0) 277 | { 278 | chainheader = Create_tablenode (array.name, array.head, chainheader); 279 | } 280 | if(tmp2->children[0]->numchild != 1) 281 | { 282 | checkerror(tmp2->children[0]->children[2]); 283 | if(array.head->kind == BASIC && array.head->u.basic == 0 && tmp2->children[0]->children[2]->type == 0) 284 | {} 285 | else if(array.head->kind == BASIC && array.head->u.basic == 1 && tmp2->children[0]->children[2]->type == 1) 286 | {} 287 | else 288 | { 289 | if(tmp2->children[0]->children[2]->type != 10) 290 | { 291 | printf("Error type 5 at Line %d: Type mismatched for assignment\n", array.line); 292 | } 293 | } 294 | } 295 | while(tmp2->numchild != 1) 296 | { 297 | tmp2 = tmp2->children[2]; 298 | struct Variable array1 = getVarDec(tmp2->children[0]->children[0],nodetype); 299 | int check = checkrepeat(array1.name,array1.line); 300 | if(check == 0) 301 | { 302 | chainheader = Create_tablenode (array1.name, array1.head, chainheader); 303 | } 304 | if(tmp2->children[0]->numchild != 1) 305 | { 306 | checkerror(tmp2->children[0]->children[2]); 307 | if(array.head->kind == BASIC && array.head->u.basic == 0 && tmp2->children[0]->children[2]->type == 0) 308 | {} 309 | else if(array.head->kind == BASIC && array.head->u.basic == 1 && tmp2->children[0]->children[2]->type == 1) 310 | {} 311 | else 312 | { 313 | if(tmp2->children[0]->children[2]->type != 10) 314 | { 315 | printf("Error type 5 at Line %d: Type mismatched for assignment\n", array1.line); 316 | } 317 | } 318 | } 319 | } 320 | } 321 | 322 | else if(strcmp(root->name, "Exp") == 0) 323 | { 324 | if(root->numchild == 3) 325 | { 326 | if(strcmp(root->children[1]->name, "PLUS") == 0) 327 | { 328 | checkerror(root->children[0]); 329 | checkerror(root->children[2]); 330 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 331 | { 332 | root->type = 0; 333 | root->canleft = 0; 334 | } 335 | else if(root->children[0]->type == 1 && root->children[2]->type == 1) 336 | { 337 | root->type = 1; 338 | root->canleft = 0; 339 | } 340 | else 341 | { 342 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 343 | { 344 | printf("Error type 7 at Line %d: Type mismatched for operands\n", root->children[0]->linenum); 345 | } 346 | root->type = 10; 347 | } 348 | 349 | } 350 | if(strcmp(root->children[1]->name, "MINUS") == 0) 351 | { 352 | checkerror(root->children[0]); 353 | checkerror(root->children[2]); 354 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 355 | { 356 | root->type = 0; 357 | root->canleft = 0; 358 | } 359 | else if(root->children[0]->type == 1 && root->children[2]->type == 1) 360 | { 361 | root->type = 1; 362 | root->canleft = 0; 363 | } 364 | else 365 | { 366 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 367 | { 368 | printf("Error type 7 at Line %d: Type mismatched for operands\n", root->children[0]->linenum); 369 | } 370 | root->type = 10; 371 | } 372 | } 373 | if(strcmp(root->children[1]->name, "STAR") == 0) 374 | { 375 | checkerror(root->children[0]); 376 | checkerror(root->children[2]); 377 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 378 | { 379 | root->type = 0; 380 | root->canleft = 0; 381 | } 382 | else if(root->children[0]->type == 1 && root->children[2]->type == 1) 383 | { 384 | root->type = 1; 385 | root->canleft = 0; 386 | } 387 | else 388 | { 389 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 390 | { 391 | printf("Error type 7 at Line %d: Type mismatched for operands\n", root->children[0]->linenum); 392 | } 393 | root->type = 10; 394 | } 395 | } 396 | if(strcmp(root->children[1]->name, "DIV") == 0) 397 | { 398 | checkerror(root->children[0]); 399 | checkerror(root->children[2]); 400 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 401 | { 402 | root->type = 0; 403 | root->canleft = 0; 404 | } 405 | else if(root->children[0]->type == 1 && root->children[2]->type == 1) 406 | { 407 | root->type = 1; 408 | root->canleft = 0; 409 | } 410 | else 411 | { 412 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 413 | { 414 | printf("Error type 7 at Line %d: Type mismatched for operands\n", root->children[0]->linenum); 415 | } 416 | root->type = 10; 417 | } 418 | } 419 | if(strcmp(root->children[1]->name, "AND") == 0) 420 | { 421 | checkerror(root->children[0]); 422 | checkerror(root->children[2]); 423 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 424 | { 425 | root->type = 0; 426 | root->canleft = 0; 427 | } 428 | else 429 | { 430 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 431 | { 432 | printf("Error type 7 at Line %d: Type mismatched for operands\n", root->children[0]->linenum); 433 | } 434 | root->type = 10; 435 | } 436 | } 437 | if(strcmp(root->children[1]->name, "OR") == 0) 438 | { 439 | checkerror(root->children[0]); 440 | checkerror(root->children[2]); 441 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 442 | { 443 | root->type = 0; 444 | root->canleft = 0; 445 | } 446 | else 447 | { 448 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 449 | { 450 | printf("Error type 7 at Line %d: Type mismatched for operands\n", root->children[0]->linenum); 451 | } 452 | root->type = 10; 453 | } 454 | } 455 | if(strcmp(root->children[1]->name, "RELOP") == 0) 456 | { 457 | checkerror(root->children[0]); 458 | checkerror(root->children[2]); 459 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 460 | { 461 | root->type = 0; 462 | root->canleft = 0; 463 | } 464 | else 465 | { 466 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 467 | { 468 | printf("Error type 7 at Line %d: Type mismatched for operands\n", root->children[0]->linenum); 469 | } 470 | root->type = 10; 471 | } 472 | } 473 | if(strcmp(root->children[1]->name, "ASSIGNOP") == 0) 474 | { 475 | checkerror(root->children[0]); 476 | checkerror(root->children[2]); 477 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 478 | { 479 | if(root->children[0]->canleft == 1) 480 | { 481 | root->type = 0; 482 | root->canleft = 0; 483 | } 484 | else 485 | { 486 | printf("Error type 6 at Line %d: The left-hand side of an assignment must be a variable\n", root->children[0]->linenum); 487 | root->type = 10; 488 | } 489 | } 490 | else if(root->children[0]->type == 1 && root->children[2]->type == 1) 491 | { 492 | if(root->children[0]->canleft == 1) 493 | { 494 | root->type = 1; 495 | root->canleft = 0; 496 | } 497 | else 498 | { 499 | printf("Error type 6 at Line %d: The left-hand side of an assignment must be a variable\n", root->children[0]->linenum); 500 | root->type = 10; 501 | } 502 | } 503 | else if(root->children[0]->type == 2 && root->children[2]->type == 2) 504 | { 505 | if(root->children[0]->dim == root->children[2]->dim && root->children[0]->arraytype ==root->children[2]->arraytype) 506 | { 507 | if(root->children[0]->canleft == 1) 508 | { 509 | root->type = 2; 510 | root->dim = root->children[0]->dim; 511 | root->arraytype = root->children[0]->arraytype; 512 | root->canleft = 0; 513 | } 514 | else 515 | { 516 | printf("Error type 6 at Line %d: The left-hand side of an assignment must be a variable\n", root->children[0]->linenum); 517 | root->type = 10; 518 | } 519 | } 520 | else 521 | { 522 | printf("Error type 5 at Line %d: Type mismatched for assignment\n", root->children[0]->linenum); 523 | root->type = 10; 524 | } 525 | } 526 | else 527 | { 528 | if(root->children[0]->type != 10 && root->children[2]->type != 10) 529 | { 530 | printf("Error type 5 at Line %d: Type mismatched for assignment\n", root->children[0]->linenum); 531 | } 532 | root->type = 10; 533 | } 534 | } 535 | if(strcmp(root->children[1]->name, "Exp") == 0) 536 | { 537 | checkerror(root->children[1]); 538 | if(root->children[1]->type == 2) 539 | { 540 | root->type = 2; 541 | root->dim = root->children[0]->dim; 542 | root->arraytype = root->children[0]->arraytype; 543 | } 544 | else 545 | { 546 | root->type = root->children[1]->type; 547 | } 548 | root->canleft = 0; 549 | } 550 | } 551 | if(root->numchild == 2) 552 | { 553 | if(strcmp(root->children[0]->name, "MINUS") == 0) 554 | { 555 | checkerror(root->children[1]); 556 | if(root->children[1]->type == 0 || root->children[1]->type == 1) 557 | { 558 | root->type = root->children[1]->type; 559 | root->canleft = 0; 560 | } 561 | else 562 | { 563 | if(root->children[1]->type != 10) 564 | { 565 | printf("Error type 7 at Line %d: Type mismatched for operands\n", root->children[1]->linenum); 566 | } 567 | root->type = 10; 568 | } 569 | 570 | } 571 | if(strcmp(root->children[1]->name, "NOT") == 0) 572 | { 573 | checkerror(root->children[1]); 574 | if(root->children[1]->type == 0) 575 | { 576 | root->type = root->children[1]->type; 577 | root->canleft = 0; 578 | } 579 | else 580 | { 581 | if(root->children[1]->type != 10) 582 | { 583 | printf("Error type 7 at Line %d: Type mismatched for operands\n", root->children[1]->linenum); 584 | } 585 | root->type = 10; 586 | } 587 | } 588 | } 589 | if(root->numchild == 4) 590 | { 591 | if(strcmp(root->children[0]->name, "Exp") == 0) 592 | { 593 | checkerror(root->children[0]); 594 | checkerror(root->children[2]); 595 | if(root->children[0]->type == 2) 596 | { 597 | if(root->children[2]->type == 0) 598 | { 599 | int dimtmp = root->children[0]->dim-1; 600 | if(dimtmp != 0) 601 | { 602 | root->type =2; 603 | root->dim = dimtmp; 604 | root->arraytype = root->children[0]->arraytype; 605 | } 606 | else 607 | { 608 | if(root->children[0]->arraytype == 0) 609 | { 610 | root->type =0; 611 | } 612 | if(root->children[0]->arraytype == 1) 613 | { 614 | root->type =1; 615 | } 616 | } 617 | root->canleft = 1; 618 | } 619 | else 620 | { 621 | if(root->children[0]->type != 10) 622 | { 623 | printf("Error type 12 at Line %d: is not an integer\n", root->children[2]->linenum); 624 | } 625 | root->type = 10; 626 | } 627 | } 628 | else 629 | { 630 | if(root->children[0]->type != 10) 631 | { 632 | printf("Error type 10 at Line %d: is not an array\n", root->children[1]->linenum); 633 | } 634 | root->type = 10; 635 | } 636 | } 637 | } 638 | if(root->numchild == 1) 639 | { 640 | if(strcmp(root->children[0]->name, "ID") == 0) 641 | { 642 | tablenode* tmp = chainheader; 643 | while(tmp!=NULL) 644 | { 645 | if(strcmp(tmp->symname, root->children[0]->IDvalue) == 0) 646 | { 647 | if(tmp->symtype->kind == BASIC) 648 | { 649 | if(tmp->symtype->u.basic == 0) 650 | { 651 | root->type = 0; 652 | } 653 | if(tmp->symtype->u.basic == 1) 654 | { 655 | root->type = 1; 656 | } 657 | } 658 | if(tmp->symtype->kind == ARRAY) 659 | { 660 | int dim=1; 661 | root->type = 2; 662 | Type piao = tmp->symtype; 663 | while(piao->u.array.elem->kind != BASIC) 664 | { 665 | dim++; 666 | piao = piao->u.array.elem; 667 | } 668 | if(piao->u.array.elem->u.basic == 0) 669 | { 670 | root->arraytype = 0; 671 | } 672 | if(piao->u.array.elem->u.basic == 1) 673 | { 674 | root->arraytype = 1; 675 | } 676 | root->dim = dim; 677 | } 678 | if(tmp->symtype->kind == STRUCTURE) 679 | { 680 | root->type = 3; 681 | } 682 | if(tmp->symtype->kind == FUNC) 683 | { 684 | root->type = 4; 685 | } 686 | root->canleft = 1; 687 | //if(chainheader!= NULL) {printf("piao\n");} 688 | break; 689 | } 690 | tmp = tmp->next; 691 | } 692 | if(tmp == NULL) 693 | { 694 | root->type = 10; 695 | printf("Error type 1 at Line %d: Undefined variable\n", root->children[0]->linenum); 696 | } 697 | } 698 | if(strcmp(root->children[0]->name, "INT") == 0) 699 | { 700 | root->type = 0; 701 | root->canleft = 0; 702 | } 703 | if(strcmp(root->children[0]->name, "FLOAT") == 0) 704 | { 705 | root->type = 1; 706 | root->canleft = 0; 707 | } 708 | } 709 | 710 | 711 | } 712 | } 713 | void Analysis (node* root) 714 | { 715 | int i = 0; 716 | int iterate_child = 1; 717 | if (strcmp(root->name, "ExtDef") == 0) 718 | { 719 | checkerror(root); 720 | } 721 | else if (strcmp(root->name, "Def") == 0) 722 | { 723 | iterate_child = 0; 724 | checkerror(root); 725 | } 726 | else if (strcmp(root->name, "Exp") == 0) 727 | { 728 | iterate_child = 0; 729 | checkerror(root); 730 | } 731 | if (iterate_child) 732 | while(i<(root->numchild)) 733 | { 734 | if(root->children[i] != NULL) 735 | { 736 | Analysis(root->children[i]); 737 | i++; 738 | }else i++; 739 | } 740 | } 741 | #endif 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/symboltable.h: -------------------------------------------------------------------------------- 1 | typedef struct Type_* Type; 2 | typedef struct FieldList_* FieldList; 3 | 4 | struct Type_ 5 | { 6 | enum { BASIC, ARRAY, STRUCTURE, FUNC} kind; 7 | union 8 | { 9 | int basic; 10 | struct { Type elem; int size; } array; 11 | } u; 12 | }; 13 | 14 | struct Variable 15 | { 16 | char* name ; 17 | Type head ; 18 | int line; 19 | } 20 | 21 | typedef struct node1 22 | { 23 | char* symname; 24 | Type symtype; 25 | struct node1* next; 26 | } tablenode; 27 | 28 | tablenode* Create_tablenode (char* nodename, Type nodetype, tablenode* chainheader) 29 | { 30 | tablenode* A = (tablenode*)malloc(sizeof(tablenode)); 31 | A->symname = nodename; 32 | A->symtype = nodetype; 33 | A->next = chainheader; 34 | return A; 35 | } 36 | 37 | int checkrepeat(char* nodename,int lineno) 38 | { 39 | tablenode* tmp = chainheader; 40 | while(tmp!=NULL) 41 | { 42 | if(strcmp(tmp->symname, nodename) == 0) 43 | { 44 | printf("Error type 3 at Line %d\n", lineno); 45 | return 1; 46 | } 47 | tmp = tmp->next; 48 | } 49 | return 0; 50 | } 51 | 52 | struct Variable getVarDec(node* root,Type type) 53 | { 54 | if(strcmp(root->children[0]->name, "ID") == 0) 55 | { 56 | struct Variable array; 57 | array.name = root->children[0]->IDvalue; 58 | array.head = type; 59 | array.line = root->children[0]->linenum; 60 | return array; 61 | } 62 | else 63 | { 64 | struct Variable tmp; 65 | Type arraytype = (Type)malloc(sizeof(struct Type_)); 66 | arraytype->kind = ARRAY; 67 | arraytype->u.array.elem = type; 68 | arraytype->u.array.size = root->children[2]->intvalue; 69 | tmp = getVarDec(root->children[0],arraytype); 70 | return tmp; 71 | } 72 | 73 | } 74 | 75 | dealParam(node* param) 76 | { 77 | if(strcmp(param->children[0]->children[0]->name, "TYPE") == 0) 78 | { 79 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 80 | nodetype->kind = BASIC; 81 | if(strcmp(root->children[0]->children[0]->typevalue, "int") == 0) 82 | { 83 | nodetype->u.basic = 0; 84 | } 85 | if(strcmp(root->children[0]->children[0]->typevalue, "float") == 0) 86 | { 87 | nodetype->u.basic = 1; 88 | } 89 | struct Variable array = getVarDec(param->children[1],nodetype); 90 | int check = checkrepeat(array.name,array.line); 91 | if(check == 0) 92 | { 93 | chainheader = Create_tablenode (array.name, array.head, chainheader); 94 | } 95 | } 96 | if(strcmp(param->children[0]->children[0]->name, "StructSpecifier") == 0) 97 | { 98 | if(strcmp(param->children[0]->children[0]->children[1]->name, "Tag") == 0) 99 | { 100 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 101 | nodetype->kind = STRUCTURE; 102 | int check = checkrepeat(param->children[0]->children[0]->children[1]->children[0]->IDvalue,param->children[0]->children[0]->children[1]->children[0]->linenum); 103 | if(check == 0) 104 | { 105 | chainheader = Create_tablenode (param->children[0]->children[0]->children[1]->children[0]->IDvalue, nodetype, chainheader); 106 | } 107 | } 108 | } 109 | 110 | } 111 | 112 | int lookuptable(char* name) 113 | { 114 | tablenode* tmp = chainheader; 115 | while(tmp!=NULL) 116 | { 117 | if(strcmp(tmp->symname, name) == 0) 118 | { 119 | if(tmp->symtype->kind == BASIC) 120 | { 121 | if(tmp->symtype->u.basic == 0) 122 | { 123 | return 0; 124 | } 125 | if(tmp->symtype->u.basic == 1) 126 | { 127 | return 1; 128 | } 129 | } 130 | if(tmp->symtype->kind == ARRAY) 131 | { 132 | return 1; 133 | } 134 | } 135 | tmp = tmp->next; 136 | } 137 | return 0; 138 | } 139 | 140 | void checkerror(node* root) 141 | { 142 | if(strcmp(root->name, "ExtDef") == 0) 143 | { 144 | if(strcmp(root->children[0]->children[0]->name, "TYPE") == 0) 145 | { 146 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 147 | nodetype->kind = BASIC; 148 | if(strcmp(root->children[0]->children[0]->typevalue, "int") == 0) 149 | { 150 | nodetype->u.basic = 0; 151 | } 152 | if(strcmp(root->children[0]->children[0]->typevalue, "float") == 0) 153 | { 154 | nodetype->u.basic = 1; 155 | } 156 | } 157 | if(strcmp(root->children[0]->children[0]->name, "StructSpecifier") == 0) 158 | { 159 | if(root->children[0]->children[0]->children[1] != NULL) 160 | { 161 | if(strcmp(root->children[0]->children[0]->children[1]->name, "Tag") == 0) 162 | { 163 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 164 | nodetype->kind = STRUCTURE; 165 | int check = checkrepeat(root->children[0]->children[0]->children[1]->children[0]->IDvalue,root->children[0]->children[0]->children[1]->children[0]->linenum); 166 | if(check == 0) 167 | { 168 | chainheader = Create_tablenode (root->children[0]->children[0]->children[1]->children[0]->IDvalue, nodetype, chainheader); 169 | } 170 | } 171 | if(strcmp(root->children[0]->children[0]->children[1]->name, "OptTag") == 0) 172 | { 173 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 174 | nodetype->kind = STRUCTURE; 175 | int check = checkrepeat(root->children[0]->children[0]->children[1]->children[0]->IDvalue,root->children[0]->children[0]->children[1]->children[0]->linenum); 176 | if(check == 0) 177 | { 178 | chainheader = Create_tablenode (root->children[0]->children[0]->children[1]->children[0]->IDvalue, nodetype, chainheader); 179 | } 180 | } 181 | } 182 | else 183 | { 184 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 185 | nodetype->kind = STRUCTURE; 186 | } 187 | } 188 | if(strcmp(root->children[1]->name, "ExtDecList") == 0) 189 | { 190 | node* tmp1 = root->children[1]; 191 | struct Variable array = getVarDec(tmp1->children[0],nodetype); 192 | int check = checkrepeat(array.name,array.line); 193 | if(check == 0) 194 | { 195 | chainheader = Create_tablenode (array.name, array.head, chainheader); 196 | } 197 | while(tmp1->numchild != 1) 198 | { 199 | tmp1 = tmp1->children[2]; 200 | struct Variable array1 = getVarDec(tmp1->children[0],nodetype); 201 | int check = checkrepeat(array1.name,array1.line); 202 | if(check == 0) 203 | { 204 | chainheader = Create_tablenode (array1.name, array1.head, chainheader); 205 | } 206 | } 207 | } 208 | else if(strcmp(root->children[1]->name, "FunDec") == 0) 209 | { 210 | Type funtype = (Type)malloc(sizeof(struct Type_)); 211 | funtype->kind = FUNC; 212 | int check = checkrepeat(root->children[1]->children[0]->IDvalue,root->children[1]->children[0]->linenum); 213 | if(check == 0) 214 | { 215 | chainheader = Create_tablenode (root->children[1]->children[0]->IDvalue, funtype, chainheader); 216 | } 217 | if(strcmp(root->children[1]->children[2]->name, "VarList") == 0) 218 | { 219 | node* tmp2 = root->children[1]->children[2]; 220 | dealparam(tmp2->children[0]); 221 | while(tmp2->numchild != 1) 222 | { 223 | tmp2 = tmp2->children[2]; 224 | dealparam(tmp2->children[0]); 225 | } 226 | } 227 | } 228 | else {} 229 | } 230 | 231 | else if(strcmp(root->name, "Def") == 0) 232 | { 233 | if(strcmp(root->children[0]->children[0]->name, "TYPE") == 0) 234 | { 235 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 236 | nodetype->kind = BASIC; 237 | if(strcmp(root->children[0]->children[0]->typevalue, "int") == 0) 238 | { 239 | nodetype->u.basic = 0; 240 | } 241 | if(strcmp(root->children[0]->children[0]->typevalue, "float") == 0) 242 | { 243 | nodetype->u.basic = 1; 244 | } 245 | } 246 | if(strcmp(root->children[0]->children[0]->name, "StructSpecifier") == 0) 247 | { 248 | if(root->children[0]->children[0]->children[1] != NULL) 249 | { 250 | if(strcmp(root->children[0]->children[0]->children[1]->name, "Tag") == 0) 251 | { 252 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 253 | nodetype->kind = STRUCTURE; 254 | int check = checkrepeat(root->children[0]->children[0]->children[1]->children[0]->IDvalue,root->children[0]->children[0]->children[1]->children[0]->linenum); 255 | if(check == 0) 256 | { 257 | chainheader = Create_tablenode (root->children[0]->children[0]->children[1]->children[0]->IDvalue, nodetype, chainheader); 258 | } 259 | } 260 | if(strcmp(root->children[0]->children[0]->children[1]->name, "OptTag") == 0) 261 | { 262 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 263 | nodetype->kind = STRUCTURE; 264 | int check = checkrepeat(root->children[0]->children[0]->children[1]->children[0]->IDvalue,root->children[0]->children[0]->children[1]->children[0]->linenum); 265 | if(check == 0) 266 | { 267 | chainheader = Create_tablenode (root->children[0]->children[0]->children[1]->children[0]->IDvalue, nodetype, chainheader); 268 | } 269 | } 270 | } 271 | else 272 | { 273 | Type nodetype = (Type)malloc(sizeof(struct Type_)); 274 | nodetype->kind = STRUCTURE; 275 | } 276 | } 277 | node* tmp2 = root->children[1]; 278 | struct Variable array = getVarDec(tmp2->children[0]->children[0],nodetype); 279 | int check = checkrepeat(array.name,array.line); 280 | if(check == 0) 281 | { 282 | chainheader = Create_tablenode (array.name, array.head, chainheader); 283 | } 284 | if(tmp2->children[0]->numchild != 1) 285 | { 286 | checkerror(tmp2->children[0]->children[2]); 287 | if(array.head->kind == BASIC && array.head->u.basic == 0 && tmp2->children[0]->children[2]->type == 0) 288 | {} 289 | else if(array.head->kind == BASIC && array.head->u.basic == 1 && tmp2->children[0]->children[2]->type == 1) 290 | {} 291 | else 292 | { 293 | if(tmp2->children[0]->children[2]->type != 10) 294 | { 295 | printf("Error type 5 at Line %d\n", array.line); 296 | } 297 | } 298 | } 299 | while(tmp2->numchild != 1) 300 | { 301 | tmp2 = tmp2->children[2]; 302 | struct Variable array1 = getVarDec(tmp2->children[0]->children[0],nodetype); 303 | int check = checkrepeat(array1.name,array1.line); 304 | if(check == 0) 305 | { 306 | chainheader = Create_tablenode (array1.name, array1.head, chainheader); 307 | } 308 | if(tmp2->children[0]->numchild != 1) 309 | { 310 | checkerror(tmp2->children[0]->children[2]); 311 | if(array.head->kind == BASIC && array.head->u.basic == 0 && tmp2->children[0]->children[2]->type == 0) 312 | {} 313 | else if(array.head->kind == BASIC && array.head->u.basic == 1 && tmp2->children[0]->children[2]->type == 1) 314 | {} 315 | else 316 | { 317 | if(tmp2->children[0]->children[2]->type != 10) 318 | { 319 | printf("Error type 5 at Line %d\n", array1.line); 320 | } 321 | } 322 | } 323 | } 324 | } 325 | 326 | else if(strcmp(root->name, "Exp") == 0) 327 | { 328 | if(root->numchild == 3) 329 | { 330 | if(strcmp(root->children[1]->name, "PLUS") == 0) 331 | { 332 | checkerror(root->children[0]); 333 | checkerror(root->children[2]); 334 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 335 | { 336 | root->type = 0; 337 | root->canleft = 0; 338 | } 339 | else if(root->children[0]->type == 1 && root->children[2]->type == 1) 340 | { 341 | root->type = 1; 342 | root->canleft = 0; 343 | } 344 | else 345 | { 346 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 347 | { 348 | printf("Error type 7 at Line %d\n", root->children[0]->linenum); 349 | } 350 | root->type = 10; 351 | } 352 | 353 | } 354 | if(strcmp(root->children[1]->name, "MINUS") == 0) 355 | { 356 | checkerror(root->children[0]); 357 | checkerror(root->children[2]); 358 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 359 | { 360 | root->type = 0; 361 | root->canleft = 0; 362 | } 363 | else if(root->children[0]->type == 1 && root->children[2]->type == 1) 364 | { 365 | root->type = 1; 366 | root->canleft = 0; 367 | } 368 | else 369 | { 370 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 371 | { 372 | printf("Error type 7 at Line %d\n", root->children[0]->linenum); 373 | } 374 | root->type = 10; 375 | } 376 | } 377 | if(strcmp(root->children[1]->name, "STAR") == 0) 378 | { 379 | checkerror(root->children[0]); 380 | checkerror(root->children[2]); 381 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 382 | { 383 | root->type = 0; 384 | root->canleft = 0; 385 | } 386 | else if(root->children[0]->type == 1 && root->children[2]->type == 1) 387 | { 388 | root->type = 1; 389 | root->canleft = 0; 390 | } 391 | else 392 | { 393 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 394 | { 395 | printf("Error type 7 at Line %d\n", root->children[0]->linenum); 396 | } 397 | root->type = 10; 398 | } 399 | } 400 | if(strcmp(root->children[1]->name, "DIV") == 0) 401 | { 402 | checkerror(root->children[0]); 403 | checkerror(root->children[2]); 404 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 405 | { 406 | root->type = 0; 407 | root->canleft = 0; 408 | } 409 | else if(root->children[0]->type == 1 && root->children[2]->type == 1) 410 | { 411 | root->type = 1; 412 | root->canleft = 0; 413 | } 414 | else 415 | { 416 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 417 | { 418 | printf("Error type 7 at Line %d\n", root->children[0]->linenum); 419 | } 420 | root->type = 10; 421 | } 422 | } 423 | if(strcmp(root->children[1]->name, "AND") == 0) 424 | { 425 | checkerror(root->children[0]); 426 | checkerror(root->children[2]); 427 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 428 | { 429 | root->type = 0; 430 | root->canleft = 0; 431 | } 432 | else 433 | { 434 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 435 | { 436 | printf("Error type 7 at Line %d\n", root->children[0]->linenum); 437 | } 438 | root->type = 10; 439 | } 440 | } 441 | if(strcmp(root->children[1]->name, "OR") == 0) 442 | { 443 | checkerror(root->children[0]); 444 | checkerror(root->children[2]); 445 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 446 | { 447 | root->type = 0; 448 | root->canleft = 0; 449 | } 450 | else 451 | { 452 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 453 | { 454 | printf("Error type 7 at Line %d\n", root->children[0]->linenum); 455 | } 456 | root->type = 10; 457 | } 458 | } 459 | if(strcmp(root->children[1]->name, "RELOP") == 0) 460 | { 461 | checkerror(root->children[0]); 462 | checkerror(root->children[2]); 463 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 464 | { 465 | root->type = 0; 466 | root->canleft = 0; 467 | } 468 | else 469 | { 470 | if(root->children[0]->type != 10 && root->children[0]->type != 10) 471 | { 472 | printf("Error type 7 at Line %d\n", root->children[0]->linenum); 473 | } 474 | root->type = 10; 475 | } 476 | } 477 | if(strcmp(root->children[1]->name, "ASSIGNOP") == 0) 478 | { 479 | checkerror(root->children[0]); 480 | checkerror(root->children[2]); 481 | if(root->children[0]->type == 0 && root->children[2]->type == 0) 482 | { 483 | if(root->children[0]->canleft == 1) 484 | { 485 | root->type = 0; 486 | root->canleft = 0; 487 | } 488 | else 489 | { 490 | printf("Error type 6 at Line %d\n", root->children[0]->linenum); 491 | root->type = 10; 492 | } 493 | } 494 | if(root->children[0]->type == 1 && root->children[2]->type == 1) 495 | { 496 | if(root->children[0]->canleft == 1) 497 | { 498 | root->type = 1; 499 | root->canleft = 0; 500 | } 501 | else 502 | { 503 | printf("Error type 6 at Line %d\n", root->children[0]->linenum); 504 | root->type = 10; 505 | } 506 | } 507 | if(root->children[0]->type == 2 && root->children[2]->type == 2) 508 | { 509 | if(root->children[0]->dim == root->children[2]->dim && root->children[0]->arraytype ==root->children[2]->arraytype) 510 | { 511 | if(root->children[0]->canleft == 1) 512 | { 513 | root->type = 2; 514 | root->dim = root->children[0]->dim; 515 | root->arraytype = root->children[0]->arraytype; 516 | root->canleft = 0; 517 | } 518 | else 519 | { 520 | printf("Error type 6 at Line %d\n", root->children[0]->linenum); 521 | root->type = 10; 522 | } 523 | } 524 | else 525 | { 526 | printf("Error type 5 at Line %d\n", root->children[0]->linenum); 527 | root->type = 10; 528 | } 529 | } 530 | else 531 | { 532 | if(root->children[0]->type != 10 && root->children[2]->type != 10) 533 | { 534 | printf("Error type 5 at Line %d\n", root->children[0]->linenum); 535 | } 536 | root->type = 10; 537 | } 538 | } 539 | if(strcmp(root->children[1]->name, "Exp") == 0) 540 | { 541 | checkerror(root->children[1]); 542 | if(root->children[1]->type == 2) 543 | { 544 | root->type = 2; 545 | root->dim = root->children[0]->dim; 546 | root->arraytype = root->children[0]->arraytype; 547 | } 548 | else 549 | { 550 | root->type = root->children[1]->type; 551 | } 552 | root->canleft = 0; 553 | } 554 | } 555 | if(root->numchild == 2) 556 | { 557 | if(strcmp(root->children[0]->name, "MINUS") == 0) 558 | { 559 | checkerror(root->children[1]); 560 | if(root->children[1]->type == 0 || root->children[1]->type == 1) 561 | { 562 | root->type = root->children[1]->type; 563 | root->canleft = 0; 564 | } 565 | else 566 | { 567 | if(root->children[1]->type != 10) 568 | { 569 | printf("Error type 7 at Line %d\n", root->children[1]->linenum); 570 | } 571 | root->type = 10; 572 | } 573 | 574 | } 575 | if(strcmp(root->children[1]->name, "NOT") == 0) 576 | { 577 | checkerror(root->children[1]); 578 | if(root->children[1]->type == 0) 579 | { 580 | root->type = root->children[1]->type; 581 | root->canleft = 0; 582 | } 583 | else 584 | { 585 | if(root->children[1]->type != 10) 586 | { 587 | printf("Error type 7 at Line %d\n", root->children[1]->linenum); 588 | } 589 | root->type = 10; 590 | } 591 | } 592 | } 593 | if(root->numchild == 4) 594 | { 595 | if(strcmp(root->children[0]->name, "Exp") == 0) 596 | { 597 | checkerror(root->children[0]); 598 | checkerror(root->children[2]); 599 | if(root->children[0]->type == 2) 600 | { 601 | if(root->children[2]->type == 0) 602 | { 603 | int dimtmp = root->->children[0]->dim-1; 604 | if(dimtmp != 0) 605 | { 606 | root->type =2; 607 | root->dim = dimtmp; 608 | root->arraytype = root->children[0]->arraytype; 609 | } 610 | else 611 | { 612 | if(root->children[0]->arraytype == 0) 613 | { 614 | root->type =0; 615 | } 616 | if(root->children[0]->arraytype == 1) 617 | { 618 | root->type =1; 619 | } 620 | } 621 | root->canleft = 1; 622 | } 623 | else 624 | { 625 | if(root->children[0]->type != 10) 626 | { 627 | printf("Error type 12 at Line %d\n", root->children[2]->linenum); 628 | } 629 | root->type = 10; 630 | } 631 | } 632 | else 633 | { 634 | if(root->children[0]->type != 10) 635 | { 636 | printf("Error type 10 at Line %d\n", root->children[1]->linenum); 637 | } 638 | root->type = 10; 639 | } 640 | } 641 | } 642 | if(root->numchild == 1) 643 | { 644 | if(strcmp(root->children[0]->name, "ID") == 0) 645 | { 646 | tablenode* tmp = chainheader; 647 | while(tmp!=NULL) 648 | { 649 | if(strcmp(tmp->symname, root->children[0]->IDvalue) == 0) 650 | { 651 | if(tmp->symtype->kind == BASIC) 652 | { 653 | if(tmp->symtype->u.basic == 0) 654 | { 655 | root->type = 0; 656 | } 657 | if(tmp->symtype->u.basic == 1) 658 | { 659 | root->type = 1; 660 | } 661 | } 662 | if(tmp->symtype->kind == ARRAY) 663 | { 664 | int dim=1; 665 | root->type = 2; 666 | Type piao = tmp->symtype; 667 | while(piao->u.array.elem->kind != BASIC) 668 | { 669 | dim++; 670 | piao = piao->u.array.elem; 671 | } 672 | if(piao->u.array.elem->u.basic == 0) 673 | { 674 | root->arraytype = 0; 675 | } 676 | if(piao->u.array.elem->u.basic == 1) 677 | { 678 | root->arraytype = 1; 679 | } 680 | root->dim = dim; 681 | } 682 | if(tmp->symtype->kind == STRUCTURE) 683 | { 684 | root->type = 3; 685 | } 686 | if(tmp->symtype->kind == FUNC) 687 | { 688 | root->type = 4; 689 | } 690 | root->canleft = 1; 691 | break; 692 | } 693 | tmp = tmp->next; 694 | } 695 | if(tmp == NULL) 696 | { 697 | root->type = 10; 698 | printf("Error type 1 at Line %d\n", root->children[0]->linenum); 699 | } 700 | } 701 | if(strcmp(root->children[0]->name, "INT") == 0) 702 | { 703 | root->type = 0; 704 | root->canleft = 0; 705 | } 706 | if(strcmp(root->children[0]->name, "FLOAT") == 0) 707 | { 708 | root->type = 1; 709 | root->canleft = 0; 710 | } 711 | } 712 | 713 | 714 | } 715 | 716 | 717 | 718 | 719 | } 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/syntax.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 3.5.1. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2020 Free Software Foundation, 6 | Inc. 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . */ 20 | 21 | /* As a special exception, you may create a larger work that contains 22 | part or all of the Bison parser skeleton and distribute that work 23 | under terms of your choice, so long as that work isn't itself a 24 | parser generator using the skeleton or a modified version thereof 25 | as a parser skeleton. Alternatively, if you modify or redistribute 26 | the parser skeleton itself, you may (at your option) remove this 27 | special exception, which will cause the skeleton and the resulting 28 | Bison output files to be licensed under the GNU General Public 29 | License without this special exception. 30 | 31 | This special exception was added by the Free Software Foundation in 32 | version 2.2 of Bison. */ 33 | 34 | /* Undocumented macros, especially those whose name start with YY_, 35 | are private implementation details. Do not rely on them. */ 36 | 37 | #ifndef YY_YY_SYNTAX_TAB_H_INCLUDED 38 | # define YY_YY_SYNTAX_TAB_H_INCLUDED 39 | /* Debug traces. */ 40 | #ifndef YYDEBUG 41 | # define YYDEBUG 1 42 | #endif 43 | #if YYDEBUG 44 | extern int yydebug; 45 | #endif 46 | 47 | /* Token type. */ 48 | #ifndef YYTOKENTYPE 49 | # define YYTOKENTYPE 50 | enum yytokentype 51 | { 52 | INT = 258, 53 | FLOAT = 259, 54 | ID = 260, 55 | SEMI = 261, 56 | COMMA = 262, 57 | ASSIGNOP = 263, 58 | RELOP = 264, 59 | PLUS = 265, 60 | MINUS = 266, 61 | STAR = 267, 62 | DIV = 268, 63 | AND = 269, 64 | OR = 270, 65 | DOT = 271, 66 | NOT = 272, 67 | TYPE = 273, 68 | LP = 274, 69 | RP = 275, 70 | LB = 276, 71 | RB = 277, 72 | LC = 278, 73 | RC = 279, 74 | STRUCT = 280, 75 | RETURN = 281, 76 | IF = 282, 77 | ELSE = 283, 78 | WHILE = 284, 79 | LOWER_THAN_ELSE = 285 80 | }; 81 | #endif 82 | 83 | /* Value type. */ 84 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 85 | union YYSTYPE 86 | { 87 | #line 13 "syntax.y" 88 | 89 | node* type_node; 90 | 91 | #line 92 "syntax.tab.h" 92 | 93 | }; 94 | typedef union YYSTYPE YYSTYPE; 95 | # define YYSTYPE_IS_TRIVIAL 1 96 | # define YYSTYPE_IS_DECLARED 1 97 | #endif 98 | 99 | 100 | extern YYSTYPE yylval; 101 | 102 | int yyparse (void); 103 | 104 | #endif /* !YY_YY_SYNTAX_TAB_H_INCLUDED */ 105 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/syntax.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include "lex.yy.c" 4 | #include "multitree.h" 5 | #include 6 | #include "semantic.h" 7 | #include "intercodegenerate.h" 8 | int syn_error = 0; 9 | extern FILE* out; 10 | extern tablenode* chainheader; 11 | %} 12 | 13 | %union { 14 | node* type_node; 15 | } 16 | 17 | %token INT 18 | %token FLOAT 19 | %token ID 20 | %token SEMI 21 | %token COMMA 22 | %token ASSIGNOP 23 | %token RELOP 24 | %token PLUS 25 | %token MINUS 26 | %token STAR 27 | %token DIV 28 | %token AND 29 | %token OR 30 | %token DOT 31 | %token NOT 32 | %token TYPE 33 | %token LP 34 | %token RP 35 | %token LB 36 | %token RB 37 | %token LC 38 | %token RC 39 | %token STRUCT 40 | %token RETURN 41 | %token IF 42 | %token ELSE 43 | %token WHILE 44 | %type Program ExtDefList ExtDef ExtDecList Specifier StructSpecifier OptTag Tag 45 | %type VarDec FunDec VarList ParamDec CompSt StmtList Stmt DefList Def DecList Dec Exp Args 46 | %right ASSIGNOP 47 | %left OR 48 | %left AND 49 | %left RELOP 50 | %left PLUS MINUS 51 | %left STAR DIV 52 | %right NOT 53 | %left DOT LP RP LB RB 54 | %nonassoc LOWER_THAN_ELSE 55 | %nonassoc ELSE 56 | 57 | 58 | %% 59 | Program : ExtDefList { 60 | node** temp = (node**)malloc(sizeof(node*)*1); 61 | temp[0] = $1; 62 | $$ = Create_new_node( "Program" , 1 , temp , $1->linenum); 63 | node* root = $$; 64 | if(syn_error == 0 && lex_error == 0) 65 | { 66 | Analysis (root); 67 | intergenerate(root, chainheader, out); 68 | } 69 | } 70 | ; 71 | 72 | ExtDefList : ExtDef ExtDefList { 73 | node** temp = (node**)malloc(sizeof(node*)*2); 74 | temp[0] = $1; 75 | temp[1] = $2; 76 | $$ = Create_new_node( "ExtDefList" , 2 , temp , $1->linenum); 77 | } 78 | | { $$ = NULL;} 79 | ; 80 | 81 | ExtDef : Specifier ExtDecList SEMI { 82 | node** temp = (node**)malloc(sizeof(node*)*3); 83 | temp[0] = $1; 84 | temp[1] = $2; 85 | temp[2] = $3; 86 | $$ = Create_new_node( "ExtDef" , 3 , temp , $1->linenum); 87 | } 88 | | Specifier SEMI { 89 | node** temp = (node**)malloc(sizeof(node*)*2); 90 | temp[0] = $1; 91 | temp[1] = $2; 92 | $$ = Create_new_node( "ExtDef" , 2 , temp , $1->linenum); 93 | } 94 | | Specifier FunDec CompSt { 95 | node** temp = (node**)malloc(sizeof(node*)*3); 96 | temp[0] = $1; 97 | temp[1] = $2; 98 | temp[2] = $3; 99 | $$ = Create_new_node( "ExtDef" , 3 , temp , $1->linenum); 100 | } 101 | ; 102 | 103 | ExtDecList : VarDec { 104 | node** temp = (node**)malloc(sizeof(node*)*1); 105 | temp[0] = $1; 106 | $$ = Create_new_node( "ExtDecList" , 1 , temp , $1->linenum); 107 | } 108 | | VarDec COMMA ExtDecList { 109 | node** temp = (node**)malloc(sizeof(node*)*3); 110 | temp[0] = $1; 111 | temp[1] = $2; 112 | temp[2] = $3; 113 | $$ = Create_new_node( "ExtDecList" , 3 , temp , $1->linenum); 114 | } 115 | //| error ExtDecList { syn_error = 1; } 116 | ; 117 | 118 | Specifier : TYPE { 119 | node** temp = (node**)malloc(sizeof(node*)*1); 120 | temp[0] = $1; 121 | $$ = Create_new_node( "Specifier" , 1 , temp , $1->linenum); 122 | } 123 | | StructSpecifier { 124 | node** temp = (node**)malloc(sizeof(node*)*1); 125 | temp[0] = $1; 126 | $$ = Create_new_node( "Specifier" , 1 , temp , $1->linenum); 127 | } 128 | | error { syn_error = 1; } 129 | ; 130 | 131 | StructSpecifier : STRUCT OptTag LC DefList RC { 132 | node** temp = (node**)malloc(sizeof(node*)*5); 133 | temp[0] = $1; 134 | temp[1] = $2; 135 | temp[2] = $3; 136 | temp[3] = $4; 137 | temp[4] = $5; 138 | $$ = Create_new_node( "StructSpecifier" , 5 , temp , $1->linenum); 139 | } 140 | | STRUCT Tag { 141 | node** temp = (node**)malloc(sizeof(node*)*2); 142 | temp[0] = $1; 143 | temp[1] = $2; 144 | $$ = Create_new_node( "StructSpecifier" , 2 , temp , $1->linenum); 145 | } 146 | ; 147 | 148 | OptTag : ID { node** temp = (node**)malloc(sizeof(node*)*1); 149 | temp[0] = $1; 150 | $$ = Create_new_node( "OptTag" , 1 , temp , $1->linenum); 151 | } 152 | | { $$ = NULL;} 153 | ; 154 | 155 | Tag : ID { node** temp = (node**)malloc(sizeof(node*)*1); 156 | temp[0] = $1; 157 | $$ = Create_new_node( "Tag" , 1 , temp , $1->linenum); 158 | } 159 | ; 160 | 161 | VarDec : ID { node** temp = (node**)malloc(sizeof(node*)*1); 162 | temp[0] = $1; 163 | $$ = Create_new_node( "VarDec" , 1 , temp , $1->linenum); 164 | } 165 | | VarDec LB INT RB { 166 | node** temp = (node**)malloc(sizeof(node*)*4); 167 | temp[0] = $1; 168 | temp[1] = $2; 169 | temp[2] = $3; 170 | temp[3] = $4; 171 | $$ = Create_new_node( "VarDec" , 4 , temp , $1->linenum); 172 | } 173 | ; 174 | 175 | FunDec : ID LP VarList RP { 176 | node** temp = (node**)malloc(sizeof(node*)*4); 177 | temp[0] = $1; 178 | temp[1] = $2; 179 | temp[2] = $3; 180 | temp[3] = $4; 181 | $$ = Create_new_node( "FunDec" , 4 , temp , $1->linenum); 182 | } 183 | | ID LP RP { 184 | node** temp = (node**)malloc(sizeof(node*)*3); 185 | temp[0] = $1; 186 | temp[1] = $2; 187 | temp[2] = $3; 188 | $$ = Create_new_node( "FunDec" , 3 , temp , $1->linenum); 189 | } 190 | | error RP { syn_error = 1; } 191 | | error { syn_error = 1; } 192 | ; 193 | 194 | VarList : ParamDec COMMA VarList { 195 | node** temp = (node**)malloc(sizeof(node*)*3); 196 | temp[0] = $1; 197 | temp[1] = $2; 198 | temp[2] = $3; 199 | $$ = Create_new_node( "VarList" , 3 , temp , $1->linenum); 200 | } 201 | | error VarList { syn_error = 1; } 202 | | ParamDec { 203 | node** temp = (node**)malloc(sizeof(node*)*1); 204 | temp[0] = $1; 205 | $$ = Create_new_node( "VarList" , 1 , temp , $1->linenum); 206 | } 207 | ; 208 | 209 | ParamDec : Specifier VarDec { 210 | node** temp = (node**)malloc(sizeof(node*)*2); 211 | temp[0] = $1; 212 | temp[1] = $2; 213 | $$ = Create_new_node( "ParamDec" , 2 , temp , $1->linenum); 214 | } 215 | ; 216 | 217 | CompSt : LC DefList StmtList RC { 218 | node** temp = (node**)malloc(sizeof(node*)*4); 219 | temp[0] = $1; 220 | temp[1] = $2; 221 | temp[2] = $3; 222 | temp[3] = $4; 223 | $$ = Create_new_node( "CompSt" , 4 , temp , $1->linenum); 224 | } 225 | //| error DefList StmtList RC { syn_error = 1; } 226 | //| error { syn_error = 1; } 227 | ; 228 | 229 | StmtList : Stmt StmtList { 230 | node** temp = (node**)malloc(sizeof(node*)*2); 231 | temp[0] = $1; 232 | temp[1] = $2; 233 | $$ = Create_new_node( "StmtList" , 2 , temp , $1->linenum); 234 | } 235 | | { $$ = NULL;} 236 | ; 237 | 238 | Stmt : Exp SEMI { 239 | node** temp = (node**)malloc(sizeof(node*)*2); 240 | temp[0] = $1; 241 | temp[1] = $2; 242 | $$ = Create_new_node( "Stmt" , 2 , temp , $1->linenum); 243 | } 244 | | CompSt { 245 | node** temp = (node**)malloc(sizeof(node*)*1); 246 | temp[0] = $1; 247 | $$ = Create_new_node( "Stmt" , 1 , temp , $1->linenum); 248 | } 249 | | RETURN Exp SEMI { 250 | node** temp = (node**)malloc(sizeof(node*)*3); 251 | temp[0] = $1; 252 | temp[1] = $2; 253 | temp[2] = $3; 254 | $$ = Create_new_node( "Stmt" , 3 , temp , $1->linenum); 255 | } 256 | | IF LP Exp RP Stmt %prec LOWER_THAN_ELSE { 257 | node** temp = (node**)malloc(sizeof(node*)*5); 258 | temp[0] = $1; 259 | temp[1] = $2; 260 | temp[2] = $3; 261 | temp[3] = $4; 262 | temp[4] = $5; 263 | $$ = Create_new_node( "Stmt" , 5 , temp , $1->linenum); 264 | } 265 | | IF LP Exp RP Stmt ELSE Stmt { 266 | node** temp = (node**)malloc(sizeof(node*)*7); 267 | temp[0] = $1; 268 | temp[1] = $2; 269 | temp[2] = $3; 270 | temp[3] = $4; 271 | temp[4] = $5; 272 | temp[5] = $6; 273 | temp[6] = $7; 274 | $$ = Create_new_node( "Stmt" , 7 , temp , $1->linenum); 275 | } 276 | | WHILE LP Exp RP Stmt { 277 | node** temp = (node**)malloc(sizeof(node*)*5); 278 | temp[0] = $1; 279 | temp[1] = $2; 280 | temp[2] = $3; 281 | temp[3] = $4; 282 | temp[4] = $5; 283 | $$ = Create_new_node( "Stmt" , 5 , temp , $1->linenum); 284 | } 285 | | error SEMI { syn_error = 1; } 286 | ; 287 | 288 | DefList : Def DefList { 289 | node** temp = (node**)malloc(sizeof(node*)*2); 290 | temp[0] = $1; 291 | temp[1] = $2; 292 | $$ = Create_new_node( "DefList" , 2 , temp , $1->linenum); 293 | } 294 | | { $$ = NULL;} 295 | ; 296 | 297 | Def : Specifier DecList SEMI { 298 | node** temp = (node**)malloc(sizeof(node*)*3); 299 | temp[0] = $1; 300 | temp[1] = $2; 301 | temp[2] = $3; 302 | $$ = Create_new_node( "Def" , 3 , temp , $1->linenum); 303 | } 304 | | error SEMI {syn_error = 1;} 305 | ; 306 | 307 | DecList : Dec { 308 | node** temp = (node**)malloc(sizeof(node*)*1); 309 | temp[0] = $1; 310 | $$ = Create_new_node( "DecList" , 1 , temp , $1->linenum); 311 | } 312 | | Dec COMMA DecList { 313 | node** temp = (node**)malloc(sizeof(node*)*3); 314 | temp[0] = $1; 315 | temp[1] = $2; 316 | temp[2] = $3; 317 | $$ = Create_new_node( "DecList" , 3 , temp , $1->linenum); 318 | } 319 | ; 320 | 321 | Dec : VarDec { 322 | node** temp = (node**)malloc(sizeof(node*)*1); 323 | temp[0] = $1; 324 | $$ = Create_new_node( "Dec" , 1 , temp , $1->linenum); 325 | } 326 | | VarDec ASSIGNOP Exp { 327 | node** temp = (node**)malloc(sizeof(node*)*3); 328 | temp[0] = $1; 329 | temp[1] = $2; 330 | temp[2] = $3; 331 | $$ = Create_new_node( "Dec" , 3 , temp , $1->linenum); 332 | } 333 | ; 334 | 335 | Exp : Exp ASSIGNOP Exp { 336 | node** temp = (node**)malloc(sizeof(node*)*3); 337 | temp[0] = $1; 338 | temp[1] = $2; 339 | temp[2] = $3; 340 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 341 | } 342 | | Exp AND Exp { 343 | node** temp = (node**)malloc(sizeof(node*)*3); 344 | temp[0] = $1; 345 | temp[1] = $2; 346 | temp[2] = $3; 347 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 348 | } 349 | | Exp OR Exp { 350 | node** temp = (node**)malloc(sizeof(node*)*3); 351 | temp[0] = $1; 352 | temp[1] = $2; 353 | temp[2] = $3; 354 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 355 | } 356 | | Exp RELOP Exp { 357 | node** temp = (node**)malloc(sizeof(node*)*3); 358 | temp[0] = $1; 359 | temp[1] = $2; 360 | temp[2] = $3; 361 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 362 | } 363 | | Exp PLUS Exp { 364 | node** temp = (node**)malloc(sizeof(node*)*3); 365 | temp[0] = $1; 366 | temp[1] = $2; 367 | temp[2] = $3; 368 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 369 | } 370 | | Exp MINUS Exp { 371 | node** temp = (node**)malloc(sizeof(node*)*3); 372 | temp[0] = $1; 373 | temp[1] = $2; 374 | temp[2] = $3; 375 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 376 | } 377 | | Exp STAR Exp { 378 | node** temp = (node**)malloc(sizeof(node*)*3); 379 | temp[0] = $1; 380 | temp[1] = $2; 381 | temp[2] = $3; 382 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 383 | } 384 | | Exp DIV Exp { 385 | node** temp = (node**)malloc(sizeof(node*)*3); 386 | temp[0] = $1; 387 | temp[1] = $2; 388 | temp[2] = $3; 389 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 390 | } 391 | | LP Exp RP { 392 | node** temp = (node**)malloc(sizeof(node*)*3); 393 | temp[0] = $1; 394 | temp[1] = $2; 395 | temp[2] = $3; 396 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 397 | } 398 | | MINUS Exp { 399 | node** temp = (node**)malloc(sizeof(node*)*2); 400 | temp[0] = $1; 401 | temp[1] = $2; 402 | $$ = Create_new_node( "Exp" , 2 , temp , $1->linenum); 403 | } 404 | | NOT Exp { 405 | node** temp = (node**)malloc(sizeof(node*)*2); 406 | temp[0] = $1; 407 | temp[1] = $2; 408 | $$ = Create_new_node( "Exp" , 2 , temp , $1->linenum); 409 | } 410 | | ID LP Args RP { 411 | node** temp = (node**)malloc(sizeof(node*)*4); 412 | temp[0] = $1; 413 | temp[1] = $2; 414 | temp[2] = $3; 415 | temp[3] = $4; 416 | $$ = Create_new_node( "Exp" , 4 , temp , $1->linenum); 417 | } 418 | | ID LP RP { 419 | node** temp = (node**)malloc(sizeof(node*)*3); 420 | temp[0] = $1; 421 | temp[1] = $2; 422 | temp[2] = $3; 423 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 424 | } 425 | | Exp LB Exp RB { 426 | node** temp = (node**)malloc(sizeof(node*)*4); 427 | temp[0] = $1; 428 | temp[1] = $2; 429 | temp[2] = $3; 430 | temp[3] = $4; 431 | $$ = Create_new_node( "Exp" , 4 , temp , $1->linenum); 432 | } 433 | | Exp DOT ID { 434 | node** temp = (node**)malloc(sizeof(node*)*3); 435 | temp[0] = $1; 436 | temp[1] = $2; 437 | temp[2] = $3; 438 | $$ = Create_new_node( "Exp" , 3 , temp , $1->linenum); 439 | } 440 | | ID { 441 | node** temp = (node**)malloc(sizeof(node*)*1); 442 | temp[0] = $1; 443 | $$ = Create_new_node( "Exp" , 1 , temp , $1->linenum); 444 | } 445 | | INT { 446 | node** temp = (node**)malloc(sizeof(node*)*1); 447 | temp[0] = $1; 448 | $$ = Create_new_node( "Exp" , 1 , temp , $1->linenum); 449 | } 450 | | FLOAT { 451 | node** temp = (node**)malloc(sizeof(node*)*1); 452 | temp[0] = $1; 453 | $$ = Create_new_node( "Exp" , 1 , temp , $1->linenum); 454 | } 455 | ; 456 | Args : Exp COMMA Args { 457 | node** temp = (node**)malloc(sizeof(node*)*3); 458 | temp[0] = $1; 459 | temp[1] = $2; 460 | temp[2] = $3; 461 | $$ = Create_new_node( "Args" , 3 , temp , $1->linenum); 462 | } 463 | | Exp { 464 | node** temp = (node**)malloc(sizeof(node*)*1); 465 | temp[0] = $1; 466 | $$ = Create_new_node( "Args" , 1 , temp , $1->linenum); 467 | } 468 | ; 469 | %% 470 | int yyerror(char* msg) 471 | { 472 | syn_error = 1; 473 | fprintf(stderr, "Error type B at Line %d: %s\n", yylineno, msg); 474 | } 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/test1.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int n; 4 | n = read(); 5 | if (n > 0) write(1); 6 | else if (n < 0) write (-1); 7 | else write(0); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /编译系统实验/实验三/codes/test2.cmm: -------------------------------------------------------------------------------- 1 | int fact(int n) 2 | { 3 | if (n == 1) 4 | return n; 5 | else 6 | return (n * fact(n - 1)); 7 | } 8 | int main() 9 | { 10 | int m, result; 11 | m = read(); 12 | if (m > 1) 13 | result = fact(m); 14 | else 15 | result = 1; 16 | write(result); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /编译系统实验/实验三/编译原理实验指导书-中间代码生成.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验三/编译原理实验指导书-中间代码生成.pdf -------------------------------------------------------------------------------- /编译系统实验/实验三/编译系统实验三-1190201215-冯开来.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验三/编译系统实验三-1190201215-冯开来.docx -------------------------------------------------------------------------------- /编译系统实验/实验三/编译系统实验三-1190201215-冯开来.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验三/编译系统实验三-1190201215-冯开来.pdf -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/Makefile: -------------------------------------------------------------------------------- 1 | # GNU make手册:http://www.gnu.org/software/make/manual/make.html 2 | # ************ 遇到不明白的地方请google以及阅读手册 ************* 3 | 4 | # 编译器设定和编译选项 5 | CC = gcc 6 | FLEX = flex 7 | BISON = bison 8 | CFLAGS = -std=c99 9 | 10 | # 编译目标:src目录下的所有.c文件 11 | CFILES = $(shell find ./ -name "*.c") 12 | OBJS = $(CFILES:.c=.o) 13 | LFILE = $(shell find ./ -name "*.l") 14 | YFILE = $(shell find ./ -name "*.y") 15 | LFC = $(shell find ./ -name "*.l" | sed s/[^/]*\\.l/lex.yy.c/) 16 | YFC = $(shell find ./ -name "*.y" | sed s/[^/]*\\.y/syntax.tab.c/) 17 | LFO = $(LFC:.c=.o) 18 | YFO = $(YFC:.c=.o) 19 | 20 | parser: syntax $(filter-out $(LFO),$(OBJS)) 21 | $(CC) -o parser $(filter-out $(LFO),$(OBJS)) -lfl 22 | 23 | syntax: lexical syntax-c 24 | $(CC) -c $(YFC) -o $(YFO) 25 | 26 | lexical: $(LFILE) 27 | $(FLEX) -o $(LFC) $(LFILE) 28 | 29 | syntax-c: $(YFILE) 30 | $(BISON) -o $(YFC) -d -v $(YFILE) 31 | 32 | -include $(patsubst %.o, %.d, $(OBJS)) 33 | 34 | # 定义的一些伪目标 35 | .PHONY: clean test 36 | test: 37 | ./parser ../Test/test1.cmm 38 | clean: 39 | rm -f parser lex.yy.c syntax.tab.c syntax.tab.h syntax.output 40 | rm -f $(OBJS) $(OBJS:.o=.d) 41 | rm -f $(LFC) $(YFC) $(YFC:.c=.h) 42 | rm -f *~ 43 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/enum.h: -------------------------------------------------------------------------------- 1 | #ifndef ENUM_H 2 | #define ENUM_H 3 | 4 | // define node type 5 | typedef enum _nodeType { 6 | TOKEN_INT, 7 | TOKEN_FLOAT, 8 | TOKEN_ID, 9 | TOKEN_TYPE, 10 | // TOKEN_COMMA, 11 | // TOKEN_SEMI, 12 | // TOKEN_ASSIGNOP, 13 | // TOKEN_RELOP, 14 | // TOKEN_PLUS, 15 | // TOKEN_MINUS, 16 | TOKEN_OTHER, 17 | NOT_A_TOKEN 18 | 19 | } NodeType; 20 | 21 | typedef enum _kind { BASIC, ARRAY, STRUCTURE, FUNCTION } Kind; 22 | typedef enum _basicType { INT_TYPE, FLOAT_TYPE } BasicType; 23 | typedef enum _errorType { 24 | UNDEF_VAR = 1, // Undefined Variable 25 | UNDEF_FUNC, // Undefined Function 26 | REDEF_VAR, // Redefined Variable 27 | REDEF_FUNC, // Redefined Function 28 | TYPE_MISMATCH_ASSIGN, // Type mismatchedfor assignment. 29 | LEFT_VAR_ASSIGN, // The left-hand side of an assignment must be a variable. 30 | TYPE_MISMATCH_OP, // Type mismatched for operands. 31 | TYPE_MISMATCH_RETURN, // Type mismatched for return. 32 | FUNC_AGRC_MISMATCH, // Function is not applicable for arguments 33 | NOT_A_ARRAY, // Variable is not a Array 34 | NOT_A_FUNC, // Variable is not a Function 35 | NOT_A_INT, // Variable is not a Integer 36 | ILLEGAL_USE_DOT, // Illegal use of "." 37 | NONEXISTFIELD, // Non-existentfield 38 | REDEF_FEILD, // Redefined field 39 | DUPLICATED_NAME, // Duplicated name 40 | UNDEF_STRUCT // Undefined structure 41 | } ErrorType; 42 | 43 | #endif -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input1.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int i=0; 4 | j=i+1; 5 | } 6 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input10.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int i; 4 | i[0]; 5 | } 6 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input11.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int i; 4 | i(10); 5 | } 6 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input12.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int i[10]; 4 | i[1.5]=10; 5 | } 6 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input13.cmm: -------------------------------------------------------------------------------- 1 | struct Position 2 | { 3 | float x,y; 4 | }; 5 | 6 | int main() 7 | { 8 | int i; 9 | i.x; 10 | } 11 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input14.cmm: -------------------------------------------------------------------------------- 1 | struct Position 2 | { 3 | float x,y; 4 | }; 5 | 6 | int main() 7 | { 8 | struct Position p; 9 | if (p.n==3.7) 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input15.cmm: -------------------------------------------------------------------------------- 1 | struct Position 2 | { 3 | float x,y; 4 | int x; 5 | }; 6 | 7 | int main() 8 | { 9 | } 10 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input16.cmm: -------------------------------------------------------------------------------- 1 | struct Position 2 | { 3 | float x,y; 4 | }; 5 | 6 | struct Position 7 | { 8 | int y; 9 | }; 10 | int main() 11 | { 12 | } 13 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input17.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | struct Position pos; 4 | } 5 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input2.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int i=0; 4 | inc(i); 5 | } 6 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input3.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int i,j; 4 | int i; 5 | } 6 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input4.cmm: -------------------------------------------------------------------------------- 1 | int func(int i) 2 | { 3 | return i; 4 | } 5 | 6 | int func() 7 | { 8 | return 0; 9 | } 10 | 11 | int main() 12 | { 13 | } 14 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input5.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int i; 4 | i=3.7; 5 | } 6 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input6.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int i; 4 | 10=i; 5 | } 6 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input7.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | float j; 4 | 10+j; 5 | } 6 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input8.cmm: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | float j=1.7; 4 | return j; 5 | } 6 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/input9.cmm: -------------------------------------------------------------------------------- 1 | int func(int i) 2 | { 3 | return i; 4 | } 5 | 6 | int main() 7 | { 8 | func(1,2); 9 | } 10 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/lexical.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include"node.h" 3 | #include "syntax.tab.h" 4 | 5 | extern boolean lexError; 6 | int lines = 0; 7 | int yycolumn = 1; 8 | 9 | #define YY_USER_ACTION \ 10 | yylloc.first_line=yylloc.last_line=yylineno; \ 11 | yylloc.first_column=yycolumn; \ 12 | yylloc.last_column=yycolumn+yyleng-1; \ 13 | yycolumn+=yyleng; 14 | %} 15 | 16 | %option yylineno 17 | 18 | ws [ \t] 19 | digit [0-9] 20 | letter [_a-zA-Z] 21 | ld[0-9_a-zA-Z] 22 | nonletter [^_a-zA-Z] 23 | RELOP >|<|>=|<=|==|!= 24 | IF if 25 | ELSE else 26 | WHILE while 27 | TYPE int|float 28 | STRUCT struct 29 | RETURN return 30 | PLUS "+" 31 | MINUS "-" 32 | STAR "*" 33 | DIV "/" 34 | AND "&&" 35 | OR "||" 36 | NOT "!" 37 | DOT "." 38 | SEMI ";" 39 | COMMA "," 40 | ASSIGNOP "=" 41 | LP "(" 42 | RP ")" 43 | LB "[" 44 | RB "]" 45 | LC "{" 46 | RC "}" 47 | ID {letter}{ld}* 48 | INT 0|[1-9]+[0-9]* 49 | FLOAT {digit}+"."{digit}+|{digit}*"."{digit}+[eE][+-]?{digit}+|{digit}+"."{digit}*[eE][+-]?{digit}+ 50 | %% 51 | {ws}+ {;} 52 | \n|\r { yycolumn = 1; } 53 | {IF} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "IF", yytext); return IF; } 54 | {ELSE} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "ELSE", yytext); return ELSE; } 55 | {WHILE} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "WHILE", yytext); return WHILE; } 56 | {TYPE} { yylval.node = newTokenNode(yylineno, TOKEN_TYPE, "TYPE", yytext); return TYPE; } 57 | {STRUCT} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "STRUCT", yytext); return STRUCT; } 58 | {RETURN} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "RETURN", yytext); return RETURN; } 59 | {RELOP} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "RELOP", yytext); return RELOP; } 60 | 61 | {PLUS} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "PLUS", yytext); return PLUS; } 62 | {MINUS} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "MINUS", yytext); return MINUS; } 63 | {STAR} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "STAR", yytext); return STAR; } 64 | {DIV} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "DIV", yytext); return DIV; } 65 | {AND} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "AND", yytext); return AND; } 66 | {OR} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "OR", yytext); return OR; } 67 | {NOT} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "NOT", yytext); return NOT; } 68 | 69 | {DOT} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "DOT", yytext); return DOT; } 70 | {SEMI} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "SEMI", yytext); return SEMI; } 71 | {COMMA} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "COMMA", yytext); return COMMA; } 72 | {ASSIGNOP} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "ASSIGNOP", yytext); return ASSIGNOP; } 73 | 74 | {LP} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "LP", yytext); return LP; } 75 | {RP} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "RP", yytext); return RP; } 76 | {LB} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "LB", yytext); return LB; } 77 | {RB} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "RB", yytext); return RB; } 78 | {LC} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "LC", yytext); return LC; } 79 | {RC} { yylval.node = newTokenNode(yylineno, TOKEN_OTHER, "RC", yytext); return RC; } 80 | 81 | {ID} { yylval.node = newTokenNode(yylineno, TOKEN_ID, "ID", yytext); return ID;} 82 | {INT} { yylval.node = newTokenNode(yylineno, TOKEN_INT, "INT", yytext); return INT;} 83 | {FLOAT} { yylval.node = newTokenNode(yylineno, TOKEN_FLOAT, "FLOAT", yytext); return FLOAT;} 84 | 85 | {digit}+{ID} {lexError = TRUE; printf("Error type A at Line %d: Illegal ID \"%s\".\n", yylineno, yytext); } 86 | "."{digit}+ { lexError = TRUE; printf("Error type A at Line %d: Illegal floating point number \"%s\".\n", yylineno, yytext); } 87 | {digit}+"." { lexError = TRUE; printf("Error type A at Line %d: Illegal floating point number \"%s\".\n", yylineno, yytext); } 88 | {digit}*"."{digit}+[eE] { lexError = TRUE; printf("Error type A at Line %d: Illegal floating point number \"%s\".\n", yylineno, yytext); } 89 | {digit}+"."{digit}*[eE] { lexError = TRUE; printf("Error type A at Line %d: Illegal floating point number \"%s\".\n", yylineno, yytext); } 90 | {digit}+[eE][+-]?{digit}* { lexError = TRUE; printf("Error type A at Line %d: Illegal floating point number \"%s\".\n", yylineno, yytext); } 91 | "."[eE][+-]?{digit}+ { lexError = TRUE; printf("Error type A at Line %d: Illegal floating point number \"%s\".\n", yylineno, yytext); } 92 | . { lexError = TRUE; printf("Error type A at Line %d: Mysterious character \'%s\'.\n", yylineno, yytext); } 93 | %% 94 | 95 | // int main(int argc, char** argv) { 96 | // if (argc > 1) { 97 | // if (!(yyin = fopen(argv[1], "r"))) { 98 | // perror(argv[1]); 99 | // return 1; 100 | // } 101 | // } 102 | // yylex(); 103 | // printf("%d\n",lines); 104 | // return 0; 105 | // } 106 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/main.c: -------------------------------------------------------------------------------- 1 | #include "semantic.h" 2 | #include "syntax.tab.h" 3 | 4 | extern pNode root; 5 | 6 | extern int yylineno; 7 | extern int yyparse(); 8 | extern void yyrestart(FILE*); 9 | 10 | unsigned lexError = FALSE; 11 | unsigned synError = FALSE; 12 | 13 | int main(int argc, char** argv) { 14 | if (argc <= 1) { 15 | yyparse(); 16 | return 1; 17 | } 18 | 19 | FILE* f = fopen(argv[1], "r"); 20 | if (!f) { 21 | perror(argv[1]); 22 | return 1; 23 | } 24 | 25 | yyrestart(f); 26 | yyparse(); 27 | if (!lexError && !synError) { 28 | table = initTable(); 29 | // printTreeInfo(root, 0); 30 | traverseTree(root); 31 | deleteTable(table); 32 | } 33 | delNode(&root); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验二/lab2/Code/main.o -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/node.h: -------------------------------------------------------------------------------- 1 | #ifndef NODE_H 2 | #define NODE_H 3 | 4 | // #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "enum.h" 12 | 13 | // #define NAME_LENGTH 32 14 | // #define VAL_LENGTH 64 15 | 16 | #define TRUE 1 17 | #define FALSE 0 18 | 19 | // typedef uint_32 bool; 20 | 21 | // node type declared 22 | typedef struct node { 23 | int lineNo; // node in which line 24 | // int depth; // node depth, for count white space for print 25 | NodeType type; // node type 26 | char* name; // node name 27 | char* val; // node value 28 | 29 | struct node* child; // non-terminals node first child node 30 | struct node* next; // non-terminals node next brother node 31 | 32 | } Node; 33 | 34 | typedef unsigned boolean; 35 | typedef Node* pNode; 36 | 37 | // 38 | static inline char* newString(char* src) { 39 | if (src == NULL) return NULL; 40 | int length = strlen(src) + 1; 41 | char* p = (char*)malloc(sizeof(char) * length); 42 | assert(p != NULL); 43 | strncpy(p, src, length); 44 | return p; 45 | } 46 | 47 | static inline pNode newNode(int lineNo, NodeType type, char* name, int argc, 48 | ...) { 49 | pNode curNode = NULL; 50 | // int nameLength = strlen(name) + 1; 51 | 52 | curNode = (pNode)malloc(sizeof(Node)); 53 | 54 | assert(curNode != NULL); 55 | 56 | // curNode->name = (char*)malloc(sizeof(char) * NAME_LENGTH); 57 | // curNode->val = (char*)malloc(sizeof(char) * VAL_LENGTH); 58 | // curNode->name = (char*)malloc(sizeof(char) * nameLength); 59 | // assert(curNode->name != NULL); 60 | // // assert(curNode->val != NULL); 61 | 62 | curNode->lineNo = lineNo; 63 | curNode->type = type; 64 | curNode->name = newString(name); 65 | // strncpy(curNode->name, name, nameLength); 66 | 67 | va_list vaList; 68 | va_start(vaList, argc); 69 | 70 | pNode tempNode = va_arg(vaList, pNode); 71 | 72 | curNode->child = tempNode; 73 | 74 | for (int i = 1; i < argc; i++) { 75 | tempNode->next = va_arg(vaList, pNode); 76 | if (tempNode->next != NULL) { 77 | tempNode = tempNode->next; 78 | } 79 | } 80 | 81 | va_end(vaList); 82 | return curNode; 83 | } 84 | 85 | static inline pNode newTokenNode(int lineNo, NodeType type, char* tokenName, 86 | char* tokenText) { 87 | pNode tokenNode = (pNode)malloc(sizeof(Node)); 88 | // int nameLength = strlen(tokenName) + 1; 89 | // int textLength = strlen(tokenText) + 1; 90 | 91 | assert(tokenNode != NULL); 92 | 93 | tokenNode->lineNo = lineNo; 94 | tokenNode->type = type; 95 | 96 | // tokenNode->name = (char*)malloc(sizeof(char) * nameLength); 97 | // tokenNode->val = (char*)malloc(sizeof(char) * textLength); 98 | 99 | // assert(tokenNode->name != NULL); 100 | // assert(tokenNode->val != NULL); 101 | 102 | // strncpy(tokenNode->name, tokenName, nameLength); 103 | // strncpy(tokenNode->val, tokenText, textLength); 104 | 105 | tokenNode->name = newString(tokenName); 106 | tokenNode->val = newString(tokenText); 107 | 108 | tokenNode->child = NULL; 109 | tokenNode->next = NULL; 110 | 111 | return tokenNode; 112 | } 113 | 114 | static inline void delNode(pNode* node) { 115 | if (node == NULL) return; 116 | pNode p = *node; 117 | while (p->child != NULL) { 118 | pNode temp = p->child; 119 | p->child = p->child->next; 120 | delNode(&temp); 121 | } 122 | free(p->name); 123 | free(p->val); 124 | free(p); 125 | p = NULL; 126 | } 127 | 128 | static inline void printTreeInfo(pNode curNode, int height) { 129 | if (curNode == NULL) { 130 | return; 131 | } 132 | 133 | for (int i = 0; i < height; i++) { 134 | printf(" "); 135 | } 136 | printf("%s", curNode->name); 137 | if (curNode->type == NOT_A_TOKEN) { 138 | printf(" (%d)", curNode->lineNo); 139 | } else if (curNode->type == TOKEN_TYPE || curNode->type == TOKEN_ID || 140 | curNode->type == TOKEN_INT) { 141 | printf(": %s", curNode->val); 142 | } else if (curNode->type == TOKEN_FLOAT) { 143 | printf(": %lf", atof(curNode->val)); 144 | } 145 | printf("\n"); 146 | printTreeInfo(curNode->child, height + 1); 147 | printTreeInfo(curNode->next, height); 148 | } 149 | 150 | #endif -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/parser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验二/lab2/Code/parser -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/scanner: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验二/lab2/Code/scanner -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/semantic.h: -------------------------------------------------------------------------------- 1 | #ifndef SEMANTIC_H 2 | #define SEMENTIC_H 3 | 4 | #define HASH_TABLE_SIZE 0x3fff 5 | #define STACK_DEEP 6 | 7 | #include "node.h" 8 | 9 | typedef struct type* pType; 10 | typedef struct fieldList* pFieldList; 11 | typedef struct tableItem* pItem; 12 | typedef struct hashTable* pHash; 13 | typedef struct stack* pStack; 14 | typedef struct table* pTable; 15 | 16 | 17 | typedef struct fieldList { 18 | char* name; // 域的名字 19 | pType type; // 域的类型 20 | pFieldList tail; // 下一个域 21 | } FieldList; 22 | 23 | typedef struct type { 24 | Kind kind; 25 | // 确定数据存储的大小 26 | union { 27 | // 基本类型 28 | BasicType basic; 29 | // 数组:元素类型、大小 30 | struct { 31 | pType elem; 32 | int size; 33 | } array; 34 | // 结构体:链表 35 | struct { 36 | char* structName; 37 | pFieldList field; // 域名,类型,下一个 38 | } structure; 39 | // 函数 40 | struct { 41 | int argc; // argument counter 42 | pFieldList argv; // argument vector 43 | pType returnType; // returnType 44 | } function; 45 | } u; 46 | } Type; 47 | 48 | 49 | 50 | typedef struct tableItem { 51 | int symbolDepth; 52 | pFieldList field; 53 | pItem nextSymbol; // same depth next symbol, linked from stack 54 | pItem nextHash; // same hash code next symbol, linked from hash table 55 | } TableItem; 56 | 57 | 58 | typedef struct hashTable { 59 | pItem* hashArray; 60 | } HashTable; 61 | 62 | typedef struct stack { 63 | pItem* stackArray; 64 | int curStackDepth; // 栈高 65 | } Stack; 66 | 67 | typedef struct table { 68 | pHash hash; // 哈希表 69 | pStack stack; // 栈 70 | int unNamedStructNum; 71 | // int enterStructLayer; 72 | } Table; 73 | 74 | extern pTable table; 75 | 76 | 77 | // Type functions 78 | pType newType(Kind kind, ...); 79 | pType copyType(pType src); 80 | void deleteType(pType type); 81 | boolean checkType(pType type1, pType type2); 82 | void printType(pType type); 83 | 84 | // FieldList functions 85 | 86 | // inline pFieldList newFieldList() { 87 | // pFieldList p = (pFieldList)malloc(sizeof(FieldList)); 88 | // p->name = NULL; 89 | // p->type = NULL; 90 | // p->tail = NULL; 91 | // return p; 92 | // } 93 | 94 | pFieldList newFieldList(char* newName, pType newType); 95 | pFieldList copyFieldList(pFieldList src); 96 | void deleteFieldList(pFieldList fieldList); 97 | void setFieldListName(pFieldList p, char* newName); 98 | void printFieldList(pFieldList fieldList); 99 | 100 | // tableItem functions 101 | 102 | // inline pItem newItem() { 103 | // pItem p = (pItem)malloc(sizeof(TableItem)); 104 | // p->symbolDepth = 0; 105 | // p->field = NULL; 106 | // p->nextHash = NULL; 107 | // p->nextSymbol = NULL; 108 | // return p; 109 | // } 110 | 111 | pItem newItem(int symbolDepth, pFieldList pfield); 112 | void deleteItem(pItem item); 113 | boolean isStructDef(pItem src); 114 | 115 | // Hash functions 116 | pHash newHash(); 117 | void deleteHash(pHash hash); 118 | pItem getHashHead(pHash hash, int index); 119 | void setHashHead(pHash hash, int index, pItem newVal); 120 | 121 | // Stack functions 122 | pStack newStack(); 123 | void deleteStack(pStack stack); 124 | void addStackDepth(pStack stack); 125 | void minusStackDepth(pStack stack); 126 | pItem getCurDepthStackHead(pStack stack); 127 | void setCurDepthStackHead(pStack stack, pItem newVal); 128 | 129 | // Table functions 130 | pTable initTable(); 131 | void deleteTable(pTable table); 132 | pItem searchTableItem(pTable table, char* name); 133 | boolean checkTableItemConflict(pTable table, pItem item); 134 | void addTableItem(pTable table, pItem item); 135 | void deleteTableItem(pTable table, pItem item); 136 | void clearCurDepthStackList(pTable table); 137 | // void addStructLayer(pTable table); 138 | // void minusStructLayer(pTable table); 139 | // boolean isInStructLayer(pTable table); 140 | void printTable(pTable table); 141 | 142 | // Global functions 143 | static inline unsigned int getHashCode(char* name) { 144 | unsigned int val = 0, i; 145 | for (; *name; ++name) { 146 | val = (val << 2) + *name; 147 | if (i = val & ~HASH_TABLE_SIZE) 148 | val = (val ^ (i >> 12)) & HASH_TABLE_SIZE; 149 | } 150 | return val; 151 | } 152 | 153 | static inline void pError(ErrorType type, int line, char* msg) { 154 | printf("Error type %d at Line %d: %s\n", type, line, msg); 155 | } 156 | 157 | void traverseTree(pNode node); 158 | 159 | // Generate symbol table functions 160 | void ExtDef(pNode node); 161 | void ExtDecList(pNode node, pType specifier); 162 | pType Specifier(pNode node); 163 | pType StructSpecifier(pNode node); 164 | pItem VarDec(pNode node, pType specifier); 165 | void FunDec(pNode node, pType returnType); 166 | void VarList(pNode node, pItem func); 167 | pFieldList ParamDec(pNode node); 168 | void CompSt(pNode node, pType returnType); 169 | void StmtList(pNode node, pType returnType); 170 | void Stmt(pNode node, pType returnType); 171 | void DefList(pNode node, pItem structInfo); 172 | void Def(pNode node, pItem structInfo); 173 | void DecList(pNode node, pType specifier, pItem structInfo); 174 | void Dec(pNode node, pType specifier, pItem structInfo); 175 | pType Exp(pNode node); 176 | void Args(pNode node, pItem funcInfo); 177 | 178 | #endif -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/semantic.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验二/lab2/Code/semantic.o -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/syntax.tab.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验二/lab2/Code/syntax.tab.o -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/syntax.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include"node.h" 4 | #include"lex.yy.c" 5 | extern boolean synError; 6 | pNode root; 7 | #define YYERROR_VERBOSE 1 8 | 9 | %} 10 | 11 | // types 12 | 13 | %union{ 14 | pNode node; 15 | } 16 | 17 | // tokens 18 | 19 | %token INT 20 | %token FLOAT 21 | %token ID 22 | %token TYPE 23 | %token COMMA 24 | %token DOT 25 | %token SEMI 26 | %token RELOP 27 | %token ASSIGNOP 28 | %token PLUS MINUS STAR DIV 29 | %token AND OR NOT 30 | %token LP RP LB RB LC RC 31 | %token IF 32 | %token ELSE 33 | %token WHILE 34 | %token STRUCT 35 | %token RETURN 36 | 37 | // non-terminals 38 | 39 | %type Program ExtDefList ExtDef ExtDecList // High-level Definitions 40 | %type Specifier StructSpecifier OptTag Tag // Specifiers 41 | %type VarDec FunDec VarList ParamDec // Declarators 42 | %type CompSt StmtList Stmt // Statements 43 | %type DefList Def Dec DecList // Local Definitions 44 | %type Exp Args // Expressions 45 | 46 | // precedence and associativity 47 | 48 | %right ASSIGNOP 49 | %left OR 50 | %left AND 51 | %left RELOP 52 | %left PLUS MINUS 53 | %left STAR DIV 54 | %right NOT 55 | %left DOT 56 | %left LB RB 57 | %left LP RP 58 | %nonassoc LOWER_THAN_ELSE 59 | %nonassoc ELSE 60 | 61 | %% 62 | // High-level Definitions 63 | Program: ExtDefList { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Program", 1, $1); root = $$; } 64 | ; 65 | ExtDefList: ExtDef ExtDefList { $$ = newNode(@$.first_line, NOT_A_TOKEN, "ExtDefList", 2, $1, $2); } 66 | | { $$ = NULL; } 67 | ; 68 | ExtDef: Specifier ExtDecList SEMI { $$ = newNode(@$.first_line, NOT_A_TOKEN, "ExtDef", 3, $1, $2, $3); } 69 | | Specifier SEMI { $$ = newNode(@$.first_line, NOT_A_TOKEN, "ExtDef", 2, $1, $2); } 70 | | Specifier FunDec CompSt { $$ = newNode(@$.first_line, NOT_A_TOKEN, "ExtDef", 3, $1, $2, $3); } 71 | | error SEMI { synError = TRUE; } 72 | ; 73 | ExtDecList: VarDec { $$ = newNode(@$.first_line, NOT_A_TOKEN, "ExtDecList", 1, $1); } 74 | | VarDec COMMA ExtDecList { $$ = newNode(@$.first_line, NOT_A_TOKEN, "ExtDecList", 3, $1, $2, $3); } 75 | ; 76 | 77 | // Specifiers 78 | Specifier: TYPE { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Specifier", 1, $1); } 79 | | StructSpecifier { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Specifier", 1, $1); } 80 | ; 81 | StructSpecifier: STRUCT OptTag LC DefList RC { $$ = newNode(@$.first_line, NOT_A_TOKEN, "StructSpecifier", 5, $1, $2, $3, $4, $5); } 82 | | STRUCT Tag { $$ = newNode(@$.first_line, NOT_A_TOKEN, "StructSpecifier", 2, $1, $2); } 83 | ; 84 | OptTag: ID { $$ = newNode(@$.first_line, NOT_A_TOKEN, "OptTag", 1, $1); } 85 | | { $$ = NULL; } 86 | ; 87 | Tag: ID { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Tag", 1, $1); } 88 | ; 89 | 90 | // Declarators 91 | VarDec: ID { $$ = newNode(@$.first_line, NOT_A_TOKEN, "VarDec", 1, $1); } 92 | | VarDec LB INT RB { $$ = newNode(@$.first_line, NOT_A_TOKEN, "VarDec", 4, $1, $2, $3, $4); } 93 | | error RB { synError = TRUE; } 94 | ; 95 | FunDec: ID LP VarList RP { $$ = newNode(@$.first_line, NOT_A_TOKEN, "FunDec", 4, $1, $2, $3, $4); } 96 | | ID LP RP { $$ = newNode(@$.first_line, NOT_A_TOKEN, "FunDec", 3, $1, $2, $3); } 97 | | error RP { synError = TRUE; } 98 | ; 99 | VarList: ParamDec COMMA VarList { $$ = newNode(@$.first_line, NOT_A_TOKEN, "VarList", 3, $1, $2, $3); } 100 | | ParamDec { $$ = newNode(@$.first_line, NOT_A_TOKEN, "VarList", 1, $1); } 101 | ; 102 | ParamDec: Specifier VarDec { $$ = newNode(@$.first_line, NOT_A_TOKEN, "ParamDec", 2, $1, $2); } 103 | ; 104 | // Statements 105 | CompSt: LC DefList StmtList RC { $$ = newNode(@$.first_line, NOT_A_TOKEN, "CompSt", 4, $1, $2, $3, $4); } 106 | | error RC { synError = TRUE; } 107 | ; 108 | StmtList: Stmt StmtList { $$ = newNode(@$.first_line, NOT_A_TOKEN, "StmtList", 2, $1, $2); } 109 | | { $$ = NULL; } 110 | ; 111 | Stmt: Exp SEMI { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Stmt", 2, $1, $2); } 112 | | CompSt { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Stmt", 1, $1); } 113 | | RETURN Exp SEMI { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Stmt", 3, $1, $2, $3); } 114 | | IF LP Exp RP Stmt %prec LOWER_THAN_ELSE { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Stmt", 5, $1, $2, $3, $4, $5); } 115 | | IF LP Exp RP Stmt ELSE Stmt { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Stmt", 7, $1, $2, $3, $4, $5, $6, $7); } 116 | | WHILE LP Exp RP Stmt { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Stmt", 5, $1, $2, $3, $4, $5); } 117 | | error SEMI { synError = TRUE; } 118 | ; 119 | // Local Definitions 120 | DefList: Def DefList { $$ = newNode(@$.first_line, NOT_A_TOKEN, "DefList", 2, $1, $2); } 121 | | { $$ = NULL; } 122 | ; 123 | Def: Specifier DecList SEMI { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Def", 3, $1, $2, $3); } 124 | ; 125 | DecList: Dec { $$ = newNode(@$.first_line, NOT_A_TOKEN, "DecList", 1, $1); } 126 | | Dec COMMA DecList { $$ = newNode(@$.first_line, NOT_A_TOKEN, "DecList", 3, $1, $2, $3); } 127 | ; 128 | Dec: VarDec { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Dec", 1, $1); } 129 | | VarDec ASSIGNOP Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Dec", 3, $1, $2, $3); } 130 | ; 131 | //7.1.7 Expressions 132 | Exp: Exp ASSIGNOP Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 133 | | Exp AND Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 134 | | Exp OR Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 135 | | Exp RELOP Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 136 | | Exp PLUS Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 137 | | Exp MINUS Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 138 | | Exp STAR Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 139 | | Exp DIV Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 140 | | LP Exp RP { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 141 | | MINUS Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 2, $1, $2); } 142 | | NOT Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 2, $1, $2); } 143 | | ID LP Args RP { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 4, $1, $2, $3, $4); } 144 | | ID LP RP { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 145 | | Exp LB Exp RB { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 4, $1, $2, $3, $4); } 146 | | Exp DOT ID { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 3, $1, $2, $3); } 147 | | ID { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 1, $1); } 148 | | INT { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 1, $1); } 149 | | FLOAT { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Exp", 1, $1); } 150 | ; 151 | Args : Exp COMMA Args { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Args", 3, $1, $2, $3); } 152 | | Exp { $$ = newNode(@$.first_line, NOT_A_TOKEN, "Args", 1, $1); } 153 | ; 154 | %% 155 | 156 | int yyerror(char* msg){ 157 | fprintf(stderr, "Error type B at line %d: %s.\n", yylineno, msg); 158 | } -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Code/test.cmm: -------------------------------------------------------------------------------- 1 | struct FastFood { 2 | int yum; 3 | float price; 4 | }; 5 | 6 | struct Burger { 7 | int juice; 8 | float bucks; 9 | }; 10 | 11 | float buyAFood(float money) { 12 | struct FastFood cheap; 13 | struct Burger expensive; 14 | float realMoney, Burger; 15 | cheap.price = 1.3; 16 | cheap.yum = 3; 17 | expensive.bucks = 3.4; 18 | if(cheap.yum == 4) { 19 | realMoney = money - cheap.price; 20 | return realMoney; 21 | } else { 22 | Burger = money - expensive.bucks; 23 | return Burger; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Makefile: -------------------------------------------------------------------------------- 1 | objs = lex.yy.c scanner syntax.yy.c\ 2 | syntax.tab.c syntax.tab.h parser\ 3 | syntax.output 4 | 5 | cc: main.c lex.yy.c syntax.tab.c semantic.c 6 | gcc main.c syntax.tab.c semantic.c -lfl -ly -o cc 7 | 8 | scanner: main.c lex.yy.c 9 | gcc main.c lex.yy.c -lfl -o scanner 10 | 11 | lex.yy.c: lexical.l 12 | flex lexical.l 13 | 14 | syntax.tab.c: syntax.y 15 | bison -d -v syntax.y 16 | 17 | syntax.tab.h: syntax.y 18 | bison -d syntax.y 19 | 20 | .PHONY: clean 21 | clean: 22 | rm -rf $(objs) -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Tree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Tree.h" 6 | 7 | Tree createTerminal(int line, char* unit, char* src) 8 | { 9 | Tree t = (Tree)malloc(sizeof(struct TNode)); 10 | t->child = t->brother = NULL; 11 | t->line = line; 12 | t->flag = 1; 13 | strncpy(t->unit, unit, 35); 14 | if (!src) 15 | t->s[0] = '\0'; 16 | else 17 | strncpy(t->s, src, 35); 18 | return t; 19 | } 20 | 21 | Tree createNonTerminal(char* unit) 22 | { 23 | Tree t = (Tree)malloc(sizeof(struct TNode)); 24 | t->child = t->brother = NULL; 25 | t->flag = 0; 26 | strncpy(t->unit, unit, 35); 27 | return t; 28 | } 29 | 30 | Tree insertNode(Tree p, int num, ...) 31 | { 32 | va_list tlist; 33 | va_start(tlist, num); 34 | Tree tmp; 35 | for (int i = 0; i < num; ++i) 36 | { 37 | tmp = va_arg(tlist, Tree); 38 | if (!tmp) 39 | continue; 40 | tmp->brother = p->child; 41 | p->child = tmp; 42 | p->line = tmp->line; 43 | } 44 | va_end(tlist); 45 | return p; 46 | } 47 | 48 | void preTraversal(Tree head, int num) 49 | { 50 | if (!head) return ; 51 | for (int i = 0; i < num; ++i) 52 | printf(" "); 53 | if (head->flag) 54 | { 55 | printf("%s", head->unit); 56 | if (head->s[0] != '\0') 57 | printf(": %s", head->s); 58 | printf("\n"); 59 | } 60 | else 61 | { 62 | printf("%s (%d)\n", head->unit, head->line); 63 | } 64 | preTraversal(head->child, num + 2); 65 | preTraversal(head->brother, num); 66 | } 67 | 68 | // int main() 69 | // { 70 | // Tree p1 = createNonTerminal("1"); 71 | // Tree p2 = createTerminal(2, "2", "2"); 72 | // Tree p3 = createNonTerminal("3"); 73 | // Tree p4 = createNonTerminal("4"); 74 | // Tree p5 = createTerminal(3, "5", "5"); 75 | // Tree p6 = createTerminal(3, "6", NULL); 76 | // Tree p7 = createTerminal(3, "7", NULL); 77 | // p3 = insertNode(p3, 1, p5); 78 | // p4 = insertNode(p4, 2, p7, p6); 79 | // p1 = insertNode(p1, 3, p4, p3, p2); 80 | // preTraversal(p1, 0); 81 | // return 0; 82 | // } -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/Tree.h: -------------------------------------------------------------------------------- 1 | #ifndef __TREE_H 2 | #define __TREE_H 3 | 4 | typedef struct TNode* Tree; 5 | struct TNode 6 | { 7 | Tree child; 8 | Tree brother; 9 | int line; 10 | int flag; // 表示是否为终结符 11 | char unit[35]; // 语法/词法单元 12 | char s[35]; // 词法单元对应的信息 13 | }; 14 | 15 | Tree createTerminal(int line, char* unit, char* src); 16 | Tree createNonTerminal(char* unit); 17 | Tree insertNode(Tree p, int num, ...); 18 | void preTraversal(Tree head, int num); 19 | 20 | #endif -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/lexical.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "syntax.tab.h" 3 | #include "Tree.c" 4 | extern int hasError; 5 | 6 | int yycolumn = 1; 7 | #define YY_USER_ACTION \ 8 | yylloc.first_line = yylloc.last_line = yylineno; \ 9 | yylloc.first_column = yycolumn; \ 10 | yylloc.last_column = yycolumn + yyleng - 1; \ 11 | yycolumn += yyleng; 12 | %} 13 | 14 | %option yylineno 15 | 16 | INT 0|[1-9][0-9]* 17 | FLOAT {INT}(\.{INT})? 18 | ID [_a-zA-Z][_a-zA-Z0-9]* 19 | SEMI ; 20 | COMMA , 21 | ASSIGNOP = 22 | RELOP >|<|>=|<=|==|!= 23 | PLUS \+ 24 | MINUS - 25 | STAR \* 26 | DIV \/ 27 | AND && 28 | OR \|\| 29 | DOT \. 30 | NOT ! 31 | TYPE int|float 32 | LP \( 33 | RP \) 34 | LB \[ 35 | RB \] 36 | LC \{ 37 | RC \} 38 | STRUCT struct 39 | RETURN return 40 | IF if 41 | ELSE else 42 | WHILE while 43 | 44 | %% 45 | 46 | {INT} { yylval.node = createTerminal(yylineno, "INT", yytext); return INT; } 47 | {FLOAT} { yylval.node = createTerminal(yylineno, "FLOAT", yytext); return FLOAT; } 48 | {SEMI} { yylval.node = createTerminal(yylineno, "SEMI", NULL); return SEMI; } 49 | {COMMA} { yylval.node = createTerminal(yylineno, "COMMA", NULL); return COMMA; } 50 | {ASSIGNOP} { yylval.node = createTerminal(yylineno, "ASSIGNOP", NULL); return ASSIGNOP; } 51 | {RELOP} { yylval.node = createTerminal(yylineno, "RELOP", NULL); return RELOP; } 52 | {PLUS} { yylval.node = createTerminal(yylineno, "PLUS", NULL); return PLUS; } 53 | {MINUS} { yylval.node = createTerminal(yylineno, "MINUS", NULL); return MINUS; } 54 | {STAR} { yylval.node = createTerminal(yylineno, "STAR", NULL); return STAR; } 55 | {DIV} { yylval.node = createTerminal(yylineno, "DIV", NULL); return DIV; } 56 | {AND} { yylval.node = createTerminal(yylineno, "AND", NULL); return AND; } 57 | {OR} { yylval.node = createTerminal(yylineno, "OR", NULL); return OR; } 58 | {DOT} { yylval.node = createTerminal(yylineno, "DOT", NULL); return DOT; } 59 | {NOT} { yylval.node = createTerminal(yylineno, "NOT", NULL); return NOT; } 60 | {TYPE} { yylval.node = createTerminal(yylineno, "TYPE", yytext); return TYPE; } 61 | {LP} { yylval.node = createTerminal(yylineno, "LP", NULL); return LP; } 62 | {RP} { yylval.node = createTerminal(yylineno, "RP", NULL); return RP; } 63 | {LB} { yylval.node = createTerminal(yylineno, "LB", NULL); return LB; } 64 | {RB} { yylval.node = createTerminal(yylineno, "RB", NULL); return RB; } 65 | {LC} { yylval.node = createTerminal(yylineno, "LC", NULL); return LC; } 66 | {RC} { yylval.node = createTerminal(yylineno, "RC", NULL); return RC; } 67 | {STRUCT} { yylval.node = createTerminal(yylineno, "STRUCT", NULL); return STRUCT; } 68 | {RETURN} { yylval.node = createTerminal(yylineno, "RETURN", NULL); return RETURN; } 69 | {IF} { yylval.node = createTerminal(yylineno, "IF", NULL); return IF; } 70 | {ELSE} { yylval.node = createTerminal(yylineno, "ELSE", NULL); return ELSE; } 71 | {WHILE} { yylval.node = createTerminal(yylineno, "WHILE", NULL); return WHILE; } 72 | {ID} { yylval.node = createTerminal(yylineno, "ID", yytext); return ID; } 73 | [ \t] { } 74 | \n { yycolumn = 1; } 75 | . { hasError = 1; printf("Error type A at Line %d: Mysteriout characters \'%s\'\n", yylineno, yytext); } 76 | 77 | %% -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Tree.h" 3 | #include "semantic.h" 4 | 5 | Tree root; 6 | extern int yyparse(); 7 | extern void yyrestart(FILE*); 8 | 9 | int main(int argc, char **argv) 10 | { 11 | if (argc <= 1) return 1; 12 | FILE *f = fopen(argv[1], "r"); 13 | if (!f) 14 | { 15 | perror(argv[1]); 16 | return 1; 17 | } 18 | yyrestart(f); 19 | yyparse(); 20 | 21 | initSymbolTable(); 22 | semantic(root, 0); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/parser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验二/lab2/parser -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/semantic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "semantic.h" 6 | #include "Tree.h" 7 | 8 | FieldList head = NULL; 9 | 10 | void initSymbolTable() 11 | { 12 | head = createField(); 13 | } 14 | 15 | // 检查符号表是否有相同变量 16 | // 返回 1 表示可以向符号表添加符号 17 | int check(FieldList field) 18 | { 19 | // printf("in check\n"); 20 | FieldList p = head->tail; 21 | while (p) 22 | { 23 | if (!strcmp(p->name, field->name)) 24 | return 0; 25 | p = p->tail; 26 | } 27 | return 1; 28 | } 29 | 30 | // 查找符号 31 | FieldList search(char* name) 32 | { 33 | // printf("in search\n"); 34 | FieldList p = head->tail; 35 | while (p) 36 | { 37 | if (!strcmp(p->name, name)) 38 | return p; 39 | p = p->tail; 40 | } 41 | return NULL; 42 | } 43 | 44 | // 向符号表添加符号 45 | void addTable(FieldList field) 46 | { 47 | // printf("in addTable\n"); 48 | field->tail = head->tail; 49 | head->tail = field; 50 | } 51 | 52 | void printTable() 53 | { 54 | // printf("in printTable\n"); 55 | FieldList p = head->tail; 56 | while (p) 57 | { 58 | printf("symbol: %s\n", p->name); 59 | p = p->tail; 60 | } 61 | } 62 | 63 | // 创建类型 64 | Type createType(Kind kind, int num, ...) 65 | { 66 | Type t = (Type)malloc(sizeof(struct Type_)); 67 | t->kind = kind; 68 | va_list tlist; 69 | va_start(tlist, num); 70 | switch(kind) 71 | { 72 | case BASIC: 73 | t->u.basic = va_arg(tlist, Basic_type); 74 | // printf("创建基本类型: %d\n", t->u.basic); 75 | break; 76 | case ARRAY: 77 | t->u.array.size = va_arg(tlist, int); 78 | t->u.array.elem = va_arg(tlist, Type); 79 | // printf("创建数组类型\n"); 80 | break; 81 | } 82 | va_end(tlist); 83 | return t; 84 | } 85 | 86 | // 创建符号 87 | FieldList createField() 88 | { 89 | FieldList tmp = (FieldList)malloc(sizeof(struct FieldList_)); 90 | tmp->name = (char*)malloc(sizeof(35 * sizeof(char))); 91 | tmp->type = NULL; 92 | tmp->tail = NULL; 93 | return tmp; 94 | } 95 | 96 | // 释放符号 97 | void freeField(FieldList field) 98 | { 99 | // printf("in freeField\n"); 100 | free(field->name); 101 | while (field->tail) 102 | freeField(field->tail); 103 | free(field); 104 | } 105 | 106 | void ExtDef(Tree root) 107 | { 108 | // ExtDef -> Specifier ExtDecList SEMI 109 | // | Specifier SEMI 110 | // | Specifier FunDec CompSt 111 | // printf("in ExtDef\n"); 112 | 113 | FieldList tmp = (FieldList)malloc(sizeof(struct FieldList_)); 114 | tmp->name = (char*)malloc(sizeof(35 * sizeof(char))); 115 | 116 | Type specifier_type = Specifier(root->child); 117 | if (!strcmp(root->child->brother->unit, "ExtDecList")) // 定义基本类型变量 118 | ExtDecList(root->child->brother, specifier_type); 119 | 120 | return ; 121 | } 122 | 123 | // 解析类型 124 | Type Specifier(Tree root) 125 | { 126 | // printf("in Specifier\n"); 127 | if (!strcmp(root->child->unit, "TYPE")) 128 | { 129 | // printf("TYPE: %s\n", root->child->s); 130 | if (!strcmp(root->child->s, "int")) 131 | return createType(BASIC, 1, INT); 132 | else 133 | return createType(BASIC, 1, FLOAT); 134 | } 135 | else 136 | return NULL; 137 | } 138 | 139 | void ExtDecList(Tree root, Type type) 140 | { 141 | // ExtDecList -> VarDec 142 | // | VarDec COMMA ExtDecList 143 | // printf("in ExtDecList\n"); 144 | 145 | Tree p = root; 146 | while (p) 147 | { 148 | VarDec(p->child, type); 149 | 150 | // 同时定义多个变量 151 | if (p->child->brother) 152 | p = p->child->brother->brother; 153 | else 154 | break; 155 | } 156 | } 157 | 158 | void VarDec(Tree root, Type type) 159 | { 160 | // VarDec -> ID 161 | // | VarDec LB INT RB 162 | // printf("in VarDec\n"); 163 | 164 | Tree id = root; 165 | // 不断迭代找到变量名 166 | while (id->child) 167 | id = id->child; 168 | 169 | FieldList tmp = createField(); 170 | strcpy(tmp->name, id->s); 171 | if (!strcmp(root->child->unit, "ID")) 172 | {// 基本类型变量 173 | tmp->type = type; 174 | // printf("创建基本变量: %d %s\n", tmp->type->u.basic, tmp->name); 175 | } 176 | else 177 | {// 数组 178 | Tree vardec = root->child; 179 | while (vardec->brother) 180 | { 181 | Type array = createType(ARRAY, 2, atoi(vardec->brother->brother->s), 182 | type); 183 | type = array; 184 | vardec = vardec->child; 185 | } 186 | tmp->type = type; 187 | // printf("创建数组变量: %s\n", tmp->name); 188 | } 189 | 190 | if (!check(tmp)) 191 | { 192 | printf("Error type 3 at Line %d: Redefined variable \"%s\".\n", 193 | root->line, tmp->name); 194 | freeField(tmp); 195 | } 196 | else 197 | addTable(tmp); 198 | } 199 | 200 | void Def(Tree root) 201 | { 202 | // Def -> Specifier DecList SEMI 203 | // printf("in Def\n"); 204 | 205 | Type type = Specifier(root->child); 206 | DecList(root->child->brother, type); 207 | } 208 | 209 | void DecList(Tree root, Type type) 210 | { 211 | // DecList -> Dec 212 | // | Dec COMMA DecList 213 | // printf("in DecList\n"); 214 | 215 | Tree p = root; 216 | while (p) 217 | { 218 | Dec(p->child, type); 219 | 220 | // 同时定义多个变量 221 | if (p->child->brother) 222 | p = p->child->brother->brother; 223 | else 224 | break; 225 | } 226 | } 227 | 228 | void Dec(Tree root, Type type) 229 | { 230 | // Dec -> VarDec 231 | // | VarDec ASSIGNOP Exp 232 | // printf("in Dec\n"); 233 | 234 | VarDec(root->child, type); 235 | } 236 | 237 | Type Exp(Tree root) 238 | { 239 | // Exp -> Exp ASSIGNOP Exp 240 | // | Exp AND Exp 241 | // | Exp OR Exp 242 | // | Exp RELOP Exp 243 | // | Exp PLUS Exp 244 | // | Exp MINUS Exp 245 | // | Exp STAR Exp 246 | // | Exp DIV Exp 247 | // | LP Exp RP 248 | // | MINUS Exp 249 | // | NOT Exp 250 | // | ID LP Args RP 251 | // | ID LP RP 252 | // | Exp LB Exp RB 253 | // | Exp DOT ID 254 | // | ID 255 | // | INT 256 | // | FLOAT 257 | // printf("in Exp\n"); 258 | 259 | Tree p = root->child; 260 | if (!strcmp(p->unit, "INT")) 261 | return createType(BASIC, 1, INT); 262 | else if (!strcmp(p->unit, "FLOAT")) 263 | return createType(BASIC, 1, FLOAT); 264 | else if (!strcmp(p->unit, "ID")) 265 | { 266 | FieldList sym = search(p->s); 267 | if (!sym) 268 | printf("Error type 1 at Line %d: Undefined variable \"%s\".\n", 269 | p->line, p->s); 270 | else 271 | return sym->type; 272 | } 273 | // Exp -> Exp ASSIGNOP Exp 274 | // | Exp AND Exp 275 | // | Exp OR Exp 276 | // | Exp RELOP Exp 277 | // | Exp PLUS Exp 278 | // | Exp MINUS Exp 279 | // | Exp STAR Exp 280 | // | Exp DIV Exp 281 | // | Exp LB Exp RB 282 | // | Exp DOT ID 283 | else if (!strcmp(p->unit, "Exp")) 284 | { 285 | if (!strcmp(p->brother->unit, "LB")) 286 | {// 数组 287 | Type t1 = Exp(p); 288 | Type t2 = Exp(p->brother->brother); 289 | if (!t1 || !t2) 290 | return NULL; 291 | if (t1->kind != ARRAY) 292 | printf("Error type 10 at Line %d: \"%s\" is not an array.\n", 293 | p->line, p->child->s); 294 | else if (t2->kind != BASIC || t2->u.basic != INT) 295 | printf("Error type 12 at Line %d: \"%s\" is not an integer.\n", 296 | p->line, p->brother->brother->child->s); 297 | } 298 | else if (strcmp(p->brother->unit, "DOT")) 299 | {// 二元运算 300 | Type t1 = Exp(p); 301 | Type t2 = Exp(p->brother->brother); 302 | if (!t1 || !t2) 303 | return NULL; 304 | 305 | if (!strcmp(p->brother->unit, "ASSIGNOP")) 306 | { 307 | if (!strcmp(p->child->unit, "INT") || 308 | !strcmp(p->child->unit, "FLOAT")) 309 | printf("Error type 6 at Line %d: The left-hand side of an assignment must be a variable.\n", p->line); 310 | else if (!strcmp(p->child->unit, "ID")) 311 | { 312 | if (t1->u.basic != t2->u.basic) 313 | printf("Error type 5 at Line %d: Type mismatched for assignment.\n", 314 | p->line); 315 | } 316 | } 317 | else 318 | { 319 | if (t1->u.basic != t2->u.basic) 320 | printf("Error type 7 at Line %d: Type mismatched for operands.\n", 321 | p->line); 322 | } 323 | } 324 | } 325 | return NULL; 326 | } 327 | 328 | void semantic(Tree root, int num) 329 | { 330 | if (!root) return ; 331 | 332 | // 语法分析树 333 | // for (int i = 0; i < num; ++i) 334 | // printf(" "); 335 | // if (root->flag) 336 | // { 337 | // printf("%s", root->unit); 338 | // if (root->s[0] != '\0') 339 | // printf(": %s", root->s); 340 | // printf("\n"); 341 | // } 342 | // else 343 | // { 344 | // printf("%s (%d)\n", root->unit, root->line); 345 | // } 346 | 347 | if (!strcmp(root->unit, "ExtDef")) 348 | ExtDef(root); 349 | if (!strcmp(root->unit, "Def")) 350 | Def(root); 351 | if (!strcmp(root->unit, "Exp")) 352 | Exp(root); 353 | semantic(root->child, num + 2); 354 | semantic(root->brother, num); 355 | } 356 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/semantic.h: -------------------------------------------------------------------------------- 1 | #ifndef __SEMANTIC_H 2 | #define __SEMANTIC_H 3 | 4 | #include "Tree.h" 5 | 6 | typedef struct Type_* Type; 7 | typedef struct FieldList_* FieldList; 8 | typedef enum { BASIC, ARRAY, STRUCTURE} Kind; 9 | typedef enum {INT, FLOAT} Basic_type; 10 | 11 | struct Type_ 12 | { 13 | Kind kind; 14 | union 15 | { 16 | // 基本类型 17 | Basic_type basic; 18 | 19 | // 数组类型 20 | struct { Type elem; int size;} array; 21 | 22 | // 结构体类型 23 | FieldList structure; 24 | }u; 25 | }; 26 | 27 | struct FieldList_ 28 | { 29 | char* name; 30 | Type type; 31 | FieldList tail; 32 | }; 33 | 34 | void initSymbolTable(); 35 | int check(FieldList field); 36 | void addTable(FieldList field); 37 | FieldList search(char* name); 38 | void printTable(); 39 | Type createType(Kind kind, int num, ...); 40 | FieldList createField(); 41 | void freeField(FieldList field); 42 | void ExtDef(Tree root); 43 | Type Specifier(Tree root); 44 | void ExtDecList(Tree root, Type type); 45 | void VarDec(Tree root, Type type); 46 | void Def(Tree root); 47 | void DecList(Tree root, Type type); 48 | void Dec(Tree root, Type type); 49 | Type Exp(Tree root); 50 | 51 | void semantic(Tree root, int num); 52 | 53 | #endif -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/syntax.output: -------------------------------------------------------------------------------- 1 | 语法 2 | 3 | 0 $accept: Program $end 4 | 5 | 1 Program: ExtDefList 6 | 7 | 2 ExtDefList: ExtDef ExtDefList 8 | 3 | %empty 9 | 10 | 4 ExtDef: Specifier ExtDecList SEMI 11 | 5 | Specifier SEMI 12 | 6 | Specifier FunDec CompSt 13 | 7 | error SEMI 14 | 15 | 8 ExtDecList: VarDec 16 | 9 | VarDec COMMA ExtDecList 17 | 18 | 10 Specifier: TYPE 19 | 11 | StructSpecifier 20 | 21 | 12 StructSpecifier: STRUCT OptTag LC DefList RC 22 | 13 | STRUCT Tag 23 | 24 | 14 OptTag: ID 25 | 15 | %empty 26 | 27 | 16 Tag: ID 28 | 29 | 17 VarDec: ID 30 | 18 | VarDec LB INT RB 31 | 19 | error RB 32 | 33 | 20 FunDec: ID LP VarList RP 34 | 21 | ID LP RP 35 | 22 | error RP 36 | 37 | 23 VarList: ParamDec COMMA VarList 38 | 24 | ParamDec 39 | 40 | 25 ParamDec: Specifier VarDec 41 | 42 | 26 CompSt: LC DefList StmtList RC 43 | 27 | error RC 44 | 45 | 28 StmtList: Stmt StmtList 46 | 29 | %empty 47 | 48 | 30 Stmt: Exp SEMI 49 | 31 | CompSt 50 | 32 | RETURN Exp SEMI 51 | 33 | IF LP Exp RP Stmt 52 | 34 | IF LP Exp RP Stmt ELSE Stmt 53 | 35 | WHILE LP Exp RP Stmt 54 | 36 | error SEMI 55 | 56 | 37 DefList: Def DefList 57 | 38 | %empty 58 | 59 | 39 Def: Specifier DecList SEMI 60 | 61 | 40 DecList: Dec 62 | 41 | Dec COMMA DecList 63 | 64 | 42 Dec: VarDec 65 | 43 | VarDec ASSIGNOP Exp 66 | 67 | 44 Exp: Exp ASSIGNOP Exp 68 | 45 | Exp AND Exp 69 | 46 | Exp OR Exp 70 | 47 | Exp RELOP Exp 71 | 48 | Exp PLUS Exp 72 | 49 | Exp MINUS Exp 73 | 50 | Exp STAR Exp 74 | 51 | Exp DIV Exp 75 | 52 | LP Exp RP 76 | 53 | MINUS Exp 77 | 54 | NOT Exp 78 | 55 | ID LP Args RP 79 | 56 | ID LP RP 80 | 57 | Exp LB Exp RB 81 | 58 | Exp DOT ID 82 | 59 | ID 83 | 60 | INT 84 | 61 | FLOAT 85 | 86 | 62 Args: Exp COMMA Args 87 | 63 | Exp 88 | 89 | 90 | 终结语词,附有它们出现处的规则 91 | 92 | $end (0) 0 93 | error (256) 7 19 22 27 36 94 | INT (258) 18 60 95 | FLOAT (259) 61 96 | ID (260) 14 16 17 20 21 55 56 58 59 97 | TYPE (261) 10 98 | COMMA (262) 9 23 41 62 99 | DOT (263) 58 100 | SEMI (264) 4 5 7 30 32 36 39 101 | RELOP (265) 47 102 | ASSIGNOP (266) 43 44 103 | PLUS (267) 48 104 | MINUS (268) 49 53 105 | STAR (269) 50 106 | DIV (270) 51 107 | AND (271) 45 108 | OR (272) 46 109 | NOT (273) 54 110 | LP (274) 20 21 33 34 35 52 55 56 111 | RP (275) 20 21 22 33 34 35 52 55 56 112 | LB (276) 18 57 113 | RB (277) 18 19 57 114 | LC (278) 12 26 115 | RC (279) 12 26 27 116 | IF (280) 33 34 117 | ELSE (281) 34 118 | WHILE (282) 35 119 | STRUCT (283) 12 13 120 | RETURN (284) 32 121 | LOWER_THAN_ELSE (285) 122 | 123 | 124 | 非终结语词,附有它们出现处的规则 125 | 126 | $accept (31) 127 | on left: 0 128 | Program (32) 129 | on left: 1 130 | on right: 0 131 | ExtDefList (33) 132 | on left: 2 3 133 | on right: 1 2 134 | ExtDef (34) 135 | on left: 4 5 6 7 136 | on right: 2 137 | ExtDecList (35) 138 | on left: 8 9 139 | on right: 4 9 140 | Specifier (36) 141 | on left: 10 11 142 | on right: 4 5 6 25 39 143 | StructSpecifier (37) 144 | on left: 12 13 145 | on right: 11 146 | OptTag (38) 147 | on left: 14 15 148 | on right: 12 149 | Tag (39) 150 | on left: 16 151 | on right: 13 152 | VarDec (40) 153 | on left: 17 18 19 154 | on right: 8 9 18 25 42 43 155 | FunDec (41) 156 | on left: 20 21 22 157 | on right: 6 158 | VarList (42) 159 | on left: 23 24 160 | on right: 20 23 161 | ParamDec (43) 162 | on left: 25 163 | on right: 23 24 164 | CompSt (44) 165 | on left: 26 27 166 | on right: 6 31 167 | StmtList (45) 168 | on left: 28 29 169 | on right: 26 28 170 | Stmt (46) 171 | on left: 30 31 32 33 34 35 36 172 | on right: 28 33 34 35 173 | DefList (47) 174 | on left: 37 38 175 | on right: 12 26 37 176 | Def (48) 177 | on left: 39 178 | on right: 37 179 | DecList (49) 180 | on left: 40 41 181 | on right: 39 41 182 | Dec (50) 183 | on left: 42 43 184 | on right: 40 41 185 | Exp (51) 186 | on left: 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 187 | on right: 30 32 33 34 35 43 44 45 46 47 48 49 50 51 52 53 54 57 58 62 63 188 | Args (52) 189 | on left: 62 63 190 | on right: 55 62 191 | 192 | 193 | 状态 0 194 | 195 | 0 $accept: • Program $end 196 | 197 | error 偏移,并进入状态 1 198 | TYPE 偏移,并进入状态 2 199 | STRUCT 偏移,并进入状态 3 200 | 201 | $end 使用规则 3 以归约 (ExtDefList) 202 | 203 | Program 转到状态 4 204 | ExtDefList 转到状态 5 205 | ExtDef 转到状态 6 206 | Specifier 转到状态 7 207 | StructSpecifier 转到状态 8 208 | 209 | 210 | 状态 1 211 | 212 | 7 ExtDef: error • SEMI 213 | 214 | SEMI 偏移,并进入状态 9 215 | 216 | 217 | 状态 2 218 | 219 | 10 Specifier: TYPE • 220 | 221 | $default 使用规则 10 以归约 (Specifier) 222 | 223 | 224 | 状态 3 225 | 226 | 12 StructSpecifier: STRUCT • OptTag LC DefList RC 227 | 13 | STRUCT • Tag 228 | 229 | ID 偏移,并进入状态 10 230 | 231 | $default 使用规则 15 以归约 (OptTag) 232 | 233 | OptTag 转到状态 11 234 | Tag 转到状态 12 235 | 236 | 237 | 状态 4 238 | 239 | 0 $accept: Program • $end 240 | 241 | $end 偏移,并进入状态 13 242 | 243 | 244 | 状态 5 245 | 246 | 1 Program: ExtDefList • 247 | 248 | $default 使用规则 1 以归约 (Program) 249 | 250 | 251 | 状态 6 252 | 253 | 2 ExtDefList: ExtDef • ExtDefList 254 | 255 | error 偏移,并进入状态 1 256 | TYPE 偏移,并进入状态 2 257 | STRUCT 偏移,并进入状态 3 258 | 259 | $end 使用规则 3 以归约 (ExtDefList) 260 | 261 | ExtDefList 转到状态 14 262 | ExtDef 转到状态 6 263 | Specifier 转到状态 7 264 | StructSpecifier 转到状态 8 265 | 266 | 267 | 状态 7 268 | 269 | 4 ExtDef: Specifier • ExtDecList SEMI 270 | 5 | Specifier • SEMI 271 | 6 | Specifier • FunDec CompSt 272 | 273 | error 偏移,并进入状态 15 274 | ID 偏移,并进入状态 16 275 | SEMI 偏移,并进入状态 17 276 | 277 | ExtDecList 转到状态 18 278 | VarDec 转到状态 19 279 | FunDec 转到状态 20 280 | 281 | 282 | 状态 8 283 | 284 | 11 Specifier: StructSpecifier • 285 | 286 | $default 使用规则 11 以归约 (Specifier) 287 | 288 | 289 | 状态 9 290 | 291 | 7 ExtDef: error SEMI • 292 | 293 | $default 使用规则 7 以归约 (ExtDef) 294 | 295 | 296 | 状态 10 297 | 298 | 14 OptTag: ID • 299 | 16 Tag: ID • 300 | 301 | LC 使用规则 14 以归约 (OptTag) 302 | $default 使用规则 16 以归约 (Tag) 303 | 304 | 305 | 状态 11 306 | 307 | 12 StructSpecifier: STRUCT OptTag • LC DefList RC 308 | 309 | LC 偏移,并进入状态 21 310 | 311 | 312 | 状态 12 313 | 314 | 13 StructSpecifier: STRUCT Tag • 315 | 316 | $default 使用规则 13 以归约 (StructSpecifier) 317 | 318 | 319 | 状态 13 320 | 321 | 0 $accept: Program $end • 322 | 323 | $default 接受 324 | 325 | 326 | 状态 14 327 | 328 | 2 ExtDefList: ExtDef ExtDefList • 329 | 330 | $default 使用规则 2 以归约 (ExtDefList) 331 | 332 | 333 | 状态 15 334 | 335 | 19 VarDec: error • RB 336 | 22 FunDec: error • RP 337 | 338 | RP 偏移,并进入状态 22 339 | RB 偏移,并进入状态 23 340 | 341 | 342 | 状态 16 343 | 344 | 17 VarDec: ID • 345 | 20 FunDec: ID • LP VarList RP 346 | 21 | ID • LP RP 347 | 348 | LP 偏移,并进入状态 24 349 | 350 | $default 使用规则 17 以归约 (VarDec) 351 | 352 | 353 | 状态 17 354 | 355 | 5 ExtDef: Specifier SEMI • 356 | 357 | $default 使用规则 5 以归约 (ExtDef) 358 | 359 | 360 | 状态 18 361 | 362 | 4 ExtDef: Specifier ExtDecList • SEMI 363 | 364 | SEMI 偏移,并进入状态 25 365 | 366 | 367 | 状态 19 368 | 369 | 8 ExtDecList: VarDec • 370 | 9 | VarDec • COMMA ExtDecList 371 | 18 VarDec: VarDec • LB INT RB 372 | 373 | COMMA 偏移,并进入状态 26 374 | LB 偏移,并进入状态 27 375 | 376 | $default 使用规则 8 以归约 (ExtDecList) 377 | 378 | 379 | 状态 20 380 | 381 | 6 ExtDef: Specifier FunDec • CompSt 382 | 383 | error 偏移,并进入状态 28 384 | LC 偏移,并进入状态 29 385 | 386 | CompSt 转到状态 30 387 | 388 | 389 | 状态 21 390 | 391 | 12 StructSpecifier: STRUCT OptTag LC • DefList RC 392 | 393 | TYPE 偏移,并进入状态 2 394 | STRUCT 偏移,并进入状态 3 395 | 396 | $default 使用规则 38 以归约 (DefList) 397 | 398 | Specifier 转到状态 31 399 | StructSpecifier 转到状态 8 400 | DefList 转到状态 32 401 | Def 转到状态 33 402 | 403 | 404 | 状态 22 405 | 406 | 22 FunDec: error RP • 407 | 408 | $default 使用规则 22 以归约 (FunDec) 409 | 410 | 411 | 状态 23 412 | 413 | 19 VarDec: error RB • 414 | 415 | $default 使用规则 19 以归约 (VarDec) 416 | 417 | 418 | 状态 24 419 | 420 | 20 FunDec: ID LP • VarList RP 421 | 21 | ID LP • RP 422 | 423 | TYPE 偏移,并进入状态 2 424 | RP 偏移,并进入状态 34 425 | STRUCT 偏移,并进入状态 3 426 | 427 | Specifier 转到状态 35 428 | StructSpecifier 转到状态 8 429 | VarList 转到状态 36 430 | ParamDec 转到状态 37 431 | 432 | 433 | 状态 25 434 | 435 | 4 ExtDef: Specifier ExtDecList SEMI • 436 | 437 | $default 使用规则 4 以归约 (ExtDef) 438 | 439 | 440 | 状态 26 441 | 442 | 9 ExtDecList: VarDec COMMA • ExtDecList 443 | 444 | error 偏移,并进入状态 38 445 | ID 偏移,并进入状态 39 446 | 447 | ExtDecList 转到状态 40 448 | VarDec 转到状态 19 449 | 450 | 451 | 状态 27 452 | 453 | 18 VarDec: VarDec LB • INT RB 454 | 455 | INT 偏移,并进入状态 41 456 | 457 | 458 | 状态 28 459 | 460 | 27 CompSt: error • RC 461 | 462 | RC 偏移,并进入状态 42 463 | 464 | 465 | 状态 29 466 | 467 | 26 CompSt: LC • DefList StmtList RC 468 | 469 | TYPE 偏移,并进入状态 2 470 | STRUCT 偏移,并进入状态 3 471 | 472 | $default 使用规则 38 以归约 (DefList) 473 | 474 | Specifier 转到状态 31 475 | StructSpecifier 转到状态 8 476 | DefList 转到状态 43 477 | Def 转到状态 33 478 | 479 | 480 | 状态 30 481 | 482 | 6 ExtDef: Specifier FunDec CompSt • 483 | 484 | $default 使用规则 6 以归约 (ExtDef) 485 | 486 | 487 | 状态 31 488 | 489 | 39 Def: Specifier • DecList SEMI 490 | 491 | error 偏移,并进入状态 38 492 | ID 偏移,并进入状态 39 493 | 494 | VarDec 转到状态 44 495 | DecList 转到状态 45 496 | Dec 转到状态 46 497 | 498 | 499 | 状态 32 500 | 501 | 12 StructSpecifier: STRUCT OptTag LC DefList • RC 502 | 503 | RC 偏移,并进入状态 47 504 | 505 | 506 | 状态 33 507 | 508 | 37 DefList: Def • DefList 509 | 510 | TYPE 偏移,并进入状态 2 511 | STRUCT 偏移,并进入状态 3 512 | 513 | $default 使用规则 38 以归约 (DefList) 514 | 515 | Specifier 转到状态 31 516 | StructSpecifier 转到状态 8 517 | DefList 转到状态 48 518 | Def 转到状态 33 519 | 520 | 521 | 状态 34 522 | 523 | 21 FunDec: ID LP RP • 524 | 525 | $default 使用规则 21 以归约 (FunDec) 526 | 527 | 528 | 状态 35 529 | 530 | 25 ParamDec: Specifier • VarDec 531 | 532 | error 偏移,并进入状态 38 533 | ID 偏移,并进入状态 39 534 | 535 | VarDec 转到状态 49 536 | 537 | 538 | 状态 36 539 | 540 | 20 FunDec: ID LP VarList • RP 541 | 542 | RP 偏移,并进入状态 50 543 | 544 | 545 | 状态 37 546 | 547 | 23 VarList: ParamDec • COMMA VarList 548 | 24 | ParamDec • 549 | 550 | COMMA 偏移,并进入状态 51 551 | 552 | $default 使用规则 24 以归约 (VarList) 553 | 554 | 555 | 状态 38 556 | 557 | 19 VarDec: error • RB 558 | 559 | RB 偏移,并进入状态 23 560 | 561 | 562 | 状态 39 563 | 564 | 17 VarDec: ID • 565 | 566 | $default 使用规则 17 以归约 (VarDec) 567 | 568 | 569 | 状态 40 570 | 571 | 9 ExtDecList: VarDec COMMA ExtDecList • 572 | 573 | $default 使用规则 9 以归约 (ExtDecList) 574 | 575 | 576 | 状态 41 577 | 578 | 18 VarDec: VarDec LB INT • RB 579 | 580 | RB 偏移,并进入状态 52 581 | 582 | 583 | 状态 42 584 | 585 | 27 CompSt: error RC • 586 | 587 | $default 使用规则 27 以归约 (CompSt) 588 | 589 | 590 | 状态 43 591 | 592 | 26 CompSt: LC DefList • StmtList RC 593 | 594 | error 偏移,并进入状态 53 595 | INT 偏移,并进入状态 54 596 | FLOAT 偏移,并进入状态 55 597 | ID 偏移,并进入状态 56 598 | MINUS 偏移,并进入状态 57 599 | NOT 偏移,并进入状态 58 600 | LP 偏移,并进入状态 59 601 | LC 偏移,并进入状态 29 602 | IF 偏移,并进入状态 60 603 | WHILE 偏移,并进入状态 61 604 | RETURN 偏移,并进入状态 62 605 | 606 | RC 使用规则 29 以归约 (StmtList) 607 | 608 | CompSt 转到状态 63 609 | StmtList 转到状态 64 610 | Stmt 转到状态 65 611 | Exp 转到状态 66 612 | 613 | 614 | 状态 44 615 | 616 | 18 VarDec: VarDec • LB INT RB 617 | 42 Dec: VarDec • 618 | 43 | VarDec • ASSIGNOP Exp 619 | 620 | ASSIGNOP 偏移,并进入状态 67 621 | LB 偏移,并进入状态 27 622 | 623 | $default 使用规则 42 以归约 (Dec) 624 | 625 | 626 | 状态 45 627 | 628 | 39 Def: Specifier DecList • SEMI 629 | 630 | SEMI 偏移,并进入状态 68 631 | 632 | 633 | 状态 46 634 | 635 | 40 DecList: Dec • 636 | 41 | Dec • COMMA DecList 637 | 638 | COMMA 偏移,并进入状态 69 639 | 640 | $default 使用规则 40 以归约 (DecList) 641 | 642 | 643 | 状态 47 644 | 645 | 12 StructSpecifier: STRUCT OptTag LC DefList RC • 646 | 647 | $default 使用规则 12 以归约 (StructSpecifier) 648 | 649 | 650 | 状态 48 651 | 652 | 37 DefList: Def DefList • 653 | 654 | $default 使用规则 37 以归约 (DefList) 655 | 656 | 657 | 状态 49 658 | 659 | 18 VarDec: VarDec • LB INT RB 660 | 25 ParamDec: Specifier VarDec • 661 | 662 | LB 偏移,并进入状态 27 663 | 664 | $default 使用规则 25 以归约 (ParamDec) 665 | 666 | 667 | 状态 50 668 | 669 | 20 FunDec: ID LP VarList RP • 670 | 671 | $default 使用规则 20 以归约 (FunDec) 672 | 673 | 674 | 状态 51 675 | 676 | 23 VarList: ParamDec COMMA • VarList 677 | 678 | TYPE 偏移,并进入状态 2 679 | STRUCT 偏移,并进入状态 3 680 | 681 | Specifier 转到状态 35 682 | StructSpecifier 转到状态 8 683 | VarList 转到状态 70 684 | ParamDec 转到状态 37 685 | 686 | 687 | 状态 52 688 | 689 | 18 VarDec: VarDec LB INT RB • 690 | 691 | $default 使用规则 18 以归约 (VarDec) 692 | 693 | 694 | 状态 53 695 | 696 | 27 CompSt: error • RC 697 | 36 Stmt: error • SEMI 698 | 699 | SEMI 偏移,并进入状态 71 700 | RC 偏移,并进入状态 42 701 | 702 | 703 | 状态 54 704 | 705 | 60 Exp: INT • 706 | 707 | $default 使用规则 60 以归约 (Exp) 708 | 709 | 710 | 状态 55 711 | 712 | 61 Exp: FLOAT • 713 | 714 | $default 使用规则 61 以归约 (Exp) 715 | 716 | 717 | 状态 56 718 | 719 | 55 Exp: ID • LP Args RP 720 | 56 | ID • LP RP 721 | 59 | ID • 722 | 723 | LP 偏移,并进入状态 72 724 | 725 | $default 使用规则 59 以归约 (Exp) 726 | 727 | 728 | 状态 57 729 | 730 | 53 Exp: MINUS • Exp 731 | 732 | INT 偏移,并进入状态 54 733 | FLOAT 偏移,并进入状态 55 734 | ID 偏移,并进入状态 56 735 | MINUS 偏移,并进入状态 57 736 | NOT 偏移,并进入状态 58 737 | LP 偏移,并进入状态 59 738 | 739 | Exp 转到状态 73 740 | 741 | 742 | 状态 58 743 | 744 | 54 Exp: NOT • Exp 745 | 746 | INT 偏移,并进入状态 54 747 | FLOAT 偏移,并进入状态 55 748 | ID 偏移,并进入状态 56 749 | MINUS 偏移,并进入状态 57 750 | NOT 偏移,并进入状态 58 751 | LP 偏移,并进入状态 59 752 | 753 | Exp 转到状态 74 754 | 755 | 756 | 状态 59 757 | 758 | 52 Exp: LP • Exp RP 759 | 760 | INT 偏移,并进入状态 54 761 | FLOAT 偏移,并进入状态 55 762 | ID 偏移,并进入状态 56 763 | MINUS 偏移,并进入状态 57 764 | NOT 偏移,并进入状态 58 765 | LP 偏移,并进入状态 59 766 | 767 | Exp 转到状态 75 768 | 769 | 770 | 状态 60 771 | 772 | 33 Stmt: IF • LP Exp RP Stmt 773 | 34 | IF • LP Exp RP Stmt ELSE Stmt 774 | 775 | LP 偏移,并进入状态 76 776 | 777 | 778 | 状态 61 779 | 780 | 35 Stmt: WHILE • LP Exp RP Stmt 781 | 782 | LP 偏移,并进入状态 77 783 | 784 | 785 | 状态 62 786 | 787 | 32 Stmt: RETURN • Exp SEMI 788 | 789 | INT 偏移,并进入状态 54 790 | FLOAT 偏移,并进入状态 55 791 | ID 偏移,并进入状态 56 792 | MINUS 偏移,并进入状态 57 793 | NOT 偏移,并进入状态 58 794 | LP 偏移,并进入状态 59 795 | 796 | Exp 转到状态 78 797 | 798 | 799 | 状态 63 800 | 801 | 31 Stmt: CompSt • 802 | 803 | $default 使用规则 31 以归约 (Stmt) 804 | 805 | 806 | 状态 64 807 | 808 | 26 CompSt: LC DefList StmtList • RC 809 | 810 | RC 偏移,并进入状态 79 811 | 812 | 813 | 状态 65 814 | 815 | 28 StmtList: Stmt • StmtList 816 | 817 | error 偏移,并进入状态 53 818 | INT 偏移,并进入状态 54 819 | FLOAT 偏移,并进入状态 55 820 | ID 偏移,并进入状态 56 821 | MINUS 偏移,并进入状态 57 822 | NOT 偏移,并进入状态 58 823 | LP 偏移,并进入状态 59 824 | LC 偏移,并进入状态 29 825 | IF 偏移,并进入状态 60 826 | WHILE 偏移,并进入状态 61 827 | RETURN 偏移,并进入状态 62 828 | 829 | RC 使用规则 29 以归约 (StmtList) 830 | 831 | CompSt 转到状态 63 832 | StmtList 转到状态 80 833 | Stmt 转到状态 65 834 | Exp 转到状态 66 835 | 836 | 837 | 状态 66 838 | 839 | 30 Stmt: Exp • SEMI 840 | 44 Exp: Exp • ASSIGNOP Exp 841 | 45 | Exp • AND Exp 842 | 46 | Exp • OR Exp 843 | 47 | Exp • RELOP Exp 844 | 48 | Exp • PLUS Exp 845 | 49 | Exp • MINUS Exp 846 | 50 | Exp • STAR Exp 847 | 51 | Exp • DIV Exp 848 | 57 | Exp • LB Exp RB 849 | 58 | Exp • DOT ID 850 | 851 | DOT 偏移,并进入状态 81 852 | SEMI 偏移,并进入状态 82 853 | RELOP 偏移,并进入状态 83 854 | ASSIGNOP 偏移,并进入状态 84 855 | PLUS 偏移,并进入状态 85 856 | MINUS 偏移,并进入状态 86 857 | STAR 偏移,并进入状态 87 858 | DIV 偏移,并进入状态 88 859 | AND 偏移,并进入状态 89 860 | OR 偏移,并进入状态 90 861 | LB 偏移,并进入状态 91 862 | 863 | 864 | 状态 67 865 | 866 | 43 Dec: VarDec ASSIGNOP • Exp 867 | 868 | INT 偏移,并进入状态 54 869 | FLOAT 偏移,并进入状态 55 870 | ID 偏移,并进入状态 56 871 | MINUS 偏移,并进入状态 57 872 | NOT 偏移,并进入状态 58 873 | LP 偏移,并进入状态 59 874 | 875 | Exp 转到状态 92 876 | 877 | 878 | 状态 68 879 | 880 | 39 Def: Specifier DecList SEMI • 881 | 882 | $default 使用规则 39 以归约 (Def) 883 | 884 | 885 | 状态 69 886 | 887 | 41 DecList: Dec COMMA • DecList 888 | 889 | error 偏移,并进入状态 38 890 | ID 偏移,并进入状态 39 891 | 892 | VarDec 转到状态 44 893 | DecList 转到状态 93 894 | Dec 转到状态 46 895 | 896 | 897 | 状态 70 898 | 899 | 23 VarList: ParamDec COMMA VarList • 900 | 901 | $default 使用规则 23 以归约 (VarList) 902 | 903 | 904 | 状态 71 905 | 906 | 36 Stmt: error SEMI • 907 | 908 | $default 使用规则 36 以归约 (Stmt) 909 | 910 | 911 | 状态 72 912 | 913 | 55 Exp: ID LP • Args RP 914 | 56 | ID LP • RP 915 | 916 | INT 偏移,并进入状态 54 917 | FLOAT 偏移,并进入状态 55 918 | ID 偏移,并进入状态 56 919 | MINUS 偏移,并进入状态 57 920 | NOT 偏移,并进入状态 58 921 | LP 偏移,并进入状态 59 922 | RP 偏移,并进入状态 94 923 | 924 | Exp 转到状态 95 925 | Args 转到状态 96 926 | 927 | 928 | 状态 73 929 | 930 | 44 Exp: Exp • ASSIGNOP Exp 931 | 45 | Exp • AND Exp 932 | 46 | Exp • OR Exp 933 | 47 | Exp • RELOP Exp 934 | 48 | Exp • PLUS Exp 935 | 49 | Exp • MINUS Exp 936 | 50 | Exp • STAR Exp 937 | 51 | Exp • DIV Exp 938 | 53 | MINUS Exp • 939 | 57 | Exp • LB Exp RB 940 | 58 | Exp • DOT ID 941 | 942 | DOT 偏移,并进入状态 81 943 | STAR 偏移,并进入状态 87 944 | DIV 偏移,并进入状态 88 945 | LB 偏移,并进入状态 91 946 | 947 | $default 使用规则 53 以归约 (Exp) 948 | 949 | 950 | 状态 74 951 | 952 | 44 Exp: Exp • ASSIGNOP Exp 953 | 45 | Exp • AND Exp 954 | 46 | Exp • OR Exp 955 | 47 | Exp • RELOP Exp 956 | 48 | Exp • PLUS Exp 957 | 49 | Exp • MINUS Exp 958 | 50 | Exp • STAR Exp 959 | 51 | Exp • DIV Exp 960 | 54 | NOT Exp • 961 | 57 | Exp • LB Exp RB 962 | 58 | Exp • DOT ID 963 | 964 | DOT 偏移,并进入状态 81 965 | LB 偏移,并进入状态 91 966 | 967 | $default 使用规则 54 以归约 (Exp) 968 | 969 | 970 | 状态 75 971 | 972 | 44 Exp: Exp • ASSIGNOP Exp 973 | 45 | Exp • AND Exp 974 | 46 | Exp • OR Exp 975 | 47 | Exp • RELOP Exp 976 | 48 | Exp • PLUS Exp 977 | 49 | Exp • MINUS Exp 978 | 50 | Exp • STAR Exp 979 | 51 | Exp • DIV Exp 980 | 52 | LP Exp • RP 981 | 57 | Exp • LB Exp RB 982 | 58 | Exp • DOT ID 983 | 984 | DOT 偏移,并进入状态 81 985 | RELOP 偏移,并进入状态 83 986 | ASSIGNOP 偏移,并进入状态 84 987 | PLUS 偏移,并进入状态 85 988 | MINUS 偏移,并进入状态 86 989 | STAR 偏移,并进入状态 87 990 | DIV 偏移,并进入状态 88 991 | AND 偏移,并进入状态 89 992 | OR 偏移,并进入状态 90 993 | RP 偏移,并进入状态 97 994 | LB 偏移,并进入状态 91 995 | 996 | 997 | 状态 76 998 | 999 | 33 Stmt: IF LP • Exp RP Stmt 1000 | 34 | IF LP • Exp RP Stmt ELSE Stmt 1001 | 1002 | INT 偏移,并进入状态 54 1003 | FLOAT 偏移,并进入状态 55 1004 | ID 偏移,并进入状态 56 1005 | MINUS 偏移,并进入状态 57 1006 | NOT 偏移,并进入状态 58 1007 | LP 偏移,并进入状态 59 1008 | 1009 | Exp 转到状态 98 1010 | 1011 | 1012 | 状态 77 1013 | 1014 | 35 Stmt: WHILE LP • Exp RP Stmt 1015 | 1016 | INT 偏移,并进入状态 54 1017 | FLOAT 偏移,并进入状态 55 1018 | ID 偏移,并进入状态 56 1019 | MINUS 偏移,并进入状态 57 1020 | NOT 偏移,并进入状态 58 1021 | LP 偏移,并进入状态 59 1022 | 1023 | Exp 转到状态 99 1024 | 1025 | 1026 | 状态 78 1027 | 1028 | 32 Stmt: RETURN Exp • SEMI 1029 | 44 Exp: Exp • ASSIGNOP Exp 1030 | 45 | Exp • AND Exp 1031 | 46 | Exp • OR Exp 1032 | 47 | Exp • RELOP Exp 1033 | 48 | Exp • PLUS Exp 1034 | 49 | Exp • MINUS Exp 1035 | 50 | Exp • STAR Exp 1036 | 51 | Exp • DIV Exp 1037 | 57 | Exp • LB Exp RB 1038 | 58 | Exp • DOT ID 1039 | 1040 | DOT 偏移,并进入状态 81 1041 | SEMI 偏移,并进入状态 100 1042 | RELOP 偏移,并进入状态 83 1043 | ASSIGNOP 偏移,并进入状态 84 1044 | PLUS 偏移,并进入状态 85 1045 | MINUS 偏移,并进入状态 86 1046 | STAR 偏移,并进入状态 87 1047 | DIV 偏移,并进入状态 88 1048 | AND 偏移,并进入状态 89 1049 | OR 偏移,并进入状态 90 1050 | LB 偏移,并进入状态 91 1051 | 1052 | 1053 | 状态 79 1054 | 1055 | 26 CompSt: LC DefList StmtList RC • 1056 | 1057 | $default 使用规则 26 以归约 (CompSt) 1058 | 1059 | 1060 | 状态 80 1061 | 1062 | 28 StmtList: Stmt StmtList • 1063 | 1064 | $default 使用规则 28 以归约 (StmtList) 1065 | 1066 | 1067 | 状态 81 1068 | 1069 | 58 Exp: Exp DOT • ID 1070 | 1071 | ID 偏移,并进入状态 101 1072 | 1073 | 1074 | 状态 82 1075 | 1076 | 30 Stmt: Exp SEMI • 1077 | 1078 | $default 使用规则 30 以归约 (Stmt) 1079 | 1080 | 1081 | 状态 83 1082 | 1083 | 47 Exp: Exp RELOP • Exp 1084 | 1085 | INT 偏移,并进入状态 54 1086 | FLOAT 偏移,并进入状态 55 1087 | ID 偏移,并进入状态 56 1088 | MINUS 偏移,并进入状态 57 1089 | NOT 偏移,并进入状态 58 1090 | LP 偏移,并进入状态 59 1091 | 1092 | Exp 转到状态 102 1093 | 1094 | 1095 | 状态 84 1096 | 1097 | 44 Exp: Exp ASSIGNOP • Exp 1098 | 1099 | INT 偏移,并进入状态 54 1100 | FLOAT 偏移,并进入状态 55 1101 | ID 偏移,并进入状态 56 1102 | MINUS 偏移,并进入状态 57 1103 | NOT 偏移,并进入状态 58 1104 | LP 偏移,并进入状态 59 1105 | 1106 | Exp 转到状态 103 1107 | 1108 | 1109 | 状态 85 1110 | 1111 | 48 Exp: Exp PLUS • Exp 1112 | 1113 | INT 偏移,并进入状态 54 1114 | FLOAT 偏移,并进入状态 55 1115 | ID 偏移,并进入状态 56 1116 | MINUS 偏移,并进入状态 57 1117 | NOT 偏移,并进入状态 58 1118 | LP 偏移,并进入状态 59 1119 | 1120 | Exp 转到状态 104 1121 | 1122 | 1123 | 状态 86 1124 | 1125 | 49 Exp: Exp MINUS • Exp 1126 | 1127 | INT 偏移,并进入状态 54 1128 | FLOAT 偏移,并进入状态 55 1129 | ID 偏移,并进入状态 56 1130 | MINUS 偏移,并进入状态 57 1131 | NOT 偏移,并进入状态 58 1132 | LP 偏移,并进入状态 59 1133 | 1134 | Exp 转到状态 105 1135 | 1136 | 1137 | 状态 87 1138 | 1139 | 50 Exp: Exp STAR • Exp 1140 | 1141 | INT 偏移,并进入状态 54 1142 | FLOAT 偏移,并进入状态 55 1143 | ID 偏移,并进入状态 56 1144 | MINUS 偏移,并进入状态 57 1145 | NOT 偏移,并进入状态 58 1146 | LP 偏移,并进入状态 59 1147 | 1148 | Exp 转到状态 106 1149 | 1150 | 1151 | 状态 88 1152 | 1153 | 51 Exp: Exp DIV • Exp 1154 | 1155 | INT 偏移,并进入状态 54 1156 | FLOAT 偏移,并进入状态 55 1157 | ID 偏移,并进入状态 56 1158 | MINUS 偏移,并进入状态 57 1159 | NOT 偏移,并进入状态 58 1160 | LP 偏移,并进入状态 59 1161 | 1162 | Exp 转到状态 107 1163 | 1164 | 1165 | 状态 89 1166 | 1167 | 45 Exp: Exp AND • Exp 1168 | 1169 | INT 偏移,并进入状态 54 1170 | FLOAT 偏移,并进入状态 55 1171 | ID 偏移,并进入状态 56 1172 | MINUS 偏移,并进入状态 57 1173 | NOT 偏移,并进入状态 58 1174 | LP 偏移,并进入状态 59 1175 | 1176 | Exp 转到状态 108 1177 | 1178 | 1179 | 状态 90 1180 | 1181 | 46 Exp: Exp OR • Exp 1182 | 1183 | INT 偏移,并进入状态 54 1184 | FLOAT 偏移,并进入状态 55 1185 | ID 偏移,并进入状态 56 1186 | MINUS 偏移,并进入状态 57 1187 | NOT 偏移,并进入状态 58 1188 | LP 偏移,并进入状态 59 1189 | 1190 | Exp 转到状态 109 1191 | 1192 | 1193 | 状态 91 1194 | 1195 | 57 Exp: Exp LB • Exp RB 1196 | 1197 | INT 偏移,并进入状态 54 1198 | FLOAT 偏移,并进入状态 55 1199 | ID 偏移,并进入状态 56 1200 | MINUS 偏移,并进入状态 57 1201 | NOT 偏移,并进入状态 58 1202 | LP 偏移,并进入状态 59 1203 | 1204 | Exp 转到状态 110 1205 | 1206 | 1207 | 状态 92 1208 | 1209 | 43 Dec: VarDec ASSIGNOP Exp • 1210 | 44 Exp: Exp • ASSIGNOP Exp 1211 | 45 | Exp • AND Exp 1212 | 46 | Exp • OR Exp 1213 | 47 | Exp • RELOP Exp 1214 | 48 | Exp • PLUS Exp 1215 | 49 | Exp • MINUS Exp 1216 | 50 | Exp • STAR Exp 1217 | 51 | Exp • DIV Exp 1218 | 57 | Exp • LB Exp RB 1219 | 58 | Exp • DOT ID 1220 | 1221 | DOT 偏移,并进入状态 81 1222 | RELOP 偏移,并进入状态 83 1223 | ASSIGNOP 偏移,并进入状态 84 1224 | PLUS 偏移,并进入状态 85 1225 | MINUS 偏移,并进入状态 86 1226 | STAR 偏移,并进入状态 87 1227 | DIV 偏移,并进入状态 88 1228 | AND 偏移,并进入状态 89 1229 | OR 偏移,并进入状态 90 1230 | LB 偏移,并进入状态 91 1231 | 1232 | $default 使用规则 43 以归约 (Dec) 1233 | 1234 | 1235 | 状态 93 1236 | 1237 | 41 DecList: Dec COMMA DecList • 1238 | 1239 | $default 使用规则 41 以归约 (DecList) 1240 | 1241 | 1242 | 状态 94 1243 | 1244 | 56 Exp: ID LP RP • 1245 | 1246 | $default 使用规则 56 以归约 (Exp) 1247 | 1248 | 1249 | 状态 95 1250 | 1251 | 44 Exp: Exp • ASSIGNOP Exp 1252 | 45 | Exp • AND Exp 1253 | 46 | Exp • OR Exp 1254 | 47 | Exp • RELOP Exp 1255 | 48 | Exp • PLUS Exp 1256 | 49 | Exp • MINUS Exp 1257 | 50 | Exp • STAR Exp 1258 | 51 | Exp • DIV Exp 1259 | 57 | Exp • LB Exp RB 1260 | 58 | Exp • DOT ID 1261 | 62 Args: Exp • COMMA Args 1262 | 63 | Exp • 1263 | 1264 | COMMA 偏移,并进入状态 111 1265 | DOT 偏移,并进入状态 81 1266 | RELOP 偏移,并进入状态 83 1267 | ASSIGNOP 偏移,并进入状态 84 1268 | PLUS 偏移,并进入状态 85 1269 | MINUS 偏移,并进入状态 86 1270 | STAR 偏移,并进入状态 87 1271 | DIV 偏移,并进入状态 88 1272 | AND 偏移,并进入状态 89 1273 | OR 偏移,并进入状态 90 1274 | LB 偏移,并进入状态 91 1275 | 1276 | $default 使用规则 63 以归约 (Args) 1277 | 1278 | 1279 | 状态 96 1280 | 1281 | 55 Exp: ID LP Args • RP 1282 | 1283 | RP 偏移,并进入状态 112 1284 | 1285 | 1286 | 状态 97 1287 | 1288 | 52 Exp: LP Exp RP • 1289 | 1290 | $default 使用规则 52 以归约 (Exp) 1291 | 1292 | 1293 | 状态 98 1294 | 1295 | 33 Stmt: IF LP Exp • RP Stmt 1296 | 34 | IF LP Exp • RP Stmt ELSE Stmt 1297 | 44 Exp: Exp • ASSIGNOP Exp 1298 | 45 | Exp • AND Exp 1299 | 46 | Exp • OR Exp 1300 | 47 | Exp • RELOP Exp 1301 | 48 | Exp • PLUS Exp 1302 | 49 | Exp • MINUS Exp 1303 | 50 | Exp • STAR Exp 1304 | 51 | Exp • DIV Exp 1305 | 57 | Exp • LB Exp RB 1306 | 58 | Exp • DOT ID 1307 | 1308 | DOT 偏移,并进入状态 81 1309 | RELOP 偏移,并进入状态 83 1310 | ASSIGNOP 偏移,并进入状态 84 1311 | PLUS 偏移,并进入状态 85 1312 | MINUS 偏移,并进入状态 86 1313 | STAR 偏移,并进入状态 87 1314 | DIV 偏移,并进入状态 88 1315 | AND 偏移,并进入状态 89 1316 | OR 偏移,并进入状态 90 1317 | RP 偏移,并进入状态 113 1318 | LB 偏移,并进入状态 91 1319 | 1320 | 1321 | 状态 99 1322 | 1323 | 35 Stmt: WHILE LP Exp • RP Stmt 1324 | 44 Exp: Exp • ASSIGNOP Exp 1325 | 45 | Exp • AND Exp 1326 | 46 | Exp • OR Exp 1327 | 47 | Exp • RELOP Exp 1328 | 48 | Exp • PLUS Exp 1329 | 49 | Exp • MINUS Exp 1330 | 50 | Exp • STAR Exp 1331 | 51 | Exp • DIV Exp 1332 | 57 | Exp • LB Exp RB 1333 | 58 | Exp • DOT ID 1334 | 1335 | DOT 偏移,并进入状态 81 1336 | RELOP 偏移,并进入状态 83 1337 | ASSIGNOP 偏移,并进入状态 84 1338 | PLUS 偏移,并进入状态 85 1339 | MINUS 偏移,并进入状态 86 1340 | STAR 偏移,并进入状态 87 1341 | DIV 偏移,并进入状态 88 1342 | AND 偏移,并进入状态 89 1343 | OR 偏移,并进入状态 90 1344 | RP 偏移,并进入状态 114 1345 | LB 偏移,并进入状态 91 1346 | 1347 | 1348 | 状态 100 1349 | 1350 | 32 Stmt: RETURN Exp SEMI • 1351 | 1352 | $default 使用规则 32 以归约 (Stmt) 1353 | 1354 | 1355 | 状态 101 1356 | 1357 | 58 Exp: Exp DOT ID • 1358 | 1359 | $default 使用规则 58 以归约 (Exp) 1360 | 1361 | 1362 | 状态 102 1363 | 1364 | 44 Exp: Exp • ASSIGNOP Exp 1365 | 45 | Exp • AND Exp 1366 | 46 | Exp • OR Exp 1367 | 47 | Exp • RELOP Exp 1368 | 47 | Exp RELOP Exp • 1369 | 48 | Exp • PLUS Exp 1370 | 49 | Exp • MINUS Exp 1371 | 50 | Exp • STAR Exp 1372 | 51 | Exp • DIV Exp 1373 | 57 | Exp • LB Exp RB 1374 | 58 | Exp • DOT ID 1375 | 1376 | DOT 偏移,并进入状态 81 1377 | PLUS 偏移,并进入状态 85 1378 | MINUS 偏移,并进入状态 86 1379 | STAR 偏移,并进入状态 87 1380 | DIV 偏移,并进入状态 88 1381 | LB 偏移,并进入状态 91 1382 | 1383 | $default 使用规则 47 以归约 (Exp) 1384 | 1385 | 1386 | 状态 103 1387 | 1388 | 44 Exp: Exp • ASSIGNOP Exp 1389 | 44 | Exp ASSIGNOP Exp • 1390 | 45 | Exp • AND Exp 1391 | 46 | Exp • OR Exp 1392 | 47 | Exp • RELOP Exp 1393 | 48 | Exp • PLUS Exp 1394 | 49 | Exp • MINUS Exp 1395 | 50 | Exp • STAR Exp 1396 | 51 | Exp • DIV Exp 1397 | 57 | Exp • LB Exp RB 1398 | 58 | Exp • DOT ID 1399 | 1400 | DOT 偏移,并进入状态 81 1401 | RELOP 偏移,并进入状态 83 1402 | ASSIGNOP 偏移,并进入状态 84 1403 | PLUS 偏移,并进入状态 85 1404 | MINUS 偏移,并进入状态 86 1405 | STAR 偏移,并进入状态 87 1406 | DIV 偏移,并进入状态 88 1407 | AND 偏移,并进入状态 89 1408 | OR 偏移,并进入状态 90 1409 | LB 偏移,并进入状态 91 1410 | 1411 | $default 使用规则 44 以归约 (Exp) 1412 | 1413 | 1414 | 状态 104 1415 | 1416 | 44 Exp: Exp • ASSIGNOP Exp 1417 | 45 | Exp • AND Exp 1418 | 46 | Exp • OR Exp 1419 | 47 | Exp • RELOP Exp 1420 | 48 | Exp • PLUS Exp 1421 | 48 | Exp PLUS Exp • 1422 | 49 | Exp • MINUS Exp 1423 | 50 | Exp • STAR Exp 1424 | 51 | Exp • DIV Exp 1425 | 57 | Exp • LB Exp RB 1426 | 58 | Exp • DOT ID 1427 | 1428 | DOT 偏移,并进入状态 81 1429 | STAR 偏移,并进入状态 87 1430 | DIV 偏移,并进入状态 88 1431 | LB 偏移,并进入状态 91 1432 | 1433 | $default 使用规则 48 以归约 (Exp) 1434 | 1435 | 1436 | 状态 105 1437 | 1438 | 44 Exp: Exp • ASSIGNOP Exp 1439 | 45 | Exp • AND Exp 1440 | 46 | Exp • OR Exp 1441 | 47 | Exp • RELOP Exp 1442 | 48 | Exp • PLUS Exp 1443 | 49 | Exp • MINUS Exp 1444 | 49 | Exp MINUS Exp • 1445 | 50 | Exp • STAR Exp 1446 | 51 | Exp • DIV Exp 1447 | 57 | Exp • LB Exp RB 1448 | 58 | Exp • DOT ID 1449 | 1450 | DOT 偏移,并进入状态 81 1451 | STAR 偏移,并进入状态 87 1452 | DIV 偏移,并进入状态 88 1453 | LB 偏移,并进入状态 91 1454 | 1455 | $default 使用规则 49 以归约 (Exp) 1456 | 1457 | 1458 | 状态 106 1459 | 1460 | 44 Exp: Exp • ASSIGNOP Exp 1461 | 45 | Exp • AND Exp 1462 | 46 | Exp • OR Exp 1463 | 47 | Exp • RELOP Exp 1464 | 48 | Exp • PLUS Exp 1465 | 49 | Exp • MINUS Exp 1466 | 50 | Exp • STAR Exp 1467 | 50 | Exp STAR Exp • 1468 | 51 | Exp • DIV Exp 1469 | 57 | Exp • LB Exp RB 1470 | 58 | Exp • DOT ID 1471 | 1472 | DOT 偏移,并进入状态 81 1473 | LB 偏移,并进入状态 91 1474 | 1475 | $default 使用规则 50 以归约 (Exp) 1476 | 1477 | 1478 | 状态 107 1479 | 1480 | 44 Exp: Exp • ASSIGNOP Exp 1481 | 45 | Exp • AND Exp 1482 | 46 | Exp • OR Exp 1483 | 47 | Exp • RELOP Exp 1484 | 48 | Exp • PLUS Exp 1485 | 49 | Exp • MINUS Exp 1486 | 50 | Exp • STAR Exp 1487 | 51 | Exp • DIV Exp 1488 | 51 | Exp DIV Exp • 1489 | 57 | Exp • LB Exp RB 1490 | 58 | Exp • DOT ID 1491 | 1492 | DOT 偏移,并进入状态 81 1493 | LB 偏移,并进入状态 91 1494 | 1495 | $default 使用规则 51 以归约 (Exp) 1496 | 1497 | 1498 | 状态 108 1499 | 1500 | 44 Exp: Exp • ASSIGNOP Exp 1501 | 45 | Exp • AND Exp 1502 | 45 | Exp AND Exp • 1503 | 46 | Exp • OR Exp 1504 | 47 | Exp • RELOP Exp 1505 | 48 | Exp • PLUS Exp 1506 | 49 | Exp • MINUS Exp 1507 | 50 | Exp • STAR Exp 1508 | 51 | Exp • DIV Exp 1509 | 57 | Exp • LB Exp RB 1510 | 58 | Exp • DOT ID 1511 | 1512 | DOT 偏移,并进入状态 81 1513 | RELOP 偏移,并进入状态 83 1514 | PLUS 偏移,并进入状态 85 1515 | MINUS 偏移,并进入状态 86 1516 | STAR 偏移,并进入状态 87 1517 | DIV 偏移,并进入状态 88 1518 | LB 偏移,并进入状态 91 1519 | 1520 | $default 使用规则 45 以归约 (Exp) 1521 | 1522 | 1523 | 状态 109 1524 | 1525 | 44 Exp: Exp • ASSIGNOP Exp 1526 | 45 | Exp • AND Exp 1527 | 46 | Exp • OR Exp 1528 | 46 | Exp OR Exp • 1529 | 47 | Exp • RELOP Exp 1530 | 48 | Exp • PLUS Exp 1531 | 49 | Exp • MINUS Exp 1532 | 50 | Exp • STAR Exp 1533 | 51 | Exp • DIV Exp 1534 | 57 | Exp • LB Exp RB 1535 | 58 | Exp • DOT ID 1536 | 1537 | DOT 偏移,并进入状态 81 1538 | RELOP 偏移,并进入状态 83 1539 | PLUS 偏移,并进入状态 85 1540 | MINUS 偏移,并进入状态 86 1541 | STAR 偏移,并进入状态 87 1542 | DIV 偏移,并进入状态 88 1543 | AND 偏移,并进入状态 89 1544 | LB 偏移,并进入状态 91 1545 | 1546 | $default 使用规则 46 以归约 (Exp) 1547 | 1548 | 1549 | 状态 110 1550 | 1551 | 44 Exp: Exp • ASSIGNOP Exp 1552 | 45 | Exp • AND Exp 1553 | 46 | Exp • OR Exp 1554 | 47 | Exp • RELOP Exp 1555 | 48 | Exp • PLUS Exp 1556 | 49 | Exp • MINUS Exp 1557 | 50 | Exp • STAR Exp 1558 | 51 | Exp • DIV Exp 1559 | 57 | Exp • LB Exp RB 1560 | 57 | Exp LB Exp • RB 1561 | 58 | Exp • DOT ID 1562 | 1563 | DOT 偏移,并进入状态 81 1564 | RELOP 偏移,并进入状态 83 1565 | ASSIGNOP 偏移,并进入状态 84 1566 | PLUS 偏移,并进入状态 85 1567 | MINUS 偏移,并进入状态 86 1568 | STAR 偏移,并进入状态 87 1569 | DIV 偏移,并进入状态 88 1570 | AND 偏移,并进入状态 89 1571 | OR 偏移,并进入状态 90 1572 | LB 偏移,并进入状态 91 1573 | RB 偏移,并进入状态 115 1574 | 1575 | 1576 | 状态 111 1577 | 1578 | 62 Args: Exp COMMA • Args 1579 | 1580 | INT 偏移,并进入状态 54 1581 | FLOAT 偏移,并进入状态 55 1582 | ID 偏移,并进入状态 56 1583 | MINUS 偏移,并进入状态 57 1584 | NOT 偏移,并进入状态 58 1585 | LP 偏移,并进入状态 59 1586 | 1587 | Exp 转到状态 95 1588 | Args 转到状态 116 1589 | 1590 | 1591 | 状态 112 1592 | 1593 | 55 Exp: ID LP Args RP • 1594 | 1595 | $default 使用规则 55 以归约 (Exp) 1596 | 1597 | 1598 | 状态 113 1599 | 1600 | 33 Stmt: IF LP Exp RP • Stmt 1601 | 34 | IF LP Exp RP • Stmt ELSE Stmt 1602 | 1603 | error 偏移,并进入状态 53 1604 | INT 偏移,并进入状态 54 1605 | FLOAT 偏移,并进入状态 55 1606 | ID 偏移,并进入状态 56 1607 | MINUS 偏移,并进入状态 57 1608 | NOT 偏移,并进入状态 58 1609 | LP 偏移,并进入状态 59 1610 | LC 偏移,并进入状态 29 1611 | IF 偏移,并进入状态 60 1612 | WHILE 偏移,并进入状态 61 1613 | RETURN 偏移,并进入状态 62 1614 | 1615 | CompSt 转到状态 63 1616 | Stmt 转到状态 117 1617 | Exp 转到状态 66 1618 | 1619 | 1620 | 状态 114 1621 | 1622 | 35 Stmt: WHILE LP Exp RP • Stmt 1623 | 1624 | error 偏移,并进入状态 53 1625 | INT 偏移,并进入状态 54 1626 | FLOAT 偏移,并进入状态 55 1627 | ID 偏移,并进入状态 56 1628 | MINUS 偏移,并进入状态 57 1629 | NOT 偏移,并进入状态 58 1630 | LP 偏移,并进入状态 59 1631 | LC 偏移,并进入状态 29 1632 | IF 偏移,并进入状态 60 1633 | WHILE 偏移,并进入状态 61 1634 | RETURN 偏移,并进入状态 62 1635 | 1636 | CompSt 转到状态 63 1637 | Stmt 转到状态 118 1638 | Exp 转到状态 66 1639 | 1640 | 1641 | 状态 115 1642 | 1643 | 57 Exp: Exp LB Exp RB • 1644 | 1645 | $default 使用规则 57 以归约 (Exp) 1646 | 1647 | 1648 | 状态 116 1649 | 1650 | 62 Args: Exp COMMA Args • 1651 | 1652 | $default 使用规则 62 以归约 (Args) 1653 | 1654 | 1655 | 状态 117 1656 | 1657 | 33 Stmt: IF LP Exp RP Stmt • 1658 | 34 | IF LP Exp RP Stmt • ELSE Stmt 1659 | 1660 | ELSE 偏移,并进入状态 119 1661 | 1662 | $default 使用规则 33 以归约 (Stmt) 1663 | 1664 | 1665 | 状态 118 1666 | 1667 | 35 Stmt: WHILE LP Exp RP Stmt • 1668 | 1669 | $default 使用规则 35 以归约 (Stmt) 1670 | 1671 | 1672 | 状态 119 1673 | 1674 | 34 Stmt: IF LP Exp RP Stmt ELSE • Stmt 1675 | 1676 | error 偏移,并进入状态 53 1677 | INT 偏移,并进入状态 54 1678 | FLOAT 偏移,并进入状态 55 1679 | ID 偏移,并进入状态 56 1680 | MINUS 偏移,并进入状态 57 1681 | NOT 偏移,并进入状态 58 1682 | LP 偏移,并进入状态 59 1683 | LC 偏移,并进入状态 29 1684 | IF 偏移,并进入状态 60 1685 | WHILE 偏移,并进入状态 61 1686 | RETURN 偏移,并进入状态 62 1687 | 1688 | CompSt 转到状态 63 1689 | Stmt 转到状态 120 1690 | Exp 转到状态 66 1691 | 1692 | 1693 | 状态 120 1694 | 1695 | 34 Stmt: IF LP Exp RP Stmt ELSE Stmt • 1696 | 1697 | $default 使用规则 34 以归约 (Stmt) 1698 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/syntax.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 3.7.5. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2021 Free Software Foundation, 6 | Inc. 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . */ 20 | 21 | /* As a special exception, you may create a larger work that contains 22 | part or all of the Bison parser skeleton and distribute that work 23 | under terms of your choice, so long as that work isn't itself a 24 | parser generator using the skeleton or a modified version thereof 25 | as a parser skeleton. Alternatively, if you modify or redistribute 26 | the parser skeleton itself, you may (at your option) remove this 27 | special exception, which will cause the skeleton and the resulting 28 | Bison output files to be licensed under the GNU General Public 29 | License without this special exception. 30 | 31 | This special exception was added by the Free Software Foundation in 32 | version 2.2 of Bison. */ 33 | 34 | /* DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual, 35 | especially those whose name start with YY_ or yy_. They are 36 | private implementation details that can be changed or removed. */ 37 | 38 | #ifndef YY_YY_SYNTAX_TAB_H_INCLUDED 39 | # define YY_YY_SYNTAX_TAB_H_INCLUDED 40 | /* Debug traces. */ 41 | #ifndef YYDEBUG 42 | # define YYDEBUG 0 43 | #endif 44 | #if YYDEBUG 45 | extern int yydebug; 46 | #endif 47 | 48 | /* Token kinds. */ 49 | #ifndef YYTOKENTYPE 50 | # define YYTOKENTYPE 51 | enum yytokentype 52 | { 53 | YYEMPTY = -2, 54 | YYEOF = 0, /* "end of file" */ 55 | YYerror = 256, /* error */ 56 | YYUNDEF = 257, /* "invalid token" */ 57 | INT = 258, /* INT */ 58 | FLOAT = 259, /* FLOAT */ 59 | ID = 260, /* ID */ 60 | TYPE = 261, /* TYPE */ 61 | COMMA = 262, /* COMMA */ 62 | DOT = 263, /* DOT */ 63 | SEMI = 264, /* SEMI */ 64 | RELOP = 265, /* RELOP */ 65 | ASSIGNOP = 266, /* ASSIGNOP */ 66 | PLUS = 267, /* PLUS */ 67 | MINUS = 268, /* MINUS */ 68 | STAR = 269, /* STAR */ 69 | DIV = 270, /* DIV */ 70 | AND = 271, /* AND */ 71 | OR = 272, /* OR */ 72 | NOT = 273, /* NOT */ 73 | LP = 274, /* LP */ 74 | RP = 275, /* RP */ 75 | LB = 276, /* LB */ 76 | RB = 277, /* RB */ 77 | LC = 278, /* LC */ 78 | RC = 279, /* RC */ 79 | IF = 280, /* IF */ 80 | ELSE = 281, /* ELSE */ 81 | WHILE = 282, /* WHILE */ 82 | STRUCT = 283, /* STRUCT */ 83 | RETURN = 284, /* RETURN */ 84 | LOWER_THAN_ELSE = 285 /* LOWER_THAN_ELSE */ 85 | }; 86 | typedef enum yytokentype yytoken_kind_t; 87 | #endif 88 | 89 | /* Value type. */ 90 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 91 | union YYSTYPE 92 | { 93 | #line 13 "./syntax.y" 94 | 95 | pNode node; 96 | 97 | #line 98 "./syntax.tab.h" 98 | 99 | }; 100 | typedef union YYSTYPE YYSTYPE; 101 | # define YYSTYPE_IS_TRIVIAL 1 102 | # define YYSTYPE_IS_DECLARED 1 103 | #endif 104 | 105 | /* Location type. */ 106 | #if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED 107 | typedef struct YYLTYPE YYLTYPE; 108 | struct YYLTYPE 109 | { 110 | int first_line; 111 | int first_column; 112 | int last_line; 113 | int last_column; 114 | }; 115 | # define YYLTYPE_IS_DECLARED 1 116 | # define YYLTYPE_IS_TRIVIAL 1 117 | #endif 118 | 119 | 120 | extern YYSTYPE yylval; 121 | extern YYLTYPE yylloc; 122 | int yyparse (void); 123 | 124 | #endif /* !YY_YY_SYNTAX_TAB_H_INCLUDED */ 125 | -------------------------------------------------------------------------------- /编译系统实验/实验二/lab2/syntax.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "lex.yy.c" 3 | int hasError = 0; 4 | void yyerror(char* msg); 5 | extern Tree root; 6 | %} 7 | 8 | %locations 9 | %union 10 | { 11 | struct TNode* node; 12 | } 13 | 14 | %token INT FLOAT ID 15 | %token SEMI COMMA 16 | %token ASSIGNOP RELOP PLUS MINUS STAR DIV 17 | %token AND OR NOT 18 | %token DOT TYPE 19 | %token LP RP LB RB LC RC 20 | %token STRUCT RETURN IF ELSE WHILE 21 | 22 | %type Program ExtDefList ExtDef ExtDecList 23 | %type Specifier StructSpecifier OptTag Tag 24 | %type VarDec FunDec VarList ParamDec 25 | %type CompSt StmtList Stmt 26 | %type DefList Def Dec DecList 27 | %type Exp Args 28 | 29 | %right ASSIGNOP 30 | %left OR 31 | %left AND 32 | %left RELOP 33 | %left PLUS MINUS 34 | %left STAR DIV 35 | %right NOT 36 | %left LP RP LB RB DOT 37 | 38 | %nonassoc LOWER_THAN_ELSE 39 | %nonassoc ELSE 40 | 41 | %% 42 | 43 | Program: ExtDefList { 44 | $$ = createNonTerminal("Program"); 45 | insertNode($$, 1, $1); 46 | root = $$; 47 | } 48 | ; 49 | ExtDefList: ExtDef ExtDefList { 50 | $$ = createNonTerminal("ExtDefList"); 51 | insertNode($$, 2, $2, $1); 52 | } 53 | | { $$ = NULL; } 54 | ; 55 | ExtDef: Specifier ExtDecList SEMI { 56 | $$ = createNonTerminal("ExtDef"); 57 | insertNode($$, 3, $3, $2, $1); 58 | } 59 | | Specifier SEMI { 60 | $$ = createNonTerminal("ExtDef"); 61 | insertNode($$, 2, $2, $1); 62 | } 63 | | Specifier FunDec CompSt { 64 | $$ = createNonTerminal("ExtDef"); 65 | insertNode($$, 3, $3, $2, $1); 66 | } 67 | | error SEMI { hasError = 1; } 68 | ; 69 | ExtDecList: VarDec { 70 | $$ = createNonTerminal("ExtDecList"); 71 | insertNode($$, 1, $1); 72 | } 73 | | VarDec COMMA ExtDecList { 74 | $$ = createNonTerminal("ExtDecList"); 75 | insertNode($$, 3, $3, $2, $1); 76 | } 77 | ; 78 | 79 | 80 | Specifier: TYPE { 81 | $$ = createNonTerminal("Specifier"); 82 | insertNode($$, 1, $1); 83 | } 84 | | StructSpecifier { 85 | $$ = createNonTerminal("Specifier"); 86 | insertNode($$, 1, $1); 87 | } 88 | ; 89 | StructSpecifier: STRUCT OptTag LC DefList RC { 90 | $$ = createNonTerminal("StructSpecifier"); 91 | insertNode($$, 5, $5, $4, $3, $2, $1); 92 | } 93 | | STRUCT Tag { 94 | $$ = createNonTerminal("StructSpecifier"); 95 | insertNode($$, 2, $2, $1); 96 | } 97 | | error RC { hasError = 1; } 98 | ; 99 | OptTag: ID { 100 | $$ = createNonTerminal("OptTag"); 101 | insertNode($$, 1, $1); 102 | } 103 | | { $$ = NULL; } 104 | ; 105 | Tag: ID { 106 | $$ = createNonTerminal("Tag"); 107 | insertNode($$, 1, $1); 108 | } 109 | ; 110 | 111 | 112 | VarDec: ID { 113 | $$ = createNonTerminal("VarDec"); 114 | insertNode($$, 1, $1); 115 | } 116 | | VarDec LB INT RB { 117 | $$ = createNonTerminal("VarDec"); 118 | insertNode($$, 4, $4, $3, $2, $1); 119 | } 120 | | error RB { hasError = 1; } 121 | ; 122 | FunDec: ID LP VarList RP { 123 | $$ = createNonTerminal("FunDec"); 124 | insertNode($$, 4, $4, $3, $2, $1); 125 | } 126 | | ID LP RP { 127 | $$ = createNonTerminal("FunDec"); 128 | insertNode($$, 3, $3, $2, $1); 129 | } 130 | | error RP { hasError = 1; } 131 | ; 132 | VarList: ParamDec COMMA VarList { 133 | $$ = createNonTerminal("VarList"); 134 | insertNode($$, 3, $3, $2, $1); 135 | } 136 | | ParamDec { 137 | $$ = createNonTerminal("VarList"); 138 | insertNode($$, 1, $1); 139 | } 140 | ; 141 | ParamDec: Specifier VarDec { 142 | $$ = createNonTerminal("ParamDec"); 143 | insertNode($$, 2, $2, $1); 144 | } 145 | ; 146 | 147 | 148 | CompSt: LC DefList StmtList RC { 149 | $$ = createNonTerminal("CompSt"); 150 | insertNode($$, 4, $4, $3, $2, $1); 151 | } 152 | | error RC { hasError = 1; } 153 | ; 154 | StmtList: Stmt StmtList { 155 | $$ = createNonTerminal("StmtList"); 156 | insertNode($$, 2, $2, $1); 157 | } 158 | | { $$ = NULL; } 159 | ; 160 | Stmt: Exp SEMI { 161 | $$ = createNonTerminal("Stmt"); 162 | insertNode($$, 2, $2, $1); 163 | } 164 | | CompSt { 165 | $$ = createNonTerminal("Stmt"); 166 | insertNode($$, 1, $1); 167 | } 168 | | RETURN Exp SEMI { 169 | $$ = createNonTerminal("Stmt"); 170 | insertNode($$, 3, $3, $2, $1); 171 | } 172 | | IF LP Exp RP Stmt %prec LOWER_THAN_ELSE { 173 | $$ = createNonTerminal("Stmt"); 174 | insertNode($$, 5, $5, $4, $3, $2, $1); 175 | } 176 | | IF LP Exp RP Stmt ELSE Stmt { 177 | $$ = createNonTerminal("Stmt"); 178 | insertNode($$, 7, $7, $6, $5, $4, $3, $2, $1); 179 | } 180 | | WHILE LP Exp RP Stmt { 181 | $$ = createNonTerminal("Stmt"); 182 | insertNode($$, 5, $5, $4, $3, $2, $1); 183 | } 184 | | error SEMI { hasError = 1; } 185 | ; 186 | 187 | 188 | DefList: Def DefList { 189 | $$ = createNonTerminal("DefList"); 190 | insertNode($$, 2, $2, $1); 191 | } 192 | | { $$ = NULL; } 193 | ; 194 | Def: Specifier DecList SEMI { 195 | $$ = createNonTerminal("Def"); 196 | insertNode($$, 3, $3, $2, $1); 197 | } 198 | | error SEMI { hasError = 1; } 199 | ; 200 | DecList: Dec { 201 | $$ = createNonTerminal("DecList"); 202 | insertNode($$, 1, $1); 203 | } 204 | | Dec COMMA DecList { 205 | $$ = createNonTerminal("DecList"); 206 | insertNode($$, 3, $3, $2, $1); 207 | } 208 | ; 209 | Dec: VarDec { 210 | $$ = createNonTerminal("Dec"); 211 | insertNode($$, 1, $1); 212 | } 213 | | VarDec ASSIGNOP Exp { 214 | $$ = createNonTerminal("Dec"); 215 | insertNode($$, 3, $3, $2, $1); 216 | } 217 | ; 218 | 219 | 220 | Exp: Exp ASSIGNOP Exp { 221 | $$ = createNonTerminal("Exp"); 222 | insertNode($$, 3, $3, $2, $1); 223 | } 224 | | Exp AND Exp { 225 | $$ = createNonTerminal("Exp"); 226 | insertNode($$, 3, $3, $2, $1); 227 | } 228 | | Exp OR Exp { 229 | $$ = createNonTerminal("Exp"); 230 | insertNode($$, 3, $3, $2, $1); 231 | } 232 | | Exp RELOP Exp { 233 | $$ = createNonTerminal("Exp"); 234 | insertNode($$, 3, $3, $2, $1); 235 | } 236 | | Exp PLUS Exp { 237 | $$ = createNonTerminal("Exp"); 238 | insertNode($$, 3, $3, $2, $1); 239 | } 240 | | Exp MINUS Exp { 241 | $$ = createNonTerminal("Exp"); 242 | insertNode($$, 3, $3, $2, $1); 243 | } 244 | | Exp STAR Exp { 245 | $$ = createNonTerminal("Exp"); 246 | insertNode($$, 3, $3, $2, $1); 247 | } 248 | | Exp DIV Exp { 249 | $$ = createNonTerminal("Exp"); 250 | insertNode($$, 3, $3, $2, $1); 251 | } 252 | | LP Exp RP { 253 | $$ = createNonTerminal("Exp"); 254 | insertNode($$, 3, $3, $2, $1); 255 | } 256 | | MINUS Exp { 257 | $$ = createNonTerminal("Exp"); 258 | insertNode($$, 2, $2, $1); 259 | } 260 | | NOT Exp { 261 | $$ = createNonTerminal("Exp"); 262 | insertNode($$, 2, $2, $1); 263 | } 264 | | ID LP Args RP { 265 | $$ = createNonTerminal("Exp"); 266 | insertNode($$, 4, $4, $3, $2, $1); 267 | } 268 | | ID LP RP { 269 | $$ = createNonTerminal("Exp"); 270 | insertNode($$, 3, $3, $2, $1); 271 | } 272 | | Exp LB Exp RB { 273 | $$ = createNonTerminal("Exp"); 274 | insertNode($$, 4, $4, $3, $2, $1); 275 | } 276 | | Exp DOT ID { 277 | $$ = createNonTerminal("Exp"); 278 | insertNode($$, 3, $3, $2, $1); 279 | } 280 | | ID { 281 | $$ = createNonTerminal("Exp"); 282 | insertNode($$, 1, $1); 283 | } 284 | | INT { 285 | $$ = createNonTerminal("Exp"); 286 | insertNode($$, 1, $1); 287 | } 288 | | FLOAT { 289 | $$ = createNonTerminal("Exp"); 290 | insertNode($$, 1, $1); 291 | } 292 | | error RP { hasError = 1; } 293 | | error RB { hasError = 1; } 294 | ; 295 | Args: Exp COMMA Args { 296 | $$ = createNonTerminal("Args"); 297 | insertNode($$, 3, $3, $2, $1); 298 | } 299 | | Exp { 300 | $$ = createNonTerminal("Args"); 301 | insertNode($$, 1, $1); 302 | } 303 | ; 304 | 305 | %% 306 | 307 | void yyerror(char* msg) 308 | { 309 | fprintf(stderr, "Error type B at Line %d: %s\n", yylineno, msg); 310 | } -------------------------------------------------------------------------------- /编译系统实验/实验二/编译原理实验指导书-语义分析(标注版).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验二/编译原理实验指导书-语义分析(标注版).pdf -------------------------------------------------------------------------------- /编译系统实验/实验二/编译系统实验二-1190201215-冯开来.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验二/编译系统实验二-1190201215-冯开来.docx -------------------------------------------------------------------------------- /编译系统实验/实验二/编译系统实验二-1190201215-冯开来.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Carlofkl/HIT2022CompilerPrinciple/df33a922afec02363a1ff706f9738cc83e535669/编译系统实验/实验二/编译系统实验二-1190201215-冯开来.pdf --------------------------------------------------------------------------------