KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > Grammar


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 import java.util.Hashtable JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 import persistence.antlr.collections.impl.BitSet;
14 import persistence.antlr.collections.impl.Vector;
15
16 /**A Grammar holds a set of rules (which are stored
17  * in a symbol table). Most of the time a grammar
18  * needs a code generator and an LLkAnalyzer too.
19  */

20 public abstract class Grammar {
21     protected Tool antlrTool;
22     protected CodeGenerator generator;
23     protected LLkGrammarAnalyzer theLLkAnalyzer;
24     protected Hashtable JavaDoc symbols;
25     protected boolean buildAST = false;
26     protected boolean analyzerDebug = false;
27     protected boolean interactive = false;
28     protected String JavaDoc superClass = null;
29
30     /** The token manager associated with the grammar, if any.
31      // The token manager is responsible for maintaining the set of valid tokens, and
32      // is conceptually shared between the lexer and parser. This may be either a
33      // LexerGrammar or a ImportVocabTokenManager.
34      */

35     protected TokenManager tokenManager;
36
37     /** The name of the export vocabulary...used to generate the output
38      * token types interchange file.
39      */

40     protected String JavaDoc exportVocab = null;
41
42     /** The name of the import vocabulary. "Initial conditions"
43      */

44     protected String JavaDoc importVocab = null;
45
46     // Mapping from String keys to Token option values
47
protected Hashtable JavaDoc options;
48     // Vector of RuleSymbol entries
49
protected Vector rules;
50
51     protected Token preambleAction = new CommonToken(Token.INVALID_TYPE, "");
52     protected String JavaDoc className = null;
53     protected String JavaDoc fileName = null;
54     protected Token classMemberAction = new CommonToken(Token.INVALID_TYPE, "");
55     protected boolean hasSyntacticPredicate = false;
56     protected boolean hasUserErrorHandling = false;
57
58     // max lookahead that can be attempted for this parser.
59
protected int maxk = 1;
60
61     // options
62
protected boolean traceRules = false;
63     protected boolean debuggingOutput = false;
64     protected boolean defaultErrorHandler = true;
65
66     protected String JavaDoc comment = null; // javadoc comment
67

68     public Grammar(String JavaDoc className_, Tool tool_, String JavaDoc superClass) {
69         className = className_;
70         antlrTool = tool_;
71         symbols = new Hashtable JavaDoc();
72         options = new Hashtable JavaDoc();
73         rules = new Vector(100);
74         this.superClass = superClass;
75     }
76
77     /** Define a rule */
78     public void define(RuleSymbol rs) {
79         rules.appendElement(rs);
80         // add the symbol to the rules hash table
81
symbols.put(rs.getId(), rs);
82     }
83
84     /** Top-level call to generate the code for this grammar */
85     public abstract void generate() throws IOException JavaDoc;
86
87     protected String JavaDoc getClassName() {
88         return className;
89     }
90
91     /* Does this grammar have a default error handler? */
92     public boolean getDefaultErrorHandler() {
93         return defaultErrorHandler;
94     }
95
96     public String JavaDoc getFilename() {
97         return fileName;
98     }
99
100     /** Get an integer option. Given the name of the option find its
101      * associated integer value. If the associated value is not an integer or
102      * is not in the table, then throw an exception of type NumberFormatException.
103      * @param key The name of the option
104      * @return The value associated with the key.
105      */

106     public int getIntegerOption(String JavaDoc key) throws NumberFormatException JavaDoc {
107         Token t = (Token)options.get(key);
108         if (t == null || t.getType() != ANTLRTokenTypes.INT) {
109             throw new NumberFormatException JavaDoc();
110         }
111         else {
112             return Integer.parseInt(t.getText());
113         }
114     }
115
116     /** Get an option. Given the name of the option find its associated value.
117      * @param key The name of the option
118      * @return The value associated with the key, or null if the key has not been set.
119      */

120     public Token getOption(String JavaDoc key) {
121         return (Token)options.get(key);
122     }
123
124     // Get name of class from which generated parser/lexer inherits
125
protected abstract String JavaDoc getSuperClass();
126
127     public GrammarSymbol getSymbol(String JavaDoc s) {
128         return (GrammarSymbol)symbols.get(s);
129     }
130
131     public Enumeration JavaDoc getSymbols() {
132         return symbols.elements();
133     }
134
135     /** Check the existence of an option in the table
136      * @param key The name of the option
137      * @return true if the option is in the table
138      */

139     public boolean hasOption(String JavaDoc key) {
140         return options.containsKey(key);
141     }
142
143     /** Is a rule symbol defined? (not used for tokens) */
144     public boolean isDefined(String JavaDoc s) {
145         return symbols.containsKey(s);
146     }
147
148     /**Process command line arguments. Implemented in subclasses */
149     public abstract void processArguments(String JavaDoc[] args);
150
151     public void setCodeGenerator(CodeGenerator gen) {
152         generator = gen;
153     }
154
155     public void setFilename(String JavaDoc s) {
156         fileName = s;
157     }
158
159     public void setGrammarAnalyzer(LLkGrammarAnalyzer a) {
160         theLLkAnalyzer = a;
161     }
162
163     /** Set a generic option.
164      * This associates a generic option key with a Token value.
165      * No validation is performed by this method, although users of the value
166      * (code generation and/or analysis) may require certain formats.
167      * The value is stored as a token so that the location of an error
168      * can be reported.
169      * @param key The name of the option.
170      * @param value The value to associate with the key.
171      * @return true if the option was a valid generic grammar option, false o/w
172      */

173     public boolean setOption(String JavaDoc key, Token value) {
174         options.put(key, value);
175         String JavaDoc s = value.getText();
176         int i;
177         if (key.equals("k")) {
178             try {
179                 maxk = getIntegerOption("k");
180                 if ( maxk<=0 ) {
181                     antlrTool.error("option 'k' must be greater than 0 (was " +
182                                     value.getText() + ")",
183                                     getFilename(),
184                                     value.getLine(),
185                                     value.getColumn());
186                     maxk = 1;
187                 }
188             }
189             catch (NumberFormatException JavaDoc e) {
190                 antlrTool.error("option 'k' must be an integer (was " + value.getText() + ")", getFilename(), value.getLine(), value.getColumn());
191             }
192             return true;
193         }
194         if (key.equals("codeGenMakeSwitchThreshold")) {
195             try {
196                 i = getIntegerOption("codeGenMakeSwitchThreshold");
197             }
198             catch (NumberFormatException JavaDoc e) {
199                 antlrTool.error("option 'codeGenMakeSwitchThreshold' must be an integer", getFilename(), value.getLine(), value.getColumn());
200             }
201             return true;
202         }
203         if (key.equals("codeGenBitsetTestThreshold")) {
204             try {
205                 i = getIntegerOption("codeGenBitsetTestThreshold");
206             }
207             catch (NumberFormatException JavaDoc e) {
208                 antlrTool.error("option 'codeGenBitsetTestThreshold' must be an integer", getFilename(), value.getLine(), value.getColumn());
209             }
210             return true;
211         }
212         if (key.equals("defaultErrorHandler")) {
213             if (s.equals("true")) {
214                 defaultErrorHandler = true;
215             }
216             else if (s.equals("false")) {
217                 defaultErrorHandler = false;
218             }
219             else {
220                 antlrTool.error("Value for defaultErrorHandler must be true or false", getFilename(), value.getLine(), value.getColumn());
221             }
222             return true;
223         }
224         if (key.equals("analyzerDebug")) {
225             if (s.equals("true")) {
226                 analyzerDebug = true;
227             }
228             else if (s.equals("false")) {
229                 analyzerDebug = false;
230             }
231             else {
232                 antlrTool.error("option 'analyzerDebug' must be true or false", getFilename(), value.getLine(), value.getColumn());
233             }
234             return true;
235         }
236         if (key.equals("codeGenDebug")) {
237             if (s.equals("true")) {
238                 analyzerDebug = true;
239             }
240             else if (s.equals("false")) {
241                 analyzerDebug = false;
242             }
243             else {
244                 antlrTool.error("option 'codeGenDebug' must be true or false", getFilename(), value.getLine(), value.getColumn());
245             }
246             return true;
247         }
248         if (key.equals("classHeaderSuffix")) {
249             return true;
250         }
251         if (key.equals("classHeaderPrefix")) {
252             return true;
253         }
254         if (key.equals("namespaceAntlr")) {
255             return true;
256         }
257         if (key.equals("namespaceStd")) {
258             return true;
259         }
260         if (key.equals("genHashLines")) {
261             return true;
262         }
263         if (key.equals("noConstructors")) {
264             return true;
265         }
266         return false;
267     }
268
269     public void setTokenManager(TokenManager tokenManager_) {
270         tokenManager = tokenManager_;
271     }
272
273     /** Print out the grammar without actions */
274     public String JavaDoc toString() {
275         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(20000);
276         Enumeration JavaDoc ids = rules.elements();
277         while (ids.hasMoreElements()) {
278             RuleSymbol rs = (RuleSymbol)ids.nextElement();
279             if (!rs.id.equals("mnextToken")) {
280                 buf.append(rs.getBlock().toString());
281                 buf.append("\n\n");
282             }
283         }
284         return buf.toString();
285     }
286
287 }
288
Popular Tags