private long CalcPossibilitiesOfDepth(CFGExpressionGrammar grammar, int depth) {
      long pos = 0;

      var terminalSymbols = grammar.AllowedSymbols.Where(x => x.MaximumArity <= 0 && !(x is GroupSymbol));
      var nonterminalSymbols = grammar.AllowedSymbols.Where(x => x.MinimumArity > 0 && !(x is ProgramRootSymbol) && !(x is StartSymbol) && !(x is Defun) && (x is CFGProduction || !(x is CFGSymbol)));

      var startSymbols = grammar.GetAllowedChildSymbols(grammar.StartSymbol, 0);

      foreach (var symbol in nonterminalSymbols) {
        depthPerSymbolTrees.Add(symbol, new Dictionary<int, int>());
      }

      for (int i = 2; i < depth + 1; i++) {
        foreach (var ntSymbol in nonterminalSymbols) {
          int symPos = 1;

          for (int j = 0; j < ntSymbol.MaximumArity; j++) {
            int symbolArityPos = 0;

            foreach (var childSymbol in grammar.GetAllowedChildSymbols(ntSymbol, j)) {
              if (terminalSymbols.Contains(childSymbol)) {
                symbolArityPos++;
              } else {
                if (depthPerSymbolTrees[childSymbol].ContainsKey(i - 1)) {
                  symbolArityPos += depthPerSymbolTrees[childSymbol][i - 1];
                }
              }
            }
            symPos *= symbolArityPos;
          }

          depthPerSymbolTrees[ntSymbol].Add(i, symPos);
        }
      }

      foreach (var sy in startSymbols) {
        if (depthPerSymbolTrees.ContainsKey(sy)) {
          pos += depthPerSymbolTrees[sy].ContainsKey(depth) ? depthPerSymbolTrees[sy][depth] : 0;
        } else {
          pos += 1;
        }
      }

      return pos;
    }
