/// <summary> /// Generates a CNF for a given grammar or returns null if the Grammar doesn't produce any words. /// </summary> /// <param name="g">the original grammar</param> /// <returns>the CNF or null</returns> public static ContextFreeGrammar getEquivalentCNF(ContextFreeGrammar g) { if (g == null) { return(null); } if (g.IsInCNF()) { return(g); } try { ContextFreeGrammar res = ContextFreeGrammar.MkCNF(g); //handle empty string res.setAcceptanceForEmptyString(g.acceptsEmptyString()); return(res); } catch (AutomataException e) { if (g.acceptsEmptyString()) { var res = new ContextFreeGrammar(new Nonterminal("S"), new Production[] { new Production(new Nonterminal("S"), new GrammarSymbol[] { new Nonterminal("S"), new Nonterminal("S") }) }); res.setAcceptanceForEmptyString(true); return(res); } return(null); } }