Example #1
0
    private static SubruleParses RemoveWorstPaths(ParsedSequence seq, int end, SubruleParses parses, out int comulativeMin)
    {
      var comulativeCost = new SubruleParses();
      bool updated = true;
      while (updated)
      {
        updated = false;
        foreach (var parse in parses)
        {
          var subrule = parse.Key;
          int oldCount;
          if (!comulativeCost.TryGetValue(subrule, out oldCount))
            updated = true;
          int min;
          if (seq.StartPos == subrule.Begin && seq.ParsingSequence.StartStates.Contains(subrule.State))
            min = 0;
          else
          {
            min = Fail;
            int prevCount;
            foreach (var prevSubrule in seq.GetPrevSubrules(subrule, parses.Keys))
              if (comulativeCost.TryGetValue(prevSubrule, out prevCount))
                min = Math.Min(min, prevCount);
          }
          var newCount = AddOrFail(min, parses[subrule]);
          comulativeCost[subrule] = newCount;
          updated = updated || oldCount != newCount;
        }
      }

      var toProcess = new SCG.Queue<ParsedSubrule>(seq.GetLastSubrules(parses.Keys, end));
      var comulativeMin2 = toProcess.Min(s => comulativeCost[s]);
      comulativeMin = comulativeMin2;
      toProcess = new SCG.Queue<ParsedSubrule>(toProcess.Where(s => comulativeCost[s] == comulativeMin2));
      var good = new SubruleParses();
      while (toProcess.Count > 0)
      {
        var subrule = toProcess.Dequeue();
        if (good.ContainsKey(subrule))
          continue;
        good.Add(subrule, parses[subrule]);
        var prev = seq.GetPrevSubrules(subrule, parses.Keys).ToList();
        if (prev.Count > 0)
        {
          int min;
          if (seq.StartPos == subrule.Begin && seq.ParsingSequence.StartStates.Contains(subrule.State))
            min = 0;
          else
            min = prev.Min(s => comulativeCost[s]);
          foreach (var prevSubrule in prev)
            if (comulativeCost[prevSubrule] == min)
              toProcess.Enqueue(prevSubrule);
        }
      }
      return good;
    }
Example #2
0
        public async STT.Task TaskTraverseTest(int n)
        {
            var queue    = new SCG.Queue <int>(n);
            var sequence = Enumerable.Range(0, n);
            await sequence.Traverse(async n => {
                await STT.Task.Yield();
                queue.Enqueue(n);
            }).ConfigureAwait(false);

            Assert.True(sequence.SequenceEqual(queue));
        }
Example #3
0
    private void RecoverAllWays(RecoveryParser rp)
    {
      // ReSharper disable once RedundantAssignment
      int maxPos = rp.MaxPos;
      var failPositions = new HashSet<int>();
      var deleted = new List<Tuple<int, ParsedSequence>>();


      do
      {

        var tmpDeleted = FindMaxFailPos(rp);
        if (rp.MaxPos != rp.ParseResult.Text.Length)
          UpdateParseErrorCount();

        if (!CheckUnclosedToken(rp))
          deleted.AddRange(tmpDeleted);
        else
        { }


        maxPos = rp.MaxPos;
        failPositions.Add(maxPos);

        var records = new SCG.Queue<ParseRecord>(rp.Records[maxPos]);
        var prevRecords = new SCG.HashSet<ParseRecord>(rp.Records[maxPos]);

        do
        {
          if (_parseResult.TerminateParsing)
            throw new OperationCanceledException();

          while (records.Count > 0)
          {
            var record = records.Dequeue();

            if (record.IsComplete)
            {
              rp.StartParseSubrule(maxPos, record);
              continue;
            }
            if (record.Sequence.IsToken)
              continue;

            foreach (var state in record.ParsingState.Next)
            {
              var newRecord = new ParseRecord(record.Sequence, state, maxPos);
              if (!rp.Records[maxPos].Contains(newRecord))
              {
                records.Enqueue(newRecord);
                prevRecords.Add(newRecord);
              }
            }

            rp.SubruleParsed(maxPos, maxPos, record);
            rp.PredictionOrScanning(maxPos, record, false);
          }

          rp.Parse();

          foreach (var record in rp.Records[maxPos])
            if (!prevRecords.Contains(record))
              records.Enqueue(record);
          prevRecords.UnionWith(rp.Records[maxPos]);
        }
        while (records.Count > 0);
      }
      while (rp.MaxPos > maxPos);

      foreach (var del in deleted)
        DeleteTokens(rp, del.Item1, del.Item2, NumberOfTokensForSpeculativeDeleting);
      rp.Parse();
    }
