File: cat.lex

package info (click to toggle)
flex 2.6.4-8.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 7,168 kB
  • sloc: ansic: 12,044; sh: 5,363; lex: 3,699; yacc: 990; makefile: 717; perl: 238; awk: 72; cpp: 25; sed: 16
file content (45 lines) | stat: -rw-r--r-- 849 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * cat.lex: A demonstration of YY_NEW_FILE.
 */

%{
#include <stdio.h>

char **names = NULL;
int  current = 1;
%}

%%
<<EOF>> {
           current += 1;
           if(names[current] != NULL){
              yyin = fopen(names[current],"r");
              if(yyin == NULL){
                fprintf(stderr,"cat: unable to open %s\n",
                        names[current]);
                yyterminate();
              }
              YY_NEW_FILE;
           } else {
             yyterminate();
           }
        }
%%

int main(int argc, char **argv)
{
    if(argc < 2){
       fprintf(stderr,"Usage: cat files....\n");
       exit(1);
    }
    names = argv;

    yyin = fopen(names[current],"r");
    if(yyin == NULL){
      fprintf(stderr,"cat: unable to open %s\n",
              names[current]);
      yyterminate();
    }

    yylex();
}