%{ #include extern int yylval; extern int symcount; typedef struct { char mysymbol[20]; int value;} SYMBOL_TBL; SYMBOL_TBL sym_tbl[100]; #include "y.tab.h" int x; %} letter [A-Za-z] digit [0-9] ident {letter}({letter}|{digit})* op "+"|"-"|"*"|"/"|"("|")"|";" ws [ \t\n]+ other . %% {ws} ; "if" {return IF;} "print" { return PRINT;} "then" { return THEN;} "else" { return ELSE;} "endif" { return ENDIF; } {ident} { yylval = install(yytext,0); return ID; } {op} return yytext[0]; {digit}+ { sscanf(yytext,"%d",&x); yylval = install(yytext,x); return NUM; } "=" return ASSIGN; . ; %% int install(char *ident, int val) { int i; i = 1; while ((i <= symcount) && strcmp(ident, sym_tbl[i].mysymbol)) i++; if (i <= symcount) return i; else { symcount++; strcpy(sym_tbl[symcount].mysymbol,ident); sym_tbl[symcount].value = val; return symcount; } }