summaryrefslogtreecommitdiffstats
path: root/src/lexer.l
diff options
context:
space:
mode:
authorkugwa <kugwa2000@gmail.com>2015-11-11 19:06:01 +0800
committerkugwa <kugwa2000@gmail.com>2015-11-11 19:06:01 +0800
commit62d9753dac503b34bb27635faf96a395d465f54d (patch)
tree62c06eaa6e0a06fdad263b27fc0843bd2af7d46d /src/lexer.l
parent97c3aabffdc7ed612d322dd839f9ae4ab76efc6a (diff)
downloadcompiler2015-62d9753dac503b34bb27635faf96a395d465f54d.tar
compiler2015-62d9753dac503b34bb27635faf96a395d465f54d.tar.gz
compiler2015-62d9753dac503b34bb27635faf96a395d465f54d.tar.bz2
compiler2015-62d9753dac503b34bb27635faf96a395d465f54d.tar.lz
compiler2015-62d9753dac503b34bb27635faf96a395d465f54d.tar.xz
compiler2015-62d9753dac503b34bb27635faf96a395d465f54d.tar.zst
compiler2015-62d9753dac503b34bb27635faf96a395d465f54d.zip
Merge TA's codes to ours
Delete tmp funtion used in HW2. Move AST functions into ast.c. CONST_INT, CONST_FLOAT, and CONST_STRING all return CONST.
Diffstat (limited to 'src/lexer.l')
-rw-r--r--src/lexer.l110
1 files changed, 30 insertions, 80 deletions
diff --git a/src/lexer.l b/src/lexer.l
index 07e3dbd..b15668d 100644
--- a/src/lexer.l
+++ b/src/lexer.l
@@ -7,50 +7,7 @@
#define SIZE_OF_ARR(x) (sizeof(x)/sizeof(x[0]))
-static int line_number = 1;
-
-/* You need to define for all tokens in C--, here are some examples */
-typedef enum CcmmcToken_enum {
- CCMMC_TOKEN_ID = 1,
- CCMMC_TOKEN_CONST_INT,
- CCMMC_TOKEN_CONST_FLOAT,
- CCMMC_TOKEN_CONST_STRING,
- CCMMC_TOKEN_COMMENT,
- CCMMC_TOKEN_OP_ASSIGN,
- CCMMC_TOKEN_OP_OR,
- CCMMC_TOKEN_OP_AND,
- CCMMC_TOKEN_OP_NOT,
- CCMMC_TOKEN_OP_ADD,
- CCMMC_TOKEN_OP_SUB,
- CCMMC_TOKEN_OP_MUL,
- CCMMC_TOKEN_OP_DIV,
- CCMMC_TOKEN_OP_GT,
- CCMMC_TOKEN_OP_LT,
- CCMMC_TOKEN_OP_GE,
- CCMMC_TOKEN_OP_LE,
- CCMMC_TOKEN_OP_NE,
- CCMMC_TOKEN_OP_EQ,
- CCMMC_TOKEN_DL_LPAREN,
- CCMMC_TOKEN_DL_RPAREN,
- CCMMC_TOKEN_DL_LBRACK,
- CCMMC_TOKEN_DL_RBRACK,
- CCMMC_TOKEN_DL_LBRACE,
- CCMMC_TOKEN_DL_RBRACE,
- CCMMC_TOKEN_DL_COMMA,
- 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;
+int line_number = 1;
%}
letter [A-Za-z]
@@ -102,15 +59,8 @@ 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};
+ enum yytokentype reserved_token[] = {RETURN, TYPEDEF,
+ IF, ELSE, INT, FLOAT, FOR, VOID, WHILE};
static_assert(
SIZE_OF_ARR(reserved) == SIZE_OF_ARR(reserved_token),
"Reserved words array and reserved tokens array "
@@ -127,11 +77,11 @@ ERROR .
else
ptr->counter++;
}
- return CCMMC_TOKEN_ID;
+ return ID;
}
-{CONST_INT} return CCMMC_TOKEN_CONST_INT;
-{CONST_FLOAT} return CCMMC_TOKEN_CONST_FLOAT;
-{CONST_STRING} return CCMMC_TOKEN_CONST_STRING;
+{CONST_INT} return CONST; //TODO
+{CONST_FLOAT} return CONST;
+{CONST_STRING} return CONST;
{COMMENT} {
int i;
@@ -139,32 +89,32 @@ ERROR .
if (yytext[i] == '\n')
line_number++;
}
-{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;
+{OP_ASSIGN} return OP_ASSIGN;
+{OP_OR} return OP_OR;
+{OP_AND} return OP_AND;
+{OP_NOT} return OP_NOT;
+{OP_ADD} return OP_ADD;
+{OP_SUB} return OP_SUB;
+{OP_MUL} return OP_MUL;
+{OP_DIV} return OP_DIV;
+{OP_GT} return OP_GT;
+{OP_LT} return OP_LT;
+{OP_GE} return OP_GE;
+{OP_LE} return OP_LE;
+{OP_NE} return OP_NE;
+{OP_EQ} return OP_EQ;
{NEWLINE} line_number++;
-{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;
+{DL_LPAREN} return DL_LPAREN;
+{DL_RPAREN} return DL_RPAREN;
+{DL_LBRACK} return DL_LBRACK;
+{DL_RBRACK} return DL_RBRACK;
+{DL_LBRACE} return DL_LBRACE;
+{DL_RBRACE} return DL_RBRACE;
+{DL_COMMA} return DL_COMMA;
+{DL_SEMICOL} return DL_SEMICOL;
+{DL_DOT} return DL_DOT;
{ERROR} {
fprintf(stderr, "%d: error: undefined character `%s'\n",