示例#1
0
        public void SimplePalTest()
        {
            var q  = new States(3);
            var f  = new AcceptingStates(q[2]);
            var tf = new PushdownTransitionFunction()
            {
                new PushdownTransition(q[0], 'a', Alphabet.Z, q[0], $"a{Alphabet.Z}"),
                new PushdownTransition(q[0], 'b', Alphabet.Z, q[0], $"b{Alphabet.Z}"),
                new PushdownTransition(q[0], 'a', 'a', q[0], "aa"),
                new PushdownTransition(q[0], 'b', 'b', q[0], "bb"),
                new PushdownTransition(q[0], 'b', 'a', q[0], "ba"),
                new PushdownTransition(q[0], 'a', 'b', q[0], "ab"),
                new PushdownTransition(q[0], 'c', 'a', q[1], "a"),
                new PushdownTransition(q[0], 'c', 'b', q[1], "b"),
                new PushdownTransition(q[0], 'c', Alphabet.Z, q[1], $"{Alphabet.Z}"),
                new PushdownTransition(q[1], 'a', 'a', q[1], Alphabet.EmptyString.ToString()),
                new PushdownTransition(q[1], 'b', 'b', q[1], Alphabet.EmptyString.ToString()),
                new PushdownTransition(q[1], Alphabet.EmptyString, Alphabet.Z, q[2], Alphabet.Z.ToString())
            };
            var m = new PushdownAutomaton(q, Alphabet.Abc, new StackAlphabet(Alphabet.Abc), tf, q[0], f);

            Assert.True(m.Run("aca"));
            Assert.True(m.Run("abacaba"));
            Assert.True(m.Run("c"));
            Assert.True(m.Run("bacab"));
            Assert.False(m.Run("ab"));
            Assert.False(m.Run("babb"));
            Assert.False(m.Run("acab"));
            Assert.False(m.Run("bacba"));
        }
示例#2
0
        public void AbabuabaTest()
        {
            var q  = new States(6);
            var f  = new AcceptingStates(q[1], q[2]);
            var tf = new NondeterministicTransitionFunction()
            {
                new Transition(q[0], 'a', q[1]),
                new Transition(q[1], 'b', q[3]),
                new Transition(q[3], 'a', q[4]),
                new Transition(q[4], 'b', q[1]),
                new Transition(q[0], 'a', q[2]),
                new Transition(q[2], 'b', q[5]),
                new Transition(q[5], 'a', q[2])
            };
            var m = new NondeterministicFiniteAutomaton(q, Alphabet.Ab, tf, q[0], f);

            Assert.True(m.Run("ababbabbab"));
            Assert.True(m.Run("abab"));
            Assert.True(m.Run("abababa"));
            Assert.True(m.Run("aba"));
            Assert.False(m.Run("ba"));
            Assert.False(m.Run("bbbbb"));
            Assert.False(m.Run("abb"));
            Assert.False(m.Run("b"));
            Assert.False(m.Run(""));
            Assert.False(m.Run(new [] { Alphabet.EmptyString }));
        }
示例#3
0
 public DfaBuilder(Matches matches, AcceptingStates acceptingStates)
 {
     this.matches         = matches;
     edges                = matches.NextId + 1;
     this.acceptingStates = acceptingStates;
     States               = new List <DfaState>();
 }
示例#4
0
        public FiniteAutomaton(States q, Alphabet a, TransitionFunction d, State q0, AcceptingStates f)
        {
            States          = q;
            Alphabet        = a;
            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 initial 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} does not exist in the set of states!");
                }
            }
            foreach (var t in Transitions)
            {
                if (!States.Contains(t.P) || !States.Contains(t.Q) || (t.A != Alphabet.EmptyString && !Alphabet.Contains(t.A)))
                {
                    throw new ArgumentException($"Invalid transition {t}");
                }
            }
        }
示例#5
0
        public void EvenZeroesOrOnesTest()
        {
            var q  = new States(5);
            var f  = new AcceptingStates(q[1], q[3]);
            var tf = new NondeterministicTransitionFunction()
            {
                new Transition(q[0], Alphabet.EmptyString, q[1]),
                new Transition(q[0], Alphabet.EmptyString, q[3]),
                new Transition(q[1], '1', q[1]),
                new Transition(q[1], '0', q[2]),
                new Transition(q[2], '1', q[2]),
                new Transition(q[2], '0', q[1]),
                new Transition(q[3], '0', q[3]),
                new Transition(q[3], '1', q[4]),
                new Transition(q[4], '0', q[4]),
                new Transition(q[4], '1', q[3])
            };
            var m = new NondeterministicFiniteAutomaton(q, Alphabet.Binary, tf, q[0], f);

            Assert.True(m.Run(""));
            Assert.True(m.Run("00"));
            Assert.True(m.Run("11"));
            Assert.True(m.Run("0011"));
            Assert.True(m.Run("00110101100"));
            Assert.True(m.Run("011010111"));
            Assert.False(m.Run("10"));
            Assert.False(m.Run("11101100"));
            Assert.False(m.Run("10001111"));
        }
