internal static PDA <char, char> ParsePDAFromXmlPDA(XElement xmlPDA)
        {
            var properties = xmlPDA.Elements().Where(el => el.Name.LocalName == XmlNames.Properties).First();

            var deterministic       = bool.Parse(properties.Elements().Where(el => el.Name.LocalName == XmlNames.Determinism).First().Value);
            var acceptanceCondition = AcceptanceCondition.GetAcceptanceConditionById(properties.Elements().Where(el => el.Name.LocalName == XmlNames.AcceptanceCondition).First().Value);

            var allStackSymbolsAsStrings = properties.Elements().Where(el => el.Name.LocalName == XmlNames.StackAlphabet).First().Elements().Select(s => s.Value).ToList();

            Assertion.Assert(allStackSymbolsAsStrings.All(s => s.Length == 1), "stack symbols must be chars");
            var allStackSymbols  = allStackSymbolsAsStrings.Select(s => s.First());
            var firstStackSymbol = allStackSymbols.First();
            //FIXME: what if all keys are used as stack symbols??

            var initialState = xmlPDA.Elements().Where(el => el.Name.LocalName == XmlNames.Nodes).First().Elements().Where(el => el.Name.LocalName == XmlNames.InitialState).First();

            Assertion.Assert(int.Parse(initialState.Attribute(XmlAttr.Id).Value) == PDA <char, char> .initialStateId, "the initial state has to have id " + PDA <char, char> .initialStateId);
            var initialStateIsFinal = bool.Parse(initialState.Attribute(XmlAttr.IsFinal).Value);
            var pda = new PDA <char, char>(acceptanceCondition, deterministic, firstStackSymbol, initialStateIsFinal, allStackSymbols, allStackAlphabetSymbols.First(k => !allStackSymbols.Contains(k)));

            AddStatesToPda(pda, xmlPDA);

            AddTransitionsToPda(pda, xmlPDA);

            return(pda);
        }
Exemplo n.º 2
0
        public PDA(AcceptanceCondition acceptanceCondition, bool deterministic, S firstStackSymbol, bool initialStateIsFinal, IEnumerable <S> allStackSymbols, S furtherStackSymbol)
            : this(acceptanceCondition, deterministic, firstStackSymbol, initialStateIsFinal, allStackSymbols)
        {
            Assertion.Assert(!allStackSymbols.Contains(furtherStackSymbol), "the further stack symbol must not be in the list of all stack symbols");

            FurtherStackSymbol = furtherStackSymbol;
        }
Exemplo n.º 3
0
        public bool Equals(PDA <A, S> other)
        {
            var part1 = AcceptanceCondition.Equals(other.AcceptanceCondition);
            var part2 = Deterministic == other.Deterministic;
            var part3 = InitialState.Equals(other.InitialState);
            var part4 = FirstStackSymbol.Equals(other.FirstStackSymbol);
            var part5 = AllStackSymbols.OrderBy(s => s.ToString()).SequenceEqual(other.AllStackSymbols.OrderBy(s => s.ToString()));
            var part6 = States.Values.OrderBy(s => s.Id).SequenceEqual(other.States.Values.OrderBy(s => s.Id));

            return(part1 && part2 && part3 && part4 && part5 && part6);
        }
Exemplo n.º 4
0
        public PDA(AcceptanceCondition acceptanceCondition, bool deterministic, S firstStackSymbol, bool initialStateIsFinal, IEnumerable <S> allStackSymbols)
        {
            Assertion.Assert(allStackSymbols.Distinct().Count() == allStackSymbols.Count(), "at least one stack symbol occurs twice in the stack symbol list");

            AcceptanceCondition = acceptanceCondition;
            Deterministic       = deterministic;
            FirstStackSymbol    = firstStackSymbol;
            InitialState        = new State <A, S>(initialStateId, initialStateIsFinal);
            AllStackSymbols     = allStackSymbols.ToList();
            States = new Dictionary <int, State <A, S> >
            {
                { InitialState.Id, InitialState }
            };
        }
 public override AcceptanceCondition Clone()
 {
     return(AcceptanceCondition.GetAcceptanceConditionById(GetId()));
 }
 public override bool Equals(AcceptanceCondition other)
 {
     return(other.IsFinalState() && !other.IsEmptyStack());
 }