From 07f50899a327f5463b0c081f4edecf5b2cdc87fc Mon Sep 17 00:00:00 2001 From: tutuwel <12111744@mail.sustech.edu.cn> Date: Wed, 25 Oct 2023 15:09:45 +0800 Subject: [PATCH] pass all case except error --- phase1/Makefile | 7 +- phase1/lex.l | 193 +++++++++--- phase1/myOutput/output1.txt | 131 ++++----- phase1/myOutput/output10.txt | 1 - phase1/myOutput/output11.txt | 7 +- phase1/myOutput/output12.txt | 3 +- phase1/myOutput/output2.txt | 163 ++++++----- phase1/myOutput/output3.txt | 2 +- phase1/myOutput/output4.txt | 1 - phase1/myOutput/output5.txt | 151 +++++----- phase1/myOutput/output6.txt | 1 - phase1/myOutput/output7.txt | 2 +- phase1/myOutput/output8.txt | 1 - phase1/myOutput/output9.txt | 189 ++++++------ phase1/syntax.y | 552 ++++++----------------------------- phase1/tree.cpp | 144 ++++----- phase1/tree.hpp | 2 + 17 files changed, 650 insertions(+), 900 deletions(-) diff --git a/phase1/Makefile b/phase1/Makefile index 0a1083b..6f08e71 100644 --- a/phase1/Makefile +++ b/phase1/Makefile @@ -1,11 +1,10 @@ -CC=gcc +CC=g++ FLEX=flex BISON=bison # Dependency Tree splc: syntax.tab.* lex.yy.c - @mkdir -p bin - $(CC) syntax.tab.c -ll -o bin/splc + $(CC) syntax.tab.c tree.cpp -ll -o bin/splc @chmod +x bin/splc lex.yy.c: lex.l $(FLEX) lex.l @@ -17,4 +16,4 @@ lexer: syntax.tab.* lex.yy.c # For debugging lex.l .PHONY: clean clean: - @rm -rf lex.yy.c syntax.tab.c syntax.tab.h lexer bin + @rm -rf lex.yy.c syntax.tab.c syntax.tab.h lexer bin \ No newline at end of file diff --git a/phase1/lex.l b/phase1/lex.l index d46506a..a30b9b7 100644 --- a/phase1/lex.l +++ b/phase1/lex.l @@ -1,11 +1,10 @@ -%{ +%{ #define EXIT_OK 0 #define EXIT_FAIL 1 - #define YYSTYPE char * #include "string.h" #include "syntax.tab.h" - - /* Rewrite the internal macro in Flex to update the location */ +%} +%{ int yycolno = 1; #define YY_USER_ACTION \ yylloc.first_line = yylineno; \ @@ -16,7 +15,7 @@ %} %option yylineno - +%option debug valid_decimal_int 0|[1-9][0-9]* valid_hex_int [xX](0|[1-9a-f][0-9a-f]*) @@ -26,50 +25,164 @@ valid_hex_int [xX](0|[1-9a-f][0-9a-f]*) /*TODO: handle string*/ "//".* {} + "/*"((("*"[^/])?)|[^*])*"*/" {} -int|float|char { yylval = strdup(yytext); return TYPE; } -struct { yylval = "STRUCT"; return STRUCT; } -if { yylval = "IF"; return IF; } -else { yylval = "ELSE"; return ELSE; } -while { yylval = "WHILE"; return WHILE; } -return { yylval = "RETURN"; return RETURN; } -\\x[^0-9a-fA-F]* { - printf("Error type A at Line %d: unknown lexeme %s\n", yylineno, yytext); + +int|float|char { + yylval.node = new Node("TYPE",strdup(yytext)); + return TYPE; } + +\'(\\x{valid_hex_int}{2}|.)\' { + yylval.node = new Node("CHAR",strdup(yytext)); + return CHAR; +} + +"struct" { + yylval.node = new Node("STRUCT"); + return STRUCT; } + +"if" { + yylval.node = new Node("IF"); + return IF; +} + +"else" { + yylval.node = new Node("ELSE"); + return ELSE; +} + +"while" { + yylval.node = new Node("WHILE"); + return WHILE; +} + +"return" { + yylval.node = new Node("RETURN"); + return RETURN; +} + +";" { + yylval.node = new Node("SEMI"); + return SEMI; +} +"," { + yylval.node = new Node("COMMA"); + return COMMA; +} +"==" { + yylval.node = new Node("EQ"); + return EQ; +} +"<=" { + yylval.node = new Node("LE"); + return LE; +} +">=" { + yylval.node = new Node("GE"); + return GE; +} +"!=" { + yylval.node = new Node("NE"); + return NE; +} +"=" { + yylval.node = new Node("ASSIGN"); + return ASSIGN; } -'([^']|\\{valid_hex_int})' { yylval = yytext; return CHAR; } -";" { yylval = "SEMI"; return SEMI; } -"," { yylval = "COMMA"; return COMMA; } -"==" { yylval = "EQ"; return EQ; } -"<=" { yylval = "LE"; return LE; } -">=" { yylval = "GE"; return GE; } -"!=" { yylval = "NE"; return NE; } -"=" { yylval = "ASSIGN"; return ASSIGN; } -"!" { yylval = "NOT"; return NOT; } -"<" { yylval = "LT"; return LT; } -">" { yylval = "GT"; return GT; } -"+" { yylval = "PLUS"; return PLUS; } -"-" { yylval = "MINUS"; return MINUS; } -"*" { yylval = "MUL"; return MUL; } -"/" { yylval = "DIV"; return DIV; } -"&&" { yylval = "AND"; return AND; } -"||" { yylval = "OR"; return OR; } -"(" { yylval = "LP"; return LP; } -")" { yylval = "RP"; return RP; } -"[" { yylval = "LB"; return LB; } -"]" { yylval = "RB"; return RB; } -"{" { yylval = "LC"; return LC; } -"}" { yylval = "RC"; return RC; } -[_a-zA-Z][_0-9a-zA-Z]* { yylval = strdup(yytext); return ID; } -({valid_decimal_int})\.[0-9]+ { yylval = strdup(yytext); return FLOAT; } -{valid_decimal_int}|0{valid_hex_int} { yylval = strdup(yytext); return INT; /* not include minus number */ } +"!" { + yylval.node = new Node("NOT"); + return NOT; +} +"<" { + yylval.node = new Node("LT"); + return LT; +} +">" { + yylval.node = new Node("GT"); + return GT; +} +"+" { + yylval.node = new Node("PLUS"); + return PLUS; +} +"-" { + yylval.node = new Node("MINUS"); + return MINUS; +} +"*" { + yylval.node = new Node("MUL"); + return MUL; +} +"/" { + yylval.node = new Node("DIV"); + return DIV; +} +"&&" { + yylval.node = new Node("AND"); + return AND; +} +"||" { + yylval.node = new Node("OR"); + return OR; +} +"(" { + yylval.node = new Node("LP"); + return LP; +} +")" { + yylval.node = new Node("RP"); + return RP; +} +"[" { + yylval.node = new Node("LB"); + return LB; +} +"]" { + yylval.node = new Node("RB"); + return RB; +} +"{" { + yylval.node = new Node("LC"); + return LC; +} +"}" { + yylval.node = new Node("RC"); + return RC; +} + +[_a-zA-Z][_0-9a-zA-Z]* { + yylval.node = new Node("ID",strdup(yytext)); + return ID; } + +({valid_decimal_int})\.[0-9]+ { + yylval.node = new Node("FLOAT", (float)atof(yytext)); + return FLOAT; } + +{valid_decimal_int}|0{valid_hex_int} { + yylval.node = new Node("INT", atoi(yytext)); + return INT; /* not include minus number */ } + +"." { + yylval.node = new Node("DOT"); + return DOT; } + \\x(0|[1-9a-f][0-9a-f]*)[^0-9a-fA-F] { printf("Error type A at Line %d: unknown lexeme %s\n", yylineno, strdup(yytext)); } -"." { yylval = "DOT"; return DOT; } + 0[Xx]([^0-9a-fA-F]|([1-9a-fA-F][0-9a-fA-F]*[^0-9a-fA-F]+)) { printf("Error type A at Line %d: unknown lexeme %s\n", yylineno, strdup(yytext)); } [$@] { printf("Error type A at Line %d: unknown lexeme %s\n", yylineno, strdup(yytext)); } +\\x[^0-9a-fA-F]* { + printf("Error type A at Line %d: unknown lexeme %s\n", yylineno, yytext); +} + +\n { yycolno = 1;} + +" " {} %% + + \ No newline at end of file diff --git a/phase1/myOutput/output1.txt b/phase1/myOutput/output1.txt index 038cba0..b4b7534 100644 --- a/phase1/myOutput/output1.txt +++ b/phase1/myOutput/output1.txt @@ -1,65 +1,66 @@ - Program (1) -ExtDefList (1) -ExtDef (1) -Specifier (1) -TYPE: int -FunDec (1) -ID: test_1_r01 -LP -VarList (1) -ParamDec (1) -Specifier (1) -TYPE: int -VarDec (1) -ID: a -COMMA -VarList (1) -ParamDec (1) -Specifier (1) -TYPE: int -VarDec (1) -ID: b -RP -CompSt (2) -LC -StmtList (3) -Stmt (3) -Exp (3) -Exp (3) -ID: c -ASSIGN -Exp (3) -CHAR: 'c' -SEMI -Stmt (4) -IF -LP -Exp (4) -Exp (4) -ID: a -GT -Exp (4) -ID: b -RP -Stmt (5) -CompSt (5) -LC -StmtList (6) -Stmt (6) -RETURN -Exp (6) -ID: a -SEMI -RC -ELSE -Stmt (9) -CompSt (9) -LC -StmtList (10) -Stmt (10) -RETURN -Exp (10) -ID: b -SEMI -RC -RC +Program (1) + ExtDefList (1) + ExtDef (1) + Specifier (1) + TYPE: int + FunDec (1) + ID: test_1_r01 + LP + VarList (1) + ParamDec (1) + Specifier (1) + TYPE: int + VarDec (1) + ID: a + COMMA + VarList (1) + ParamDec (1) + Specifier (1) + TYPE: int + VarDec (1) + ID: b + RP + CompSt (2) + LC + StmtList (3) + Stmt (3) + Exp (3) + Exp (3) + ID: c + ASSIGN + Exp (3) + CHAR: 'c' + SEMI + StmtList (4) + Stmt (4) + IF + LP + Exp (4) + Exp (4) + ID: a + GT + Exp (4) + ID: b + RP + Stmt (5) + CompSt (5) + LC + StmtList (6) + Stmt (6) + RETURN + Exp (6) + ID: a + SEMI + RC + ELSE + Stmt (9) + CompSt (9) + LC + StmtList (10) + Stmt (10) + RETURN + Exp (10) + ID: b + SEMI + RC + RC diff --git a/phase1/myOutput/output10.txt b/phase1/myOutput/output10.txt index 288ce69..e69de29 100644 --- a/phase1/myOutput/output10.txt +++ b/phase1/myOutput/output10.txt @@ -1 +0,0 @@ - Error type B at Line 5: Missing semicolon int diff --git a/phase1/myOutput/output11.txt b/phase1/myOutput/output11.txt index d50e494..251ca29 100644 --- a/phase1/myOutput/output11.txt +++ b/phase1/myOutput/output11.txt @@ -1,7 +1,6 @@ - Error type A at Line 4: unknown lexeme 0x77G; +Error type A at Line 4: unknown lexeme 0x77G; int _wrong_h - Error type A at Line 5: unknown lexeme 0xCS; +Error type A at Line 5: unknown lexeme 0xCS; int _right_h - Error type A at Line 6: unknown lexeme 0xdeadbeef; +Error type A at Line 6: unknown lexeme 0xdeadbeef; -Error type B at Line 6: Missing semicolon char diff --git a/phase1/myOutput/output12.txt b/phase1/myOutput/output12.txt index df68761..dbeb753 100644 --- a/phase1/myOutput/output12.txt +++ b/phase1/myOutput/output12.txt @@ -1,3 +1,2 @@ - Error type A at Line 5: unknown lexeme 0xFFF; +Error type A at Line 5: unknown lexeme 0xFFF; -Error type B at Line 5: Missing semicolon char diff --git a/phase1/myOutput/output2.txt b/phase1/myOutput/output2.txt index 7dd9766..7cd3d46 100644 --- a/phase1/myOutput/output2.txt +++ b/phase1/myOutput/output2.txt @@ -1,80 +1,83 @@ - Program (1) -ExtDefList (1) -ExtDef (1) -Specifier (1) -TYPE: int -ExtDecList (1) -VarDec (1) -ID: global -SEMI -ExtDef (2) -Specifier (2) -StructSpecifier (2) -STRUCT (2) -STRUCT -ID: my_struct -LC -DefList (4) -Def (4) -Specifier (4) -TYPE: int -Specifier (4) -Dec (4) -VarDec (4) -ID: code -SEMI -Def (5) -Specifier (5) -TYPE: char -Specifier (5) -Dec (5) -VarDec (5) -ID: data -SEMI -RC -SEMI -ExtDef (7) -Specifier (7) -TYPE: int -FunDec (7) -ID: test_1_r02 -LP -RP -CompSt (8) -LC -Def (9) -Specifier (9) -StructSpecifier (9) -STRUCT (9) -STRUCT -ID: my_struct -Specifier (9) -Dec (9) -VarDec (9) -ID: obj -SEMI -StmtList (10) -Stmt (10) -Exp (10) -Exp (10) -Exp (10) -ID: obj -RC -ID: code -ASSIGN -Exp (10) -ID: global -SEMI -Stmt (11) -Exp (11) -Exp (11) -Exp (11) -ID: global -ASSIGN -Exp (11) -ID: global -PLUS -Exp (11) -INT: 1 -SEMI -RC +Program (1) + ExtDefList (1) + ExtDef (1) + Specifier (1) + TYPE: int + ExtDecList (1) + VarDec (1) + ID: global + SEMI + ExtDefList (2) + ExtDef (2) + Specifier (2) + StructSpecifier (2) + STRUCT + ID: my_struct + LC + DefList (4) + Def (4) + Specifier (4) + TYPE: int + DecList (4) + Dec (4) + VarDec (4) + ID: code + SEMI + DefList (5) + Def (5) + Specifier (5) + TYPE: char + DecList (5) + Dec (5) + VarDec (5) + ID: data + SEMI + RC + SEMI + ExtDefList (7) + ExtDef (7) + Specifier (7) + TYPE: int + FunDec (7) + ID: test_1_r02 + LP + RP + CompSt (8) + LC + DefList (9) + Def (9) + Specifier (9) + StructSpecifier (9) + STRUCT + ID: my_struct + DecList (9) + Dec (9) + VarDec (9) + ID: obj + SEMI + StmtList (10) + Stmt (10) + Exp (10) + Exp (10) + Exp (10) + ID: obj + DOT + ID: code + ASSIGN + Exp (10) + ID: global + SEMI + StmtList (11) + Stmt (11) + Exp (11) + Exp (11) + ID: global + ASSIGN + Exp (11) + Exp (11) + ID: global + PLUS + Exp (11) + INT: 1 + SEMI + RC diff --git a/phase1/myOutput/output3.txt b/phase1/myOutput/output3.txt index 05c6d71..49b2e89 100644 --- a/phase1/myOutput/output3.txt +++ b/phase1/myOutput/output3.txt @@ -1 +1 @@ - $Error type B at Line 4: Missing semicolon ; + Error type A at Line 4: unknown lexeme $ diff --git a/phase1/myOutput/output4.txt b/phase1/myOutput/output4.txt index 7e00ceb..e69de29 100644 --- a/phase1/myOutput/output4.txt +++ b/phase1/myOutput/output4.txt @@ -1 +0,0 @@ - Error type B at Line 10: Missing semicolon { diff --git a/phase1/myOutput/output5.txt b/phase1/myOutput/output5.txt index 226574c..002dda6 100644 --- a/phase1/myOutput/output5.txt +++ b/phase1/myOutput/output5.txt @@ -1,74 +1,77 @@ - Program (1) -ExtDefList (1) -ExtDef (1) -Specifier (1) -TYPE: int -FunDec (1) -ID: test_1_r05 -LP -VarList (1) -ParamDec (1) -Specifier (1) -TYPE: int -VarDec (1) -ID: n -RP -CompSt (2) -LC -Def (3) -Specifier (3) -TYPE: int -Specifier (3) -Dec (3) -VarDec (3) -ID: res -ASSIGN -Exp (3) -INT: 1 -SEMI -StmtList (4) -Stmt (4) -WHILE -LP -Exp (4) -Exp (4) -ID: n -GT -Exp (4) -INT: 0 -RP -Stmt (5) -CompSt (5) -LC -StmtList (6) -Stmt (6) -Exp (6) -Exp (6) -Exp (6) -ID: res -ASSIGN -Exp (6) -ID: res -MUL -Exp (6) -ID: n -SEMI -Stmt (7) -Exp (7) -Exp (7) -Exp (7) -ID: n -ASSIGN -Exp (7) -ID: n -MINUS -Exp (7) -INT: 1 -SEMI -RC -Stmt (9) -RETURN -Exp (9) -ID: res -SEMI -RC +Program (1) + ExtDefList (1) + ExtDef (1) + Specifier (1) + TYPE: int + FunDec (1) + ID: test_1_r05 + LP + VarList (1) + ParamDec (1) + Specifier (1) + TYPE: int + VarDec (1) + ID: n + RP + CompSt (2) + LC + DefList (3) + Def (3) + Specifier (3) + TYPE: int + DecList (3) + Dec (3) + VarDec (3) + ID: res + ASSIGN + Exp (3) + INT: 1 + SEMI + StmtList (4) + Stmt (4) + WHILE + LP + Exp (4) + Exp (4) + ID: n + GT + Exp (4) + INT: 0 + RP + Stmt (5) + CompSt (5) + LC + StmtList (6) + Stmt (6) + Exp (6) + Exp (6) + ID: res + ASSIGN + Exp (6) + Exp (6) + ID: res + MUL + Exp (6) + ID: n + SEMI + StmtList (7) + Stmt (7) + Exp (7) + Exp (7) + ID: n + ASSIGN + Exp (7) + Exp (7) + ID: n + MINUS + Exp (7) + INT: 1 + SEMI + RC + StmtList (9) + Stmt (9) + RETURN + Exp (9) + ID: res + SEMI + RC diff --git a/phase1/myOutput/output6.txt b/phase1/myOutput/output6.txt index 826fe26..e69de29 100644 --- a/phase1/myOutput/output6.txt +++ b/phase1/myOutput/output6.txt @@ -1 +0,0 @@ - Error type B at Line 4: Missing semicolon 3 diff --git a/phase1/myOutput/output7.txt b/phase1/myOutput/output7.txt index f2e5186..a3871d4 100644 --- a/phase1/myOutput/output7.txt +++ b/phase1/myOutput/output7.txt @@ -1 +1 @@ - | Error type B at Line 7: Missing semicolon 4 +| \ No newline at end of file diff --git a/phase1/myOutput/output8.txt b/phase1/myOutput/output8.txt index c5f2e7f..e69de29 100644 --- a/phase1/myOutput/output8.txt +++ b/phase1/myOutput/output8.txt @@ -1 +0,0 @@ - Error type B at Line 4: Missing semicolon if diff --git a/phase1/myOutput/output9.txt b/phase1/myOutput/output9.txt index eeb6e20..0f323a5 100644 --- a/phase1/myOutput/output9.txt +++ b/phase1/myOutput/output9.txt @@ -1,92 +1,97 @@ - Program (1) -ExtDefList (1) -ExtDef (1) -Specifier (1) -TYPE: int -FunDec (1) -ID: test_1_r09 -LP -RP -CompSt (2) -LC -Def (3) -Specifier (3) -TYPE: int -Specifier (3) -Dec (3) -VarDec (3) -ID: m -ASSIGN -Exp (3) -INT: 1 -SEMI -Def (4) -Specifier (4) -TYPE: float -Specifier (4) -Dec (4) -VarDec (4) -ID: n -ASSIGN -Exp (4) -FLOAT: 2.2 -SEMI -Def (5) -Specifier (5) -TYPE: int -Specifier (5) -Dec (5) -VarDec (5) -VarDec (5) -ID: x -LB -INT: 5 -RB -COMMA -DecList (5) -Dec (5) -VarDec (5) -VarDec (5) -ID: y -LB -INT: 10 -RB -SEMI -StmtList (6) -Stmt (6) -Exp (6) -Exp (6) -Exp (6) -ID: x -LB -Exp (6) -ID: m -RB -ASSIGN -Exp (6) -INT: 7 -SEMI -Stmt (7) -Exp (7) -Exp (7) -Exp (7) -ID: y -LB -Exp (7) -ID: n -RB -ASSIGN -Exp (7) -INT: 8 -SEMI -Stmt (8) -RETURN -Exp (8) -Exp (8) -ID: x -LB -Exp (8) -ID: y -RB -SEMI -RC +Program (1) + ExtDefList (1) + ExtDef (1) + Specifier (1) + TYPE: int + FunDec (1) + ID: test_1_r09 + LP + RP + CompSt (2) + LC + DefList (3) + Def (3) + Specifier (3) + TYPE: int + DecList (3) + Dec (3) + VarDec (3) + ID: m + ASSIGN + Exp (3) + INT: 1 + SEMI + DefList (4) + Def (4) + Specifier (4) + TYPE: float + DecList (4) + Dec (4) + VarDec (4) + ID: n + ASSIGN + Exp (4) + FLOAT: 2.2 + SEMI + DefList (5) + Def (5) + Specifier (5) + TYPE: int + DecList (5) + Dec (5) + VarDec (5) + VarDec (5) + ID: x + LB + INT: 5 + RB + COMMA + DecList (5) + Dec (5) + VarDec (5) + VarDec (5) + ID: y + LB + INT: 10 + RB + SEMI + StmtList (6) + Stmt (6) + Exp (6) + Exp (6) + Exp (6) + ID: x + LB + Exp (6) + ID: m + RB + ASSIGN + Exp (6) + INT: 7 + SEMI + StmtList (7) + Stmt (7) + Exp (7) + Exp (7) + Exp (7) + ID: y + LB + Exp (7) + ID: n + RB + ASSIGN + Exp (7) + INT: 8 + SEMI + StmtList (8) + Stmt (8) + RETURN + Exp (8) + Exp (8) + ID: x + LB + Exp (8) + ID: y + RB + SEMI + RC diff --git a/phase1/syntax.y b/phase1/syntax.y index 2ce7bd4..645d82f 100644 --- a/phase1/syntax.y +++ b/phase1/syntax.y @@ -1,23 +1,24 @@ -%{ +%{ #include "tree.hpp" #include "lex.yy.c" #include #include extern int yylineno; void yyerror(const char*); - char* get_str2(const char* name, const char* str1); - char* get_str3(const char* name, const char* str1,const char* str2); - char* get_str4(const char* name, const char* str1, const char* str2, const char* str3); - char* get_str5(const char* name, const char* str1,const char* str2,const char* str3,const char* str4); - char* get_str6(const char* name, const char* str1,const char* str2,const char* str3,const char* str4,const char* str5); - char* get_name_posi(const char* arg, int posi); - char* get_name_val(const char* arg, const char* val); - %} + %locations -%define parse.error verbose -%token STRUCT IF ELSE WHILE RETURN SEMI COMMA -%token EQ LE GE NE ASSIGN NOT LT GT PLUS MINUS MUL DIV AND OR -%token LP RP LB RB LC RC INT FLOAT CHAR ID TYPE DOT + +%union { + Node* node; +} + +%nonassoc LOWER_ELSE +%nonassoc ELSE + +%token STRUCT IF WHILE RETURN SEMI COMMA +%token EQ LE GE NE ASSIGN NOT LT GT PLUS MINUS MUL DIV AND OR +%token LP RP LB RB LC RC DOT INT FLOAT CHAR ID TYPE +%left ASSIGN %left OR %left AND %left EQ NE @@ -25,485 +26,110 @@ %left PLUS MINUS %left MUL DIV %right NOT -%left ASSIGN %left DOT %left LB RB + +%type Program ExtDefList ExtDef ExtDecList +%type Specifier StructSpecifier +%type VarDec FunDec VarList ParamDec CompSt StmtList +%type Stmt DefList Def DecList Dec Exp Args + %% /* high-level definition */ /* global variable declarations and function definitions */ -output: Program{ -char* name1=get_name_posi("Program",@1.first_line); -char* str1=get_str2(name1,$1); -printf("%s",str1); -} -Program: ExtDefList { -char* name1=get_name_posi("ExtDefList",@1.first_line); -char* str1=get_str2(name1,$1); -$$=str1; -} -ExtDefList: %empty { - $$=""; +Program: ExtDefList { + $$=new Node("Program",1,@$.first_line,$1); + printTree($$); } - | ExtDef ExtDefList { - char* name1=get_name_posi("ExtDef",@1.first_line); - char* str1=get_str2(name1,$1); - $$=get_str2(str1,$2); - } -ExtDef: Specifier ExtDecList SEMI { - char* name1=get_name_posi("Specifier",@1.first_line); - char* str1=get_str2(name1,$1); - char* name2=get_name_posi("ExtDecList",@2.first_line); - char* str2=get_str2(name2,$2); - $$=get_str3(str1,str2,$3); - } - | Specifier SEMI { - char* name1=get_name_posi("Specifier",@1.first_line); - char* str1=get_str2(name1,$1); - $$=get_str2(str1,$2); - } - | Specifier FunDec CompSt { - char* name1=get_name_posi("Specifier",@1.first_line); - char* str1=get_str2(name1,$1); - char* name2=get_name_posi("FunDec",@2.first_line); - char* str2=get_str2(name2,$2); - char* name3=get_name_posi("CompSt",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,str2,str3); - } -ExtDecList: VarDec { - char* name1=get_name_posi("VarDec",@1.first_line); - char* str1=get_str2(name1,$1); - $$=str1; -} - | VarDec COMMA ExtDecList { - char* name1=get_name_posi("VarDec",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("ExtDecList",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); - } +ExtDefList: %empty {$$=new Node("ExtDefList");} + | ExtDef ExtDefList {$$=new Node("ExtDefList",2,@$.first_line,$1,$2);} +ExtDef: Specifier ExtDecList SEMI { $$=new Node("ExtDef",3,@$.first_line,$1,$2,$3);} + | Specifier SEMI { $$=new Node("ExtDef",2,@$.first_line,$1,$2);} + | Specifier FunDec CompSt { $$=new Node("ExtDef",3,@$.first_line,$1,$2,$3);} +ExtDecList: VarDec {$$=new Node("ExtDecList",1,@$.first_line,$1);} + | VarDec COMMA ExtDecList { $$=new Node("ExtDecList",3,@$.first_line,$1,$2,$3);} /* specifier */ /* primitive types (int, float and char) and structure type */ -Specifier: TYPE { - char* name1=get_name_val("TYPE",$1); - $$=name1; - } - | StructSpecifier { - char* name1=get_name_posi("StructSpecifier",@1.first_line); - char* str1=get_str2(name1,$1); - $$=str1; - } -StructSpecifier: STRUCT ID LC DefList RC { - char* name1=get_name_posi("STRUCT",@1.first_line); - char* str1=get_str2(name1,$1); - char* name2=get_name_val("ID",$2); - char* name4=get_name_posi("DefList",@4.first_line); - char* str4=get_str2(name4,$4); - $$=get_str5(str1,name2,$3,str4,$5); - } - | STRUCT ID { - char* name1=get_name_posi("STRUCT",@1.first_line); - char* str1=get_str2(name1,$1); - char* name2=get_name_val("ID",$2); - $$=get_str2(str1,name2); - } +Specifier: TYPE {$$=new Node("Specifier",1,@$.first_line,$1);} + | StructSpecifier {$$=new Node("Specifier",1,@$.first_line,$1);} +StructSpecifier: STRUCT ID LC DefList RC {$$=new Node("StructSpecifier", 5, @$.first_line, $1, $2, $3, $4, $5);} + | STRUCT ID { $$=new Node("StructSpecifier",2,@$.first_line,$1,$2);} /* declarator */ /* variable and function declaration */ -VarDec: ID { - char* name1=get_name_val("ID",$1); - $$=name1; - } - | VarDec LB INT RB { - char* name1=get_name_posi("VarDec",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_val("INT",$3); - $$=get_str4(str1,$2,name3,$4); - } -FunDec: ID LP VarList RP { - char* name1=get_name_val("ID",$1); - char* name3=get_name_posi("VarList",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str4(name1,$2,str3,$4); - } - | ID LP RP { - char* name1=get_name_val("ID",$1); - $$=get_str3(name1,$2,$3); - } -VarList: ParamDec COMMA VarList { - char* name1=get_name_posi("ParamDec",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("VarList",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); - } - | ParamDec { - char* name1=get_name_posi("ParamDec",@1.first_line); - char* str1=get_str2(name1,$1); - $$=str1; - } -ParamDec: Specifier VarDec { - char* name1=get_name_posi("Specifier",@1.first_line); - char* str1=get_str2(name1,$1); - char* name2=get_name_posi("VarDec",@2.first_line); - char* str2=get_str2(name2,$2); - $$=get_str2(str1,str2); - } +VarDec: ID {$$=new Node("VarDec",1,@$.first_line,$1); } + | VarDec LB INT RB { $$=new Node("VarDec",4,@$.first_line,$1,$2,$3,$4);} +FunDec: ID LP VarList RP { $$=new Node("FunDec",4,@$.first_line,$1,$2,$3,$4);} + | ID LP RP {$$=new Node("FunDec",3,@$.first_line,$1,$2,$3);} +VarList: ParamDec COMMA VarList {$$=new Node("VarList",3,@$.first_line,$1,$2,$3);} + | ParamDec { $$=new Node("VarList",1,@$.first_line,$1);} +ParamDec: Specifier VarDec { $$=new Node("ParamDec",2,@$.first_line,$1,$2);} /* statement */ /* specifies several program structures */ /*Here is a change on DefList, may need to deal*/ /*deflist is empty now!*/ -CompSt: LC DefList StmtList RC { - char* name3=get_name_posi("StmtList",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str4($1,$2,str3,$4); - } -StmtList: %empty -{ - $$=""; -} - | Stmt StmtList { - char* name1=get_name_posi("Stmt",@1.first_line); - char* str1=get_str2(name1,$1); - $$=get_str2(str1,$2); - } - | ifelseStmt StmtList { - char* name1=get_name_posi("Stmt",@1.first_line); - char* str1=get_str2(name1,$1); - $$=get_str2(str1,$2); } - | ifstmt StmtList { - char* name1=get_name_posi("Stmt",@1.first_line); - char* str1=get_str2(name1,$1); - $$=get_str2(str1,$2); } +CompSt: LC DefList StmtList RC {$$=new Node("CompSt",4,@$.first_line,$1,$2,$3,$4);} +StmtList: + %empty + {$$=new Node("StmtList",@$.first_line);} + | Stmt StmtList { $$=new Node("StmtList",2,@$.first_line,$1,$2);} -Stmt: Exp SEMI { - char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - $$=get_str2(str1,$2); - } - | CompSt { - char* name1=get_name_posi("CompSt",@1.first_line); - char* str1=get_str2(name1,$1); - $$=str1; - } - | RETURN Exp SEMI { - char* name2=get_name_posi("Exp",@2.first_line); - char* str2=get_str2(name2,$2); - $$=get_str3($1,str2,$3); - } - | WHILE LP Exp RP Stmt { - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - char* name5=get_name_posi("Stmt",@5.first_line); - char* str5=get_str2(name5,$5); - $$=get_str5($1,$2,str3,$4,str5); - } -ifelseStmt: ifstmt elsest { - $$=get_str2($1,$2); -} -ifstmt: IF LP Exp RP Stmt { - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - char* name5=get_name_posi("Stmt",@5.first_line); - char* str5=get_str2(name5,$5); - $$=get_str5($1,$2,str3,$4,str5); - } -elsest: ELSE Stmt { - char* name2=get_name_posi("Stmt",@2.first_line); - char* str2=get_str2(name2,$2); - $$=get_str2($1,str2); - } +Stmt: Exp SEMI {$$=new Node("Stmt",2,@$.first_line,$1,$2);} + | CompSt {$$=new Node("Stmt",1,@$.first_line,$1);} + | RETURN Exp SEMI {$$=new Node("Stmt",3,@$.first_line,$1,$2,$3);} + | WHILE LP Exp RP Stmt {$$=new Node("Stmt", 5, @$.first_line, $1, $2, $3, $4, $5);} + | IF LP Exp RP Stmt %prec LOWER_ELSE {$$=new Node("Stmt", 5, @$.first_line, $1, $2, $3, $4, $5);} + | IF LP Exp RP Stmt ELSE Stmt {$$=new Node( "Stmt", 7, @$.first_line, $1, $2, $3, $4, $5, $6, $7);} /* local definition */ /* declaration and assignment of local variables */ -DefList: %empty -{ - $$=""; -} - | Def DefList { - char* name1=get_name_posi("Def",@1.first_line); - char* str1=get_str2(name1,$1); - $$=get_str2(str1,$2); - } -Def: Specifier DecList SEMI { - char* name1=get_name_posi("Specifier",@1.first_line); - char* str1=get_str2(name1,$1); - char* name2=get_name_posi("DecList",@2.first_line); - char* str2=get_str2(name1,$2); - $$=get_str3(str1,str2,$3); - } -DecList: Dec { - char* name1=get_name_posi("Dec",@1.first_line); - char* str1=get_str2(name1,$1); - $$=str1; - } - | Dec COMMA DecList { - char* name1=get_name_posi("Dec",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("DecList",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); - } -Dec: VarDec { - char* name1=get_name_posi("VarDec",@1.first_line); - char* str1=get_str2(name1,$1); - $$=str1; - } - | VarDec ASSIGN Exp { - char* name1=get_name_posi("VarDec",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); - } +DefList: %empty{$$=new Node("DefList",@$.first_line);} + | Def DefList {$$=new Node("DefList",2,@$.first_line,$1,$2);} +Def: Specifier DecList SEMI {$$=new Node("Def",3,@$.first_line,$1,$2,$3);} +DecList: Dec {$$=new Node("DecList",1,@$.first_line,$1);} + | Dec COMMA DecList {$$=new Node("DecList",3,@$.first_line,$1,$2,$3);} + +Dec: VarDec {$$=new Node("Dec",1,@$.first_line,$1);} + | VarDec ASSIGN Exp {$$=new Node("Dec",3,@$.first_line,$1,$2,$3);} /* Expression */ /* a single constant, or operations on variables */ -Exp: Exp ASSIGN Exp { - char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); - } - | Exp AND Exp { - char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); - } - | Exp OR Exp { - char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); - } - | Exp LT Exp { - char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); } - | Exp LE Exp { char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); } - | Exp GT Exp { char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); } - | Exp GE Exp { char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); } - | Exp NE Exp { char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); } - | Exp EQ Exp { char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); } - | Exp PLUS Exp { char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); } - | Exp MINUS Exp { char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); } - | Exp MUL Exp { char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); } - | Exp DIV Exp { char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str3(str1,$2,str3); } - | LP Exp RP { - char* name2=get_name_posi("Exp",@2.first_line); - char* str2=get_str2(name2,$2); - $$=get_str3($1,str2,$3); } - | MINUS Exp { - char* name2=get_name_posi("Exp",@2.first_line); - char* str2=get_str2(name2,$2); - $$=get_str2($1,str2); } - | NOT Exp { char* name2=get_name_posi("Exp",@2.first_line); - char* str2=get_str2(name2,$2); - $$=get_str2($1,str2); } - | ID LP Args RP { - char* name1=get_name_val("ID",$1); - char* name3=get_name_posi("Args",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str4(name1,$2,str3,$4);} - | ID LP RP { - char* name1=get_name_val("ID",$1); - $$=get_str3(name1,$2,$3); } - | Exp LB Exp RB { - char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Exp",@3.first_line); - char* str3=get_str2(name3,$3); - $$=get_str4(str1,$2,str3,$4);} - | Exp DOT ID { - char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_val("ID",$3); - $$=get_str3(str1,$2,name3); - } - | ID { - $$= get_name_val("ID",$1); - } - | INT { - $$= get_name_val("INT",$1); - } - | FLOAT { - $$= get_name_val("FLOAT",$1); - } - | CHAR { - $$= get_name_val("CHAR",$1); - } - -Args: Exp COMMA Args { - char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - char* name3=get_name_posi("Args",@3.first_line); - char* str3=get_str2(name3,$3); - $$=(char*)get_str3(str1,$2,str3); - } - | Exp { - char* name1=get_name_posi("Exp",@1.first_line); - char* str1=get_str2(name1,$1); - $$=str1; - } +Exp: Exp ASSIGN Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp AND Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp OR Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp LT Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp LE Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp GT Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp GE Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp NE Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp EQ Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp PLUS Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp MINUS Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp MUL Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp DIV Exp {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | LP Exp RP {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | MINUS Exp {$$=new Node("Exp",2,@$.first_line,$1,$2);} + | NOT Exp {$$=new Node("Exp",2,@$.first_line,$1,$2);} + | ID LP Args RP {$$=new Node("Exp",4,@$.first_line,$1,$2,$3,$4);} + | ID LP RP {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | Exp LB Exp RB {$$=new Node("Exp",4,@$.first_line,$1,$2,$3,$4);} + | Exp DOT ID {$$=new Node("Exp",3,@$.first_line,$1,$2,$3);} + | ID {$$=new Node("Exp",1,@$.first_line,$1);} + | INT {$$=new Node("Exp",1,@$.first_line,$1);} + | FLOAT {$$=new Node("Exp",1,@$.first_line,$1);} + | CHAR {$$=new Node("Exp",1,@$.first_line,$1);} + +Args: Exp COMMA Args {$$=new Node("Args",3,@$.first_line,$1,$2,$3);} + | Exp {$$=new Node("Args",1,@$.first_line,$1);} %% -char* get_name_posi(const char* arg, int posi){ - char* name= (char *)malloc(50); - if (name){ - sprintf(name, "%s (%d)\n",arg, posi); - } - return name; -} - -char* get_name_val(const char* arg, const char* val){ - char* name= (char *)malloc(50); - if (name){ - sprintf(name, "%s: %s\n",arg,val); - } - return name; -} - -char* get_str6(const char* name, const char* str1,const char* str2,const char* str3,const char* str4,const char* str5){ - char* result = (char*)malloc(strlen(name)+strlen(str1) + strlen(str2) + strlen(str3) + strlen(str4) + strlen(str5) + 1); - if(result){ - strcpy(result, " "); - strcat(result, name); // Copy the first string - - strcat(result, str1); - - strcat(result, str2); // Append the second string - - strcat(result, str3); // Append the third string - - strcat(result, str4); - - strcat(result, str5); - } - else{ - printf("get_str6 fail"); - } - - return result; -} - -char* get_str5(const char* name, const char* str1,const char* str2,const char* str3,const char* str4){ - char* result = (char*)malloc(strlen(name)+strlen(str1) + strlen(str2) + strlen(str3) + strlen(str4) + 11); - if(result){ - strcpy(result, " "); - strcat(result, name); // Copy the first string - - strcat(result, str1); - - strcat(result, str2); // Append the second string - - strcat(result, str3); // Append the third string - - strcat(result, str4); - } - else{ - printf("get_str5 fail"); - } - - return result; -} - -char* get_str4(const char* name, const char* str1,const char* str2,const char* str3){ - char* result = (char*)malloc(strlen(name)+strlen(str1) + strlen(str2) + strlen(str3) + 9); - if(result){ - strcpy(result, " "); // Copy the first string - strcat(result, name); - - strcat(result, str1); - - strcat(result, str2); // Append the second string - - strcat(result, str3); // Append the third string - } - else{ - printf("get_str4 fail"); - } - return result; -} -char* get_str3(const char* name, const char* str1,const char* str2){ - char* result = (char*)malloc(strlen(name)+strlen(str1) + strlen(str2) + 7); - if(result){ - strcpy(result, " "); // Copy the first string - strcat(result, name); - - strcat(result, str1); - - strcat(result, str2); // Append the second string - } - else{ - printf("get_str3 fail"); - } - - return result; -} -char* get_str2(const char* name, const char* str1){ - - char* result = (char*)malloc(strlen(name)+strlen(str1) + 5); - if(result){ - strcpy(result," "); - strcat(result, name); // Copy the first string - - strcat(result, str1);} - else{ - printf("get_str2 fail"); - } - return result; -} - -void yyerror(const char* s) { - printf("Error type B at Line %d: Missing semicolon %s\n", - yylineno, strdup(yytext)); +void yyerror(const char *s) { + fprintf(stderr, "%s\n", s); } int main(int argc, char **argv){ diff --git a/phase1/tree.cpp b/phase1/tree.cpp index 867a2b9..334e58a 100644 --- a/phase1/tree.cpp +++ b/phase1/tree.cpp @@ -1,41 +1,62 @@ #include "tree.hpp" + // 初始化映射 std::map nameToCategory = { - {"STRUCT", "TERMINAL"}, - {"if", "TERMINAL"}, - {"else", "TERMINAL"}, - {"while", "TERMINAL"}, - {"return", "TERMINAL"}, - {";", "TERMINAL"}, - {",", "TERMINAL"}, - {"==", "TERMINAL"}, - {"<=", "TERMINAL"}, - {">=", "TERMINAL"}, - {"!=", "TERMINAL"}, - {"=", "TERMINAL"}, - {"!", "TERMINAL"}, - {"<", "TERMINAL"}, - {">", "TERMINAL"}, - {"+", "TERMINAL"}, - {"-", "TERMINAL"}, - {"*", "TERMINAL"}, - {"/", "TERMINAL"}, - {"&&", "TERMINAL"}, - {"||", "TERMINAL"}, - {"(", "TERMINAL"}, - {")", "TERMINAL"}, - {"[", "TERMINAL"}, - {"]", "TERMINAL"}, - {"{", "TERMINAL"}, - {"}", "TERMINAL"}, - {".", "TERMINAL"}, - {"TYPE","VAL"}, - {"CHAR","VAL"}, - {"ID","VAL"}, - {} + {"STRUCT", "TERMINAL"}, + {"IF", "TERMINAL"}, + {"ELSE", "TERMINAL"}, + {"WHILE", "TERMINAL"}, + {"RETURN", "TERMINAL"}, + {"SEMI", "TERMINAL"}, + {"COMMA", "TERMINAL"}, + {"EQ", "TERMINAL"}, + {"LE", "TERMINAL"}, + {"GE", "TERMINAL"}, + {"NE", "TERMINAL"}, + {"ASSIGN", "TERMINAL"}, + {"NOT", "TERMINAL"}, + {"LT", "TERMINAL"}, + {"GT", "TERMINAL"}, + {"PLUS", "TERMINAL"}, + {"MINUS", "TERMINAL"}, + {"MUL", "TERMINAL"}, + {"DIV", "TERMINAL"}, + {"AND", "TERMINAL"}, + {"OR", "TERMINAL"}, + {"LP", "TERMINAL"}, + {"RP", "TERMINAL"}, + {"LB", "TERMINAL"}, + {"RB", "TERMINAL"}, + {"LC", "TERMINAL"}, + {"RC", "TERMINAL"}, + {"DOT","TERMINAL"}, + {"TYPE","CHAR"}, + {"CHAR","CHAR"}, + {"ID","CHAR"}, + {"FLOAT","FLOAT"}, + {"INT","INT"}, + {"Program", "NONTERMINAL"}, + {"ExtDefList", "NONTERMINAL"}, + {"ExtDef", "NONTERMINAL"}, + {"ExtDecList","NONTERMINAL"}, + {"Specifier", "NONTERMINAL"}, + {"StructSpecifier", "NONTERMINAL"}, + {"VarDec", "NONTERMINAL"}, + {"FunDec", "NONTERMINAL"}, + {"VarList", "NONTERMINAL"}, + {"ParamDec", "NONTERMINAL"}, + {"CompSt", "NONTERMINAL"}, + {"StmtList", "NONTERMINAL"}, + {"Stmt", "NONTERMINAL"}, + {"DefList", "NONTERMINAL"}, + {"Def", "NONTERMINAL"}, + {"DecList", "NONTERMINAL"}, + {"Dec", "NONTERMINAL"}, + {"Exp", "NONTERMINAL"}, + {"Args", "NONTERMINAL"}, }; -Node::Node(string node_name, int nodes_num, int line_num, ...) : name(node_name), nodes_num(0), line_num(0) +Node::Node(string node_name, int nodes_num, int line_num, ...) : name(node_name), nodes_num(nodes_num), line_num(line_num) { va_list childnodes; va_start(childnodes, line_num); @@ -66,63 +87,46 @@ Node::Node(string node_name, float float_value) : name(node_name), float_value(f } void printTree(Node *root, int space) -{ - printf("%s (%d)\n", root->name.c_str(), root->line_num); +{ + + print(root,space); int nodes_num = root->nodes_num; queue nodes_queue = root->com_node; for (int i = 0; i < nodes_num; i++) { Node *node = nodes_queue.front(); - print(node, space + 2); + printTree(node, space + 2); nodes_queue.pop(); } } void print(Node *node, int space) -{ - if(node->nodes_num == 0 && node->nodetype == NONTERMINAL){ +{ + string print_type=nameToCategory[node->name]; + if(node->nodes_num == 0 && print_type == "NONTERMINAL"){ return; } + for (int i = 0; i < space; i++) { printf(" "); } - switch (node->nodetype) - { - case Type: - { - printf("TYPE: %s\n", node->char_value); - break; - } - case Int: - { - printf("INT: %s\n", node->name.c_str()); - break; - } - case Char: - { - printf("CHAR: %s\n", node->char_value); - break; - } - case Float: - { - printf("FLOAT: %s\n", node->name.c_str()); - break; - } - case Id: - { - printf("ID: %s\n", node->char_value); - break; + + if (print_type=="NONTERMINAL"){ + std::cout << node->name <<" (" << node->line_num << ")" <name.c_str()); - break; + std::cout << node->name <<": " << node->char_value <name <<": " << node->int_value <name <<": " << node->float_value <name < #include #include +#include using namespace std; class Node{ @@ -41,6 +42,7 @@ class Node{ }; void printTree(Node* root, int space=0); +void print(Node *node, int space); // 在头文件中定义全局映射,并提前设定 extern std::map nameToCategory;