diff options
author | kugwa <kugwa2000@gmail.com> | 2015-11-10 04:51:30 +0800 |
---|---|---|
committer | Ting-Wei Lan <lantw44@gmail.com> | 2015-11-11 16:25:35 +0800 |
commit | 9d68c0dbeaaad0fd760121b8dc3f01bd7eb90a44 (patch) | |
tree | 424853bf483030136f97a484ca21088cf9ddd5ce | |
parent | d7d90d6fbcafaf67e181a4046f2bedde851d1baa (diff) | |
download | compiler2015-9d68c0dbeaaad0fd760121b8dc3f01bd7eb90a44.tar compiler2015-9d68c0dbeaaad0fd760121b8dc3f01bd7eb90a44.tar.gz compiler2015-9d68c0dbeaaad0fd760121b8dc3f01bd7eb90a44.tar.bz2 compiler2015-9d68c0dbeaaad0fd760121b8dc3f01bd7eb90a44.tar.lz compiler2015-9d68c0dbeaaad0fd760121b8dc3f01bd7eb90a44.tar.xz compiler2015-9d68c0dbeaaad0fd760121b8dc3f01bd7eb90a44.tar.zst compiler2015-9d68c0dbeaaad0fd760121b8dc3f01bd7eb90a44.zip |
All actions in lexer.l return the currect token
Define tokens in enum for reserved words and the action of {ID} will
return the currect token. Other actions also return tokens.
-rw-r--r-- | src/lexer.l | 74 |
1 files changed, 47 insertions, 27 deletions
diff --git a/src/lexer.l b/src/lexer.l index 45e696b..18c373f 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -39,6 +39,15 @@ typedef enum CcmmcToken_enum { CCMMC_TOKEN_DL_SEMICOL, CCMMC_TOKEN_DL_DOT, CCMMC_TOKEN_NEWLINE, + CCMMC_TOKEN_RETURN, + CCMMC_TOKEN_TYPEDEF, + CCMMC_TOKEN_IF, + CCMMC_TOKEN_ELSE, + CCMMC_TOKEN_INT, + CCMMC_TOKEN_FLOAT, + CCMMC_TOKEN_FOR, + CCMMC_TOKEN_VOID, + CCMMC_TOKEN_WHILE, CCMMC_TOKEN_ERROR = 100 } CcmmcToken; %} @@ -93,9 +102,19 @@ ERROR . int i; char *reserved[] = {"return", "typedef", "if", "else", "int", "float", "for", "void", "while"}; + CcmmcToken reserved_token[] = {CCMMC_TOKEN_RETURN, + CCMMC_TOKEN_TYPEDEF, + CCMMC_TOKEN_IF, + CCMMC_TOKEN_ELSE, + CCMMC_TOKEN_INT, + CCMMC_TOKEN_FLOAT, + CCMMC_TOKEN_FOR, + CCMMC_TOKEN_VOID, + CCMMC_TOKEN_WHILE}; for (i = 0; i < SIZE_OF_ARR(reserved); i++) - if (strcmp(yytext, reserved[i]) == 0) break; + if (strcmp(yytext, reserved[i]) == 0) + return reserved_token[i]; if (i == SIZE_OF_ARR(reserved)) { CcmmcSymbol * ptr; ptr = ccmmc_symbol_table_lookup(yytext); @@ -104,10 +123,11 @@ ERROR . else ptr->counter++; } + return CCMMC_TOKEN_ID; } -{CONST_INT} {} -{CONST_FLOAT} {} -{CONST_STRING} {} +{CONST_INT} return CCMMC_TOKEN_CONST_INT; +{CONST_FLOAT} return CCMMC_TOKEN_CONST_FLOAT; +{CONST_STRING} return CCMMC_TOKEN_CONST_STRING; {COMMENT} { int i; @@ -115,32 +135,32 @@ ERROR . if (yytext[i] == '\n') line_number++; } -{OP_ASSIGN} {} -{OP_OR} {} -{OP_AND} {} -{OP_NOT} {} -{OP_ADD} {} -{OP_SUB} {} -{OP_MUL} {} -{OP_DIV} {} -{OP_GT} {} -{OP_LT} {} -{OP_GE} {} -{OP_LE} {} -{OP_NE} {} -{OP_EQ} {} +{OP_ASSIGN} return CCMMC_TOKEN_OP_ASSIGN; +{OP_OR} return CCMMC_TOKEN_OP_OR; +{OP_AND} return CCMMC_TOKEN_OP_AND; +{OP_NOT} return CCMMC_TOKEN_OP_NOT; +{OP_ADD} return CCMMC_TOKEN_OP_ADD; +{OP_SUB} return CCMMC_TOKEN_OP_SUB; +{OP_MUL} return CCMMC_TOKEN_OP_MUL; +{OP_DIV} return CCMMC_TOKEN_OP_DIV; +{OP_GT} return CCMMC_TOKEN_OP_GT; +{OP_LT} return CCMMC_TOKEN_OP_LT; +{OP_GE} return CCMMC_TOKEN_OP_GE; +{OP_LE} return CCMMC_TOKEN_OP_LE; +{OP_NE} return CCMMC_TOKEN_OP_NE; +{OP_EQ} return CCMMC_TOKEN_OP_EQ; {NEWLINE} line_number++; -{DL_LPAREN} {} -{DL_RPAREN} {} -{DL_LBRACK} {} -{DL_RBRACK} {} -{DL_LBRACE} {} -{DL_RBRACE} {} -{DL_COMMA} {} -{DL_SEMICOL} {} -{DL_DOT} {} +{DL_LPAREN} return CCMMC_TOKEN_DL_LPAREN; +{DL_RPAREN} return CCMMC_TOKEN_DL_RPAREN; +{DL_LBRACK} return CCMMC_TOKEN_DL_LBRACK; +{DL_RBRACK} return CCMMC_TOKEN_DL_RBRACK; +{DL_LBRACE} return CCMMC_TOKEN_DL_RBRACE; +{DL_RBRACE} return CCMMC_TOKEN_DL_LBRACE; +{DL_COMMA} return CCMMC_TOKEN_DL_COMMA; +{DL_SEMICOL} return CCMMC_TOKEN_DL_SEMICOL; +{DL_DOT} return CCMMC_TOKEN_DL_DOT; {ERROR} { fprintf(stderr, "%d: error: undefined character `%s'\n", |