1 | import java.io.*; |
---|
2 | import antlr.RecognitionException; |
---|
3 | import antlr.TokenStreamException; |
---|
4 | import antlr.TokenStreamRecognitionException; |
---|
5 | import antlr.TokenStreamRetryException; |
---|
6 | import antlr.CharBuffer; |
---|
7 | |
---|
8 | class Main { |
---|
9 | // find new line chars for this OS |
---|
10 | private static final String nl = System.getProperty("line.separator","\n"); |
---|
11 | |
---|
12 | public static void main(String[] args) { |
---|
13 | |
---|
14 | InputStreamReader reader = null; |
---|
15 | FileInputStream inputStream = null; |
---|
16 | |
---|
17 | // this one command redirects all System.out and System.err to debug file |
---|
18 | Debug.log.println(" Output for Unicode example"); |
---|
19 | |
---|
20 | System.out.println(" The input file is:"+args[0]); |
---|
21 | try { |
---|
22 | inputStream = new FileInputStream(args[0]); |
---|
23 | } catch (FileNotFoundException ex) { |
---|
24 | System.out.println("Could not find file."); |
---|
25 | System.exit(1); |
---|
26 | } |
---|
27 | |
---|
28 | try { |
---|
29 | reader = new InputStreamReader(inputStream,"SJIS"); |
---|
30 | } catch (UnsupportedEncodingException ex) { |
---|
31 | System.out.println("Invalid encoding"); |
---|
32 | System.exit(1); |
---|
33 | } |
---|
34 | BufferedReader bufferedReader = new BufferedReader(reader); |
---|
35 | try { |
---|
36 | String line = bufferedReader.readLine(); |
---|
37 | while (line != null) { |
---|
38 | System.out.println(line); |
---|
39 | line = bufferedReader.readLine(); |
---|
40 | } |
---|
41 | inputStream.close(); |
---|
42 | } catch (IOException ex) { |
---|
43 | System.out.println(ex.getMessage()); |
---|
44 | System.exit(1); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | System.out.println("\nThe parse output is:"); |
---|
49 | |
---|
50 | try { |
---|
51 | inputStream = new FileInputStream(args[0]); |
---|
52 | } catch (FileNotFoundException ex) { |
---|
53 | System.out.println("Could not find file."); |
---|
54 | System.exit(1); |
---|
55 | } |
---|
56 | try { |
---|
57 | reader = new InputStreamReader(inputStream,"SJIS"); |
---|
58 | } catch (UnsupportedEncodingException ex) { |
---|
59 | System.out.println("Invalid encoding"); |
---|
60 | System.exit(1); |
---|
61 | } |
---|
62 | CharBuffer cb = new CharBuffer(reader); |
---|
63 | try { |
---|
64 | UnicodeLexer lexer = new UnicodeLexer(cb); |
---|
65 | UnicodeParser parser = new UnicodeParser(lexer); |
---|
66 | parser.program(); |
---|
67 | |
---|
68 | } catch(Exception e) { |
---|
69 | // catch all mainly IOExceptions |
---|
70 | System.err.println("exception: "+e); |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|