Esempio n. 1
0
 public InverseKeyType(InverseKeyType anotherInverseKey)
 {
     Production =
         anotherInverseKey.Production.Select(nonterminal => new NonTerminalObject(nonterminal)).ToArray();
     HeadPosition = anotherInverseKey.HeadPosition;
     IsStartSymbol = anotherInverseKey.IsStartSymbol;
 }
Esempio n. 2
0
 public InverseKeyType(InverseKeyType anotherInverseKey)
 {
     Production =
         anotherInverseKey.Production.Select(nonterminal => new NonTerminalObject(nonterminal)).ToArray();
     HeadPosition  = anotherInverseKey.HeadPosition;
     IsStartSymbol = anotherInverseKey.IsStartSymbol;
 }
Esempio n. 3
0
        public void DeleteRule(Rule rule)
        {
            //delete from rules dictionary
            var lhs        = rule.Name.NonTerminal;
            var rulesOfLHS = Rules[lhs];

            rulesOfLHS.Remove(rule);
            var numberOfRemainingRules = rulesOfLHS.Count;

            if (numberOfRemainingRules == 0)
            {
                Rules.Remove(lhs);
            }

            //delete from inverse rules dictionary
            if (!rule.IsEpsilonRule())
            {
                var inverseKey = new InverseKeyType(rule.Production, rule.HeadPosition,
                                                    rule.Name.NonTerminal == StartSymbol);
                inverseRules.Remove(inverseKey);
            }

            RemoveNonTerminalCounts(rule);
            ruleNumberDictionary.Remove(rule.Number);
            numberOfRules--;
            ReturnUnusedRuleNumber(rule.Number);
        }
Esempio n. 4
0
        public string AddRule(Rule rule)
        {
            EnforceHeadRelations(rule);

            // if production already exists under some other rule name, do not re-add the rule
            //that is, if exists A->BC and we encounter D->BC, then A=D.
            var inverseKey    = new InverseKeyType(rule.Production, rule.HeadPosition, rule.Name.NonTerminal == StartSymbol);
            var isEpislonRule = rule.IsEpsilonRule();

            if (isEpislonRule || !inverseRules.ContainsKey(inverseKey))
            {
                // add the rule:
                //1) to inverse rules dictionary:
                numberOfRules++;
                rule.Number = GetNextAvailableRuleNumber(); //note: depends on value of self.numberOfRules

                if (!isEpislonRule)
                {
                    inverseRules[inverseKey] = rule.Name.NonTerminal;
                }
                //if the rule is epsilon rule, it does not have inverse key.
                else
                {
                    nullableProductions[rule.Name] = 1.0f; //TODO - temporary probabilioty of 1.0.
                }
                AddNonTerminalCounts(rule);

                //3) to rules dictionary
                if (!Rules.ContainsKey(rule.Name.NonTerminal))
                {
                    Rules[rule.Name.NonTerminal] = new List <Rule>();
                    rule.Occurrences             = 1;
                }
                else
                {
                    if (rule.Occurrences == 0) //if rule does not come with positive occurrences (= 0):
                    {
                        //make the occurrences average of the current occurrences.
                        var l     = Rules[rule.Name.NonTerminal];
                        var count = l.Count;
                        rule.Occurrences = l.Sum(x => x.Occurrences) / count;
                    }
                }

                Rules[rule.Name.NonTerminal].Add(rule);
                ruleNumberDictionary[rule.Number] = rule;
            }
            //for the sake of convenience, return the rule name that was added
            //it is useful when replacing no longer used symbols with the
            //new rule names.
            if (!isEpislonRule)
            {
                return(inverseRules[inverseKey]);
            }
            return(rule.Name.NonTerminal);
        }
Esempio n. 5
0
        public string AddRule(Rule rule)
        {
            EnforceHeadRelations(rule);

            // if production already exists under some other rule name, do not re-add the rule
            //that is, if exists A->BC and we encounter D->BC, then A=D.
            var inverseKey = new InverseKeyType(rule.Production, rule.HeadPosition, rule.Name.NonTerminal == StartSymbol);
            var isEpislonRule = rule.IsEpsilonRule();

            if (isEpislonRule || !inverseRules.ContainsKey(inverseKey))
            {
                // add the rule:
                //1) to inverse rules dictionary:
                numberOfRules++;
                rule.Number = GetNextAvailableRuleNumber(); //note: depends on value of self.numberOfRules

                if (!isEpislonRule)
                    inverseRules[inverseKey] = rule.Name.NonTerminal;
                //if the rule is epsilon rule, it does not have inverse key.
                else
                    nullableProductions[rule.Name] = 1.0f; //TODO - temporary probabilioty of 1.0.

                AddNonTerminalCounts(rule);

                //3) to rules dictionary
                if (!Rules.ContainsKey(rule.Name.NonTerminal))
                {
                    Rules[rule.Name.NonTerminal] = new List<Rule>();
                    rule.Occurrences = 1;
                }
                else
                {
                    if (rule.Occurrences == 0) //if rule does not come with positive occurrences (= 0):
                    {
                        //make the occurrences average of the current occurrences.
                        var l = Rules[rule.Name.NonTerminal];
                        var count = l.Count;
                        rule.Occurrences = l.Sum(x => x.Occurrences)/count;
                    }
                }

                Rules[rule.Name.NonTerminal].Add(rule);
                ruleNumberDictionary[rule.Number] = rule;
            }
            //for the sake of convenience, return the rule name that was added
            //it is useful when replacing no longer used symbols with the
            //new rule names.
            if (!isEpislonRule)
                return inverseRules[inverseKey];
            return rule.Name.NonTerminal;
        }
Esempio n. 6
0
        public void DeleteRule(Rule rule)
        {
            //delete from rules dictionary
            var lhs = rule.Name.NonTerminal;
            var rulesOfLHS = Rules[lhs];

            rulesOfLHS.Remove(rule);
            var numberOfRemainingRules = rulesOfLHS.Count;

            if (numberOfRemainingRules == 0)
                Rules.Remove(lhs);

            //delete from inverse rules dictionary
            if (!rule.IsEpsilonRule())
            {
                var inverseKey = new InverseKeyType(rule.Production, rule.HeadPosition,
                    rule.Name.NonTerminal == StartSymbol);
                inverseRules.Remove(inverseKey);
            }

            RemoveNonTerminalCounts(rule);
            ruleNumberDictionary.Remove(rule.Number);
            numberOfRules--;
            ReturnUnusedRuleNumber(rule.Number);
        }