You need to sign in or sign up before continuing.
Commit 38c8231f authored by 李晓奇's avatar 李晓奇

fix the miswritten error from bracket and make the comment rule right!

parent f3c79837
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include "syntax_tree.h" #include "syntax_tree.h"
#include "syntax_analyzer.h" #include "syntax_analyzer.h"
/* #define __DEBUG_COMMENT__ */
int lines = 1; int lines = 1;
int pos_start; int pos_start;
int pos_end; int pos_end;
...@@ -16,27 +18,32 @@ void pass_node(char *text){ ...@@ -16,27 +18,32 @@ void pass_node(char *text){
} }
void comment_helper(char *comment, unsigned int len) void comment_helper(char *comment, unsigned int len)
{ {
pos_end += 2; #ifdef __DEBUG_COMMENT__
for (int i = 2; i < len-2; i++){ printf("Get COMMENT in line<%d>: \"%s\"\n", lines, comment);
if (comment[i] == '\n' || comment[i] == '\r'){ #endif
lines++; /* pos_end += 2;
pos_end = 0; * for (int i = 2; i < len-2; i++){
} else * if (comment[i] == '\n' || comment[i] == '\r'){
pos_end++; * lines++;
} * pos_end = 0;
pos_end += 2; * } else
* pos_end++;
* }
* pos_end += 2; */
} }
/*****************声明和选项设置 end*****************/ /*****************声明和选项设置 end*****************/
%} %}
/* use exclusive state */
%x COMMENT
letter [a-zA-Z] letter [a-zA-Z]
digit [0-9] digit [0-9]
ID {letter}+ ID {letter}+
INTEGER {digit}+ INTEGER {digit}+
FLOAT {digit}+\.|{digit}*\.{digit}+ FLOAT {digit}+\.|{digit}*\.{digit}+
COMMENT "/*".*"*/"
NEWLINE \r\n|\r|\n NEWLINE \r\n|\r|\n
WHITESPACE [ \t] WHITESPACE [ \t]
...@@ -48,9 +55,14 @@ WHITESPACE [ \t] ...@@ -48,9 +55,14 @@ WHITESPACE [ \t]
%token <node> _SEMI _COMMA _ID _INTEGER _FLOATPOINT %token <node> _SEMI _COMMA _ID _INTEGER _FLOATPOINT
*/ */
/* <COMMENT>.* { pos_end += yyleng; comment_helper(yytext, yyleng); } */
%% %%
{COMMENT} { comment_helper(yytext, yyleng); }
<INITIAL>"/*" { BEGIN(COMMENT); pos_end += 2; }
<COMMENT>[^*\n]*|"*"+[^*/\n]* { pos_end += yyleng; comment_helper(yytext, yyleng); }
<COMMENT>"*/" { BEGIN(0); pos_end += 2; }
if { pos_start = pos_end; pos_end += 2; pass_node("if"); return _IF;} 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;} 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;} 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;} ...@@ -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;} ","|";" { pos_start = pos_end; pos_end += 1; pass_node(yytext); return yytext[0] == ',' ? _COMMA : _SEMI;}
{WHITESPACE} { pos_end++; } {WHITESPACE} { pos_end++; }
{NEWLINE} { lines++; pos_end = 0;} <*>{NEWLINE} { lines++; pos_end = 0;}
%% %%
...@@ -82,7 +82,7 @@ DEC: VAR_DEC {$$ = node("declaration", 1, $1); } ...@@ -82,7 +82,7 @@ DEC: VAR_DEC {$$ = node("declaration", 1, $1); }
; ;
VAR_DEC: TYPE_SPEC _ID _SEMI {$$ = node("var-declaration", 3, $1, $2, $3); } 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); } TYPE_SPEC: _INT {$$ = node("type-specifier", 1, $1); }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment