Example #1
0
        public static StackAlphabet operator -(StackAlphabet a, Alphabet b)
        {
            var c = new StackAlphabet();

            foreach (var c1 in a)
            {
                if (!b.Contains(c1))
                {
                    c.Add(c1);
                }
            }
            return(c);
        }
        /// <param name="q">The set of states</param>
        /// <param name="a">The language alphabet</param>
        /// <param name="g">The stack alphabet</param>
        /// <param name="d">The transition function containing Transition and PushdownTransition objects</param>
        /// <param name="q0">The initial state</param>
        /// <param name="f">The set of accepting states</param>
        /// <exception cref="ArgumentException"></exception>
        public PushdownAutomaton(States q, Alphabet a, StackAlphabet g, PushdownTransitionFunction d, State q0, AcceptingStates f)
        {
            States          = q;
            Alphabet        = a;
            StackAlphabet   = g;
            Transitions     = d;
            InitialState    = q0;
            AcceptingStates = f;

            if (States.Count == 0)
            {
                throw new ArgumentException("The set of states cannot be empty.");
            }
            if (!States.Contains(InitialState))
            {
                throw new ArgumentException("The inital state does not exist in the set of states!");
            }

            foreach (var s in AcceptingStates)
            {
                if (!States.Contains(s))
                {
                    throw new ArgumentException($"The accepting state {s} is not in the set of states!");
                }
            }

            foreach (var t in Transitions)
            {
                if (!States.Contains(t.P) || !States.Contains(t.Q) ||
                    (!Alphabet.Contains(t.A) && t.A != Alphabet.EmptyString) ||
                    (!StackAlphabet.Contains(t.Top) && t.Top != Alphabet.Wildcard && t.Top != Alphabet.EmptyString))
                {
                    throw new ArgumentException($"Invalid transition {t}");
                }
                if (t.Replace.Length == 1 && ((t.Replace[0] != Alphabet.Wildcard && t.Replace[0] != Alphabet.EmptyString) && !StackAlphabet.Contains(t.Replace[0])))
                {
                    throw new ArgumentException($"Invalid replace char for transition {t}");
                }
                if (t.Replace.Length > 1 && Array.Exists(t.Replace, x => !StackAlphabet.Contains(x)))
                {
                    throw new ArgumentException($"Invalid replace char sequence for transition {t}");
                }
            }
        }