Пример #1
0
        private FSM <int> ParseRegex()
        {
            FSM <int> result = ConcatRegex();

            while (_parseIndex < this.Pattern.Length)
            {
                switch (this.Pattern[_parseIndex])
                {
                case '|':
                    _parseIndex++;
                    result = result.Or(ConcatRegex());
                    break;

                case ')':
                    _parseIndex++;
                    return(result);

                default:
                    throw new Exception("Invalid regex pattern");
                }
            }

            return(result);
        }
Пример #2
0
 public abstract FSM <T> And(FSM <T> fsm);
Пример #3
0
 public abstract FSM <T> Or(FSM <T> fsm);
        public override FSM <T> Or(FSM <T> fsm)
        {
            DFSM <T> result = (DFSM <T>) this.Clone();
            //DFSM<T> result = new DFSM<T>(this);
            Dictionary <int, int> conv = new Dictionary <int, int>();


            if (this._transitions[0].Count == 0)
            {
                return(fsm);
            }

            int        offset = result.States.Last();
            List <int> states = fsm.States;

            conv[0] = 0;

            while (states.Count != 0)
            {
                int start = states[0];
                if (fsm._transitions.ContainsKey(start))
                {
                    foreach (T symbol in fsm._transitions[start].Keys)
                    {
                        foreach (int end in fsm._transitions[start][symbol])
                        {
                            if (!conv.ContainsKey(end))
                            {
                                offset++;
                                conv[end] = offset;
                            }

                            if (!result._transitions.ContainsKey(conv[start]))
                            {
                                result._transitions[conv[start]] = new Dictionary <T, List <int> >();
                            }

                            if (!result._transitions.ContainsKey(conv[end]))
                            {
                                result._transitions[conv[end]] = new Dictionary <T, List <int> >();
                            }

                            if (!result._transitions[conv[start]].ContainsKey(symbol))
                            {
                                result._transitions[conv[start]][symbol] = new List <int>();
                            }

                            result._transitions[conv[start]][symbol].Add(conv[end]);

                            if (fsm.AcceptingStates.ContainsKey(end))
                            {
                                if (!result.AcceptingStates.ContainsKey(conv[end]))
                                {
                                    result.AcceptingStates[conv[end]] = new List <string>(fsm.AcceptingStates[end]);
                                }
                            }
                        }
                    }
                }

                states.RemoveAt(0);
            }

            return(result);
        }