KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ppg > PPG


1 package ppg;
2
3 import java.io.*;
4 import ppg.cmds.*;
5 import ppg.lex.*;
6 import ppg.parse.*;
7 import ppg.spec.*;
8 import ppg.util.*;
9
10 public class PPG
11 {
12     public static final String JavaDoc HEADER = "ppg: ";
13     public static final String JavaDoc DEBUG_HEADER = "ppg [debug]: ";
14     public static boolean debug = false;
15     public static String JavaDoc SYMBOL_CLASS_NAME = "sym";
16     public static String JavaDoc OUTPUT_FILE = null;
17
18     public static void DEBUG (String JavaDoc s)
19     {
20     if (debug)
21         System.out.println (DEBUG_HEADER + s);
22     }
23
24     public static void main (String JavaDoc args[])
25     {
26     FileInputStream fileInput;
27     String JavaDoc filename = null;
28
29     try
30     {
31         for (int i = 0; i < args.length; i++)
32           {
33           // assume all switches begin with a dash '-'
34
if (args[i].charAt (0) == '-')
35             {
36             if (args[i].equals ("-symbols"))
37               {
38                   if (args.length > i)
39                   SYMBOL_CLASS_NAME = args[++i];
40                   else
41                   throw new
42                       Exception JavaDoc
43                       ("No filename specified after -symbols");
44               }
45                         else if (args[i].equals ("-o"))
46                           {
47                               if (args.length > i)
48                                   OUTPUT_FILE = args[++i];
49                               else
50                   throw new
51                       Exception JavaDoc
52                       ("No filename specified after -o");
53                           }
54             else // invalid switch
55
throw new Exception JavaDoc ("Invalid switch: " +
56                          args[i]);
57             }
58           else
59             {
60             // not a switch: this must be a filename
61
// but only do the 1st filename on the command line
62
if (filename == null)
63                 filename = args[i];
64             else
65                 throw new
66                 Exception JavaDoc
67                 ("Error: multiple source files specified.");
68             }
69           }
70     }
71     catch (Exception JavaDoc e)
72     {
73         System.err.println (HEADER + e.getMessage ());
74         usage ();
75     }
76
77
78     if (filename == null)
79       {
80           System.err.println ("Error: no filename specified.");
81           usage ();
82       }
83
84     try
85     {
86         fileInput = new FileInputStream (filename);
87     }
88     catch (FileNotFoundException e)
89     {
90         System.out.println ("Error: " + filename + " is not found.");
91         return;
92     }
93     catch (ArrayIndexOutOfBoundsException JavaDoc e)
94     {
95         System.out.println (HEADER + "Error: No file name given.");
96         return;
97     }
98
99     Lexer lex = new Lexer (fileInput, filename);
100
101     Parser parser = new Parser (filename, lex);
102     try
103     {
104         parser.parse ();
105     }
106     catch (Exception JavaDoc e)
107     {
108         System.out.println (HEADER + "Exception: " + e.getMessage ());
109         return;
110     }
111     Spec spec = (Spec) parser.getProgramNode ();
112
113         File file = new File(filename);
114     String JavaDoc parent = file.getParent ();
115     spec.parseChain (parent == null ? "" : parent);
116
117         PrintStream out = System.out;
118
119
120     /* now we have a linked list of inheritance, namely
121      * PPG_1, PPG_2, ..., PPG_n, CUP
122      * We combine two at a time, starting from the end with the CUP spec
123      */

124     try
125     {
126             if (OUTPUT_FILE != null) {
127                 out = new PrintStream(new FileOutputStream(OUTPUT_FILE));
128             }
129
130         CUPSpec combined = spec.coalesce ();
131         CodeWriter cw = new CodeWriter (out, 72);
132         combined.unparse (cw);
133         cw.flush ();
134     }
135     catch (PPGError e)
136     {
137         System.out.println (e.getMessage ());
138         System.exit (1);
139     }
140     catch (IOException e)
141     {
142         System.out.println (HEADER + "exception: " + e.getMessage ());
143         System.exit (1);
144     }
145     }
146
147
148     public static void usage ()
149     {
150     System.err.
151         println ("Usage: ppg [-symbols ConstClass] <input file>\nwhere:\n" +
152              "\t-c <Class>\tclass prepended to token names to pass to <func>\n"
153              + "\t<input>\ta PPG or CUP source file\n");
154     System.exit (1);
155     }
156 }
157
Popular Tags