%{ /* * exp3.l is the flex specification for * exp3.y. The exp3.tab.h (y.tab.h) will be created * bison (yacc) compiler. Compile as * $ flex exp3.l * output: lex.yy.c */ #include #include #include #include "calc.h" #include "exp3.tab.h" /* Created by bison */ %} %option noyywrap DELIM ([ \t]) WHITESPACES ({DELIM}+) NATNUM ([0-9]+) FLOAT (([0-9]*(\.)?[0-9]+)|([0-9]+(\.)?[0-9]*)) NAME ([a-zA-Z])+ %% {WHITESPACES} { ; } {FLOAT} { yylval.val = atof(yytext) ; return NUM ; } {NAME} { char *symbuf ; symrec* symTabP ; symbuf = (char *)malloc(LENGTH); strncpy(symbuf, yytext, yyleng); symbuf[yyleng]='\0'; symTabP = getsym(symbuf); if(symTabP == NULL) symTabP = putsym(symbuf, VAR); yylval.symTabPtr = symTabP; return symTabP -> type; // VAR or FUN1 or FUN2 } "+" { return (int)'+' ; } "-" { return (int)'-' ; } "/" { return (int)'/' ; } "*" { return (int)'*' ;} "\n" { return (int)'\n';} "(" { return (int)'(';} ")" { return (int)')';} "," { return (int)',';} "=" { return (int)'=';} %% symrec *getsym (char *sym_name) { symrec *ptr; for (ptr = sym_table; ptr != (symrec *) 0; ptr = (symrec *)ptr->next) if (strcmp (ptr->name,sym_name) == 0) return ptr; return 0; }