%{ #include #include #include #include "y.tab.h" #include "lahari.h" extern int lineno; struct _Symbol *new_symbol(); %} letter [a-zA-Z_@] digit [0-9] sign [-+] integer {sign}?{digit}+ decreal ([0-9]*"."[0-9]+) decexp ([0-9]*"."[0-9]+[eE][+-]?[0-9]+) real {sign}?({decreal}|{decexp}) qstring \"(\\\"|[^"\n]*)*\" identifier {letter}[a-zA-Z0-9_@]* whitespace [ \t\f\x1a] nl \n %% {whitespace}+ ; {integer} { Symbol *sym; sym = new_symbol(); sym->ival = atoi(yytext); sym->type = NUMBER; yylval.sym = sym; return NUMBER; } {real} { struct _Symbol *sym; sym = new_symbol(); sym->dval = atof(yytext); sym->type = NUMBER; yylval.sym = sym; return NUMBER; } {qstring} { yylval.sval=strdup(yytext);return STRING; } {nl} { return *yytext; } ; { return *yytext; } [-()=+*/.^] { return *yytext; } and { return BOOL_AND; } or { return BOOL_OR; } "==" { return TEST_EQ; } "!=" { return TEST_NE; } ">" { return TEST_GT; } "<" { return TEST_LT; } while { return WHILE; } if { return IF; } else { return ELSE; } do { return DO; } then { return THEN; } end { return END; } int { return TYPE_INT; } double { return TYPE_REAL; } string { return TYPE_STR; } {identifier} { struct _Symbol *sym; sym = new_symbol(); sym->sval = strdup(yytext); sym->type = IDENT; yylval.sym = sym; return IDENT; } %% struct _Symbol *new_symbol() { Symbol *sym; sym = (Symbol*)calloc(1, sizeof(Symbol)); return sym; }