KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fri > patterns > interpreter > parsergenerator > builder > CompiledTables


1 package fri.patterns.interpreter.parsergenerator.builder;
2
3 import java.io.File JavaDoc;
4 import java.lang.reflect.*;
5 import fri.patterns.interpreter.parsergenerator.parsertables.*;
6 import fri.patterns.interpreter.parsergenerator.syntax.Syntax;
7
8 /**
9     This class is deprecated in favor of serialization.
10     <p>
11     CompiledTables buffers parser tables by generating source and compiling
12     that source. This lasts very long at first time, but is faster than
13     serialization for every subsequent call. The class will be posted in
14     current directory (has no package). The source gets removed after successful
15     compilation. The Sun compiler must be in package path, which is normally in
16     <i>$JAVA_HOME/lib/tools.jar</i>.
17     <p>
18     Building compiled Java ParserTables takes 1600, building from scratch takes 6000 millis.
19     CompiledTables seem to be slower than SerializedParser!
20     <p>
21     An alternative to compiled tables is generating the parser tables source
22     with <i>SourceGenerator</i> and integrating that source into project sources.
23     This avoids the dynamically created class in working directory and the neccessity
24     to have javac in CLASSPATH at runtime.
25     <pre>
26         ParserTables tables = new CompiledTables().get(syntaxInput, "MyParserTables");
27     </pre>
28     
29     @deprecated in favor of serialization
30 */

31
32 public class CompiledTables
33 {
34     /** The suffix for compiled ParserTables files. */
35     public static final String JavaDoc CLASSFILE_SUFFIX = "ParserTables";
36     private boolean DEVELOPMENT = false; // setting this to true in constructor will prevent the tables from being compiled
37

38     /** Creates a parser tables factory that uses compiled tables as buffer. */
39     public CompiledTables() {
40         this(false);
41     }
42     
43     /** Creates a parser tables factory that uses compiled tables as buffer. @param development when true tables will NOT be compiled. */
44     public CompiledTables(boolean development) {
45         this.DEVELOPMENT = development;
46     }
47
48
49     /**
50         Builds the ParserTables from scratch if not found as class in CLASSPATH, else loads that class.
51         LALRParserTables.class wil be the type of created parser tables.
52         @param syntaxInput the Parser syntax as File, InputStream, List of Lists, String [][] or Syntax.
53         @return ParserTables object, or object built from scratch that gets compiled into current directory.
54     */

55     public AbstractParserTables get(Object JavaDoc syntaxInput)
56         throws Exception JavaDoc
57     {
58         return get(syntaxInput, null);
59     }
60
61     /**
62         Builds the ParserTables from scratch if not found as class in CLASSPATH, else loads that class.
63         LALRParserTables.class wil be the type of created parser tables.
64         @param syntaxInput the Parser syntax as File, InputStream, List of Lists, String [][] or Syntax.
65         @param className name of target class of ParserTables, without package path, without .java extension.
66         @return ParserTables object, or object built from scratch that gets compiled into current directory.
67     */

68     public AbstractParserTables get(Object JavaDoc syntaxInput, String JavaDoc className)
69         throws Exception JavaDoc
70     {
71         return get(LALRParserTables.class, syntaxInput, className);
72     }
73
74     /**
75         Builds the ParserTables from scratch if not found as class in CLASSPATH, else loads that class.
76         LALRParserTables.class wil be the type of created parser tables.
77         @param syntaxInput the Parser syntax as File, InputStream, List of Lists, String [][] or Syntax.
78         @param className name of target class of ParserTables, without package path, without .java extension.
79         @param parserType class of ParserTables to create, e.g. LALRParserTables.class
80         @return ParserTables object, or object built from scratch that gets compiled into current directory.
81     */

82     public AbstractParserTables get(Class JavaDoc parserType, Object JavaDoc syntaxInput, String JavaDoc className)
83         throws Exception JavaDoc
84     {
85         if (className == null)
86             className = SerializedObject.baseNameFromSyntax(syntaxInput);
87         
88         className = className+CLASSFILE_SUFFIX;
89         
90         AbstractParserTables parserTables = null;
91         
92         if (DEVELOPMENT == false) {
93             try {
94                 Class JavaDoc cls = Class.forName(className);
95                 parserTables = AbstractParserTables.construct(cls, null);
96             }
97             catch (Exception JavaDoc e) { // class is not present
98
System.err.println("Could not find compiled parser tables for classname "+className+" in "+System.getProperty("java.class.path")+" - trying scratch build: "+e.toString());
99             }
100         }
101         
102         if (parserTables == null) {
103             fri.util.TimeStopper ts = new fri.util.TimeStopper();
104
105             Syntax grammar = SerializedObject.toSyntax(syntaxInput);
106             parserTables = AbstractParserTables.construct(parserType, grammar);
107             if (DEVELOPMENT == false) {
108                 String JavaDoc javaFile = parserTables.toSourceFile(className);
109                 int ret = compile(javaFile);
110                 new File JavaDoc(javaFile).delete(); // source file no more needed
111
System.err.println("ParserTables scratch compilation took "+ts.getTimeMillis()+" millis, return was "+ret);
112             }
113         }
114         
115         return parserTables;
116     }
117
118
119     /**
120         Aufruf des Sun-Compilers. Dazu muss tools.jar im CLASSPATH sein!
121         @return return-code von "new com.sun.tools.javac.Main().compile(javaFile)".
122     */

123     protected int compile(String JavaDoc javaFile) {
124         try {
125             String JavaDoc javaCompiler = "com.sun.tools.javac.Main";
126             System.err.println("compiling source: "+javaCompiler+" "+javaFile);
127
128             Class JavaDoc cls = Class.forName(javaCompiler);
129             Object JavaDoc compiler = cls.newInstance();
130             Method method = cls.getMethod("compile", new Class JavaDoc[] { String JavaDoc[].class });
131             String JavaDoc [] args = new String JavaDoc [] {
132                     "-classpath",
133                     "."+System.getProperty("path.separator")+System.getProperty("java.class.path"),
134                     javaFile
135             };
136             Object JavaDoc o = method.invoke(compiler, new Object JavaDoc [] { args });
137             int ret = ((Integer JavaDoc)o).intValue();
138
139             System.err.println("javac returns "+ret);
140             return ret;
141         }
142         catch (Exception JavaDoc e) {
143             e.printStackTrace();
144             System.err.println("com.sun.tools.javac.Main() not present. Please put $JAVA_HOME/lib/tools.jar into CLASSPATH for quick loading next time.");
145         }
146         return -1;
147     }
148
149
150
151     /** Test main. Building compiled ParserTables takes 1600, building from scratch takes 6000 millis. */
152     public static void main(String JavaDoc [] args) {
153         try {
154             File JavaDoc syntaxFile = new File JavaDoc("fri/patterns/interpreter/parsergenerator/syntax/builder/examples/SyntaxBuilder.syntax");
155             Syntax syntax = new fri.patterns.interpreter.parsergenerator.syntax.builder.SyntaxBuilder(syntaxFile).getParserSyntax();
156             fri.util.TimeStopper ts = new fri.util.TimeStopper();
157             AbstractParserTables parserTables = new CompiledTables().get(syntax, "SyntaxBuilderParserTables");
158             System.err.println("ParserTables were built in "+ts.getTimeMillis()+" millis");
159         }
160         catch (Exception JavaDoc e) {
161             e.printStackTrace();
162         }
163     }
164
165 }
166
Popular Tags