diff options
author | kugwa <kugwa2000@gmail.com> | 2015-10-19 16:33:40 +0800 |
---|---|---|
committer | kugwa <kugwa2000@gmail.com> | 2015-10-19 16:33:40 +0800 |
commit | 4aec326d0b910584f728e55c223738c033c43744 (patch) | |
tree | 51d937e5ddd19e351330ca215d3d76d1c2703291 | |
parent | add554d70fd6c904ac73502e6746e700e961ae53 (diff) | |
download | compiler2015-4aec326d0b910584f728e55c223738c033c43744.tar.gz compiler2015-4aec326d0b910584f728e55c223738c033c43744.tar.zst compiler2015-4aec326d0b910584f728e55c223738c033c43744.zip |
Complete lexer.l
-rw-r--r-- | lexer.l | 204 | ||||
-rw-r--r-- | symbol-table.c | 2 | ||||
-rw-r--r-- | symbol-table.h | 3 |
3 files changed, 135 insertions, 74 deletions
@@ -1,90 +1,150 @@ -%option noyywrap +%option noyywrap %{ #include <stdio.h> #include "symbol-table.h" + int linenumber; -symtab * lookup(); -symtab * ptr; -void insertID(); -void printSymTab(); -/* You need to define for all tokens in C--, here are some examples */ -#define INT 10 -#define FLOAT 11 -#define OP_ASSIGN 12 -#define OP_OR 13 -#define MK_LPAREN 14 -#define MK_RPAREN 15 -#define ERROR 100 +/* 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_ERROR = 100 +} CcmmcToken; %} -letter [A-Za-z] -digit [0-9] -ID {letter}({letter}|{digit}|"_")* -WS [ \t]+ -Int_constant {digit}+ - -/* You need to define the following RE's -Float_constant -String_constant -comment -*/ - -OP_assign "=" -OP_or "||" - -/* Other operators appear here */ - - -newline "\n" - -DL_lparen "(" -DL_rparen ")" -DL_lbrace "{" -DL_rbrace "}" -DL_comma "," -DL_semicol ";" -DL_dot "." - -/* Other separators appear here */ - -error . +letter [A-Za-z] +digit [0-9] +ID {letter}({letter}|{digit}|"_")* +WS [ \t]+ + +/* You need to define the following RE's */ +CONST_INT {digit}+ +CONST_FLOAT {digit}+\.{digit}+ +/* -?([0-9]+|[0-9]*\.[0-9]+([eE][-+]?[0-9]+)?) */ +CONST_STRING \"([^\"\n]|(\\.))*\" +COMMENT \/\*([^*]|\n|(\*+([^*/]|\n)))*\*+\/ + +/* operators */ +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 "==" + +NEWLINE "\n" + +/* separators */ +DL_LPAREN "(" +DL_RPAREN ")" +DL_LBRACK "[" +DL_RBRACK "]" +DL_LBRACE "{" +DL_RBRACE "}" +DL_COMMA "," +DL_SEMICOL ";" +DL_DOT "." + +ERROR . %% {WS} {} -{RWInt} {/* return INT; */ } -{RWFloat} {/* return FLOAT; */} -{ID} { - ptr = lookup(yytext); - if (ptr == NULL) - insertID(yytext); - else - ptr->counter++; - } - -{OP_assign} /* return OP_ASSIGN; */ -{OP_or} /* return OP_OR; */ -{DL_lparen} /* return MK_LPAREN; */ -{DL_rparen} /* return MK_RPAREN; */ -{DL_lbrace} /* return MK_LBRACE; */ -{DL_rbrace} /* return MK_RBRACE; */ -{DL_comma} {} -{DL_semicol} {} -{newline} linenumber += 1; -{error} printf("ERR \n");/* return ERROR; */ +{ID} { + symtab * ptr; + ptr = lookup(yytext); + if (ptr == NULL) + insertID(yytext); + else + ptr->counter++; + } +{CONST_INT} {} +{CONST_FLOAT} {} +{CONST_STRING} {} + +{COMMENT} { + puts(yytext); + } +{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} {} + +{NEWLINE} { + linenumber++; + } + +{DL_LPAREN} {} +{DL_RPAREN} {} +{DL_LBRACK} {} +{DL_RBRACK} {} +{DL_LBRACE} {} +{DL_RBRACE} {} +{DL_COMMA} {} +{DL_SEMICOL} {} +{DL_DOT} {} + +{ERROR} { + fputs("ERR", stderr);/* return ERROR; */ + } %% -main(int argc, char **argv) +int main(int argc, char **argv) { - argc--; ++argv; - if (argc > 0) - yyin = fopen(argv[0], "r"); - else - yyin = stdin; - yylex(); - printSymTab(); + if (argc > 1) + yyin = fopen(argv[1], "r"); + else + yyin = stdin; + yylex(); + printSymTab(); + return 0; } diff --git a/symbol-table.c b/symbol-table.c index c9084a4..6d98bb7 100644 --- a/symbol-table.c +++ b/symbol-table.c @@ -71,7 +71,7 @@ void printSym(symtab* ptr) printf(" References = %d \n", ptr->counter); } -void printSymTab() +void printSymTab(void) { int i; printf("----- Symbol Table ---------\n"); diff --git a/symbol-table.h b/symbol-table.h index fd9a8aa..9a54318 100644 --- a/symbol-table.h +++ b/symbol-table.h @@ -8,4 +8,5 @@ struct symtab{ typedef struct symtab symtab; symtab * lookup(char *name); -void insert(char *name); +void insertID(char *name); +void printSymTab(void); |