Example #4
0
    private static SubrulesTokenChanges RemoveWorstPaths(ParsedSequence seq, int end, SubrulesTokenChanges parses, out TokenChanges comulativeMin)
    {
      var comulativeCost = new SubrulesTokenChanges();
      bool updated = true;
      while (updated)
      {
        updated = false;
        foreach (var parse in parses)
        {
          var subrule = parse.Key;
          TokenChanges oldCount;
          if (!comulativeCost.TryGetValue(subrule, out oldCount))
            updated = true;
          TokenChanges min;
          if (seq.StartPos == subrule.Begin && seq.ParsingSequence.StartStates.Contains(subrule.State))
            min = new TokenChanges();
          else
          {
            min = TokenChanges.Fail;
            TokenChanges prevCount;
            foreach (var prevSubrule in seq.GetPrevSubrules(subrule, parses.Keys))
              if (comulativeCost.TryGetValue(prevSubrule, out prevCount))
                min = TokenChanges.Min(min, prevCount);
          }
          var newCount = AddOrFail(min, parses[subrule]);
          comulativeCost[subrule] = newCount;
          updated = updated || oldCount != newCount;
        }
      }

      var toProcess = new SCG.Queue<ParsedSubrule>(seq.GetLastSubrules(parses.Keys, end));
      var comulativeMin2 = toProcess.Min(s => comulativeCost[s]);
      comulativeMin = comulativeMin2;
      toProcess = new SCG.Queue<ParsedSubrule>(toProcess.Where(s => comulativeCost[s] == comulativeMin2));
      var good = new SubrulesTokenChanges();
      while (toProcess.Count > 0)
      {
        var subrule = toProcess.Dequeue();
        if (good.ContainsKey(subrule))
          continue;
        good.Add(subrule, parses[subrule]);
        var prev = seq.GetPrevSubrules(subrule, parses.Keys).ToList();
        if (prev.Count > 0)
        {
          TokenChanges min;
          if (seq.StartPos == subrule.Begin && seq.ParsingSequence.StartStates.Contains(subrule.State))
            min = new TokenChanges();
          else
            min = prev.Min(s => comulativeCost[s]);
          foreach (var prevSubrule in prev)
            if (comulativeCost[prevSubrule] == min)
              toProcess.Enqueue(prevSubrule);
        }
      }
      return good;
    }
Example #5
0
        int TraverseMaxDepth(CodeBlock entryBlock)
        {
            int max = 0;
            SCG.Queue<CodeBlock> worklist = new SCG.Queue<CodeBlock>();
            entryBlock.Visited = true;
            entryBlock.LastVisitEntryDepth = 0;
            worklist.Enqueue(entryBlock);
            while (worklist.Count > 0)
            {
                int count = worklist.Count;
                CodeBlock unit = worklist.Dequeue();

                int maxDepth = unit.LastVisitEntryDepth + unit.MaxDepth;
                int exitDepth = unit.LastVisitEntryDepth + unit.DeltaDistance;

                if (maxDepth > max) max = maxDepth;

                foreach (CodeBlock succ in unit.NextBlocks)
                {
                    if (succ.Visited)
                    {
                        if (succ.LastVisitEntryDepth != exitDepth)
                            throw new InvalidStackDepth("inconsistent stack depth at offset " + succ.StartIndex.ToString());
                    }
                    else
                    {
                        succ.Visited = true;
                        succ.LastVisitEntryDepth = exitDepth;
                        worklist.Enqueue(succ);
                    }
                }
            }
            return max;
        }
Example #6
0
        private void RecoverAllWays(RecoveryParser rp)
        {
            // ReSharper disable once RedundantAssignment
            int maxPos        = rp.MaxPos;
            var failPositions = new HashSet <int>();
            var deleted       = new List <Tuple <int, ParsedSequence> >();


            do
            {
                var tmpDeleted = FindMaxFailPos(rp);
                if (rp.MaxPos != rp.ParseResult.Text.Length)
                {
                    UpdateParseErrorCount();
                }

                if (!CheckUnclosedToken(rp))
                {
                    deleted.AddRange(tmpDeleted);
                }
                else
                {
                }


                maxPos = rp.MaxPos;
                failPositions.Add(maxPos);

                var records     = new SCG.Queue <ParseRecord>(rp.Records[maxPos]);
                var prevRecords = new SCG.HashSet <ParseRecord>(rp.Records[maxPos]);

                do
                {
                    if (_parseResult.TerminateParsing)
                    {
                        throw new OperationCanceledException();
                    }

                    while (records.Count > 0)
                    {
                        var record = records.Dequeue();

                        if (record.IsComplete)
                        {
                            rp.StartParseSubrule(maxPos, record);
                            continue;
                        }
                        if (record.Sequence.IsToken)
                        {
                            continue;
                        }

                        foreach (var state in record.ParsingState.Next)
                        {
                            var newRecord = new ParseRecord(record.Sequence, state, maxPos);
                            if (!rp.Records[maxPos].Contains(newRecord))
                            {
                                records.Enqueue(newRecord);
                                prevRecords.Add(newRecord);
                            }
                        }

                        rp.SubruleParsed(maxPos, maxPos, record);
                        rp.PredictionOrScanning(maxPos, record, false);
                    }

                    rp.Parse();

                    foreach (var record in rp.Records[maxPos])
                    {
                        if (!prevRecords.Contains(record))
                        {
                            records.Enqueue(record);
                        }
                    }
                    prevRecords.UnionWith(rp.Records[maxPos]);
                }while (records.Count > 0);
            }while (rp.MaxPos > maxPos);

            foreach (var del in deleted)
            {
                DeleteTokens(rp, del.Item1, del.Item2, NumberOfTokensForSpeculativeDeleting);
            }
            rp.Parse();
        }