private RunAutomaton processChemicalGrammar(boolean reversed) throws IOException {
		XMLStreamReader reader = resourceGetter.getXMLStreamReader("regexes.xml");
		Map<String, StringBuilder> regexDict = new HashMap<String, StringBuilder>();
		Pattern matchRegexReplacement = Pattern.compile("%.*?%");
		try {
			while (reader.hasNext()) {
				if (reader.next() == XMLStreamConstants.START_ELEMENT &&
						reader.getLocalName().equals("regex")) {
					String name = reader.getAttributeValue(null, "name");
					String value = reader.getAttributeValue(null, "value");
					Matcher m = matchRegexReplacement.matcher(value);
					StringBuilder newValueSB = new StringBuilder();
					int position = 0;
					while(m.find()) {
						newValueSB.append(value.substring(position, m.start()));
						StringBuilder replacement = regexDict.get(m.group()); 
						if (replacement == null){
							throw new RuntimeException("Regex entry for: " + m.group() + " missing! Check regexes.xml");
						}
						newValueSB.append(replacement);
						position = m.end();
					}
					newValueSB.append(value.substring(position));
					if (regexDict.get(name) != null){
						throw new RuntimeException("Regex entry: " + name + " has duplicate definitions! Check regexes.xml");
					}
					regexDict.put(name, newValueSB);
				}
			}
		}
		catch (XMLStreamException e) {
			throw new IOException("Parsing exception occurred while reading regexes.xml", e);
		}
		finally {
			try {
				reader.close();
			} catch (XMLStreamException e) {
				throw new IOException("Parsing exception occurred while reading regexes.xml", e);
			}
		}

		String re = regexDict.get("%chemical%").toString();
		if (!reversed){
			return automatonInitialiser.loadAutomaton("chemical", re, true, false);
		}
		else{
			return automatonInitialiser.loadAutomaton("chemical", re, true, true);
		}
	}