From a0457fc80d471a57f7337201b71f2758064b7158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?202108010301-=E6=9C=B1=E9=9F=A6=E9=BE=99?= <13659335+zwl9955@user.noreply.gitee.com> Date: Sat, 18 Nov 2023 13:15:46 +0000 Subject: [PATCH] update src/lexer/lexical_analyzer.l. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 202108010301-朱韦龙 <13659335+zwl9955@user.noreply.gitee.com> --- src/lexer/lexical_analyzer.l | 62 +++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/lexer/lexical_analyzer.l b/src/lexer/lexical_analyzer.l index c6ac08f..cde3fae 100755 --- a/src/lexer/lexical_analyzer.l +++ b/src/lexer/lexical_analyzer.l @@ -20,7 +20,49 @@ int pos_end; /******************TODO*********************/ /****请在此补全所有flex的模式与动作 start******/ //STUDENT TO DO - +/******** 运算 ********/ +\+ {pos_start = pos_end; pos_end += strlen(yytext); return ADD;} +\- {pos_start = pos_end; pos_end += strlen(yytext); return SUB;} +\* {pos_start = pos_end; pos_end += strlen(yytext); return MUL;} +\/ {pos_start = pos_end; pos_end += strlen(yytext); return DIV;} +\< {pos_start = pos_end; pos_end += strlen(yytext); return LT;} +\<\= {pos_start = pos_end; pos_end += strlen(yytext); return LTE;} +\> {pos_start = pos_end; pos_end += strlen(yytext); return GT;} +\>\= {pos_start = pos_end; pos_end += strlen(yytext); return GTE;} +\=\= {pos_start = pos_end; pos_end += strlen(yytext); return EQ;} +\!\= {pos_start = pos_end; pos_end += strlen(yytext); return NEQ;} +\= {pos_start = pos_end; pos_end += strlen(yytext); return ASSIN;} + +/******** 符号 ********/ +\; {pos_start = pos_end; pos_end += strlen(yytext); return SEMICOLON;} +\, {pos_start = pos_end; pos_end += strlen(yytext); return COMMA;} +\( {pos_start = pos_end; pos_end += strlen(yytext); return LPARENTHESE;} +\) {pos_start = pos_end; pos_end += strlen(yytext); return RPARENTHESE;} +\[ {pos_start = pos_end; pos_end += strlen(yytext); return LBRACKET;} +\] {pos_start = pos_end; pos_end += strlen(yytext); return RBRACKET;} +\{ {pos_start = pos_end; pos_end += strlen(yytext); return LBRACE;} +\} {pos_start = pos_end; pos_end += strlen(yytext); return RBRACE;} + +/******** 关键字 ********/ +else {pos_start = pos_end; pos_end += strlen(yytext); return ELSE;} +if {pos_start = pos_end; pos_end += strlen(yytext); return IF;} +int {pos_start = pos_end; pos_end += strlen(yytext); return INT;} +float {pos_start = pos_end; pos_end += strlen(yytext); return FLOAT;} +return {pos_start = pos_end; pos_end += strlen(yytext); return RETURN;} +void {pos_start = pos_end; pos_end += strlen(yytext); return VOID;} +while {pos_start = pos_end; pos_end += strlen(yytext); return WHILE;} + +/******** ID和NUM ********/ +[a-zA-Z]+ {pos_start = pos_end; pos_end += strlen(yytext); return IDENTIFIER;} +[0-9]+ {pos_start = pos_end; pos_end += strlen(yytext); return INTEGER;} +[0-9]+\.|[0-9]*\.[0-9]+ {pos_start = pos_end; pos_end += strlen(yytext); return FLOATPOINT;} +\[\] {pos_start = pos_end; pos_end += strlen(yytext); return ARRAY;} +[a-zA-Z] {pos_start=pos_end;pos_end=pos_start+1;return LETTER;} + +/******** others ********/ +[\n] {return EOL;} +\/\*([^\*]|(\*)*[^\*\/])*(\*)*\*\/ {return COMMENT;} +[ \f\n\r\t\v] {return BLANK;} . {return ERROR;} @@ -52,11 +94,29 @@ void analyzer(char* input_file, Token_Node* token_stream){ switch(token){ case COMMENT: //STUDENT TO DO + i=0; + len=strlen(yytext); + while(len--){ + if(yytext[i]==’\n’) + { + pos_start=1; + pos_end=1; + lines++; + } + else {pos_end++;} + i++; + } + break; case BLANK: //STUDENT TO DO + pos_start = pos_end; + pos_end += strlen(yytext); break; case EOL: //STUDENT TO DO + pos_start = 1; + pos_end = 1; + lines += strlen(yytext); break; case ERROR: printf("[ERR]: unable to analysize %s at %d line, from %d to %d\n", yytext, lines, pos_start, pos_end); -- Gitee