示例#2
0
        private void CreateTreeFromGrammar()
        {
            if (String.IsNullOrWhiteSpace(GrammarBNF.Value))
            {
                GrammarParameter.Value  = CFGExpressionGrammar.Empty;
                GrammarParameter.Hidden = true;
                return;
            }

            CFGParser            parser  = new CFGParser();
            CFGExpressionGrammar grammar = parser.readGrammarBNF(GrammarBNF.Value);

            Grammar = grammar;
            GrammarParameter.Hidden = false;

            var operators = Parameters.OfType <IValueParameter>().Select(p => p.Value).OfType <IOperator>().Union(Operators).ToList();

            foreach (var op in operators.OfType <ISymbolicExpressionTreeGrammarBasedOperator>())
            {
                op.SymbolicExpressionTreeGrammarParameter.Value = GrammarParameter.Value;
            }
        }
 protected CFGExpressionGrammar(CFGExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
 protected CFGExpressionGrammar(CFGExpressionGrammar original, Cloner cloner) : base(original, cloner)
 {
 }
示例#5
0
        private CFGExpressionGrammar readGrammarBNFPrivate(String bnf)
        {
            CFGExpressionGrammar treeGrammar = new CFGExpressionGrammar();

            Dictionary <CFGProduction, List <string> > productions = new Dictionary <CFGProduction, List <string> >();
            GroupSymbol root = null;
            var         rule = ruleRegex.Match(bnf);

            while (rule.Success)
            {
                GroupSymbol curRuleSymbolGroup = new GroupSymbol();
                curRuleSymbolGroup.Name = "Rule: " + rule.Groups["rulename"].Value;

                treeGrammar.AddSymbol(curRuleSymbolGroup);
                if (root == null)
                {
                    root = curRuleSymbolGroup;
                }

                var production = productionRegex.Match(rule.Groups["production"].Value);

                while (production.Success)
                {
                    string productionName = production.Groups["production"].Value.Trim();
                    if (!String.IsNullOrWhiteSpace(productionName))
                    {
                        // production rule already exists
                        // increase initial frequency
                        if (curRuleSymbolGroup.SymbolsCollection.Any(x => x.Name.Equals(productionName)))
                        {
                            var symbol = (CFGSymbol)curRuleSymbolGroup.SymbolsCollection.First(x => x.Name.Equals(productionName));
                            symbol.InitialFrequency++;
                        }
                        else
                        {
                            List <string> parts = new List <string>();
                            parts.Add(String.Empty);

                            var subRule = ruleInProdRegex.Match(productionName);

                            List <string> rulesInProduction = new List <string>();
                            while (subRule.Success)
                            {
                                if (!String.IsNullOrEmpty(subRule.Groups["subrule"].Value))
                                {
                                    rulesInProduction.Add(subRule.Groups["subrule"].Value);
                                    subRule = subRule.NextMatch();
                                    parts.Add(String.Empty);
                                    continue;
                                }

                                var part = String.Join(String.Empty, Enumerable.Range(1, subRule.Groups.Count - 1).Select(x => subRule.Groups[x].Value));
                                // unescape characters so that tab and newline can be defined in the grammar
                                if (part.Contains('\\'))
                                {
                                    part = Regex.Unescape(part);
                                }
                                parts[parts.Count - 1] = parts[parts.Count - 1] + part;

                                subRule = subRule.NextMatch();
                            }

                            CFGProduction productionSymbol = new CFGProduction(productionName, parts.Count == 0
                                                                                 ? new List <string>()
                            {
                                productionName
                            }
                                                                                 : parts);
                            productions.Add(productionSymbol, rulesInProduction);
                            curRuleSymbolGroup.SymbolsCollection.Add(productionSymbol);
                        }
                    }
                    production = production.NextMatch();
                }
                rule = rule.NextMatch();
            }

            // assign subrules for start symbol
            var ruleGroupSymbol = treeGrammar.Symbols.Where(x => x is GroupSymbol && x.Name == root.Name).First() as GroupSymbol;

            foreach (var subProduction in ruleGroupSymbol.SymbolsCollection)
            {
                treeGrammar.AddAllowedChildSymbol(treeGrammar.StartSymbol, subProduction, 0);
            }

            // assign subrules
            foreach (var allowedSymbol in treeGrammar.AllowedSymbols.Where(x => x is CFGProduction))
            {
                var cfgProduction     = allowedSymbol as CFGProduction;
                var rulesInProduction = productions[cfgProduction];
                if (rulesInProduction.Count == 0)
                {
                    continue;
                }

                treeGrammar.SetSubtreeCount(cfgProduction, rulesInProduction.Count, rulesInProduction.Count);
                for (int i = 0; i < rulesInProduction.Count; i++)
                {
                    ruleGroupSymbol = treeGrammar.Symbols.Where(x => x is GroupSymbol && x.Name.Substring("Rule: ".Length, x.Name.Length - "Rule: ".Length) == rulesInProduction[i]).First() as GroupSymbol;

                    foreach (var subProduction in ruleGroupSymbol.SymbolsCollection)
                    {
                        treeGrammar.AddAllowedChildSymbol(cfgProduction, subProduction, i);
                    }
                }
            }

            return(treeGrammar);
        }
示例#6
0
    private CFGExpressionGrammar readGrammarBNFPrivate(String bnf) {
      CFGExpressionGrammar treeGrammar = new CFGExpressionGrammar();

      Dictionary<CFGProduction, List<string>> productions = new Dictionary<CFGProduction, List<string>>();
      GroupSymbol root = null;
      var rule = ruleRegex.Match(bnf);
      while (rule.Success) {
        GroupSymbol curRuleSymbolGroup = new GroupSymbol();
        curRuleSymbolGroup.Name = "Rule: " + rule.Groups["rulename"].Value;

        treeGrammar.AddSymbol(curRuleSymbolGroup);
        if (root == null) {
          root = curRuleSymbolGroup;
        }

        var production = productionRegex.Match(rule.Groups["production"].Value);

        while (production.Success) {
          string productionName = production.Groups["production"].Value.Trim();
          if (!String.IsNullOrWhiteSpace(productionName)) {
            // production rule already exists
            // increase initial frequency
            if (curRuleSymbolGroup.SymbolsCollection.Any(x => x.Name.Equals(productionName))) {
              var symbol = (CFGSymbol)curRuleSymbolGroup.SymbolsCollection.First(x => x.Name.Equals(productionName));
              symbol.InitialFrequency++;
            } else {

              List<string> parts = new List<string>();
              parts.Add(String.Empty);

              var subRule = ruleInProdRegex.Match(productionName);

              List<string> rulesInProduction = new List<string>();
              while (subRule.Success) {
                if (!String.IsNullOrEmpty(subRule.Groups["subrule"].Value)) {
                  rulesInProduction.Add(subRule.Groups["subrule"].Value);
                  subRule = subRule.NextMatch();
                  parts.Add(String.Empty);
                  continue;
                }

                var part = String.Join(String.Empty, Enumerable.Range(1, subRule.Groups.Count - 1).Select(x => subRule.Groups[x].Value));
                // unescape characters so that tab and newline can be defined in the grammar
                if (part.Contains('\\')) {
                  part = part.Replace("\\n", Environment.NewLine);
                  part = Regex.Unescape(part);
                }
                parts[parts.Count - 1] = parts[parts.Count - 1] + part;

                subRule = subRule.NextMatch();
              }

              CFGProduction productionSymbol = new CFGProduction(productionName, parts.Count == 0
                                                                                 ? new List<string>() { productionName }
                                                                                 : parts);
              productions.Add(productionSymbol, rulesInProduction);
              curRuleSymbolGroup.SymbolsCollection.Add(productionSymbol);
            }
          }
          production = production.NextMatch();
        }
        rule = rule.NextMatch();
      }

      // assign subrules for start symbol
      var ruleGroupSymbol = treeGrammar.Symbols.Where(x => x is GroupSymbol && x.Name == root.Name).First() as GroupSymbol;
      foreach (var subProduction in ruleGroupSymbol.SymbolsCollection) {
        treeGrammar.AddAllowedChildSymbol(treeGrammar.StartSymbol, subProduction, 0);
      }

      // assign subrules
      foreach (var allowedSymbol in treeGrammar.AllowedSymbols.Where(x => x is CFGProduction)) {
        var cfgProduction = allowedSymbol as CFGProduction;
        var rulesInProduction = productions[cfgProduction];
        if (rulesInProduction.Count == 0) continue;

        treeGrammar.SetSubtreeCount(cfgProduction, rulesInProduction.Count, rulesInProduction.Count);
        for (int i = 0; i < rulesInProduction.Count; i++) {
          ruleGroupSymbol = treeGrammar.Symbols.Where(x => x is GroupSymbol && x.Name.Substring("Rule: ".Length, x.Name.Length - "Rule: ".Length) == rulesInProduction[i]).First() as GroupSymbol;

          foreach (var subProduction in ruleGroupSymbol.SymbolsCollection) {
            treeGrammar.AddAllowedChildSymbol(cfgProduction, subProduction, i);
          }
        }
      }

      return treeGrammar;
    }