File: zero-lexer.jflex

package info (click to toggle)
jflex 1.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 13,944 kB
  • sloc: java: 421,255; xml: 1,130; makefile: 123; lisp: 90; yacc: 65; sh: 13
file content (88 lines) | stat: -rw-r--r-- 2,527 bytes parent folder | download | duplicates (4)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.*;

%%

%public
%class ZeroLexer

%int
  

%{
  public static void main(String argv[]) {
    if (argv.length == 0) {
      System.out.println("Usage : java ZeroLexer [ --encoding <name> ] <inputfile(s)>");
    }
    else {
      int firstFilePos = 0;
      String encodingName = "UTF-8";
      if (argv[0].equals("--encoding")) {
        firstFilePos = 2;
        encodingName = argv[1];
        try {
          java.nio.charset.Charset.forName(encodingName); // Side-effect: is encodingName valid? 
        } catch (Exception e) {
          System.out.println("Invalid encoding '" + encodingName + "'");
          return;
        }
      }
      for (int i = firstFilePos; i < argv.length; i++) {
        ZeroLexer scanner = null;
        try {
          FileInputStream stream = new FileInputStream(argv[i]);
          // the usual Reader:
          Reader reader = new InputStreamReader(stream, encodingName);
          // the broken 0-returning Reader:
          reader = new FunkyReader(reader);
          
          try {
            scanner = new ZeroLexer(reader);
            do {
              System.out.println(scanner.yylex());
              System.out.println("--"+scanner.yytext()+"--");
            } while (!scanner.zzAtEOF);
          }
          catch (IOException e) {
            System.out.println("Expected IO exception");
          }

          reader.close();
          
          stream = new FileInputStream(argv[i]);
          reader = new InputStreamReader(stream, encodingName);
          reader = new FunkyReader(reader);
          // now the wrapper for broken Readers:
          reader = new ZeroReader(reader);
          
          scanner = new ZeroLexer(reader);
          do {
            System.out.println(scanner.yylex());
            System.out.println("--"+scanner.yytext()+"--");
          } while (!scanner.zzAtEOF);

        }
        catch (java.io.FileNotFoundException e) {
          System.out.println("File not found : \""+argv[i]+"\"");
        }
        catch (java.io.IOException e) {
          System.out.println("IO error scanning file \""+argv[i]+"\"");
          System.out.println(e);
        }
        catch (Exception e) {
          System.out.println("Unexpected exception:");
          e.printStackTrace();
        }
      }
    }
  }  
  
%}


%%

"some longer fixed-length match"   { return 1; }
"a"+                               { return 2; }
[^]                                { return 3; }

<<EOF>>                            { return YYEOF; }