Example #1
0
        internal RecursionPoint replace(LinkedListNode <Symbol> linkedListNode)
        {
            var            rule          = Data.getRule(linkedListNode);
            RecursionPoint todo          = new RecursionPoint();
            var            pointsToCheck = new HashSet <LinkedListNode <Symbol> >();

            if (rule == null)
            {
                rule = new Rule(Data.NextId, this);
#if DEBUG
                Rules.Add(rule);
#endif

                Debug.Print("New rule created:" + rule.ID);

                todo.UnionWith(Data.newRule(linkedListNode, rule));
            }
            else
            {
                Debug.Print("using old rule:" + rule.ID);
            }

            todo.UnionWith(Data.setRule(linkedListNode, rule));



            Data.removeDigrams(todo.RemovedDigrams);
            todo.RemovedDigrams.Clear();


            return(todo);
        }
Example #2
0
        public void Evaluate(List <String> items)
        {
            foreach (var item in items)
            {
                Debug.Print("read:'" + item + "'");
                _StartRule.Symbols.AddLast(new TerminalInstance(item));
                RecursionPoint todo = _StartRule.CheckTail();
                while (todo != null && !todo.Empty)
                {
                    while (todo.DigramsToCheck.Count > 1)
                    {
                        RecursionPoint newtodo = new RecursionPoint();

                        foreach (var tocheck in todo.DigramsToCheck)
                        {
                            if (tocheck == null)
                            {
                                continue;
                            }

                            OverlapInfo result = findOccurence(tocheck);
                            if (result == OverlapInfo.Found)
                            {
                                newtodo.UnionWith(replace(tocheck));
                            }
                            else if (result == OverlapInfo.NotFound)
                            {
                                noteDigram(tocheck);
                            }
                            //otherwise overlaps
                        }
                        todo = newtodo;
                    }

                    Data.removeDigrams(todo.RemovedDigrams);
                    todo.RemovedDigrams.Clear();

                    todo.UnionWith(_StartRule.CheckTail());
                }
            }
        }
Example #3
0
        internal RecursionPoint newRule(LinkedListNode <Symbol> linkedListNode, Rule rule)
        {
            RecursionPoint todo = new RecursionPoint();

            var olddigram  = new Digram(linkedListNode);
            var foundEntry = FirstOccurances[olddigram];

            var currentEntry = new DigramEntry(linkedListNode);

            var prev = foundEntry.StartPoint.Previous;

            if (prev != null)
            {
                todo.RemovedDigrams.Add(new Digram(prev));
            }

            var next = foundEntry.StartPoint.Next;

            if (next != null && next.Next != null)
            {
                todo.RemovedDigrams.Add(new Digram(next));
            }

            foundEntry.Rule           = rule;
            RuleAnchors[rule.Symbols] = foundEntry;

            if (foundEntry.StartPoint.List.First.Next == foundEntry.StartPoint.List.Last &&
                this.RuleAnchors.ContainsKey(foundEntry.StartPoint.List))
            {
                Debug.Print("refreshin a substitute rule:" + foundEntry.ToString() + " -> " + this.RuleAnchors[foundEntry.StartPoint.List].ToString());
                this.FirstOccurances[new Digram(foundEntry.StartPoint)] = this.RuleAnchors[foundEntry.StartPoint.List];
            }
            else
            {
                todo.DigramsToCheck.Add(foundEntry.StartPoint.Previous);
                todo.DigramsToCheck.Add(foundEntry.StartPoint);
            }

            return(todo);
        }
        public void Evaluate(List<String> items)
        {
            foreach (var item in items)
            {
                Debug.Print("read:'" + item + "'");
                _StartRule.Symbols.AddLast(new TerminalInstance(item));
                RecursionPoint todo = _StartRule.CheckTail();
                while (todo != null && !todo.Empty )
                {
                    while(todo.DigramsToCheck.Count > 1)
                    {

                        RecursionPoint newtodo = new RecursionPoint();

                        foreach (var tocheck in todo.DigramsToCheck)
                        {
                            if (tocheck == null)
                                continue;

                            OverlapInfo result = findOccurence(tocheck) ;
                            if (result == OverlapInfo.Found)
                            {
                                newtodo.UnionWith(replace(tocheck));
                            }
                            else if( result == OverlapInfo.NotFound)
                            {
                                noteDigram(tocheck);
                            }
                            //otherwise overlaps
                        }
                        todo = newtodo;
                    }

                    Data.removeDigrams(todo.RemovedDigrams);
                    todo.RemovedDigrams.Clear();

                    todo.UnionWith(_StartRule.CheckTail());
                }
            }
        }
