diff --git a/src/parser/lexical_analyzer.l b/src/parser/lexical_analyzer.l index e6c17282234856da5743a22d95b8b055860d133b..9cdeee8e11da3dfcbfd3e1192be29a0fc09d337f 100644 --- a/src/parser/lexical_analyzer.l +++ b/src/parser/lexical_analyzer.l @@ -7,6 +7,8 @@ #include "syntax_tree.h" #include "syntax_analyzer.h" +/* #define __DEBUG_COMMENT__ */ + int lines = 1; int pos_start; int pos_end; @@ -16,27 +18,32 @@ void pass_node(char *text){ } void comment_helper(char *comment, unsigned int len) { - pos_end += 2; - for (int i = 2; i < len-2; i++){ - if (comment[i] == '\n' || comment[i] == '\r'){ - lines++; - pos_end = 0; - } else - pos_end++; - } - pos_end += 2; +#ifdef __DEBUG_COMMENT__ + printf("Get COMMENT in line<%d>: \"%s\"\n", lines, comment); +#endif + /* pos_end += 2; + * for (int i = 2; i < len-2; i++){ + * if (comment[i] == '\n' || comment[i] == '\r'){ + * lines++; + * pos_end = 0; + * } else + * pos_end++; + * } + * pos_end += 2; */ } /*****************声明和选项设置 end*****************/ %} +/* use exclusive state */ +%x COMMENT + letter [a-zA-Z] digit [0-9] ID {letter}+ INTEGER {digit}+ FLOAT {digit}+\.|{digit}*\.{digit}+ -COMMENT "/*".*"*/" NEWLINE \r\n|\r|\n WHITESPACE [ \t] @@ -48,9 +55,14 @@ WHITESPACE [ \t] %token _SEMI _COMMA _ID _INTEGER _FLOATPOINT */ +/* .* { pos_end += yyleng; comment_helper(yytext, yyleng); } */ %% -{COMMENT} { comment_helper(yytext, yyleng); } + +"/*" { BEGIN(COMMENT); pos_end += 2; } +[^*\n]*|"*"+[^*/\n]* { pos_end += yyleng; comment_helper(yytext, yyleng); } +"*/" { BEGIN(0); pos_end += 2; } + if { pos_start = pos_end; pos_end += 2; pass_node("if"); return _IF;} else { pos_start = pos_end; pos_end += 4; pass_node("else"); return _ELSE;} while { pos_start = pos_end; pos_end += 5; pass_node("while"); return _WHILE;} @@ -75,5 +87,5 @@ void { pos_start = pos_end; pos_end += 4; pass_node("void"); return _VOID;} ","|";" { pos_start = pos_end; pos_end += 1; pass_node(yytext); return yytext[0] == ',' ? _COMMA : _SEMI;} {WHITESPACE} { pos_end++; } -{NEWLINE} { lines++; pos_end = 0;} +<*>{NEWLINE} { lines++; pos_end = 0;} %% diff --git a/src/parser/syntax_analyzer.y b/src/parser/syntax_analyzer.y index 1ae75be07e208e895ba7f2b3378e78c4158c9078..9842706f81a42d4cab30000e74e059b163ca32f8 100644 --- a/src/parser/syntax_analyzer.y +++ b/src/parser/syntax_analyzer.y @@ -82,7 +82,7 @@ DEC: VAR_DEC {$$ = node("declaration", 1, $1); } ; VAR_DEC: TYPE_SPEC _ID _SEMI {$$ = node("var-declaration", 3, $1, $2, $3); } - | TYPE_SPEC _ID _L_BRACKET _INTEGER _R_BRACKET _SEMI {$$ = node("var-declaration", 6, $1, $2, $3, $4, $5, $6); } + | TYPE_SPEC _ID _L_SQUARE _INTEGER _R_SQUARE _SEMI {$$ = node("var-declaration", 6, $1, $2, $3, $4, $5, $6); } ; TYPE_SPEC: _INT {$$ = node("type-specifier", 1, $1); } diff --git a/tests/parser/syntree_simple/*.syntax_tree b/tests/parser/syntree_simple/*.syntax_tree new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391