%{ #include #include int verbose = 0; char *progName = NULL; char *filename = "-"; %} %option noyywrap %x FNAME %% ^"-h" | ^"-?" | ^"--help" { printf("usage is: %s [--help | -h | -?] ", progName); printf("[--verbose | -v ...] "); printf("[(--file | -f) filename]\n"); exit(0); } ^"-v" | ^"--verbose" { verbose++; } ^"-f" | ^"--file" { BEGIN FNAME; filename = ""; } .* { printf("unknown option '%s'!\n", yytext); exit(1); } .+ { filename = strdup(yytext); BEGIN INITIAL; } %% int main(int argc, char *argv[]) { progName = *argv; while(++argv,--argc) { yy_scan_string(*argv); yylex(); } if(!filename || !*filename) { printf("No filename given with option --file!\n"); exit(1); } printf("Now starting to frobnicate with:\n"); printf("verbose = %d\n", verbose); printf("file = '%s'\n", filename); }