Example #5
0
        internal RecursionPoint setRule(LinkedListNode <Symbol> linkedListNode, Rule rule)
        {
            RecursionPoint todo = new RecursionPoint();

            var adigramEntry = new DigramEntry(linkedListNode);
            var olddigram    = new Digram(linkedListNode.List.First);

            {
                var prev = adigramEntry.StartPoint.Previous;
                if (prev != null)
                {
                    todo.RemovedDigrams.Add(new Digram(prev));
                }

                var next = adigramEntry.StartPoint.Next;
                if (next != null && next.Next != null)
                {
                    todo.RemovedDigrams.Add(new Digram(next));
                }
            }

            adigramEntry.Rule = rule;



            var symbolpntr = rule.Symbols.First;

            bool substituted = false;

            while (symbolpntr != null)
            {
                var symbol = symbolpntr.Value;
                if (!symbol.IsTerminal)
                {
                    Rule pointedRule = (symbol as RuleInstance).Content;
                    pointedRule.deuse();

                    if (pointedRule.Count <= 1)
                    {
                        Debug.Assert(pointedRule.ID != rule.ID);
                        Debug.Print("substituting a rule:" + pointedRule.ID);
                        Debug.Print("into a rule:" + rule.ID);

                        substituted = true;

                        todo.DigramsToCheck.Add(symbolpntr.Previous);


                        var digram = new Digram(pointedRule.Symbols.First);
                        if (FirstOccurances.ContainsKey(digram))
                        {
                            FirstOccurances.Remove(digram);
                        }

                        if (RuleAnchors.ContainsKey(pointedRule.Symbols))
                        {
                            RuleAnchors.Remove(pointedRule.Symbols);
                        }

                        var movedsymbol = pointedRule.Symbols.First;
                        while (movedsymbol != null)
                        {
                            var next = movedsymbol.Next;
                            symbolpntr.List.AddBefore(symbolpntr, movedsymbol.Value);
                            todo.DigramsToCheck.Add(symbolpntr.Previous);
                            movedsymbol = next;
                        }
                        symbolpntr = symbolpntr.Previous;
                        rule.Symbols.Remove(symbolpntr.Next);
                    }
                }
                symbolpntr = symbolpntr.Next;
            }

            if (substituted)
            {
                Debug.Print("Removing old substitute rule " + olddigram.ToString());
                this._FirstOccurances.Remove(olddigram);
                todo.DigramsToCheck.Add(adigramEntry.StartPoint.Previous);
                todo.DigramsToCheck.Add(adigramEntry.StartPoint);
            }
            else
            {
                if (adigramEntry.StartPoint.List.First.Next == adigramEntry.StartPoint.List.Last &&
                    this.RuleAnchors.ContainsKey(adigramEntry.StartPoint.List))
                {
                    Debug.Print("refreshin a substitute rule:" + adigramEntry.ToString() + " -> " + this.RuleAnchors[adigramEntry.StartPoint.List].ToString());
                    this.FirstOccurances[new Digram(adigramEntry.StartPoint)] = this.RuleAnchors[adigramEntry.StartPoint.List];
                }
                else
                {
                    todo.DigramsToCheck.Add(adigramEntry.StartPoint.Previous);
                    todo.DigramsToCheck.Add(adigramEntry.StartPoint);
                }
            }

            return(todo);
        }
Example #6
0
 internal void UnionWith(RecursionPoint recursionPoint)
 {
     RemovedDigrams.UnionWith(recursionPoint.RemovedDigrams);
     DigramsToCheck.UnionWith(recursionPoint.DigramsToCheck);
 }
 internal void UnionWith(RecursionPoint recursionPoint)
 {
     RemovedDigrams.UnionWith(recursionPoint.RemovedDigrams);
     DigramsToCheck.UnionWith(recursionPoint.DigramsToCheck);
 }
        internal RecursionPoint replace(LinkedListNode<Symbol> linkedListNode)
        {
            var rule = Data.getRule(linkedListNode);
            RecursionPoint todo = new RecursionPoint();
            var pointsToCheck = new HashSet<LinkedListNode<Symbol>>();
            if (rule == null)
            {
                rule = new Rule(Data.NextId, this);
            #if DEBUG
                Rules.Add(rule);
            #endif

                Debug.Print("New rule created:" + rule.ID);

                todo.UnionWith(Data.newRule(linkedListNode, rule));
            }
            else
            {
                Debug.Print("using old rule:" + rule.ID );
            }

            todo.UnionWith(Data.setRule(linkedListNode, rule));

            Data.removeDigrams(todo.RemovedDigrams);
            todo.RemovedDigrams.Clear();

            return todo;
        }