From 9cf8d1e0ceb40592400295d54021a0ef1cebf6e9 Mon Sep 17 00:00:00 2001 From: tutuwel <12111744@mail.sustech.edu.cn> Date: Sat, 28 Oct 2023 15:08:03 +0800 Subject: [PATCH] finish condition macro --- phase1/lex.l | 4 ++-- phase1/syntax.y | 2 +- phase1/test-ex/test_3.spl | 22 +++++++++++----------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/phase1/lex.l b/phase1/lex.l index 760e45e..041adfe 100644 --- a/phase1/lex.l +++ b/phase1/lex.l @@ -40,8 +40,8 @@ id [_a-zA-Z][_0-9a-zA-Z]* "#define" {yylval=strdup("DEFINE\n"); BEGIN(macro); return DEFINE;} "#include" {yylval=strdup("INCLUDE\n"); BEGIN(macro); return INCLUDE;} "#ifdef" {yylval=strdup("IFDEF\n"); BEGIN(macro); return IFDEF;} -"#else" {yylval=strdup("MACROELSE\n"); BEGIN(macro); return MACROELSE;} -"#endif" {yylval=strdup("ENDIF\n"); BEGIN(macro); return ENDIF;} +"#else" {yylval=strdup("MACROELSE\n"); return MACROELSE;} +"#endif" {yylval=strdup("ENDIF\n"); return ENDIF;} { "," {yylval=strdup("COMMA\n");return COMMA;} "<" {return LT;} diff --git a/phase1/syntax.y b/phase1/syntax.y index e7fa89f..fabd763 100644 --- a/phase1/syntax.y +++ b/phase1/syntax.y @@ -161,7 +161,7 @@ Stmt: | FOR LP Exp SEMI Exp SEMI Exp RP Stmt {asprintf(&$$,"Stmt (%d)\n%s\n", @$.first_line, concat_shift($1,$2,$3,$4,$5,$6,$7,$8,$9));} | IFDEF Stmt ENDIF {asprintf(&$$,"Stmt (%d)\n%s\n", @$.first_line, concat_shift($1,$2,$3));} -| IFDEF Stmt MACROELSE Stmt ENDIF {asprintf(&$$,"Stmt (%d)\n%s\n", @$.first_line, concat_shift($1,$2,$3,$4,$5));} +| IFDEF MACRO Stmt MACROELSE Stmt ENDIF {asprintf(&$$,"Stmt (%d)\n%s\n", @$.first_line, concat_shift($1,$2,$3,$4,$5));} diff --git a/phase1/test-ex/test_3.spl b/phase1/test-ex/test_3.spl index 8ac8d14..a8d97a6 100644 --- a/phase1/test-ex/test_3.spl +++ b/phase1/test-ex/test_3.spl @@ -2,14 +2,14 @@ #define MACRO_NAME1 replacement_text1 #include #include "header_file.h" -int test_1_r01(int a, int b){ - c = 'c'; - if (a > b) - { - return a; - } - else - { - return b; - } - } \ No newline at end of file +#define DEBUG 1 + +int main() { +#ifdef DEBUG + printf('Debug mode is enabled.\n'); +#else + printf('Debug mode is disabled.\n'); +#endif + + return 0; +}