%{ #include #include #include #include #define YYSTYPE void * #include "list.h" #include "cmp.h" #include "y.tab.h" #include "shared.h" #include "mem.h" int quit = 0; int int_v = 0x10; int float_v = 0x11; int *int_var= &int_v; int *float_var = &float_v; int true_v = 0x12; int false_v = 0x13; int *true_var = &true_v; int *false_var = &false_v; int int_val = 0; double float_val = 0; void *installID(); void setForBool(); void *last_id; %} /*keep track of the line numbers*/ %option yylineno letter [A-Za-z] digit [0-9] basic int|float id {letter}({letter}|{digit})* num [+-]?[0-9]+ real {num}(\.{digit}*)?(E{num})? bool_var "true"|"false" EOF <> %% {bool_var} {setForBool(); printf("%s",(char*)yytext); return BOOL; } {basic} { if(strcmp((char*)yytext,"int") == 0 ) {yylval =(void*) int_var; } else if(strcmp((char*)yytext,"char")==0) {yylval=(void*) char_var;} else if(strcmp((char*)yytext,"bool")==0) {yylval=(void*) bool_var;} else { yylval = (void*) float_var;} printf("basic"); return BASIC; } "while" {yylval = NULL; printf("while"); return WHILE; } "do" {yylval = NULL; printf("do"); return DO; } "if" {yylval= NULL; printf("if"); return IF;} "else" {yylval = NULL; printf("else"); return ELSE;} "break" {yylval = NULL; printf("break");return BREAK;} "continue" {yylval = NULL; printf("continue"); return CONTINUE;} ^quit$ {quit = 1; return QUIT;} {id} {last_id = installID();yylval=last_id; printf("id"); return ID;} {num} { int_val = atoi(yytext); yylval = (void*)&int_val; printf("num: %d",int_val); return NUM; } {real} { float_val = atof(yytext); yylval = (void*)&float_val; printf("real"); return REAL; } "<=" {yylval = yytext;printf("%s",(char*)yytext); return LE_OP;} ">=" {yylval=yytext;printf("%s",(char*)yytext); return GE_OP;} "==" {yylval=yytext;printf("%s",(char*)yytext); return EQ_OP;} "!=" {yylval=yytext;printf("%s",(char*)yytext); return NE_OP;} "&&" {yylval=yytext;printf("%s",(char*)yytext); return AND_OP;} "||" {yylval=yytext;printf("%s",(char*)yytext); return OR_OP;} /*these are needed so that the parser doesn't ignore the single character stuff I need*/ ";"|"+"|"-"|"*"|"/"|"!"|"["|"]"|"{"|"}"|"("|")"|"="|"<"|">" {yylval=yytext;printf("%s",(char*)yytext);return yytext[0];} \n|. {printf("%s",yytext);} %% //list_head_t *list; /*takes whatever is in yytext and treats it as an ID, inserting it into a list*/ void *installID() { char *new_str = memcalloc(yyleng, sizeof(char)); strncpy(new_str,(char*)yytext,yyleng); last_id = new_str; return new_str; } int yywrap() { return 1; } /*for booleans, sets yylval to true or false*/ void setForBool() { if(strcmp((char*)yytext, "true") == 0) { yylval = (void*)true_var; } else if(strcmp((char*)yytext, "false") == 0) { yylval = (void*)false_var; } else { yylval = NULL; } }