%{ /* some initial C programs */ #define BEGINSYM 1 #define INTEGER 2 #define IDNAME 3 #define REAL 4 #define STRING 5 #define SEMICOLONSYM 6 #define ASSIGNSYM 7 %} Digit [0-9] Letter [a-zA-Z] IntLit {Digit}+ Id {Letter}({Letter}|{Digit}|_)* %% [ \t\n] {/* skip white spaces */} [Bb][Ee][Gg][Ii][Nn] {return(BEGINSYM);} {IntLit} {return(INTEGER);} {Id} { printf("var has %d characters, ",yyleng); return(IDNAME); } ({IntLit}[.]{IntLit})([Ee][+-]?{IntLit})? {return(REAL);} \"[^\"\n]*\" {stripquotes(); return(STRING);} ";" {return(SEMICOLONSYM);} ":=" {return(ASSIGNSYM);} . {printf("error --- %s\n",yytext);} %% /* some final C programs */ stripquotes() { /* handling string within a quoted string */ int frompos, topos=0, numquotes = 2; for(frompos=1; frompos0 && i < 8){ printf("<%s> is %d\n",yytext,i); i = yylex(); } }