1 package antlr; 2 3 9 10 import java.util.Hashtable ; 11 import java.util.Enumeration ; 12 import java.io.IOException ; 13 14 import antlr.collections.impl.BitSet; 15 import antlr.collections.impl.Vector; 16 17 21 public abstract class Grammar { 22 protected Tool antlrTool; 23 protected CodeGenerator generator; 24 protected LLkGrammarAnalyzer theLLkAnalyzer; 25 protected Hashtable symbols; 26 protected boolean buildAST = false; 27 protected boolean analyzerDebug = false; 28 protected boolean interactive = false; 29 protected String superClass = null; 30 31 36 protected TokenManager tokenManager; 37 38 41 protected String exportVocab = null; 42 43 45 protected String importVocab = null; 46 47 protected Hashtable options; 49 protected Vector rules; 51 52 protected Token preambleAction = new CommonToken(Token.INVALID_TYPE, ""); 53 protected String className = null; 54 protected String fileName = null; 55 protected Token classMemberAction = new CommonToken(Token.INVALID_TYPE, ""); 56 protected boolean hasSyntacticPredicate = false; 57 protected boolean hasUserErrorHandling = false; 58 59 protected int maxk = 1; 61 62 protected boolean traceRules = false; 64 protected boolean debuggingOutput = false; 65 protected boolean defaultErrorHandler = true; 66 67 protected String comment = null; 69 public Grammar(String className_, Tool tool_, String superClass) { 70 className = className_; 71 antlrTool = tool_; 72 symbols = new Hashtable (); 73 options = new Hashtable (); 74 rules = new Vector(100); 75 this.superClass = superClass; 76 } 77 78 79 public void define(RuleSymbol rs) { 80 rules.appendElement(rs); 81 symbols.put(rs.getId(), rs); 83 } 84 85 86 public abstract void generate() throws IOException ; 87 88 protected String getClassName() { 89 return className; 90 } 91 92 93 public boolean getDefaultErrorHandler() { 94 return defaultErrorHandler; 95 } 96 97 public String getFilename() { 98 return fileName; 99 } 100 101 107 public int getIntegerOption(String key) throws NumberFormatException { 108 Token t = (Token)options.get(key); 109 if (t == null || t.getType() != ANTLRTokenTypes.INT) { 110 throw new NumberFormatException (); 111 } 112 else { 113 return Integer.parseInt(t.getText()); 114 } 115 } 116 117 121 public Token getOption(String key) { 122 return (Token)options.get(key); 123 } 124 125 protected abstract String getSuperClass(); 127 128 public GrammarSymbol getSymbol(String s) { 129 return (GrammarSymbol)symbols.get(s); 130 } 131 132 public Enumeration getSymbols() { 133 return symbols.elements(); 134 } 135 136 140 public boolean hasOption(String key) { 141 return options.containsKey(key); 142 } 143 144 145 public boolean isDefined(String s) { 146 return symbols.containsKey(s); 147 } 148 149 150 public abstract void processArguments(String [] args); 151 152 public void setCodeGenerator(CodeGenerator gen) { 153 generator = gen; 154 } 155 156 public void setFilename(String s) { 157 fileName = s; 158 } 159 160 public void setGrammarAnalyzer(LLkGrammarAnalyzer a) { 161 theLLkAnalyzer = a; 162 } 163 164 174 public boolean setOption(String key, Token value) { 175 options.put(key, value); 176 String s = value.getText(); 177 int i; 178 if (key.equals("k")) { 179 try { 180 maxk = getIntegerOption("k"); 181 } 183 catch (NumberFormatException e) { 184 antlrTool.error("option 'k' must be an integer (was " + value.getText() + ")", getFilename(), value.getLine(), value.getColumn()); 185 } 186 return true; 187 } 188 if (key.equals("codeGenMakeSwitchThreshold")) { 189 try { 190 i = getIntegerOption("codeGenMakeSwitchThreshold"); 191 } 192 catch (NumberFormatException e) { 193 antlrTool.error("option 'codeGenMakeSwitchThreshold' must be an integer", getFilename(), value.getLine(), value.getColumn()); 194 } 195 return true; 196 } 197 if (key.equals("codeGenBitsetTestThreshold")) { 198 try { 199 i = getIntegerOption("codeGenBitsetTestThreshold"); 200 } 201 catch (NumberFormatException e) { 202 antlrTool.error("option 'codeGenBitsetTestThreshold' must be an integer", getFilename(), value.getLine(), value.getColumn()); 203 } 204 return true; 205 } 206 if (key.equals("defaultErrorHandler")) { 207 if (s.equals("true")) { 208 defaultErrorHandler = true; 209 } 210 else if (s.equals("false")) { 211 defaultErrorHandler = false; 212 } 213 else { 214 antlrTool.error("Value for defaultErrorHandler must be true or false", getFilename(), value.getLine(), value.getColumn()); 215 } 216 return true; 217 } 218 if (key.equals("analyzerDebug")) { 219 if (s.equals("true")) { 220 analyzerDebug = true; 221 } 222 else if (s.equals("false")) { 223 analyzerDebug = false; 224 } 225 else { 226 antlrTool.error("option 'analyzerDebug' must be true or false", getFilename(), value.getLine(), value.getColumn()); 227 } 228 return true; 229 } 230 if (key.equals("codeGenDebug")) { 231 if (s.equals("true")) { 232 analyzerDebug = true; 233 } 234 else if (s.equals("false")) { 235 analyzerDebug = false; 236 } 237 else { 238 antlrTool.error("option 'codeGenDebug' must be true or false", getFilename(), value.getLine(), value.getColumn()); 239 } 240 return true; 241 } 242 if (key.equals("classHeaderSuffix")) { 243 return true; 244 } 245 if (key.equals("namespaceAntlr")) { 246 return true; 247 } 248 if (key.equals("namespaceStd")) { 249 return true; 250 } 251 if (key.equals("genHashLines")) { 252 return true; 253 } 254 return false; 255 } 256 257 public void setTokenManager(TokenManager tokenManager_) { 258 tokenManager = tokenManager_; 259 } 260 261 262 public String toString() { 263 StringBuffer buf = new StringBuffer (20000); 264 Enumeration ids = rules.elements(); 265 while (ids.hasMoreElements()) { 266 RuleSymbol rs = (RuleSymbol)ids.nextElement(); 267 if (!rs.id.equals("mnextToken")) { 268 buf.append(rs.getBlock().toString()); 269 buf.append("\n\n"); 270 } 271 } 272 return buf.toString(); 273 } 274 275 } 276 | Popular Tags |