private void AddRule(Variable variable, params Symbol[] symbols)
        {
            if (!_rules.ContainsKey(variable))
                _rules.Add(variable, new List<IEnumerable<Symbol>>());

            if (!_rules[variable].Any(r => r.IsEquivalentTo(symbols)))
                _rules[variable].Add(symbols);
        }
        private ChomskyNormalFormTransformer(Variable startVariable, IDictionary<Variable, ICollection<IEnumerable<Symbol>>> rules)
        {
            _rules = new Dictionary<Variable, ICollection<IEnumerable<Symbol>>>();
            rules.ForEach(p =>
            {
                _rules.Add(p.Key, new List<IEnumerable<Symbol>>());
                p.Value.ForEach(r => _rules[p.Key].Add(r));
            });

            _startVariable = GetNewVariableBasedOn(startVariable);
            AddRule(_startVariable, startVariable);
        }
 private IEnumerable<Terminal> GetTerminalsProducedByVariable(Variable variable)
 {
     return _terminalsProducedByVariables[variable];
 }
 public ContextFreeGrammar Produce(Variable variable, params Symbol[] symbols)
 {
     AddRule(variable, symbols);
     return this;
 }
 public IEnumerable<IEnumerable<Symbol>> GetRules(Variable variable)
 {
     return _rules.ContainsKey(variable) ? _rules[variable] : Enumerable.Empty<IEnumerable<Symbol>>();
 }
 public ContextFreeGrammar(Variable startVariable, IDictionary<Variable, ICollection<IEnumerable<Symbol>>> rules)
 {
     _startVariable = startVariable;
     _rules = rules;
 }
        private void AddRule(Variable variable, params Symbol[] symbols)
        {
            if (symbols.Count() == 1 && variable.Equals(symbols.Single()))
                throw new ArgumentException("Recursive unit rules cannot be created.");

            if (!_rules.ContainsKey(variable))
            {
                _rules.Add(variable, new List<IEnumerable<Symbol>>());
            }

            _rules[variable].Add(symbols);
        }
 public static ContextFreeGrammar Transform(Variable startVariable, IDictionary<Variable, ICollection<IEnumerable<Symbol>>> rules)
 {
     var transformer = new ChomskyNormalFormTransformer(startVariable, rules);
     return transformer.Transform();
 }
 private void ReplaceVariable(Variable variable, params Symbol[] replacement)
 {
     var variableContainingRuleCollections = _rules.Values.Where(r => r.Any(x => x.Contains(variable))).ToArray();
     foreach (var collection in variableContainingRuleCollections)
     {
         collection.ToArray().ForEach(i => collection.AddRange(i.ReplacePermutations(variable, replacement)));
     }
 }
        private void RemoveRule(Variable variable, params Symbol[] symbols)
        {
            if (!_rules.ContainsKey(variable)) return;

            _rules[variable].Where(r => r.IsEquivalentTo(symbols)).ToArray()
                .ForEach(r => _rules[variable].Remove(r));
        }
        private Variable GetNewVariableBasedOn(Variable original)
        {
            if (!Variables.Contains(original)) return original;

            for (var i = 0; ; i++)
            {
                Variable variable = String.Format("{0}_{1}", original, i);
                if (!Variables.Contains(variable)) return variable;
            }
        }