Пример #1
0
 private CFGProduction(CFGProduction original, Cloner cloner)
     : base(original, cloner)
 {
     parts = original.parts.ToList();
 }
Пример #2
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;
    }
Пример #3
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);
        }
Пример #4
0
 private CFGProduction(CFGProduction original, Cloner cloner)
   : base(original, cloner) {
   parts = original.parts.ToList();
 }