Exemplo n.º 1
0
        /// <summary>
        /// Merges the both given DPDA with acceptance condition 'empty stack' into a new one, that simply contains both DPDAs. The initial state
        /// of the result DPDA has two transitions, one to the initial state of pda1 and one to the initial state of pda2, each of these transitions
        /// reads the corresponding given stack symbol. That means, for simulating pda1 with the merged one you just have to start the simulation
        /// with the initialStackSymbol1 as start configuration
        /// </summary>
        /// <param name="dpda1">DPDA with empty stack</param>
        /// <param name="dpda2">DPDA with empty stack</param>
        /// <param name="initialStackSymbol1">the stack symbol that signals that the first DPDA should be simulated</param>
        /// <param name="initialStackSymbol2">the stack symbol that signals that the second DPDA should be simulated</param>
        /// <returns>the merged DPDA</returns>
        public static PDA <A, S> MergeDPDAsWithEmptyStack(PDA <A, S> dpda1, PDA <A, S> dpda2, S initialStackSymbol1, S initialStackSymbol2)
        {
            Assertion.Assert(dpda1.Deterministic, NotDeterministicError);
            Assertion.Assert(dpda2.Deterministic, NotDeterministicError);
            Assertion.Assert(dpda1.AcceptanceCondition.IsEmptyStack(), NotEmptyStackError);
            Assertion.Assert(dpda2.AcceptanceCondition.IsEmptyStack(), NotEmptyStackError);
            Assertion.Assert(!initialStackSymbol1.Equals(initialStackSymbol2), "the both initial stack symbols must not be the same");

            var newStackSymbols = dpda1.AllStackSymbols.Concat(dpda2.AllStackSymbols).Concat(new S[] { initialStackSymbol1, initialStackSymbol2 }).Distinct();
            var res             = new PDA <A, S>(new AcceptanceCondition.EmptyStack(), true, initialStackSymbol1, false, newStackSymbols);

            int idTransformer1(int id) => id + 1;

            int maxIdOfDPDA1 = dpda1.States.Max(s => s.Key) + 1;

            int idTransformer2(int id) => idTransformer1(id + maxIdOfDPDA1);

            AddStatesOfOriginalPDA(dpda1, res, idTransformer1);
            AddStatesOfOriginalPDA(dpda2, res, idTransformer2);

            AddTransitionsOfOriginalPDA(dpda1, res, idTransformer1);
            AddTransitionsOfOriginalPDA(dpda2, res, idTransformer2);

            res.AddTransition().From(res.InitialState.Id).To(idTransformer1(dpda1.InitialState.Id)).Read().Pop(initialStackSymbol1).Push(new List <S>()
            {
                dpda1.FirstStackSymbol
            });
            res.AddTransition().From(res.InitialState.Id).To(idTransformer2(dpda2.InitialState.Id)).Read().Pop(initialStackSymbol2).Push(new List <S>()
            {
                dpda2.FirstStackSymbol
            });

            return(res);
        }
Exemplo n.º 2
0
 private static void AddEmptyingTransitions(PDA <A, S> newPDA, int newStateId)
 {
     foreach (var stackSymbol in newPDA.AllStackSymbols)
     {
         newPDA.AddTransition().From(newStateId).To(newStateId).Read().Pop(stackSymbol).Push();
     }
 }
Exemplo n.º 3
0
 private static void AddInitialTransition(PDA <A, S> pda, PDA <A, S> newPDA, Func <int, int> transformer)
 {
     newPDA.AddTransition().From(newPDA.InitialState.Id).To(transformer(pda.InitialState.Id)).Read().Pop(newPDA.FirstStackSymbol).Push(new List <S>()
     {
         pda.FirstStackSymbol, newPDA.FirstStackSymbol
     });
 }
Exemplo n.º 4
0
 private static void AddTransitionsForAddedSymbol(PDA <A, S> pda, PDA <A, S> newPDA, S lowestStackSymbol, A addedSymbol, Func <int, int> transformer)
 {
     foreach (var state in pda.States.ToList())
     {
         var newId = transformer(state.Key);
         newPDA.AddTransition().From(newId).To(newId).Read(Symbol <A> .SymbolIn(addedSymbol)).Pop(lowestStackSymbol).Push();
     }
 }
Exemplo n.º 5
0
 private static void AddTransitionsToEmptyingState(PDA <A, S> pda, PDA <A, S> newPDA, int newStateId, Symbol <A> symbolIn, Func <int, int> transformer)
 {
     foreach (var finalState in pda.States.Where(s => s.Value.Final).ToList())
     {
         foreach (var stackSymbol in newPDA.AllStackSymbols)
         {
             newPDA.AddTransition().From(transformer(finalState.Key)).To(newStateId).Read(symbolIn).Pop(stackSymbol).Push();
         }
     }
 }
Exemplo n.º 6
0
 private static void AddTransitionsOfOriginalPDA(PDA <A, S> pda, PDA <A, S> newPDA, Func <int, int> transformer)
 {
     foreach (var state in pda.States)
     {
         var transitions = state.Value.Transitions;
         foreach (var t in transitions)
         {
             newPDA.AddTransition().From(transformer(state.Key)).To(transformer(t.Target.Id)).Read(t.SymbolIn).Pop(t.StackSymbolIn).Push(t.StackSymbolsWritten);
         }
     }
 }
Exemplo n.º 7
0
        public PDA <A, S> Clone()
        {
            var res = new PDA <A, S>(AcceptanceCondition, true, FirstStackSymbol, InitialState.Final, AllStackSymbols.ToList());

            foreach (var state in States.Where(s => s.Key != initialStateId).ToList())
            {
                res.AddState(state.Key, state.Value.Final);
            }
            foreach (var state in States)
            {
                foreach (var t in state.Value.Transitions)
                {
                    res.AddTransition().From(t.Origin.Id).To(t.Target.Id).Read(t.SymbolIn).Pop(t.StackSymbolIn).Push(t.StackSymbolsWritten);
                }
            }
            return(res);
        }
        private static void AddTransitionsToPda(PDA <char, char> pda, XElement xmlPDA)
        {
            //FIXME: if deterministic is true, but a transition violations the determinism property, then throw a suitable exception, which is catched in the Grader

            foreach (var link in xmlPDA.Elements().Where(el => el.Name.LocalName == XmlNames.Links).First().Elements().Where(el => el.Name.LocalName == XmlNames.Link))
            {
                var start  = int.Parse(link.Attribute(XmlAttr.Start).Value);
                var target = int.Parse(link.Attribute(XmlAttr.End).Value);
                foreach (var transition in link.Elements().Where(el => el.Name.LocalName == XmlNames.TransitionGroup).First().Elements().Where(el => el.Name.LocalName == XmlNames.Transition))
                {
                    var inputSymbol = transition.Value[0] == Epsilon ? Symbol <char> .EpsilonIn() : Symbol <char> .SymbolIn(transition.Value[0]);

                    var inputStackSymbol    = transition.Value[2];
                    var writtenStackSymbols = transition.Value.Skip(4);
                    pda.AddTransition().From(start).To(target).Read(inputSymbol).Pop(inputStackSymbol).Push(writtenStackSymbols);
                }
            }
        }