| 1 |
/* |
| 2 |
* cat.lex: A demonstration of YY_NEW_FILE. |
| 3 |
*/ |
| 4 |
|
| 5 |
%{ |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
char **names = NULL; |
| 9 |
int current = 1; |
| 10 |
%} |
| 11 |
|
| 12 |
%% |
| 13 |
<<EOF>> { |
| 14 |
current += 1; |
| 15 |
if(names[current] != NULL){ |
| 16 |
yyin = fopen(names[current],"r"); |
| 17 |
if(yyin == NULL){ |
| 18 |
fprintf(stderr,"cat: unable to open %s\n", |
| 19 |
names[current]); |
| 20 |
yyterminate(); |
| 21 |
} |
| 22 |
YY_NEW_FILE; |
| 23 |
} else { |
| 24 |
yyterminate(); |
| 25 |
} |
| 26 |
} |
| 27 |
%% |
| 28 |
|
| 29 |
int main(int argc, char **argv) |
| 30 |
{ |
| 31 |
if(argc < 2){ |
| 32 |
fprintf(stderr,"Usage: cat files....\n"); |
| 33 |
exit(1); |
| 34 |
} |
| 35 |
names = argv; |
| 36 |
|
| 37 |
yyin = fopen(names[current],"r"); |
| 38 |
if(yyin == NULL){ |
| 39 |
fprintf(stderr,"cat: unable to open %s\n", |
| 40 |
names[current]); |
| 41 |
yyterminate(); |
| 42 |
} |
| 43 |
|
| 44 |
yylex(); |
| 45 |
} |