Created
January 20, 2015 15:23
-
-
Save zikosw/e7f6a6f28f71ee42aefd to your computer and use it in GitHub Desktop.
CC HW2 Flex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %option noyywrap | |
| D [0-9] | |
| L [a-zA-Z_] | |
| H [a-fA-F0-9] | |
| E [Ee][+-]?{D}+ | |
| FS (f|F|l|L) | |
| IS (u|U|l|L)* | |
| %{ | |
| #include <stdio.h> | |
| enum {IDENTIFIER=300, CONSTANT, STRING_LITERAL, SIZEOF}; | |
| enum {PTR_OP=400, INC_OP, DEC_OP, LEFT_OP, RIGHT_OP, LE_OP, GE_OP, EQ_OP, NE_OP}; | |
| enum {AND_OP=500, OR_OP, MUL_ASSIGN, DIV_ASSIGN, MOD_ASSIGN, ADD_ASSIGN}; | |
| enum {SUB_ASSIGN=600, LEFT_ASSIGN, RIGHT_ASSIGN, AND_ASSIGN, XOR_ASSIGN, OR_ASSIGN,TYPE_NAME}; | |
| enum {TYPEDEF=700, EXTERN, STATIC, AUTO, REGISTER}; | |
| enum {CHAR=800, SHORT, INT, LONG, SIGNED, UNSIGNED, FLOAT, DOUBLE, CONST, VOLATILE, VOID,STRUCT, UNION, ENUM, ELLIPSIS}; | |
| enum {CASE=900, DEFAULT, IF, ELSE, SWITCH, WHILE, DO, FOR, GOTO, CONTINUE, BREAK, RETURN}; | |
| void comment(void); | |
| %} | |
| %% | |
| #.* { } | |
| \/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ { } | |
| "auto" { printf("[%d]\n",AUTO); } | |
| "break" { printf("[%d]\n",BREAK); } | |
| "case" { printf("[%d]\n",CASE); } | |
| "char" { printf("[%d]\n",CHAR); } | |
| "const" { printf("[%d]\n",CONST); } | |
| "continue" { printf("[%d]\n",CONTINUE); } | |
| "default" { printf("[%d]\n",DEFAULT); } | |
| "do" { printf("[%d]\n",DO); } | |
| "double" { printf("[%d]\n",DOUBLE); } | |
| "else" { printf("[%d]\n",ELSE); } | |
| "enum" { printf("[%d]\n",ENUM); } | |
| "extern" { printf("[%d]\n",EXTERN); } | |
| "float" { printf("[%d]\n",FLOAT); } | |
| "for" { printf("[%d]\n",FOR); } | |
| "goto" { printf("[%d]\n",GOTO); } | |
| "if" { printf("[%d]\n",IF); } | |
| "int" { printf("[%d]\n",INT); } | |
| "long" { printf("[%d]\n",LONG); } | |
| "register" { printf("[%d]\n",REGISTER); } | |
| "return" { printf("[%d]\n",RETURN); } | |
| "short" { printf("[%d]\n",SHORT); } | |
| "signed" { printf("[%d]\n",SIGNED); } | |
| "sizeof" { printf("[%d]\n",SIZEOF); } | |
| "static" { printf("[%d]\n",STATIC); } | |
| "struct" { printf("[%d]\n",STRUCT); } | |
| "switch" { printf("[%d]\n",SWITCH); } | |
| "typedef" { printf("[%d]\n",TYPEDEF); } | |
| "union" { printf("[%d]\n",UNION); } | |
| "unsigned" { printf("[%d]\n",UNSIGNED); } | |
| "void" { printf("[%d]\n",VOID); } | |
| "volatile" { printf("[%d]\n",VOLATILE); } | |
| "while" { printf("[%d]\n",WHILE); } | |
| {L}({L}|{D})* { printf("[%d, %s]\n",check_type(),yytext); } | |
| 0[xX]{H}+{IS}? { printf("[%d, %s]\n",CONSTANT,yytext); } | |
| 0{D}+{IS}? { printf("[%d, %s]\n",CONSTANT,yytext); } | |
| {D}+{IS}? { printf("[%d, %s]\n",CONSTANT,yytext); } | |
| L?'(\\.|[^\\'])+' { printf("[%d, %s]\n",CONSTANT,yytext); } | |
| {D}+{E}{FS}? { printf("[%d, %s]\n",CONSTANT,yytext); } | |
| {D}*"."{D}+({E})?{FS}? { printf("[%d, %s]\n",CONSTANT,yytext); } | |
| {D}+"."{D}*({E})?{FS}? { printf("[%d, %s]\n",CONSTANT,yytext); } | |
| L?\"(\\.|[^\\"])*\" { printf("[%d\n",STRING_LITERAL); } | |
| "..." { printf("[%d]\n",ELLIPSIS); } | |
| ">>=" { printf("[%d]\n",RIGHT_ASSIGN); } | |
| "<<=" { printf("[%d]\n",LEFT_ASSIGN); } | |
| "+=" { printf("[%d]\n",ADD_ASSIGN); } | |
| "-=" { printf("[%d]\n",SUB_ASSIGN); } | |
| "*=" { printf("[%d]\n",MUL_ASSIGN); } | |
| "/=" { printf("[%d]\n",DIV_ASSIGN); } | |
| "%=" { printf("[%d]\n",MOD_ASSIGN); } | |
| "&=" { printf("[%d]\n",AND_ASSIGN); } | |
| "^=" { printf("[%d]\n",XOR_ASSIGN); } | |
| "|=" { printf("[%d]\n",OR_ASSIGN); } | |
| ">>" { printf("[%d]\n",RIGHT_OP); } | |
| "<<" { printf("[%d]\n",LEFT_OP); } | |
| "++" { printf("[%d]\n",INC_OP); } | |
| "--" { printf("[%d]\n",DEC_OP); } | |
| "->" { printf("[%d]\n",PTR_OP); } | |
| "&&" { printf("[%d]\n",AND_OP); } | |
| "||" { printf("[%d]\n",OR_OP); } | |
| "<=" { printf("[%d]\n",LE_OP); } | |
| ">=" { printf("[%d]\n",GE_OP); } | |
| "==" { printf("[%d]\n",EQ_OP); } | |
| "!=" { printf("[%d]\n",NE_OP); } | |
| ";" { printf("[%d]\n",';'); } | |
| ("{"|"<%") { printf("[%d]\n",'{'); } | |
| ("}"|"%>") { printf("[%d]\n",'}'); } | |
| "," { printf("[%d]\n",','); } | |
| ":" { printf("[%d]\n",':'); } | |
| "=" { printf("[%d]\n",'='); } | |
| "(" { printf("[%d]\n",'('); } | |
| ")" { printf("[%d]\n",')'); } | |
| ("["|"<:") { printf("[%d]\n",'['); } | |
| ("]"|":>") { printf("[%d]\n",']'); } | |
| "." { printf("[%d]\n",'.'); } | |
| "&" { printf("[%d]\n",'&'); } | |
| "!" { printf("[%d]\n",'!'); } | |
| "~" { printf("[%d]\n",'~'); } | |
| "-" { printf("[%d]\n",'-'); } | |
| "+" { printf("[%d]\n",'+'); } | |
| "*" { printf("[%d]\n",'*'); } | |
| "/" { printf("[%d]\n",'/'); } | |
| "%" { printf("[%d]\n",'%'); } | |
| "<" { printf("[%d]\n",'<'); } | |
| ">" { printf("[%d]\n",'>'); } | |
| "^" { printf("[%d]\n",'^'); } | |
| "|" { printf("[%d]\n",'|'); } | |
| "?" { printf("[%d]\n",'?'); } | |
| [ \t\v\n\f] { /*printf("%d",count());*/ } | |
| . { /* ignore bad characters */ } | |
| %% | |
| /* | |
| yywrap() | |
| { | |
| return(1); | |
| } | |
| */ | |
| int column = 0; | |
| int count() | |
| { | |
| int i; | |
| for (i = 0; yytext[i] != '\0'; i++) | |
| if (yytext[i] == '\n') | |
| column = 0; | |
| else if (yytext[i] == '\t') | |
| column += 8 - (column % 8); | |
| else | |
| column++; | |
| ECHO; | |
| } | |
| int check_type() | |
| { | |
| /* | |
| * pseudo code --- this is what it should check | |
| * | |
| * if (yytext == type_name) | |
| * return(TYPE_NAME); | |
| * | |
| * return(IDENTIFIER); | |
| */ | |
| /* | |
| * it actually will only return IDENTIFIER | |
| */ | |
| return(IDENTIFIER); | |
| } | |
| main(int argc,char **argv) | |
| { | |
| // lex through the input: | |
| yylex(); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment