Beispiel #1
0
        //TODO: test for this
        public StackSymbolSequence <S> GetLongestPossibleCommonTail(StackSymbolSequence <S> other)
        {
            var reverseZipped = StackSequence.Reverse().Zip(other.StackSequence.Reverse(), (first, second) => new { first, second });
            var tail          = reverseZipped.TakeWhile(el => el.first.Equals(el.second)).Select(el => el.first).Reverse().ToList();

            return(new StackSymbolSequence <S>(tail));
        }
Beispiel #2
0
        private static void AddSingleTransitionToPDA(PDA <A, StackSymbolSequence <S> > targetPDA, IEnumerable <Transition <A, S> > originTransitions, Transition <A, S> outerOrigTransition)
        {
            var stackSymbolQueue = new Queue <StackSymbolSequence <S> >();

            var readStackSymbol = new StackSymbolSequence <S>(outerOrigTransition.StackSymbolIn);

            AddToCollectionsIfNotAlreadyExisting(stackSymbolQueue, targetPDA, readStackSymbol);

            while (stackSymbolQueue.Count > 0)
            {
                AddTransitionsForNewStackSymbol(targetPDA, originTransitions, stackSymbolQueue);
            }
        }
Beispiel #3
0
        private static void AddTransitionsForNewStackSymbol(PDA <A, StackSymbolSequence <S> > targetPDA, IEnumerable <Transition <A, S> > originTransitions, Queue <StackSymbolSequence <S> > stackSymbolQueue)
        {
            var next = stackSymbolQueue.Dequeue();

            foreach (var innerOriginTransition in originTransitions.Where(t => next.StackSequence.First().Equals(t.StackSymbolIn)))
            {
                var upperWrittenSymbol     = new StackSymbolSequence <S>(innerOriginTransition.StackSymbolsWritten);
                var lowerWrittenSymbl      = new StackSymbolSequence <S>(next.StackSequence.Skip(1).ToArray());
                var newStackSymbolsWritten = GetNotEmptyStackSymbols(ArrayOf(upperWrittenSymbol, lowerWrittenSymbl));

                foreach (var s in newStackSymbolsWritten)
                {
                    AddToCollectionsIfNotAlreadyExisting(stackSymbolQueue, targetPDA, s);
                }

                targetPDA.AddTransition().From(innerOriginTransition.Origin.Id).To(innerOriginTransition.Target.Id).Read(innerOriginTransition.SymbolIn).Pop(next).Push(newStackSymbolsWritten);
            }
        }
Beispiel #4
0
 private static void AddToCollectionsIfNotAlreadyExisting(Queue <StackSymbolSequence <S> > stackSymbolQueue, PDA <A, StackSymbolSequence <S> > pda, StackSymbolSequence <S> stackSymbol)
 {
     if (!pda.AllStackSymbols.Any(s => s.Equals(stackSymbol)))
     {
         pda.AddStackSymbol(stackSymbol);
         stackSymbolQueue.Enqueue(stackSymbol);
     }
 }
Beispiel #5
0
 public StackSymbolSequence <S> GetHeadWithRespectToTail(StackSymbolSequence <S> tail)
 {
     Assertion.Assert(EndsWith(tail), "the given tail has to be a tail of this stack sequence");
     return(new StackSymbolSequence <S>(StackSequence.Take(StackSequence.Count() - tail.StackSequence.Count()).ToList()));
 }
Beispiel #6
0
 public bool EndsWith(StackSymbolSequence <S> tail)
 {
     return(StackSequence.Count() >= tail.StackSequence.Count() &&
            new StackSymbolSequence <S>(StackSequence.Skip(StackSequence.Count() - tail.StackSequence.Count()).ToList()).Equals(tail));
 }
Beispiel #7
0
 public bool Equals(StackSymbolSequence <S> other)
 {
     return(StackSequence.SequenceEqual(other.StackSequence));
 }