/* $Id: scan.l.txt,v 1.1.1.1 2002/08/14 22:28:26 dan Exp $ -*-C-*- */ %{ #include #include #include #define NUM_TYPES 4 enum key_type { TITLE = 0, DESCRIPTION = 1, KEYWORDS = 2, HERE = 3, BIG_NaN = 99999999 } current_key; char *keys[NUM_TYPES]; int flag_lex = 0; int flag_key = 0; %} %% \ { if (flag_lex == 1) flag_lex = 0; else ECHO; } [a-z]+= { if (flag_lex == 1 && flag_key == 0) { flag_key = 1; if (! strcmp(yytext, "title=")) current_key = TITLE; else if (! strcmp(yytext, "description=")) current_key = DESCRIPTION; else if (! strcmp(yytext, "keywords=")) current_key = KEYWORDS; else if (! strcmp(yytext, "here=")) current_key = HERE; else current_key = BIG_NaN; } else ECHO; } \"header\" { if (flag_lex == 1 && flag_key == 1 && current_key == HERE) { fprintf(yyout, "\n" "\n" "\n" "\n{zhaoway} %s\n" "\n" "\n" "\n" "\n" "\n\n" "\n", keys[TITLE], keys[DESCRIPTION], keys[KEYWORDS] ); flag_key = 0; } else ECHO; } \"footer\" { if (flag_lex == 1 && flag_key == 1 && current_key == HERE) { fprintf(yyout, "\n" ); flag_key = 0; } else ECHO; } \"[^\"]*\" { int len; if (flag_lex != 1) ECHO; else if (flag_key != 1) { fprintf(stderr, "scan: error: ill format\n"); exit(EXIT_FAILURE); } else if (current_key != BIG_NaN) { len = strlen(yytext) + 1; keys[current_key] = (char *) malloc(len); if (keys[current_key] == NULL) exit(EXIT_FAILURE); memcpy(keys[current_key], yytext, len); } flag_key = 0; } %% int main(void) { current_key = BIG_NaN; yylex(); return(0); }