示例#6
0
 public Dfa(Matches matches,
            AcceptingStates acceptingStates,
            IEnumerable <DfaNode> states)
 {
     Matches         = matches;
     AcceptingStates = acceptingStates;
     MatchToClass    = matches.ToArray();
     States          = states.ToArray();
 }
示例#7
0
        /// <summary>
        /// Returns safe name for accepting state
        /// </summary>
        /// <param name="accepting"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool TryGetAccepting(int accepting, out string name)
        {
            var result = AcceptingStates.TryGetName(accepting, out name);

            if (result)
            {
                name = Identifier.SafeName(name);
            }
            return(result);
        }
示例#8
0
 protected override void Write(Generator generator, AcceptingStates acceptingStates)
 {
     foreach (var state in acceptingStates.AllStates)
     {
         if (state.index == 0)
         {
             generator.WriteLine("public const int SyntaxError = -1;");
         }
         var name = Identifier.SafeName(state.name);
         generator.WriteLine($"public const int {name} = {state.index};");
     }
 }
示例#9
0
文件: DfaState.cs 项目: mrfichtn/opal
        public DfaState(AcceptingStates acceptingStates, IEnumerable <int> nodes, int index, int edges)
        {
            NfaStates = nodes.ToArray();
            _index    = index;
            _next     = new int[edges];

            foreach (var node in nodes)
            {
                if (acceptingStates.TryFind(node, out var acceptingState))
                {
                    if ((_acceptingState == 0) || (_acceptingState < acceptingState))
                    {
                        _acceptingState = acceptingState;
                    }
                }
            }
        }
示例#10
0
        public void EndsWithOneTest()
        {
            var q  = new States(2);
            var f  = new AcceptingStates(q[1]);
            var tf = new NondeterministicTransitionFunction()
            {
                new Transition(q[0], '0', q[0]),
                new Transition(q[0], '1', q[0]),
                new Transition(q[0], '1', q[1])
            };
            var m = new NondeterministicFiniteAutomaton(q, Alphabet.Binary, tf, q[0], f);

            Assert.True(m.Run("1"));
            Assert.True(m.Run("01"));
            Assert.True(m.Run("011010101011"));
            Assert.True(m.Run("000101110001"));
            Assert.False(m.Run("0"));
            Assert.False(m.Run("10"));
            Assert.False(m.Run("10010101100"));
        }
示例#11
0
        // {0^n1^n | n >= 0}
        public void ZeroNOneNTest()
        {
            var q  = new States(3);
            var f  = new AcceptingStates(q[0], q[2]);
            var tf = new PushdownTransitionFunction()
            {
                new PushdownTransition(q[0], '0', Alphabet.Z, q[0], $"0{Alphabet.Z}"),
                new PushdownTransition(q[0], '0', '0', q[0], "00"),
                new PushdownTransition(q[0], '1', '0', q[1], new [] { Alphabet.EmptyString }),
                new PushdownTransition(q[1], '1', '0', q[1], new [] { Alphabet.EmptyString }),
                new PushdownTransition(q[1], Alphabet.EmptyString, Alphabet.Z, q[2], new [] { Alphabet.Z })
            };
            var m = new PushdownAutomaton(q, Alphabet.Binary, new StackAlphabet(Alphabet.Binary), tf, q[0], f);

            Assert.True(m.Run(""));
            Assert.True(m.Run("0011"));
            Assert.True(m.Run("01"));
            Assert.True(m.Run("0000011111"));
            Assert.False(m.Run("011010"));
            Assert.False(m.Run("101010001"));
            Assert.False(m.Run("111000"));
        }
示例#12
0
        public void EvenZeroesTest()
        {
            var q  = new States(2);
            var f  = new AcceptingStates(q[0]);
            var tf = new TransitionFunction()
            {
                new Transition(q[0], '1', q[0]),
                new Transition(q[0], '0', q[1]),
                new Transition(q[1], '0', q[0]),
                new Transition(q[1], '1', q[1])
            };
            var m = new FiniteAutomaton(q, Alphabet.Binary, tf, q[0], f);

            Assert.True(m.Run("1111"));
            Assert.True(m.Run("1100"));
            Assert.True(m.Run("10101"));
            Assert.True(m.Run("00"));
            Assert.True(m.Run("10111111110"));
            Assert.False(m.Run("101"));
            Assert.False(m.Run("000"));
            Assert.False(m.Run("11111011111"));
        }
示例#13
0
 protected override void Write(Generator generator, AcceptingStates acceptingStates)
 {
     generator.WriteLine("public const int SyntaxError = -1;")
     .WriteLine("public const int Empty = 0;");
 }
示例#14
0
 protected abstract void Write(Generator generator, AcceptingStates acceptingStates);
示例#15
0
 public bool Accepts()
 {
     return(AcceptingStates.Contains(CurrentState));
 }