| Building Scanners with Lex |
A Lex definition is therefore a series of descriptions of the different types of "small entities" that might appear paired with "actions" (more C code) describing what to do if they appear.
alpha [a-zA-Z]
...
%%
{alpha}+ printf("I found a word");
%{
#include "tn.h"
typedef union {
int num;
char *str;
} YYSTYPE;
extern YYSTYPE yylval;
char * makecopy( char * str);
%}
alfa [a-zA-Z]
digit [0-9]
%%
"," { return ','; }
"/" { return '/'; }
{alfa}+ { yylval.str = makecopy(yytext); return MONTH; }
{digit}+ { yylval.num = atoi(yytext); return NUMBER; }
. { }
%%
char * makecopy( char * str) {
char * result;
result = malloc( strlen( str) + 1);
strcpy(result,str);
return result;
}
| Building Scanners with Lex |