/* File: woof3.l In this version, most of the printing is done in main(). The global variable yylval holds the string to be printed. */ %option noyywrap %{ #include #include typedef char* YYSTYPE ; YYSTYPE yylval ; %} %% <> { sprintf(yylval, "\nWoof! are we done talking?\n"); return 0; } treat { sprintf(yylval, "\n!!!%s!!! Woof! yummy!\n", yytext); return 1;} walk { sprintf(yylval, "\n!!!%s!!! Woof! where's the leash?\n", yytext); return 1; } dinner { sprintf(yylval, "\n!!!%s!!! Woof! I'm hungry!\n", yytext); return 1; } good\ dog { sprintf(yylval, "\n!!!%s!!! Woof! talking about me!\n", yytext); return 1;} \n { printf("\n"); } . { printf("."); } %% int main(int argc, char **argv) { yylval = (char *) malloc(10000) ; // hope that's enough while ( yylex() ) { printf("%s", yylval) ; printf("== pat, pat, nice doggy == \n") ; } printf("%s", yylval) ; free(yylval) ; return 0 ; }